Handle outside created panels by auto-populating internal tables

Scott Yeskie [05-23-14 - 22:59]
Handle outside created panels by auto-populating internal tables

This was needed to make Wykkyd's sub-Child panels work
Filename
lib/LibAddonMenu-1.0.lua
diff --git a/lib/LibAddonMenu-1.0.lua b/lib/LibAddonMenu-1.0.lua
index e7e7bed..57bf50b 100644
--- a/lib/LibAddonMenu-1.0.lua
+++ b/lib/LibAddonMenu-1.0.lua
@@ -18,7 +18,7 @@ lam.Panel.__index = lam.Panel

 setmetatable(lam.Panel, {
     __call = function (cls, ...)
-        return cls.new(...)
+        return cls.newControlPanel(...)
     end,
 })

@@ -33,24 +33,31 @@ function lam.Panel:getUniqueName()
     end
 end

-function lam.Panel.new(controlPanelID, controlPanelName)
+function lam.Panel.new(panelID, panelName)
+    if lam.panels[panelID] then
+        return lam.panels[panelID]
+    end
+
     local self = setmetatable({}, lam.Panel)
-    self.panelName = controlPanelName
+    self.panelName = panelName
     self.uniqCounter = 0
     self.lastAddedControl = nil
+    self.panelID = panelID
+    lam.panels[panelID] = self
+    return lam.panels[panelID]
+end

+function lam.Panel.newControlPanel(controlPanelID, controlPanelName)
     if _G[controlPanelID] then
-        self.panelID = _G[controlPanelID]
-        return lam.panels[self.panelID]
+        local panelID = _G[controlPanelID]
+        return lam.Panel.new(panelID, controlPanelName)
     end

     ZO_OptionsWindow_AddUserPanel(controlPanelID, controlPanelName)

     --disables Defaults button because we don't need it, but keybind still works :/ ...
-    self.panelID = _G[controlPanelID]
-
-    lam.panels[self.panelID] = self
-    return lam.panels[self.panelID]
+    local panelID = _G[controlPanelID]
+    return lam.Panel.new(panelID, controlPanelName)
 end

 function lam.Panel:AddHeader(text)
@@ -415,6 +422,24 @@ function lam.Panel:AddDescription(text, titleText)
     return textBox
 end

+function lam.getPanel(panelID)
+    if lam.panels[panelID] ~= nil then
+        return lam.panels[panelID]
+    end
+
+    --Try a lookup in _G
+    if _G[panelID] ~= nil then
+        local lutID = _G[panelID]
+        if lam.panels[lutID] ~= nil then
+            return lam.panels[lutID]
+        end
+    end
+
+    --Fallback - get a dummy pannel
+    lam.panels[panelID] = lam.Panel.new(panelID, "Panel" .. panelID)
+    return lam.panels[panelID]
+end
+
 --window doesn't hide when escape is pressed
 --color-picker is hidden for some reason
 function lam.Panel:AddSubMenu(text, tooltip)
@@ -492,10 +517,7 @@ function lam.Panel:AddSubMenu(text, tooltip)
     lam:AddHeader(scroll, controlName.."MenuHeader", text)	--create the header for our menu


-    local panel = setmetatable({}, lam.Panel)
-    panel.panelName = controlName
-    panel.panelID = panelID
-    self.panels[panelID] = panel
+    local panel = lam.Panel.new(panelID, controlName)
     return panel, scroll
 end

@@ -507,46 +529,46 @@ end

 function lam:AddHeader(panelID, controlName, ...)
     self.forceControlName = controlName
-    return self.panels[panelID]:AddHeader(...)
+    return lam.getPanel(panelID):AddHeader(...)
 end

 function lam:AddSlider(panelID, controlName, ...)
     self.forceControlName = controlName
-    return self.panels[panelID]:AddSlider(...)
+    return lam.getPanel(panelID):AddSlider(...)
 end

 function lam:AddDropdown(panelID, controlName, ...)
     self.forceControlName = controlName
-    return self.panels[panelID]:AddDropdown(...)
+    return lam.getPanel(panelID):AddDropdown(...)
 end

 function lam:AddCheckbox(panelID, controlName, ...)
     self.forceControlName = controlName
-    return self.panels[panelID]:AddCheckbox(...)
+    return lam.getPanel(panelID):AddCheckbox(...)
 end

 function lam:AddColorPicker(panelID, controlName, ...)
     self.forceControlName = controlName
-    return self.panels[panelID]:AddColorPicker(...)
+    return lam.getPanel(panelID):AddColorPicker(...)
 end

 function lam:AddEditBox(panelID, controlName, ...)
     self.forceControlName = controlName
-    return self.panels[panelID]:AddEditBox(...)
+    return lam.getPanel(panelID):AddEditBox(...)
 end

 function lam:AddButton(panelID, controlName, ...)
     self.forceControlName = controlName
-    return self.panels[panelID]:AddButton(...)
+    return lam.getPanel(panelID):AddButton(...)
 end

 function lam:AddDescription(panelID, controlName, ...)
     self.forceControlName = controlName
-    return self.panels[panelID]:AddDescription(...)
+    return lam.getPanel(panelID):AddDescription(...)
 end

 function lam:AddSubMenu(panelID, controlName, ...)
     self.forceControlName = controlName
-    local _, retval = self.panels[panelID]:AddSubMenu(...)
+    local _, retval = lam.getPanel(panelID):AddSubMenu(...)
     return retval
 end
\ No newline at end of file