annotate modules/Bag.lua @ 218:e63aefb8a555

Demodularization of buttons - register class instead of config
author Flick <flickerstreak@gmail.com>
date Fri, 19 Nov 2010 23:06:24 -0800
parents df68b5a40490
children bb13624de7e1
rev   line source
flickerstreak@146 1 --[[
flickerstreak@146 2 ReAction Bag button module
flickerstreak@146 3
flickerstreak@146 4 --]]
flickerstreak@146 5
flickerstreak@146 6 -- local imports
flickerstreak@175 7 local addonName, addonTable = ...
flickerstreak@175 8 local ReAction = addonTable.ReAction
flickerstreak@146 9 local L = ReAction.L
flickerstreak@146 10 local _G = _G
flickerstreak@146 11
flickerstreak@146 12 -- Bag button
flickerstreak@146 13 local Button = ReAction.Button.Bag
flickerstreak@146 14
flickerstreak@146 15 -- module declaration
flickerstreak@146 16 local moduleID = "Bag"
flickerstreak@146 17 local module = ReAction:NewModule( moduleID
flickerstreak@146 18 -- mixins go here
flickerstreak@146 19 )
flickerstreak@146 20
flickerstreak@146 21 -- handlers
flickerstreak@146 22 function module:OnInitialize()
flickerstreak@146 23 self.db = ReAction.db:RegisterNamespace( moduleID,
flickerstreak@146 24 {
flickerstreak@146 25 profile = {
flickerstreak@146 26 buttons = { }
flickerstreak@146 27 }
flickerstreak@146 28 }
flickerstreak@146 29 )
flickerstreak@146 30
flickerstreak@146 31 self.buttons = { }
flickerstreak@146 32
flickerstreak@146 33 ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
flickerstreak@146 34 ReAction.RegisterCallback(self, "OnDestroyBar")
flickerstreak@146 35 ReAction.RegisterCallback(self, "OnRefreshBar")
flickerstreak@146 36 ReAction.RegisterCallback(self, "OnEraseBar")
flickerstreak@146 37 ReAction.RegisterCallback(self, "OnRenameBar")
flickerstreak@146 38 end
flickerstreak@146 39
flickerstreak@146 40 function module:OnEnable()
flickerstreak@218 41 ReAction:RegisterBarType(Button)
flickerstreak@146 42 end
flickerstreak@146 43
flickerstreak@146 44 function module:OnDisable()
flickerstreak@218 45 ReAction:UnregisterBarType(Button)
flickerstreak@146 46 end
flickerstreak@146 47
flickerstreak@146 48 function module:OnDestroyBar(event, bar, name)
flickerstreak@146 49 local btns = self.buttons[bar]
flickerstreak@146 50 if btns then
flickerstreak@146 51 for _,b in pairs(btns) do
flickerstreak@146 52 if b then
flickerstreak@146 53 b:Destroy()
flickerstreak@146 54 end
flickerstreak@146 55 end
flickerstreak@146 56 self.buttons[bar] = nil
flickerstreak@146 57 end
flickerstreak@146 58 end
flickerstreak@146 59
flickerstreak@146 60 function module:OnRefreshBar(event, bar, name)
flickerstreak@146 61 if bar.config.type == moduleID then
flickerstreak@146 62 local btns = self.buttons[bar]
flickerstreak@146 63 if btns == nil then
flickerstreak@146 64 btns = { }
flickerstreak@146 65 self.buttons[bar] = btns
flickerstreak@146 66 end
flickerstreak@146 67 local profile = self.db.profile
flickerstreak@146 68 if profile.buttons[name] == nil then
flickerstreak@146 69 profile.buttons[name] = {}
flickerstreak@146 70 end
flickerstreak@146 71 local btnCfg = profile.buttons[name]
flickerstreak@146 72
flickerstreak@146 73 local r, c = bar:GetButtonGrid()
flickerstreak@146 74 local n = r*c
flickerstreak@146 75 for i = 1, n do
flickerstreak@146 76 if btnCfg[i] == nil then
flickerstreak@146 77 btnCfg[i] = {}
flickerstreak@146 78 end
flickerstreak@146 79 if btns[i] == nil then
flickerstreak@146 80 local success, r = pcall(Button.New,Button,i,profile,bar,i>1 and btnCfg[i-1].bagID)
flickerstreak@146 81 if success and r then
flickerstreak@146 82 btns[i] = r
flickerstreak@146 83 bar:AddButton(i,r)
flickerstreak@146 84 else
flickerstreak@146 85 n = i - 1
flickerstreak@146 86 bar:ClipNButtons(n)
flickerstreak@146 87 break
flickerstreak@146 88 end
flickerstreak@146 89 end
flickerstreak@146 90 btns[i]:Refresh()
flickerstreak@146 91 end
flickerstreak@146 92 for i = n+1, #btns do
flickerstreak@146 93 if btns[i] then
flickerstreak@146 94 bar:RemoveButton(btns[i])
flickerstreak@146 95 btns[i] = btns[i]:Destroy()
flickerstreak@146 96 if btnCfg[i] then
flickerstreak@146 97 btnCfg[i] = nil
flickerstreak@146 98 end
flickerstreak@146 99 end
flickerstreak@146 100 end
flickerstreak@146 101 end
flickerstreak@146 102
flickerstreak@146 103 end
flickerstreak@146 104
flickerstreak@146 105 function module:OnEraseBar(event, bar, name)
flickerstreak@146 106 self.db.profile.buttons[name] = nil
flickerstreak@146 107 end
flickerstreak@146 108
flickerstreak@146 109 function module:OnRenameBar(event, bar, oldName, newName)
flickerstreak@146 110 local b = self.db.profile.buttons
flickerstreak@146 111 b[newname], b[oldname] = b[oldname], nil
flickerstreak@146 112 end
flickerstreak@146 113
flickerstreak@146 114
flickerstreak@146 115 -- hook some functions to propagate to our bag buttons
flickerstreak@146 116 hooksecurefunc("Disable_BagButtons",
flickerstreak@146 117 function()
flickerstreak@146 118 for _, buttons in pairs(module.buttons) do
flickerstreak@146 119 for _, b in pairs(buttons) do
flickerstreak@146 120 local f = b:GetFrame()
flickerstreak@146 121 f:Disable()
flickerstreak@146 122 SetDesaturation(b.frames.icon,1)
flickerstreak@146 123 end
flickerstreak@146 124 end
flickerstreak@146 125 end)
flickerstreak@146 126
flickerstreak@146 127 hooksecurefunc("Enable_BagButtons",
flickerstreak@146 128 function()
flickerstreak@146 129 for _, buttons in pairs(module.buttons) do
flickerstreak@146 130 for _, b in pairs(buttons) do
flickerstreak@146 131 local f = b:GetFrame()
flickerstreak@146 132 f:Enable()
flickerstreak@146 133 SetDesaturation(b.frames.icon,nil)
flickerstreak@146 134 end
flickerstreak@146 135 end
flickerstreak@146 136 end)
flickerstreak@146 137
flickerstreak@146 138 hooksecurefunc("ContainerFrame_OnHide",
flickerstreak@146 139 function()
flickerstreak@146 140 for _, buttons in pairs(module.buttons) do
flickerstreak@146 141 for _, b in pairs(buttons) do
flickerstreak@146 142 b:Update()
flickerstreak@146 143 end
flickerstreak@146 144 end
flickerstreak@146 145 end)
flickerstreak@146 146
flickerstreak@146 147 hooksecurefunc("ContainerFrame_OnShow",
flickerstreak@146 148 function()
flickerstreak@146 149 for _, buttons in pairs(module.buttons) do
flickerstreak@146 150 for _, b in pairs(buttons) do
flickerstreak@146 151 b:Update()
flickerstreak@146 152 end
flickerstreak@146 153 end
flickerstreak@146 154 end)