Change the lastAddedControl into OOP model (with panel itself)

Scott Yeskie [05-23-14 - 19:25]
Change the lastAddedControl into OOP model (with panel itself)
Filename
lib/LibAddonMenu-1.0.lua
diff --git a/lib/LibAddonMenu-1.0.lua b/lib/LibAddonMenu-1.0.lua
index 73d74f9..ca3dde1 100644
--- a/lib/LibAddonMenu-1.0.lua
+++ b/lib/LibAddonMenu-1.0.lua
@@ -1,10 +1,8 @@
-local MAJOR, MINOR = "LibAddonMenu-1.0", 7
+local MAJOR, MINOR = "LibAddonMenu-1.0", 10
 local lam, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
 if not lam then return end	--the same or newer version of this lib is already loaded into memory

 --UPVALUES--
-lam.lastAddedControl = {}
-local lastAddedControl = lam.lastAddedControl
 local wm = GetWindowManager()
 local strformat = string.format
 local tostring = tostring
@@ -38,7 +36,8 @@ end
 function lamPanel.new(controlPanelID, controlPanelName)
     local self = setmetatable({}, lamPanel)
     self.panelName = controlPanelName
-    self.uniqCounter = 1
+    self.uniqCounter = 0
+    self.lastAddedControl = nil

     if _G[controlPanelID] then
         self.panelID = _G[controlPanelID]
@@ -57,10 +56,10 @@ end
 function lamPanel:AddHeader(text)
     local isSubMenu = type(self.panelID) == "userdata"
     local controlName = self:getUniqueName()
-    --local header = wm:CreateControlFromVirtual(controlName, optionsWindow, lastAddedControl[panelID] and "ZO_Options_SectionTitle_WithDivider" or "ZO_Options_SectionTitle")
-    local header = wm:CreateControlFromVirtual(controlName, isSubMenu and self.panelID or optionsWindow, lastAddedControl[self.panelID] and "ZO_Options_SectionTitle_WithDivider" or "ZO_Options_SectionTitle")
-    if lastAddedControl[self.panelID] then
-        header:SetAnchor(TOPLEFT, lastAddedControl[self.panelID], BOTTOMLEFT, 0, 15)
+    --local header = wm:CreateControlFromVirtual(controlName, optionsWindow, self.lastAddedControl and "ZO_Options_SectionTitle_WithDivider" or "ZO_Options_SectionTitle")
+    local header = wm:CreateControlFromVirtual(controlName, isSubMenu and self.panelID or optionsWindow, self.lastAddedControl and "ZO_Options_SectionTitle_WithDivider" or "ZO_Options_SectionTitle")
+    if self.lastAddedControl then
+        header:SetAnchor(TOPLEFT, self.lastAddedControl, BOTTOMLEFT, 0, 15)
     else
         header:SetAnchor(TOPLEFT)
         if not isSubMenu then
@@ -85,7 +84,7 @@ function lamPanel:AddHeader(text)
     ZO_OptionsWindow_InitializeControl(header)
     if isSubMenu then header:SetParent(self.panelID) end

-    lastAddedControl[self.panelID] = header
+    self.lastAddedControl = header

     return header
 end
@@ -100,7 +99,7 @@ function lamPanel:AddSlider(text, tooltip, minValue, maxValue, step, getFunc, se
     local controlName = self:getUniqueName()
     local isSubMenu = type(panelID) == "userdata"
     local slider = wm:CreateControlFromVirtual(controlName, isSubMenu and panelID or optionsWindow, "ZO_Options_Slider")
-    slider:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 6)
+    slider:SetAnchor(TOPLEFT, self.lastAddedControl, BOTTOMLEFT, 0, 6)
     slider.controlType = OPTIONS_SLIDER
     slider.system = SETTING_TYPE_UI
     slider.panel = isSubMenu and panelID.panel or panelID
@@ -137,7 +136,7 @@ function lamPanel:AddSlider(text, tooltip, minValue, maxValue, step, getFunc, se
     ZO_OptionsWindow_InitializeControl(slider)
     if isSubMenu then slider:SetParent(panelID) end

-    lastAddedControl[panelID] = slider
+    self.lastAddedControl = slider

     return slider
 end
@@ -147,7 +146,7 @@ function lamPanel:AddDropdown(text, tooltip, validChoices, getFunc, setFunc, war
     local controlName = self:getUniqueName()
     local isSubMenu = type(panelID) == "userdata"
     local dropdown = wm:CreateControlFromVirtual(controlName, isSubMenu and panelID or optionsWindow, "ZO_Options_Dropdown")
-    dropdown:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 6)
+    dropdown:SetAnchor(TOPLEFT, self.lastAddedControl, BOTTOMLEFT, 0, 6)
     dropdown.controlType = OPTIONS_DROPDOWN
     dropdown.system = SETTING_TYPE_UI
     dropdown.panel = isSubMenu and panelID.panel or panelID
@@ -177,7 +176,7 @@ function lamPanel:AddDropdown(text, tooltip, validChoices, getFunc, setFunc, war
     ZO_OptionsWindow_InitializeControl(dropdown)
     if isSubMenu then dropdown:SetParent(panelID) end

-    lastAddedControl[panelID] = dropdown
+    self.lastAddedControl = dropdown

     return dropdown
 end
@@ -187,7 +186,7 @@ function lamPanel:AddCheckbox(text, tooltip, getFunc, setFunc, warning, warningT
     local controlName = self:getUniqueName()
     local isSubMenu = type(panelID) == "userdata"
     local checkbox = wm:CreateControlFromVirtual(controlName, isSubMenu and panelID or optionsWindow, "ZO_Options_Checkbox")
-    checkbox:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 6)
+    checkbox:SetAnchor(TOPLEFT, self.lastAddedControl, BOTTOMLEFT, 0, 6)
     checkbox.controlType = OPTIONS_CHECKBOX
     checkbox.system = SETTING_TYPE_UI
     checkbox.settingId = _G[strformat("SETTING_%s", controlName)]
@@ -212,7 +211,7 @@ function lamPanel:AddCheckbox(text, tooltip, getFunc, setFunc, warning, warningT
     ZO_OptionsWindow_InitializeControl(checkbox)
     if isSubMenu then checkbox:SetParent(panelID) end

-    lastAddedControl[panelID] = checkbox
+    self.lastAddedControl = checkbox

     return checkbox
 end
@@ -222,7 +221,7 @@ function lamPanel:AddColorPicker(text, tooltip, getFunc, setFunc, warning, warni
     local controlName = self:getUniqueName()
     local isSubMenu = type(panelID) == "userdata"
     local colorpicker = wm:CreateTopLevelWindow(controlName)
-    colorpicker:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 10)
+    colorpicker:SetAnchor(TOPLEFT, self.lastAddedControl, BOTTOMLEFT, 0, 10)
     colorpicker:SetParent(isSubMenu and panelID or optionsWindow)
     colorpicker:SetResizeToFitDescendents(true)
     colorpicker:SetWidth(510)
@@ -286,7 +285,7 @@ function lamPanel:AddColorPicker(text, tooltip, getFunc, setFunc, warning, warni
     ZO_OptionsWindow_InitializeControl(colorpicker)
     if isSubMenu then colorpicker:SetParent(panelID) end

-    lastAddedControl[panelID] = colorpicker
+    self.lastAddedControl = colorpicker

     return colorpicker
 end
@@ -296,7 +295,7 @@ function lamPanel:AddEditBox(tooltip, isMultiLine, getFunc, setFunc, warning, wa
     local controlName = self:getUniqueName()
     local isSubMenu = type(panelID) == "userdata"
     local editbox = wm:CreateTopLevelWindow(controlName)
-    editbox:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 10)
+    editbox:SetAnchor(TOPLEFT, self.lastAddedControl, BOTTOMLEFT, 0, 10)
     editbox:SetResizeToFitDescendents(true)
     editbox:SetWidth(510)
     editbox:SetMouseEnabled(true)
@@ -333,7 +332,7 @@ function lamPanel:AddEditBox(tooltip, isMultiLine, getFunc, setFunc, warning, wa
     ZO_OptionsWindow_InitializeControl(editbox)
     editbox:SetParent(isSubMenu and panelID or optionsWindow)

-    lastAddedControl[panelID] = editbox
+    self.lastAddedControl = editbox

     return editbox
 end
@@ -344,7 +343,7 @@ function lamPanel:AddButton(text, tooltip, onClick, warning, warningText)
     local controlName = self:getUniqueName()
     local isSubMenu = type(panelID) == "userdata"
     local button = wm:CreateTopLevelWindow(controlName)
-    button:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 6)
+    button:SetAnchor(TOPLEFT, self.lastAddedControl, BOTTOMLEFT, 0, 6)
     button:SetDimensions(510, 28)
     button:SetMouseEnabled(true)

@@ -371,7 +370,7 @@ function lamPanel:AddButton(text, tooltip, onClick, warning, warningText)
     ZO_OptionsWindow_InitializeControl(button)
     button:SetParent(isSubMenu and panelID or optionsWindow)

-    lastAddedControl[panelID] = button
+    self.lastAddedControl = button
     return button
 end

@@ -380,7 +379,7 @@ function lamPanel:AddDescription(text, titleText)
     local controlName = self:getUniqueName()
     local isSubMenu = type(panelID) == "userdata"
     local textBox = wm:CreateTopLevelWindow(controlName)
-    textBox:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 10)
+    textBox:SetAnchor(TOPLEFT, self.lastAddedControl, BOTTOMLEFT, 0, 10)
     textBox:SetResizeToFitDescendents(true)
     textBox:SetWidth(510)

@@ -411,7 +410,7 @@ function lamPanel:AddDescription(text, titleText)
     ZO_OptionsWindow_InitializeControl(textBox)
     textBox:SetParent(isSubMenu and panelID or optionsWindow)

-    lastAddedControl[panelID] = textBox
+    self.lastAddedControl = textBox

     return textBox
 end
@@ -423,7 +422,7 @@ function lamPanel:AddSubMenu(text, tooltip)
     local controlName = self:getUniqueName()
     local menubtn = wm:CreateTopLevelWindow(controlName)
     menubtn:SetParent(optionsWindow)
-    menubtn:SetAnchor(TOPLEFT, lastAddedControl[panelID], BOTTOMLEFT, 0, 6)
+    menubtn:SetAnchor(TOPLEFT, self.lastAddedControl, BOTTOMLEFT, 0, 6)
     menubtn:SetDimensions(510, 28)
     menubtn:SetMouseEnabled(true)

@@ -488,7 +487,7 @@ function lamPanel:AddSubMenu(text, tooltip)

     ZO_OptionsWindow_InitializeControl(menubtn)

-    lastAddedControl[panelID] = menubtn
+    self.lastAddedControl = menubtn

     lam:AddHeader(scroll, controlName.."MenuHeader", text)	--create the header for our menu