comparison modules/Action.lua @ 234:0e20f65375d5

Reworked button creation to not use goofy event driven semantics.
author Flick
date Tue, 22 Mar 2011 17:05:51 -0700
parents c4b134512c50
children 704f4a05a1d7
comparison
equal deleted inserted replaced
233:9b9f5fc84d34 234:0e20f65375d5
1 local addonName, addonTable = ... 1 local addonName, addonTable = ...
2 local ReAction = addonTable.ReAction 2 local ReAction = addonTable.ReAction
3 local L = ReAction.L 3 local L = ReAction.L
4 local _G = _G 4 local _G = _G
5 local CreateFrame = CreateFrame
6 local format = string.format 5 local format = string.format
7 local wipe = wipe 6 local wipe = wipe
8 7
9 local weak = { __mode="k" } 8 local weak = { __mode="k" }
10 9
20 -- Event handlers 19 -- Event handlers
21 function module:OnInitialize() 20 function module:OnInitialize()
22 self.handles = setmetatable({ }, weak) 21 self.handles = setmetatable({ }, weak)
23 22
24 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions") 23 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
25
26 ReAction.RegisterCallback(self, "OnCreateBar")
27 ReAction.RegisterCallback(self, "OnRefreshBar")
28 ReAction.RegisterCallback(self, "OnDestroyBar")
29 end 24 end
30 25
31 function module:OnEnable() 26 function module:OnEnable()
32 ReAction:GetModule("State"):RegisterStateProperty("page", nil, PropHandler.GetOptions(), PropHandler) 27 ReAction:GetModule("State"):RegisterStateProperty("page", nil, PropHandler.GetOptions(), PropHandler)
33 end 28 end
34 29
35 function module:OnDisable() 30 function module:OnDisable()
36 ReAction:GetModule("State"):UnregisterStateProperty("page") 31 ReAction:GetModule("State"):UnregisterStateProperty("page")
37 end 32 end
38 33
39 function module:OnCreateBar(event, bar, name)
40 if bar.config.type == moduleID then
41 if self.handles[bar] == nil then
42 self.handles[bar] = Handle:New(bar)
43 end
44 end
45 end
46
47 function module:OnRefreshBar(event, bar, name)
48 if self.handles[bar] then
49 self.handles[bar]:Refresh()
50 end
51 end
52
53 function module:OnDestroyBar(event, bar, name)
54 if self.handles[bar] then
55 self.handles[bar]:Destroy()
56 self.handles[bar] = nil
57 end
58 end
59 34
60 35
61 ---- Interface ---- 36 ---- Interface ----
62 function module:GetBarOptions(bar) 37 function module:GetBarOptions(bar)
63 local h = self.handles[bar] 38 self.handles[bar] = self.handles[bar] or Handle:New(bar)
64 if h then 39 return self.handles[bar]:GetOptions()
65 return h:GetOptions()
66 end
67 end 40 end
68 41
69 42
70 ---- Bar Handle ---- 43 ---- Bar Handle ----
71 44
202 } 175 }
203 176
204 local meta = { __index = Handle } 177 local meta = { __index = Handle }
205 178
206 function Handle:New( bar ) 179 function Handle:New( bar )
207 local config = bar:GetConfig() 180 return setmetatable(
208 local self = setmetatable(
209 { 181 {
210 bar = bar, 182 bar = bar,
211 config = config, 183 config = bar:GetConfig(),
212 btns = { }
213 }, 184 },
214 meta) 185 meta)
215
216 if self.config.buttons == nil then
217 self.config.buttons = { }
218 end
219 self:Refresh()
220 return self
221 end 186 end
222 187
223 function Handle:Refresh() 188 function Handle:Refresh()
224 local r, c = self.bar:GetButtonGrid() 189 Button:SetupBar(bar)
225 local n = r*c
226 local btnCfg = self.config.buttons
227 if n ~= #self.btns then
228 for i = 1, n do
229 if btnCfg[i] == nil then
230 btnCfg[i] = {}
231 end
232 if self.btns[i] == nil then
233 local lastButton = self:GetLastButton()
234 local hint = lastButton and lastButton.config.actionID
235 local b = Button:New(i, self.config, self.bar, hint)
236 self.btns[i] = b
237 self.bar:AddButton(i,b)
238 end
239 end
240 for i = n+1, #self.btns do
241 if self.btns[i] then
242 self.bar:RemoveButton(self.btns[i])
243 self.btns[i]:Destroy()
244 self.btns[i] = nil
245 btnCfg[i] = nil
246 end
247 end
248 end
249 for _, b in ipairs(self.btns) do
250 b:Refresh()
251 end
252 Button.SetupBarHeader(self.bar,self.config)
253 self:UpdateButtonLock()
254 end
255
256 function Handle:Destroy()
257 for _,b in pairs(self.btns) do
258 if b then
259 b:Destroy()
260 end
261 end
262 end 190 end
263 191
264 function Handle:UpdateButtonLock() 192 function Handle:UpdateButtonLock()
265 Button.SetButtonLock(self.bar, self.config.lockButtons, self.config.lockButtonsCombat) 193 Button.SetButtonLock(self.bar, self.config.lockButtons, self.config.lockButtonsCombat)
266 end 194 end
267 195
268 function Handle:GetLastButton() 196 function Handle:GetLastButton()
269 return self.btns[#self.btns] 197 return self.bar:GetButton(self.bar:GetNumButtons())
270 end 198 end
271 199
272 -- options handlers 200 -- options handlers
273 function Handle:GetOptions() 201 function Handle:GetOptions()
274 return { 202 return {
280 end 208 end
281 209
282 function Handle:SetHideEmpty(info, value) 210 function Handle:SetHideEmpty(info, value)
283 if value ~= self.config.hideEmpty then 211 if value ~= self.config.hideEmpty then
284 self.config.hideEmpty = value 212 self.config.hideEmpty = value
285 for _, b in pairs(self.btns) do 213 for _, b in self.bar:IterateButtons() do
286 b:ShowGrid(not value) 214 b:ShowGrid(not value)
287 end 215 end
288 end 216 end
289 end 217 end
290 218
437 function Handle:GetActionID() 365 function Handle:GetActionID()
438 local row = self.selectedRow or 1 366 local row = self.selectedRow or 1
439 local col = self.selectedColumn or 1 367 local col = self.selectedColumn or 1
440 local r, c = self.bar:GetButtonGrid() 368 local r, c = self.bar:GetButtonGrid()
441 local n = (row-1) * c + col 369 local n = (row-1) * c + col
442 local btn = self.btns[n] 370 local btn = self.bar:GetButton(n)
443 if btn then 371 if btn then
444 return tostring(btn:GetActionID(self.selectedPage or 1)) 372 return tostring(btn:GetActionID(self.selectedPage or 1))
445 end 373 end
446 end 374 end
447 375
448 function Handle:SetActionID(info, value) 376 function Handle:SetActionID(info, value)
449 local row = self.selectedRow or 1 377 local row = self.selectedRow or 1
450 local col = self.selectedColumn or 1 378 local col = self.selectedColumn or 1
451 local r, c = self.bar:GetButtonGrid() 379 local r, c = self.bar:GetButtonGrid()
452 local n = (row-1) * c + col 380 local n = (row-1) * c + col
453 local btn = self.btns[n] 381 local btn = self.bar:GetButton(n)
454 if btn then 382 if btn then
455 btn:SetActionID(tonumber(value), self.selectedPage or 1) 383 btn:SetActionID(tonumber(value), self.selectedPage or 1)
456 end 384 end
457 end 385 end
458 386
470 398
471 function Handle:GetMultiID() 399 function Handle:GetMultiID()
472 local p = { } 400 local p = { }
473 for i = 1, self.config.nPages or 1 do 401 for i = 1, self.config.nPages or 1 do
474 local b = { } 402 local b = { }
475 for _, btn in ipairs(self.btns) do 403 for _, btn in self.bar:IterateButtons() do
476 table.insert(b, btn:GetActionID(i)) 404 table.insert(b, btn:GetActionID(i))
477 end 405 end
478 table.insert(p, table.concat(b,",")) 406 table.insert(p, table.concat(b,","))
479 end 407 end
480 return table.concat(p,";\n") 408 return table.concat(p,";\n")
499 end 427 end
500 return p 428 return p
501 end 429 end
502 430
503 function Handle:SetMultiID(info, value) 431 function Handle:SetMultiID(info, value)
504 local p = ParseMultiID(#self.btns, self.config.nPages or 1, value) 432 local p = ParseMultiID(self.bar:GetNumButtons(), self.config.nPages or 1, value)
505 for page, b in ipairs(p) do 433 for page, b in ipairs(p) do
506 for button, id in ipairs(b) do 434 for button, id in ipairs(b) do
507 self.btns[button]:SetActionID(id, page) 435 self.bar:GetButton(button):SetActionID(id, page)
508 end 436 end
509 end 437 end
510 end 438 end
511 439
512 function Handle:ValidateMultiID(info, value) 440 function Handle:ValidateMultiID(info, value)
513 local bad = L["Invalid action ID list string"] 441 local bad = L["Invalid action ID list string"]
514 if value == nil or ParseMultiID(#self.btns, self.config.nPages or 1, value) == nil then 442 if value == nil or ParseMultiID(self.bar:GetNumButtons(), self.config.nPages or 1, value) == nil then
515 return bad 443 return bad
516 end 444 end
517 return true 445 return true
518 end 446 end
519 end 447 end