diff 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
line wrap: on
line diff
--- a/classes/Bar.lua	Tue Mar 22 11:48:09 2011 -0700
+++ b/classes/Bar.lua	Tue Mar 22 17:05:51 2011 -0700
@@ -184,12 +184,11 @@
 
 ---- Bar class ----
 local Bar   = { }
-local weak  = { __mode = "k" }
 local frameList = { }
 
 ReAction.Bar = Bar -- export to ReAction
 
-function Bar:New( name, config )
+function Bar:New( name, config, buttonClass )
   if type(config) ~= "table" then
     error("ReAction.Bar: config table required")
   end
@@ -197,11 +196,12 @@
   -- create new self
   self = setmetatable( 
     { 
-      config  = config,
-      name    = name,
-      buttons = setmetatable( { }, weak ),
-      width   = config.width or 480,
-      height  = config.height or 40,
+      config      = config,
+      name        = name,
+      buttons     = { },
+      buttonClass = buttonClass,
+      width       = config.width or 480,
+      height      = config.height or 40,
     }, 
     {__index = self} )
   
@@ -261,11 +261,16 @@
 
   ReAction.RegisterCallback(self, "OnConfigModeChanged")
 
+  buttonClass:SetupBar(self)
+
   return self
 end
 
 function Bar:Destroy()
   local f = self:GetFrame()
+  for idx, b in self:IterateButtons() do
+    b:Destroy()
+  end
   f:UnregisterAllEvents()
   self:ShowControls(false)
   ReAction.UnregisterAllCallbacks(self)
@@ -302,7 +307,7 @@
     -- LBF doesn't offer a method of renaming a group, so delete and remake the group.
     local c = self.config.ButtonFacade
     local g = ReAction.LBF:Group(L["ReAction"], name)
-    for b in self:IterateButtons() do
+    for idx, b in self:IterateButtons() do
       self.LBFGroup:RemoveButton(b:GetFrame(), true)
       g:AddButton(b:GetFrame())
     end
@@ -322,6 +327,10 @@
   error("Invalid Bar object: used without initialization")
 end
 
+function Bar:GetButton(idx)
+  return self.buttons[idx]
+end
+
 function Bar:GetConfig()
   return self.config
 end
@@ -394,6 +403,7 @@
     cfg.btnColumns = c
     cfg.spacing = s
   end
+  self.buttonClass:SetupBar(self)
   ReAction:RefreshBar(self)
 end
 
@@ -409,7 +419,7 @@
 end
 
 function Bar:IterateButtons()
-  -- iterator returns button, idx and does NOT iterate in index order
+  -- iterator returns idx, button, but does NOT iterate in index order
   return pairs(self.buttons)
 end
 
@@ -420,7 +430,7 @@
 function Bar:SetConfigMode(mode)
   self:SetSecureData("showAll",mode)
   self:ShowControls(mode)
-  for b in self:IterateButtons() do
+  for idx, b in self:IterateButtons() do
     b:ShowGridTemp(mode)
     b:UpdateActionIDLabel(mode)
   end
@@ -428,7 +438,7 @@
 
 function Bar:SetKeybindMode(mode)
   self:SetSecureData("showAll",mode)
-  for b in self:IterateButtons() do
+  for idx, b in self:IterateButtons() do
     b:SetKeybindMode(mode)
   end
 end
@@ -473,8 +483,7 @@
 function Bar:AddButton(idx, button)
   local f = self:GetFrame()
 
-  -- store in a weak reverse-index array
-  self.buttons[button] = idx
+  self.buttons[idx] = button
 
   -- Store a properly wrapped reference to the child frame as an attribute 
   -- (accessible via "frameref-btn#")
@@ -484,10 +493,10 @@
 end
 
 function Bar:RemoveButton(button)
-  local idx = self.buttons[button]
+  local idx = button:GetIndex()
   if idx then
     self:GetFrame():SetAttribute(format("frameref-btn%d",idx),nil)
-    self.buttons[button] = nil
+    self.buttons[idx] = nil
   end
   if self.LBFGroup then
     self.LBFGroup:RemoveButton(button:GetFrame(),true)
@@ -495,7 +504,7 @@
 end
 
 function Bar:PlaceButton(button, baseW, baseH)
-  local idx = self.buttons[button]
+  local idx = button:GetIndex()
   if idx then 
     local r, c, s = self:GetButtonGrid()
     local bh, bw = self:GetButtonSize()
@@ -517,7 +526,7 @@
 end
 
 function Bar:UpdateShowGrid()
-  for button in self:IterateButtons() do
+  for idx, button in self:IterateButtons() do
     button:UpdateShowGrid()
   end
 end
@@ -675,7 +684,9 @@
     f:SetAttribute("unit",unit)
   end
   if enable then
-    RegisterUnitWatch(self:GetFrame(),true)
+    if not self.unitwatch then
+      RegisterUnitWatch(self:GetFrame(),true)
+    end
   elseif self.unitwatch then
     UnregisterUnitWatch(self:GetFrame())
   end