comparison classes/Bar.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 8ba8ab8bf6dd
children 704f4a05a1d7
comparison
equal deleted inserted replaced
233:9b9f5fc84d34 234:0e20f65375d5
182 end 182 end
183 183
184 184
185 ---- Bar class ---- 185 ---- Bar class ----
186 local Bar = { } 186 local Bar = { }
187 local weak = { __mode = "k" }
188 local frameList = { } 187 local frameList = { }
189 188
190 ReAction.Bar = Bar -- export to ReAction 189 ReAction.Bar = Bar -- export to ReAction
191 190
192 function Bar:New( name, config ) 191 function Bar:New( name, config, buttonClass )
193 if type(config) ~= "table" then 192 if type(config) ~= "table" then
194 error("ReAction.Bar: config table required") 193 error("ReAction.Bar: config table required")
195 end 194 end
196 195
197 -- create new self 196 -- create new self
198 self = setmetatable( 197 self = setmetatable(
199 { 198 {
200 config = config, 199 config = config,
201 name = name, 200 name = name,
202 buttons = setmetatable( { }, weak ), 201 buttons = { },
203 width = config.width or 480, 202 buttonClass = buttonClass,
204 height = config.height or 40, 203 width = config.width or 480,
204 height = config.height or 40,
205 }, 205 },
206 {__index = self} ) 206 {__index = self} )
207 207
208 -- The frame type is 'Button' in order to have an OnClick handler. However, the frame itself is 208 -- The frame type is 'Button' in order to have an OnClick handler. However, the frame itself is
209 -- not mouse-clickable by the user. 209 -- not mouse-clickable by the user.
259 self.LBFGroup = g 259 self.LBFGroup = g
260 end 260 end
261 261
262 ReAction.RegisterCallback(self, "OnConfigModeChanged") 262 ReAction.RegisterCallback(self, "OnConfigModeChanged")
263 263
264 buttonClass:SetupBar(self)
265
264 return self 266 return self
265 end 267 end
266 268
267 function Bar:Destroy() 269 function Bar:Destroy()
268 local f = self:GetFrame() 270 local f = self:GetFrame()
271 for idx, b in self:IterateButtons() do
272 b:Destroy()
273 end
269 f:UnregisterAllEvents() 274 f:UnregisterAllEvents()
270 self:ShowControls(false) 275 self:ShowControls(false)
271 ReAction.UnregisterAllCallbacks(self) 276 ReAction.UnregisterAllCallbacks(self)
272 LKB.UnregisterAllCallbacks(self) 277 LKB.UnregisterAllCallbacks(self)
273 if self.LBFGroup then 278 if self.LBFGroup then
300 function Bar:SetName(name) 305 function Bar:SetName(name)
301 if self.LBFGroup then 306 if self.LBFGroup then
302 -- LBF doesn't offer a method of renaming a group, so delete and remake the group. 307 -- LBF doesn't offer a method of renaming a group, so delete and remake the group.
303 local c = self.config.ButtonFacade 308 local c = self.config.ButtonFacade
304 local g = ReAction.LBF:Group(L["ReAction"], name) 309 local g = ReAction.LBF:Group(L["ReAction"], name)
305 for b in self:IterateButtons() do 310 for idx, b in self:IterateButtons() do
306 self.LBFGroup:RemoveButton(b:GetFrame(), true) 311 self.LBFGroup:RemoveButton(b:GetFrame(), true)
307 g:AddButton(b:GetFrame()) 312 g:AddButton(b:GetFrame())
308 end 313 end
309 self.LBFGroup:Delete(true) 314 self.LBFGroup:Delete(true)
310 self.LBFGroup = g 315 self.LBFGroup = g
318 323
319 function Bar:GetFrame() 324 function Bar:GetFrame()
320 -- this method is included for documentation purposes. It is overridden 325 -- this method is included for documentation purposes. It is overridden
321 -- for each object in the :New() method. 326 -- for each object in the :New() method.
322 error("Invalid Bar object: used without initialization") 327 error("Invalid Bar object: used without initialization")
328 end
329
330 function Bar:GetButton(idx)
331 return self.buttons[idx]
323 end 332 end
324 333
325 function Bar:GetConfig() 334 function Bar:GetConfig()
326 return self.config 335 return self.config
327 end 336 end
392 local cfg = self.config 401 local cfg = self.config
393 cfg.btnRows = r 402 cfg.btnRows = r
394 cfg.btnColumns = c 403 cfg.btnColumns = c
395 cfg.spacing = s 404 cfg.spacing = s
396 end 405 end
406 self.buttonClass:SetupBar(self)
397 ReAction:RefreshBar(self) 407 ReAction:RefreshBar(self)
398 end 408 end
399 409
400 function Bar:GetAlpha() 410 function Bar:GetAlpha()
401 return self.config.alpha or 1.0 411 return self.config.alpha or 1.0
407 self:UpdateDefaultStateAlpha() 417 self:UpdateDefaultStateAlpha()
408 ReAction:RefreshBar(self) 418 ReAction:RefreshBar(self)
409 end 419 end
410 420
411 function Bar:IterateButtons() 421 function Bar:IterateButtons()
412 -- iterator returns button, idx and does NOT iterate in index order 422 -- iterator returns idx, button, but does NOT iterate in index order
413 return pairs(self.buttons) 423 return pairs(self.buttons)
414 end 424 end
415 425
416 -- 426 --
417 -- Methods 427 -- Methods
418 -- 428 --
419 429
420 function Bar:SetConfigMode(mode) 430 function Bar:SetConfigMode(mode)
421 self:SetSecureData("showAll",mode) 431 self:SetSecureData("showAll",mode)
422 self:ShowControls(mode) 432 self:ShowControls(mode)
423 for b in self:IterateButtons() do 433 for idx, b in self:IterateButtons() do
424 b:ShowGridTemp(mode) 434 b:ShowGridTemp(mode)
425 b:UpdateActionIDLabel(mode) 435 b:UpdateActionIDLabel(mode)
426 end 436 end
427 end 437 end
428 438
429 function Bar:SetKeybindMode(mode) 439 function Bar:SetKeybindMode(mode)
430 self:SetSecureData("showAll",mode) 440 self:SetSecureData("showAll",mode)
431 for b in self:IterateButtons() do 441 for idx, b in self:IterateButtons() do
432 b:SetKeybindMode(mode) 442 b:SetKeybindMode(mode)
433 end 443 end
434 end 444 end
435 445
436 function Bar:ApplyAnchor() 446 function Bar:ApplyAnchor()
471 end 481 end
472 482
473 function Bar:AddButton(idx, button) 483 function Bar:AddButton(idx, button)
474 local f = self:GetFrame() 484 local f = self:GetFrame()
475 485
476 -- store in a weak reverse-index array 486 self.buttons[idx] = button
477 self.buttons[button] = idx
478 487
479 -- Store a properly wrapped reference to the child frame as an attribute 488 -- Store a properly wrapped reference to the child frame as an attribute
480 -- (accessible via "frameref-btn#") 489 -- (accessible via "frameref-btn#")
481 f:SetFrameRef(format("btn%d",idx), button:GetFrame()) 490 f:SetFrameRef(format("btn%d",idx), button:GetFrame())
482 491
483 -- button constructors are responsible for calling SkinButton 492 -- button constructors are responsible for calling SkinButton
484 end 493 end
485 494
486 function Bar:RemoveButton(button) 495 function Bar:RemoveButton(button)
487 local idx = self.buttons[button] 496 local idx = button:GetIndex()
488 if idx then 497 if idx then
489 self:GetFrame():SetAttribute(format("frameref-btn%d",idx),nil) 498 self:GetFrame():SetAttribute(format("frameref-btn%d",idx),nil)
490 self.buttons[button] = nil 499 self.buttons[idx] = nil
491 end 500 end
492 if self.LBFGroup then 501 if self.LBFGroup then
493 self.LBFGroup:RemoveButton(button:GetFrame(),true) 502 self.LBFGroup:RemoveButton(button:GetFrame(),true)
494 end 503 end
495 end 504 end
496 505
497 function Bar:PlaceButton(button, baseW, baseH) 506 function Bar:PlaceButton(button, baseW, baseH)
498 local idx = self.buttons[button] 507 local idx = button:GetIndex()
499 if idx then 508 if idx then
500 local r, c, s = self:GetButtonGrid() 509 local r, c, s = self:GetButtonGrid()
501 local bh, bw = self:GetButtonSize() 510 local bh, bw = self:GetButtonSize()
502 local row, col = floor((idx-1)/c), fmod((idx-1),c) -- zero-based 511 local row, col = floor((idx-1)/c), fmod((idx-1),c) -- zero-based
503 local x, y = col*bw + (col+0.5)*s, -(row*bh + (row+0.5)*s) 512 local x, y = col*bw + (col+0.5)*s, -(row*bh + (row+0.5)*s)
515 self.LBFGroup:AddButton(button:GetFrame(), data) 524 self.LBFGroup:AddButton(button:GetFrame(), data)
516 end 525 end
517 end 526 end
518 527
519 function Bar:UpdateShowGrid() 528 function Bar:UpdateShowGrid()
520 for button in self:IterateButtons() do 529 for idx, button in self:IterateButtons() do
521 button:UpdateShowGrid() 530 button:UpdateShowGrid()
522 end 531 end
523 end 532 end
524 533
525 function Bar:ShowControls(show) 534 function Bar:ShowControls(show)
673 local f = self:GetFrame() 682 local f = self:GetFrame()
674 if unit then 683 if unit then
675 f:SetAttribute("unit",unit) 684 f:SetAttribute("unit",unit)
676 end 685 end
677 if enable then 686 if enable then
678 RegisterUnitWatch(self:GetFrame(),true) 687 if not self.unitwatch then
688 RegisterUnitWatch(self:GetFrame(),true)
689 end
679 elseif self.unitwatch then 690 elseif self.unitwatch then
680 UnregisterUnitWatch(self:GetFrame()) 691 UnregisterUnitWatch(self:GetFrame())
681 end 692 end
682 self.unitwatch = enable 693 self.unitwatch = enable
683 self:RefreshSecureState() 694 self:RefreshSecureState()