dynamic "statictext" items

Zig Zichterman [03-01-16 - 21:57]
dynamic "statictext" items

To eventually receive progress/status
Filename
GuildGoldDeposits.lua
diff --git a/GuildGoldDeposits.lua b/GuildGoldDeposits.lua
index 390d6f3..d39fc4e 100644
--- a/GuildGoldDeposits.lua
+++ b/GuildGoldDeposits.lua
@@ -7,6 +7,7 @@ GuildGoldDeposits.default = {
       enable_guild  = { true, true, true, true, true }
     , duration_days = 7
 }
+GuildGoldDeposits.max_guild_ct = 5

 -- Init ----------------------------------------------------------------------

@@ -30,6 +31,14 @@ end

 -- UI ------------------------------------------------------------------------

+function GuildGoldDeposits.ref_cb(i)
+    return "GuildGoldDeposits_cbg" .. i
+end
+
+function GuildGoldDeposits.ref_desc(i)
+    return "GuildGoldDeposits_desc" .. i
+end
+
 function GuildGoldDeposits:CreateSettingsWindow()
     local panelData = {
         type                = "panel",
@@ -48,7 +57,7 @@ function GuildGoldDeposits:CreateSettingsWindow()
         { type      = "button"
         , name      = "Save Data Now"
         , tooltip   = "Save guild gold deposit data to file now."
-        , func      = function() GuildGoldDeposits:SaveNow() end
+        , func      = function() self:SaveNow() end
         },
         { type      = "header"
         , name      = "Duration"
@@ -67,47 +76,98 @@ function GuildGoldDeposits:CreateSettingsWindow()
         },
     }

-    for i = 1, 5 do
+    for i = 1, self.max_guild_ct do
         table.insert(optionsData,
             { type      = "checkbox"
             , name      = "(guild " .. i .. ")"
             , tooltip   = "Save data for guild " .. i .. "?"
             , getFunc   = function() return self.savedVariables.enable_guild[i] end
             , setFunc   = function(e) self.savedVariables.enable_guild[i] = e end
-            , reference = "GuildGoldDeposits_cbg" .. i
+            , reference = self.ref_cb(i)
+            })
+
+                        -- HACK: for some reason, I cannot get "description"
+                        -- items to dynamically update their text. Color and
+                        -- hidden, yes, but text? Nope, it never changes. So
+                        -- instead of a desc for static text, I'm going to use
+                        -- a "checkbox" with the on/off field hidden. Total
+                        -- hack. Sorry.
+        table.insert(optionsData,
+            { type      = "checkbox"
+            , name      = "(desc " .. i .. ")"
+            , reference = self.ref_desc(i)
+            , getFunc   = function() return false end
+            , setFunc   = function() end
             })
     end

     LAM2:RegisterOptionControls("GuildGoldDeposits", optionsData)
     CALLBACK_MANAGER:RegisterCallback("LAM-PanelControlsCreated"
-            , GuildGoldDeposits.OnPanelControlsCreated)
-
+            , self.OnPanelControlsCreated)
 end

 -- Delayed initialization of options panel: don't waste time fetching
 -- guild names until a human actually opens our panel.
 function GuildGoldDeposits.OnPanelControlsCreated(panel)
+    self = GuildGoldDeposits
     guild_ct = GetNumGuilds()
-    for i = 1,5 do
-        cb = _G["GuildGoldDeposits_cbg" .. i]
+    for i = 1,self.max_guild_ct do
+        cb = _G[self.ref_cb(i)]
         if i <= guild_ct then
             guildId   = GetGuildId(i)
             guildName = GetGuildName(guildId)
             cb.label:SetText(guildName)
             cb:SetHidden(false)
         else
-            -- If no guild #N, hide and disable it.
+                        -- If no guild #N, hide and disable it.
             cb:SetHidden(true)
-            GuildGoldDeposits.savedVariables.enable_guild[i] = false
+            self.savedVariables.enable_guild[i] = false
         end
+
+        desc = _G[self.ref_desc(i)]
+        self.ConvertCheckboxToText(desc)
     end
 end

+-- Coerce a checkbox to act like a text label.
+--
+-- I cannot get LibAddonMenu-2.0 "description" items to dynamically update
+-- their text. SetText() has no effect. But SetText() works on "checkbox"
+-- items, so beat those into a text-like UI element.
+function GuildGoldDeposits.ConvertCheckboxToText(cb)
+    desc:SetHandler("OnMouseEnter", nil)
+    desc:SetHandler("OnMouseExit",  nil)
+    desc:SetHandler("OnMouseUp",    nil)
+    desc.label:SetFont("ZoFontGame")
+    desc.label:SetText("-")
+    desc.checkbox:SetHidden(true)
+end
+
 -- Saving Guild Data ---------------------------------------------------------

 function GuildGoldDeposits:SaveNow()
+    -- self:DumpSettings()
+    for i = 1, self.max_guild_ct do
+        self:SaveGuildIndex(i)
+    end
+end
+
+function GuildGoldDeposits:SaveGuildIndex(i)
+    guildId = GetGuildId(i)
+    desc = _G[GuildGoldDeposits.ref_desc(i)].label
+    if self.savedVariables.enable_guild[i] then
+        color = ZO_DEFAULT_ENABLED_COLOR
+        text  = "gonna do something"
+    else
+        color = ZO_DEFAULT_DISABLED_COLOR
+        text  = "not doing nothing"
+    end
+    desc:SetText(text)
+end
+
+function GuildGoldDeposits:DumpSettings()
     d("sv.days " .. self.savedVariables.duration_days)
-    for i = 1, 5 do
+    for i = 1, self.max_guild_ct do
         d("sv.eg[" .. i .. "] = "
           .. tostring(self.savedVariables.enable_guild[i]))
     end