Add update logic for Label

Sasky [03-23-15 - 07:28]
Add update logic for Label
Switch some main CyrHUD to functions with self reference
Label bugfixes
Filename
CyrHUD.lua
CyrHUD.txt
classes/Battle.lua
classes/Info.lua
classes/Label.lua
diff --git a/CyrHUD.lua b/CyrHUD.lua
index 9317a4c..d3cddf3 100644
--- a/CyrHUD.lua
+++ b/CyrHUD.lua
@@ -1,6 +1,6 @@
 -- This file is part of CyrHUD
 --
--- (C) 2014 Scott Yeskie (Sasky)
+-- (C) 2015 Scott Yeskie (Sasky)
 --
 -- This program is free software; you can redistribute it and/or modify
 -- it under the terms of the GNU General Public License as published by
@@ -42,10 +42,10 @@ function CyrHUD.formatTime(delta, inclueSec)
 end

 CyrHUD.errors = {}
-CyrHUD.error = function(val)
-    if not CyrHUD.errors[val] then
-        CyrHUD.errors[val] = 1
-        d("|cFF0000ERROR (CyrHUD): " .. val .. " / Please file this bug info at esoui.com")
+function CyrHUD:error(val)
+    if not self.errors[val] then
+        self.errors[val] = 1
+        d("|cFF0000ERROR (CyrHUD): " .. val .. "\n|CCCCCCCPlease file this bug info at |CEEEEFFesoui.com")
     end
 end

@@ -66,7 +66,7 @@ end
 ----------------------------------------------
 function CyrHUD.eventAttackChange(_, keepID, battlegroundContext, underAttack)
     if underAttack then
-        CyrHUD.add(keepID)
+        CyrHUD:add(keepID)
     elseif CyrHUD.battles[keepID] ~= nil then
         CyrHUD.battles[keepID]:update()
     end
@@ -89,45 +89,23 @@ end
 CyrHUD.entryCount = 0
 CyrHUD.entries = {}

-CyrHUD.getEntry = function()
-    CyrHUD.ptr = CyrHUD.ptr + 1
-    if #CyrHUD.entries < CyrHUD.ptr then
-        table.insert(CyrHUD.entries, CyrHUD.Label())
+function CyrHUD:getUIRow(index)
+    if #self.entries < index then
+        table.insert(self.entries, self.Label())
+        index = #self.entries
     end
-    CyrHUD.entries[CyrHUD.ptr].main:SetHidden(false)
-    return CyrHUD.entries[CyrHUD.ptr]
+    self.entries[index].main:SetHidden(false)
+    return self.entries[index]
 end

-CyrHUD.ptr = 0
-CyrHUD.reset = function()
-    for _,p in pairs(CyrHUD.entries) do
-        p:Hide()
+function CyrHUD:printAll()
+    local i = 1
+    for _,battle in pairs(self.battles) do
+        self:getUIRow(i):update(battle)
+        i = i + 1
     end
-    CyrHUD.ptr = 0
-end
-
-CyrHUD.print = function(battle)
-    local entry = CyrHUD.getEntry()
-    entry.icon:SetTexture(battle:getIcon())
-    entry.name:SetText(battle.keepName)
-    local color = CyrHUD.info[battle.defender].color
-    entry.name:SetColor(color.r, color.g, color.b)
-    entry.time:SetText(battle:getDuration())
-    local ds, dc = battle:getDefSiege()
-    entry.defSiege:SetText(ds)
-    entry.defSiege:SetColor(dc.r, dc.g, dc.b)
-    local as, ac = battle:getAttSiege()
-    entry.attSiege:SetText(as)
-    entry.attSiege:SetColor(ac.r, ac.g, ac.b)
-    local r,g,b,a = battle:getBGColor()
-    entry.main:SetCenterColor(r,g,b,a)
-    entry.ua:SetHidden(not battle.keepUA)
-end
-
-CyrHUD.printAll = function()
-    CyrHUD.reset()
-    for _,b in pairs(CyrHUD.battles) do
-        CyrHUD.print(b)
+    for j=i,#self.entries do
+        self:getUIRow(i):hide()
     end
 end

@@ -135,7 +113,7 @@ end
 -- Battle management
 ----------------------------------------------
 CyrHUD.battles = {}
-CyrHUD.add = function(keepID)
+function CyrHUD:add(keepID)
     if CyrHUD.battles[keepID] == nil then
         CyrHUD.battles[keepID] = CyrHUD.Battle(keepID)
     else
@@ -143,10 +121,11 @@ CyrHUD.add = function(keepID)
     end
 end

-CyrHUD.checkAdd = function(keepID)
+function CyrHUD:checkAdd(keepID)
     if CyrHUD.battles[keepID] == nil then
         local battle = CyrHUD.Battle(keepID)
         if battle:isBattle() then
+            d("Battle at " .. keepID)
             CyrHUD.battles[keepID] = battle
         end
     elseif CyrHUD.battles[keepID]:isBattle() then
@@ -154,20 +133,20 @@ CyrHUD.checkAdd = function(keepID)
     end
 end

-CyrHUD.updateAll = function()
-    for i,_ in pairs(CyrHUD.battles) do
+function CyrHUD:updateAll()
+    for i,_ in pairs(self.battles) do
         --Update in-place
-        CyrHUD.battles[i]:update()
+        self.battles[i]:update()
     end
 end

-CyrHUD.scanKeeps = function()
+function CyrHUD:scanKeeps()
     for i=3,20 do
-        CyrHUD.checkAdd(i)
+        CyrHUD:checkAdd(i)
     end

     for i=132,134 do
-        CyrHUD.checkAdd(i)
+        CyrHUD:checkAdd(i)
     end
 end

@@ -181,29 +160,30 @@ function CyrHUD:init()
     CyrHUD_UI:SetAnchor(pt, relTo, relPt, CyrHUD.cfg.xoff, CyrHUD.cfg.yoff)

     --Init UI
-    CyrHUD:disableQuestTrackers()
+    self:disableQuestTrackers()
     CyrHUD_UI:SetHidden(false)


     --Get initial scan
-    CyrHUD.battles = {}
-    CyrHUD.battleContext = GetCurrentCampaignId()
-    CyrHUD.scanKeeps()
+    self.battles = {}
+    self.battleContext = GetCurrentCampaignId()
+    self:scanKeeps()

     --Add events
     EVENT_MANAGER:RegisterForUpdate("CyrHUDKeepCheck", 5000, function()
-        CyrHUD.scanKeeps()
-        CyrHUD.updateAll()
+        d("CyrHUD 5s check")
+        self:scanKeeps()
+        self:updateAll()
     end)
     EVENT_MANAGER:RegisterForUpdate("CyrHUDUIUpdate", 1000, function()
-        CyrHUD.printAll()
-        CyrHUD.updateScore()
+        self:printAll()
+        self:updateScore()
     end)
-    EVENT_MANAGER:RegisterForEvent("CyrHUDAttackChange", EVENT_KEEP_UNDER_ATTACK_CHANGED, CyrHUD.eventAttackChange)
-    CyrHUD.visible = true
+    EVENT_MANAGER:RegisterForEvent("CyrHUDAttackChange", EVENT_KEEP_UNDER_ATTACK_CHANGED, self.eventAttackChange)
+    self.visible = true

-    EVENT_MANAGER:RegisterForEvent('CyrHUD', EVENT_ACTION_LAYER_POPPED, CyrHUD.actionLayerChange)
-    EVENT_MANAGER:RegisterForEvent('CyrHUD', EVENT_ACTION_LAYER_PUSHED, CyrHUD.actionLayerChange)
+    EVENT_MANAGER:RegisterForEvent('CyrHUD', EVENT_ACTION_LAYER_POPPED, self.actionLayerChange)
+    EVENT_MANAGER:RegisterForEvent('CyrHUD', EVENT_ACTION_LAYER_PUSHED, self.actionLayerChange)
 end

 function CyrHUD:disableQuestTrackers()
@@ -225,22 +205,23 @@ function CyrHUD:reEnableQuestTrackers()
 end

 function CyrHUD:deinit()
-    CyrHUD:reEnableQuestTrackers()
+    self:reEnableQuestTrackers()
     EVENT_MANAGER:UnregisterForUpdate("CyrHUDKeepCheck")
     EVENT_MANAGER:UnregisterForUpdate("CyrHUDUIUpdate")
     EVENT_MANAGER:UnregisterForUpdate("CyrHUDUpdateAPCount")
     EVENT_MANAGER:UnregisterForEvent("CyrHUD", EVENT_ACTION_LAYER_POPPED)
     EVENT_MANAGER:UnregisterForEvent("CyrHUD", EVENT_ACTION_LAYER_PUSHED)
     CyrHUD_UI:SetHidden(true)
-    CyrHUD.visible = false
+    self.visible = false
 end


 function CyrHUD.toggle()
-    if CyrHUD.visible then
-        CyrHUD.deinit()
+    local self = CyrHUD
+    if self.visible then
+        self:deinit()
     else
-        CyrHUD.init()
+        self:init()
     end
 end
 --TODO: Properly setup on Addon init or playerLoad
@@ -248,11 +229,12 @@ end
 SLASH_COMMANDS["/cyrhud"] = CyrHUD.toggle

 function CyrHUD.playerInit()
-    if not CyrHUD.initLAM then
+    local self = CyrHUD
+    if not self.initLAM then
         local LAM = LibStub("LibAddonMenu-2.0")
-        LAM:RegisterAddonPanel("CyrHUD-LAM", CyrHUD.menuPanel)
-        LAM:RegisterOptionControls("CyrHUD-LAM", CyrHUD.menuOptions)
-        CyrHUD.initLAM = true
+        LAM:RegisterAddonPanel("CyrHUD-LAM", self.menuPanel)
+        LAM:RegisterOptionControls("CyrHUD-LAM", self.menuOptions)
+        self.initLAM = true

         --Init saved variables
         local def = {
@@ -260,15 +242,15 @@ function CyrHUD.playerInit()
             yoff = 60,
             trackerDisable = false,
         }
-        CyrHUD.cfg = ZO_SavedVars:NewAccountWide("CyrHUD_SavedVars", 1.0, "config", def)
+        self.cfg = ZO_SavedVars:NewAccountWide("CyrHUD_SavedVars", 1.0, "config", def)
     end

     if IsPlayerInAvAWorld() then
-        if not CyrHUD.visible then
-            CyrHUD.init()
+        if not self.visible then
+            self:init()
         end
-    elseif CyrHUD.visible then
-        CyrHUD.deinit()
+    elseif self.visible then
+        self:deinit()
     end
 end

diff --git a/CyrHUD.txt b/CyrHUD.txt
index 90ae68a..5a94caa 100644
--- a/CyrHUD.txt
+++ b/CyrHUD.txt
@@ -1,6 +1,6 @@
 ## APIVersion: 100011
 ## Title: CyrHUD
-## Version: 1.1.2
+## Version: 1.2.0
 ## Author: Sasky
 ## SavedVariables: CyrHUD_SavedVars
 ## OptionalDependsOn: LibAddonMenu-2.0
@@ -20,6 +20,7 @@ lib/LibAddonMenu-2.0/controls/header.lua
 lib/LibAddonMenu-2.0/controls/slider.lua
 lib/LibAddonMenu-2.0/controls/texture.lua

+classes/Label.lua
 classes/Battle.lua
 classes/Info.lua

diff --git a/classes/Battle.lua b/classes/Battle.lua
index f53db08..2ccb283 100644
--- a/classes/Battle.lua
+++ b/classes/Battle.lua
@@ -22,6 +22,7 @@ local function n0(val) if val == nil then return 0 end return val end
 -- Setup class
 CyrHUD.Battle = {}
 CyrHUD.Battle.__index = CyrHUD.Battle
+CyrHUD.Battle.type = "Battle"
 setmetatable(CyrHUD.Battle, {
     __call = function (cls, ...)
         return cls.new(...)
@@ -69,6 +70,71 @@ CyrHUD.Battle.new = function(keepID)
     return self
 end

+----------------------------------------------
+-- Label update
+----------------------------------------------
+--Label fields
+local L_ICON, L_UA = "img1", "img2"
+local L_NAME, L_ATT_SIEGE, L_DEF_SIEGE, L_TIME = "txt1", "txt2", "txt3", "txt4"
+
+function CyrHUD.Battle:configureLabel(label)
+    d("Configuring label " .. label.num)
+    label:exposeControls(2,4)
+
+    label:positionControl(L_ICON, 40, 40, -2, -2)
+    -- Objective icon
+    label:getControl(L_ICON):SetDrawLayer(2)
+    -- Under attack graphic
+    label:positionControl(L_UA, 40, 40, -2, -2)
+    local ua = label:getControl(L_UA)
+    ua:SetDrawLayer(1)
+    ua:SetTexture(CyrHUD.info.underAttack)
+
+    local fontMain = "EsoUI/Common/Fonts/univers67.otf|18|soft-shadow-thick"
+    --Objective name
+    label:positionControl(L_NAME, 150, 30, 35, 5)
+    label:getControl(L_NAME):SetFont(fontMain)
+    --Defensive siege count
+    label:positionControl(L_DEF_SIEGE, 30, 30, 220, 5)
+    label:getControl(L_DEF_SIEGE):SetFont(fontMain)
+    --Attacker siege count
+    label:positionControl(L_ATT_SIEGE, 30, 30, 190, 5)
+    label:getControl(L_ATT_SIEGE):SetFont(fontMain)
+    --Time
+    label:positionControl(L_TIME, 30, 30, 250, 5)
+    label:getControl(L_TIME):SetFont(fontMain)
+end
+
+function CyrHUD.Battle:updateLabel(label)
+    --Keep icon
+    label:getControl(L_ICON):SetTexture(self:getIcon())
+    label:getControl(L_UA):SetHidden(not self.keepUA)
+    --Keep name
+    local name = label:getControl(L_NAME)
+    name:SetText(self.keepName)
+    local color = CyrHUD.info[self.defender].color
+    name:SetColor(color.r, color.g, color.b)
+    --Time
+    label:getControl(L_TIME):SetText(self:getDuration())
+    --Defensive siege
+    local ds, dc = self:getDefSiege()
+    local defSiege = label:getControl(L_DEF_SIEGE)
+    defSiege:SetText(ds)
+    defSiege:SetColor(dc.r, dc.g, dc.b)
+    --Attacing siege
+    local as, ac = self:getAttSiege()
+    local attSiege = label:getControl(L_ATT_SIEGE)
+    attSiege:SetText(as)
+    attSiege:SetColor(ac.r, ac.g, ac.b)
+    --Background color
+    local r,g,b,a = self:getBGColor()
+    label.main:SetCenterColor(r,g,b,a)
+end
+
+
+----------------------------------------------
+-- Model update
+----------------------------------------------
 function CyrHUD.Battle:update()
     self.defender = GetKeepAlliance(self.keepID, CyrHUD.battleContext)
     self.keepUA = GetKeepUnderAttack(self.keepID, CyrHUD.battleContext)
diff --git a/classes/Info.lua b/classes/Info.lua
index a29b50b..54f0f94 100644
--- a/classes/Info.lua
+++ b/classes/Info.lua
@@ -1,6 +1,6 @@
 -- This file is part of CyrHUD
 --
--- (C) 2014 Scott Yeskie (Sasky)
+-- (C) 2015 Scott Yeskie (Sasky)
 --
 -- This program is free software; you can redistribute it and/or modify
 -- it under the terms of the GNU General Public License as published by
@@ -15,16 +15,14 @@
 -- You should have received a copy of the GNU General Public License
 -- along with this program.  If not, see <http://www.gnu.org/licenses/>.

-if CyrHUD == nil then
-    --noinspection GlobalCreationOutsideO
-    CyrHUD = {}
-end
+CyrHUD = CyrHUD or {}

 local AD = ALLIANCE_ALDMERI_DOMINION
 local DC = ALLIANCE_DAGGERFALL_COVENANT
 local EP = ALLIANCE_EBONHEART_PACT

 CyrHUD.info = {}
+CyrHUD.info.underAttack = "/esoui/art/mappins/ava_attackburst_64.dds"
 CyrHUD.info.noIcon = "/esoui/art/mappins/ava_largekeep_neutral.dds"
 CyrHUD.info[ALLIANCE_NONE] = {}
 CyrHUD.info[ALLIANCE_NONE].color = { hex = "CCCCCC", r = .8, g = .8, b = .8 }
diff --git a/classes/Label.lua b/classes/Label.lua
index 3ffcf4f..02f35db 100644
--- a/classes/Label.lua
+++ b/classes/Label.lua
@@ -16,6 +16,8 @@
 -- along with this program.  If not, see <http://www.gnu.org/licenses/>.

 -- Setup class
+CyrHUD = CyrHUD or {}
+
 CyrHUD.Label = {}
 CyrHUD.Label.__index = CyrHUD.Label
 setmetatable(CyrHUD.Label, {
@@ -29,37 +31,39 @@ local Label = CyrHUD.Label
 function Label.new()
     local self = setmetatable({}, CyrHUD.Label)
     self.labelType = "uninit"
-    self.num = CyrHUD.entryCount + 1
+    self.num = (Label.entryCount or 0) + 1
     self.entryName = "CyrHUDEntry" .. self.num
+    Label.entryCount = self.num
     self.entry = {}
     local entry = self.entry

     --Main control/backdrop
     local yoff = self.num*35-5
-    entry.main = WINDOW_MANAGER:CreateControl(self.entryName .. "main", CyrHUD_UI, CT_BACKDROP)
-    entry.main:SetDimensions(280, 35)
-    entry.main:SetAnchor(TOPLEFT, CyrHUD_UI, TOPLEFT, 0, yoff)
-    entry.main:SetCenterColor( 0, 0, 0, .3 )
-    entry.main:SetEdgeColor( 0, 0, 0, 0 )
+    self.main = WINDOW_MANAGER:CreateControl(self.entryName .. "main", CyrHUD_UI, CT_BACKDROP)
+    self.main:SetDimensions(280, 35)
+    self.main:SetAnchor(TOPLEFT, CyrHUD_UI, TOPLEFT, 0, yoff)
+    self.main:SetCenterColor( 0, 0, 0, .3 )
+    self.main:SetEdgeColor( 0, 0, 0, 0 )

     -- Images
-    entry.img1 = WINDOW_MANAGER:CreateControl(self.entryName .. "img1", entry.main, CT_TEXTURE)
-    entry.img2 = WINDOW_MANAGER:CreateControl(self.entryName .. "img2", entry.main, CT_TEXTURE)
-    entry.img3 = WINDOW_MANAGER:CreateControl(self.entryName .. "img3", entry.main, CT_TEXTURE)
+    entry.img1 = WINDOW_MANAGER:CreateControl(self.entryName .. "img1", self.main, CT_TEXTURE)
+    entry.img2 = WINDOW_MANAGER:CreateControl(self.entryName .. "img2", self.main, CT_TEXTURE)
+    entry.img3 = WINDOW_MANAGER:CreateControl(self.entryName .. "img3", self.main, CT_TEXTURE)

     --Labels
-    entry.txt1 = WINDOW_MANAGER:CreateControl(self.entryName .. "txt1", entry.main, CT_LABEL)
-    entry.txt2 = WINDOW_MANAGER:CreateControl(self.entryName .. "txt2", entry.main, CT_LABEL)
-    entry.txt3 = WINDOW_MANAGER:CreateControl(self.entryName .. "txt3", entry.main, CT_LABEL)
-    entry.txt4 = WINDOW_MANAGER:CreateControl(self.entryName .. "txt4", entry.main, CT_LABEL)
+    entry.txt1 = WINDOW_MANAGER:CreateControl(self.entryName .. "txt1", self.main, CT_LABEL)
+    entry.txt2 = WINDOW_MANAGER:CreateControl(self.entryName .. "txt2", self.main, CT_LABEL)
+    entry.txt3 = WINDOW_MANAGER:CreateControl(self.entryName .. "txt3", self.main, CT_LABEL)
+    entry.txt4 = WINDOW_MANAGER:CreateControl(self.entryName .. "txt4", self.main, CT_LABEL)
+    return self
 end

-function Label:Hide()
-    self.entry.main:SetHidden(true)
+function Label:hide()
+    self.main:SetHidden(true)
 end

-function Label:Show()
-    self.entry.main:SetHidden(false)
+function Label:show()
+    self.main:SetHidden(false)
 end

 function Label:getControl(name)
@@ -67,69 +71,41 @@ function Label:getControl(name)
 end

 function Label:moveControl(name, x, y)
-    if self.entry[name] and name ~= "main" then
+    if self.entry[name] then
         self.entry[name]:ClearAnchors()
         self.entry[name]:SetAnchor(TOPLEFT, self.entry.main, TOPLEFT, x, y)
     end
 end

 function Label:resizeControl(name, width, height)
-    if self.entry[name] and name ~= "main" then
+    if self.entry[name] then
         self.entry[name]:SetDimensions(width, height)
     end
 end

-function Label:reconfigure(type, labelObj)
-    if self.type ~= labelObj.type then
-        labelObj:configure(self)
+function Label:positionControl(name, width, height, x, y)
+    if self.entry[name] then
+        self.entry[name]:ClearAnchors()
+        self.entry[name]:SetAnchor(TOPLEFT, self.entry.main, TOPLEFT, x, y)
+        self.entry[name]:SetDimensions(width, height)
     end
 end

---Original create label
-function Label:create(entryName, num)
-    local yoff = num*35-5
-    self.entry = {}
-    local entry = {}
-    entry.main = WINDOW_MANAGER:CreateControl(entryName .. "main", CyrHUD_UI, CT_BACKDROP)
-    entry.main:SetDimensions(280, 35)
-    entry.main:SetAnchor(TOPLEFT, CyrHUD_UI, TOPLEFT, 0, yoff)
-    entry.main:SetCenterColor( 0, 0, 0, .3 )
-    entry.main:SetEdgeColor( 0, 0, 0, 0 )
-
-    entry.icon = WINDOW_MANAGER:CreateControl(entryName .. "icon", entry.main, CT_TEXTURE)
-    entry.icon:SetAnchor(TOPLEFT, entry.main, TOPLEFT, -2, -2)
-    entry.icon:SetDimensions(40,40)
-    entry.icon:SetDrawLayer(2)
-
-    entry.ua = WINDOW_MANAGER:CreateControl(entryName .. "ua", entry.main, CT_TEXTURE)
-    entry.ua:SetAnchor(TOPLEFT, entry.main, TOPLEFT, -2, -2)
-    entry.ua:SetDimensions(40,40)
-    entry.ua:SetTexture("/esoui/art/mappins/ava_attackburst_64.dds")
-    entry.ua:SetDrawLayer(1)
-
-    local fontMain = "EsoUI/Common/Fonts/univers67.otf|18|soft-shadow-thick"
-    entry.name = WINDOW_MANAGER:CreateControl(entryName .. "name", entry.main, CT_LABEL)
-    entry.name:SetDimensions(150,30)
-    entry.name:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 35, 5)
-    entry.name:SetFont(fontMain)
-
-    --local fontSmall = "EsoUI/Common/Fonts/univers67.otf|14|soft-shadow-thin"
-    entry.defSiege = WINDOW_MANAGER:CreateControl(entryName .. "defSiege", entry.main, CT_LABEL)
-    entry.defSiege:SetDimensions(30,30)
-    entry.defSiege:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 220, 5)
-    entry.defSiege:SetFont(fontMain)
-
-    entry.attSiege = WINDOW_MANAGER:CreateControl(entryName .. "attSiege", entry.main, CT_LABEL)
-    entry.attSiege:SetDimensions(30,30)
-    entry.attSiege:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 190, 5)
-    entry.attSiege:SetFont(fontMain)
-
-    entry.time = WINDOW_MANAGER:CreateControl(entryName .. "time", entry.main, CT_LABEL)
-    entry.time:SetDimensions(30,30)
-    entry.time:SetAnchor(TOPLEFT, entry.main, TOPLEFT, 250, 5)
-    entry.time:SetFont(fontMain)
-    entry.time:SetColor(.8,.8,.8)
+function Label:exposeControls(nImg, nText)
+    for i=1,3 do
+        self.entry["img"..i]:SetHidden(i > nImg)
+    end
+    for i=1,3 do
+        self.entry["txt"..i]:SetHidden(i > nImg)
+    end
+end

-    table.insert(CyrHUD.entries, entry)
-    return entry
+function Label:update(model)
+    if self.type ~= model.type then
+        --TODO: Handle some form of reset
+        model:configureLabel(self)
+        self.type = model.type
+        self:show()
+    end
+    model:updateLabel(self)
 end