comparison modules/Bag.lua @ 146:86564b5cbbff

Added bag button support
author Flick <flickerstreak@gmail.com>
date Thu, 30 Apr 2009 16:51:43 +0000
parents
children 901c91dc1bf2
comparison
equal deleted inserted replaced
145:42cade25d40d 146:86564b5cbbff
1 --[[
2 ReAction Bag button module
3
4 --]]
5
6 -- local imports
7 local ReAction = ReAction
8 local L = ReAction.L
9 local _G = _G
10
11 -- Bag button
12 local Button = ReAction.Button.Bag
13
14 -- module declaration
15 local moduleID = "Bag"
16 local module = ReAction:NewModule( moduleID
17 -- mixins go here
18 )
19
20 -- libraries
21 local KB = LibStub("LibKeyBound-1.0")
22
23 -- handlers
24 function module:OnInitialize()
25 self.db = ReAction.db:RegisterNamespace( moduleID,
26 {
27 profile = {
28 buttons = { }
29 }
30 }
31 )
32
33 self.buttons = { }
34
35 ReAction.RegisterCallback(self, "OnCreateBar", "OnRefreshBar")
36 ReAction.RegisterCallback(self, "OnDestroyBar")
37 ReAction.RegisterCallback(self, "OnRefreshBar")
38 ReAction.RegisterCallback(self, "OnEraseBar")
39 ReAction.RegisterCallback(self, "OnRenameBar")
40 ReAction.RegisterCallback(self, "OnConfigModeChanged")
41
42 KB.RegisterCallback(self, "LIBKEYBOUND_ENABLED")
43 KB.RegisterCallback(self, "LIBKEYBOUND_DISABLED")
44 KB.RegisterCallback(self, "LIBKEYBOUND_MODE_COLOR_CHANGED","LIBKEYBOUND_ENABLED")
45 end
46
47 function module:OnEnable()
48 ReAction:RegisterBarType(L["Bag Bar"],
49 {
50 type = moduleID ,
51 defaultButtonSize = 30,
52 defaultBarRows = 1,
53 defaultBarCols = 6,
54 defaultBarSpacing = 4
55 })
56 end
57
58 function module:OnDisable()
59 ReAction:UnregisterBarType(L["Bag Bar"])
60 end
61
62 function module:OnDestroyBar(event, bar, name)
63 local btns = self.buttons[bar]
64 if btns then
65 for _,b in pairs(btns) do
66 if b then
67 b:Destroy()
68 end
69 end
70 self.buttons[bar] = nil
71 end
72 end
73
74 function module:OnRefreshBar(event, bar, name)
75 if bar.config.type == moduleID then
76 local btns = self.buttons[bar]
77 if btns == nil then
78 btns = { }
79 self.buttons[bar] = btns
80 end
81 local profile = self.db.profile
82 if profile.buttons[name] == nil then
83 profile.buttons[name] = {}
84 end
85 local btnCfg = profile.buttons[name]
86
87 local r, c = bar:GetButtonGrid()
88 local n = r*c
89 for i = 1, n do
90 if btnCfg[i] == nil then
91 btnCfg[i] = {}
92 end
93 if btns[i] == nil then
94 local success, r = pcall(Button.New,Button,i,profile,bar,i>1 and btnCfg[i-1].bagID)
95 if success and r then
96 btns[i] = r
97 bar:AddButton(i,r)
98 else
99 n = i - 1
100 bar:ClipNButtons(n)
101 break
102 end
103 end
104 btns[i]:Refresh()
105 end
106 for i = n+1, #btns do
107 if btns[i] then
108 bar:RemoveButton(btns[i])
109 btns[i] = btns[i]:Destroy()
110 if btnCfg[i] then
111 btnCfg[i] = nil
112 end
113 end
114 end
115 end
116
117 end
118
119 function module:OnEraseBar(event, bar, name)
120 self.db.profile.buttons[name] = nil
121 end
122
123 function module:OnRenameBar(event, bar, oldName, newName)
124 local b = self.db.profile.buttons
125 b[newname], b[oldname] = b[oldname], nil
126 end
127
128 function module:OnConfigModeChanged(event, mode)
129 for bar in pairs(self.buttons) do
130 for b in bar:IterateButtons() do
131 b:UpdateActionIDLabel(mode)
132 end
133 end
134 end
135
136 function module:LIBKEYBOUND_ENABLED(evt)
137 for _, buttons in pairs(self.buttons) do
138 for _, b in pairs(buttons) do
139 b:SetKeybindMode(true)
140 end
141 end
142 end
143
144 function module:LIBKEYBOUND_DISABLED(evt)
145 for _, buttons in pairs(self.buttons) do
146 for _, b in pairs(buttons) do
147 b:SetKeybindMode(false)
148 end
149 end
150 end
151
152 function module:RefreshAll()
153 for bar in pairs(self.buttons) do
154 self:OnRefreshBar(nil,bar,bar:GetName())
155 end
156 end
157
158
159 -- hook some functions to propagate to our bag buttons
160 hooksecurefunc("Disable_BagButtons",
161 function()
162 for _, buttons in pairs(module.buttons) do
163 for _, b in pairs(buttons) do
164 local f = b:GetFrame()
165 f:Disable()
166 SetDesaturation(b.frames.icon,1)
167 end
168 end
169 end)
170
171 hooksecurefunc("Enable_BagButtons",
172 function()
173 for _, buttons in pairs(module.buttons) do
174 for _, b in pairs(buttons) do
175 local f = b:GetFrame()
176 f:Enable()
177 SetDesaturation(b.frames.icon,nil)
178 end
179 end
180 end)
181
182 hooksecurefunc("ContainerFrame_OnHide",
183 function()
184 for _, buttons in pairs(module.buttons) do
185 for _, b in pairs(buttons) do
186 b:Update()
187 end
188 end
189 end)
190
191 hooksecurefunc("ContainerFrame_OnShow",
192 function()
193 for _, buttons in pairs(module.buttons) do
194 for _, b in pairs(buttons) do
195 b:Update()
196 end
197 end
198 end)