diff classes/Button.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 158c9299185b
children 1663c78a7f31
line wrap: on
line diff
--- a/classes/Button.lua	Tue Mar 22 11:48:09 2011 -0700
+++ b/classes/Button.lua	Tue Mar 22 17:05:51 2011 -0700
@@ -112,6 +112,10 @@
   return self.frame
 end
 
+function Button:GetIndex()
+  return self.idx
+end
+
 function Button:GetName()
   return self.name
 end
@@ -142,6 +146,50 @@
   self.actionMaxID = maxID
 end
 
+function Button:SetupBar( bar )
+  local config = bar:GetConfig()
+  if not config.buttons then
+    config.buttons = { }
+  end
+  local btnCfg = config.buttons
+
+  local r, c = bar:GetButtonGrid()
+  local n = r*c
+
+  local hint = nil
+  local i = 1
+  repeat
+    local b = bar:GetButton(i)
+    if b then
+      if i > n then
+        bar:RemoveButton(b)
+        b:Destroy()
+        btnCfg[i] = nil
+      else
+        b:Refresh()
+        hint = b:GetActionID()
+      end
+    elseif i <= n then
+      local cfg = btnCfg[i] or { }
+      local success, r = pcall(self.New, self, cfg, bar, i, hint)  -- note call semantics for derived class constructors
+      if success and r then
+        b = r
+        bar:AddButton(i,b)
+        btnCfg[i] = cfg
+        b:Refresh()
+        hint = b:GetActionID()
+      else
+        n = i - 1
+        bar:ClipNButtons(n)
+        if not success then
+          geterrorhandler()(r)
+        end
+      end
+    end
+    i = i + 1
+  until b == nil
+end
+
 function Button:AcquireActionID( id, hint, unique )
   local poolID = self.actionPoolID
   local maxID = self.actionMaxID