diff Editor.lua @ 216:376c424900f0

cleanup: editor bar options table generation
author Flick <flickerstreak@gmail.com>
date Fri, 19 Nov 2010 14:33:45 -0800
parents 85213d045acb
children e63aefb8a555
line wrap: on
line diff
--- a/Editor.lua	Fri Nov 19 14:33:11 2010 -0800
+++ b/Editor.lua	Fri Nov 19 14:33:45 2010 -0800
@@ -173,12 +173,9 @@
 
   ReAction.RegisterCallback(self,"OnCreateBar")
   ReAction.RegisterCallback(self,"OnDestroyBar")
-  ReAction.RegisterCallback(self,"OnEraseBar")
   ReAction.RegisterCallback(self,"OnRenameBar")
 
-  for name, bar in ReAction:IterateBars() do
-    self:CreateBarTree(bar)
-  end
+  self:RefreshBarOptions()
 
   return self
 end
@@ -203,160 +200,174 @@
   AceConfigReg:NotifyChange(self.configID)
 end
 
-function Editor:CreateBarTree(bar)
+function Editor:UpdateBarOptions(bar)
   local name = bar:GetName()
-  -- AceConfig doesn't allow spaces, etc, in arg key names, and they must be
-  -- unique strings. So generate a unique key (it can be whatever) for the bar
+  local key  = self.barOptMap[name]
   local args = self.options.args
-  local key
-  local i = 1
-  repeat
-    key = ("bar%s"):format(i)
-    i = i+1
-  until args[key] == nil
-  self.barOptMap[name] = key
-  args[key] = { 
-    type = "group",
-    name = name,
-    childGroups = "tab",
-    order = i+100,
-    args = {
-      general = {
-        type = "group",
-        name = L["General"],
-        order = 1,
-        args = {
-          name = {
-            type = "input",
-            name = L["Rename Bar"],
-            get  = function() return bar:GetName() end,
-            set  = function(info, value) return ReAction:RenameBar(bar, value) end,
-            order = 1,
-          },
-          delete = {
-            type = "execute",
-            name = L["Delete Bar"],
-            desc = function() return bar:GetName() end,
-            confirm = true,
-            func = function() ReAction:EraseBar(bar) end,
-            order = 2
-          },
-          anchor = {
-            type = "group",
-            name = L["Anchor"],
-            inline = true,
-            args = {
-              frame = {
-                type = "input",
-                name = L["Frame"],
-                desc = L["The frame that the bar is anchored to"],
-                get  = function() local _, f = bar:GetAnchor(); return f end,
-                set  = function(info, val) bar:SetAnchor(nil,val) end,
-                validate = function(info, name) 
-                    if name then
-                      local f = ReAction:GetBar(name)
-                      if f then
-                        return true
-                      else
-                        f = _G[name]
-                        if f and type(f) == "table" and f.IsObjectType and f:IsObjectType("Frame") then
-                          local _, explicit = f:IsProtected()
-                          return explicit
+
+  if not key then
+    -- AceConfig doesn't allow spaces, etc, in arg key names, and they must be
+    -- unique strings. So generate a unique key (it can be whatever) for the bar
+    local i = 1
+    repeat
+      key = ("bar%s"):format(i)
+      i = i+1
+    until args[key] == nil
+    self.barOptMap[name] = key
+
+    args[key] = { 
+      type = "group",
+      name = name,
+      childGroups = "tab",
+      order = i+100,
+      args = {
+        general = {
+          type = "group",
+          name = L["General"],
+          order = 1,
+          args = {
+            name = {
+              type = "input",
+              name = L["Rename Bar"],
+              get  = function() return bar:GetName() end,
+              set  = function(info, value) return ReAction:RenameBar(bar, value) end,
+              order = 1,
+            },
+            delete = {
+              type = "execute",
+              name = L["Delete Bar"],
+              desc = function() return bar:GetName() end,
+              confirm = true,
+              func = function() ReAction:EraseBar(bar) end,
+              order = 2
+            },
+            anchor = {
+              type = "group",
+              name = L["Anchor"],
+              inline = true,
+              args = {
+                frame = {
+                  type = "input",
+                  name = L["Frame"],
+                  desc = L["The frame that the bar is anchored to"],
+                  get  = function() local _, f = bar:GetAnchor(); return f end,
+                  set  = function(info, val) bar:SetAnchor(nil,val) end,
+                  validate = function(info, name) 
+                      if name then
+                        local f = ReAction:GetBar(name)
+                        if f then
+                          return true
+                        else
+                          f = _G[name]
+                          if f and type(f) == "table" and f.IsObjectType and f:IsObjectType("Frame") then
+                            local _, explicit = f:IsProtected()
+                            return explicit
+                          end
                         end
                       end
-                    end
-                    return false
-                  end,
-                width = "double",
-                order = 1
+                      return false
+                    end,
+                  width = "double",
+                  order = 1
+                },
+                point = {
+                  type = "select",
+                  name = L["Point"],
+                  desc = L["Anchor point on the bar frame"],
+                  style = "dropdown",
+                  get  = function() return bar:GetAnchor() end,
+                  set  = function(info, val) bar:SetAnchor(val) end,
+                  values = pointTable,
+                  order = 2,
+                },
+                relativePoint = {
+                  type = "select",
+                  name = L["Relative Point"],
+                  desc = L["Anchor point on the target frame"],
+                  style = "dropdown",
+                  get  = function() local p,f,r = bar:GetAnchor(); return r end,
+                  set  = function(info, val) bar:SetAnchor(nil,nil,val) end,
+                  values = pointTable,
+                  order = 3,
+                },
+                x = {
+                  type = "input",
+                  pattern = "\-?%d+",
+                  name = L["X offset"],
+                  get = function() local p,f,r,x = bar:GetAnchor(); return ("%d"):format(x) end,
+                  set = function(info,val) bar:SetAnchor(nil,nil,nil,val) end,
+                  order = 4
+                },
+                y = {
+                  type = "input",
+                  pattern = "\-?%d+",
+                  name = L["Y offset"],
+                  get = function() local p,f,r,x,y = bar:GetAnchor(); return ("%d"):format(y) end,
+                  set = function(info,val) bar:SetAnchor(nil,nil,nil,nil,val) end,
+                  order = 5
+                },
               },
-              point = {
-                type = "select",
-                name = L["Point"],
-                desc = L["Anchor point on the bar frame"],
-                style = "dropdown",
-                get  = function() return bar:GetAnchor() end,
-                set  = function(info, val) bar:SetAnchor(val) end,
-                values = pointTable,
-                order = 2,
-              },
-              relativePoint = {
-                type = "select",
-                name = L["Relative Point"],
-                desc = L["Anchor point on the target frame"],
-                style = "dropdown",
-                get  = function() local p,f,r = bar:GetAnchor(); return r end,
-                set  = function(info, val) bar:SetAnchor(nil,nil,val) end,
-                values = pointTable,
-                order = 3,
-              },
-              x = {
-                type = "input",
-                pattern = "\-?%d+",
-                name = L["X offset"],
-                get = function() local p,f,r,x = bar:GetAnchor(); return ("%d"):format(x) end,
-                set = function(info,val) bar:SetAnchor(nil,nil,nil,val) end,
-                order = 4
-              },
-              y = {
-                type = "input",
-                pattern = "\-?%d+",
-                name = L["Y offset"],
-                get = function() local p,f,r,x,y = bar:GetAnchor(); return ("%d"):format(y) end,
-                set = function(info,val) bar:SetAnchor(nil,nil,nil,nil,val) end,
-                order = 5
-              },
+              order = 3
             },
-            order = 3
-          },
-          alpha = {
-            type = "range",
-            name = L["Transparency"],
-            get  = function() return bar:GetAlpha() end,
-            set  = function(info, val) bar:SetAlpha(val) end,
-            min = 0, 
-            max = 1,
-            isPercent = true,
-            step = 0.01,
-            bigStep = 0.05,
-            order = 4,
+            alpha = {
+              type = "range",
+              name = L["Transparency"],
+              get  = function() return bar:GetAlpha() end,
+              set  = function(info, val) bar:SetAlpha(val) end,
+              min = 0, 
+              max = 1,
+              isPercent = true,
+              step = 0.01,
+              bigStep = 0.05,
+              order = 4,
+            },
           },
         },
       },
+      plugins = { }
     }
-  }
-  self:RefreshBarOptions()
+  end
+
+  if ReAction.barOptionGenerators then
+    for module, func in pairs(ReAction.barOptionGenerators) do
+      local success, r
+      if type(func) == "string" then
+        success, r = pcall(module[func], module, bar)
+      else
+        success, r = pcall(func, bar)
+      end
+      if success then
+        if r then
+          args[key].plugins[module:GetName()] = { [module:GetName()] = r }
+        end
+      else
+        geterrorhandler()(r)
+      end
+    end
+  end
 end
 
 function Editor:RefreshBarOptions()
-  for name, key in pairs(self.barOptMap) do
-    local bar = ReAction:GetBar(name)
-    if bar and key and self.options.args[key] then
-      self.options.args[key].plugins = self:GenerateBarOptionsTable(bar)
+  for key, name in pairs(self.barOptMap) do
+    if not ReAction:GetBar(name) then
+      self.barOptMap[key] = nil
     end
   end
-  AceConfigReg:NotifyChange(self.configID)
+  for name, bar in ReAction:IterateBars() do
+    self:UpdateBarOptions(bar)
+  end
+  self:Refresh()
 end
 
 function Editor:OnCreateBar(evt, bar)
-  if not self.tmp.creating then
-    -- a bit of hack to work around OnCreateBar event handler ordering
-    self:CreateBarTree(bar)
-  end
+  self:UpdateBarOptions(bar)
+  self:Refresh()
 end
 
 function Editor:OnDestroyBar(evt, bar, name)
   local key = self.barOptMap[name]
   if key then
-    self.options.args[key] = nil
-  end
-  self:Refresh()
-end
-
-function Editor:OnEraseBar(evt, name)
-  local key = self.barOptMap[name]
-  self.barOptMap[name] = nil
-  if key then
+    self.barOptMap[name] = nil
     self.options.args[key] = nil
     self:Refresh()
   end
@@ -364,8 +375,8 @@
 
 function Editor:OnRenameBar(evt, bar, oldname, newname)
   local key = self.barOptMap[oldname]
-  self.barOptMap[oldname], self.barOptMap[newname] = nil, key
   if key then
+    self.barOptMap[oldname], self.barOptMap[newname] = nil, key
     self.options.args[key].name = newname
     self:Refresh()
   end
@@ -379,41 +390,14 @@
 
 function Editor:CreateBar()
   if self.tmp.barName and self.tmp.barName ~= "" then
-    self.tmp.creating = true
     local bar = ReAction:CreateBar(self.tmp.barName, self.tmp.barType or ReAction:GetDefaultBarType(), self.tmp.barRows, self.tmp.barCols, self.tmp.barSize, self.tmp.barSpacing)
     if bar then
-      self:CreateBarTree(bar)
       AceConfigDialog:SelectGroup(self.configID, self.barOptMap[self.tmp.barName])
       self.tmp.barName = nil
     end
-    self.tmp.creating = false
   end
 end
 
-function Editor:GenerateBarOptionsTable( bar )
-  local opts = { }
-  if not ReAction.barOptionGenerators then
-    return
-  end
-
-  for module, func in pairs(ReAction.barOptionGenerators) do
-    local success, r
-    if type(func) == "string" then
-      success, r = pcall(module[func], module, bar)
-    else
-      success, r = pcall(func, bar)
-    end
-    if success then
-      if r then
-        opts[module:GetName()] = { [module:GetName()] = r }
-      end
-    else
-      geterrorhandler()(r)
-    end
-  end
-  return opts
-end
-
 
 
 ---- Export to ReAction ----