1553757 - Refactor updateIndicators
authorAleksi Blinnikka <aleksi.blinnikka@gmail.com>
Thu, 28 Dec 2017 16:55:29 +0000
committerAleksi Blinnikka <aleksi.blinnikka@gmail.com>
Thu, 28 Dec 2017 16:55:29 +0000
Indicators.lua

index ce916a6..673ae75 100644 (file)
@@ -10,7 +10,6 @@ local paddings = {
     BOTTOMRIGHT = {-pad, pad}
 };
 local watchedAuras; -- all watched auras
-local indicatorAuras; -- watched auras per indicator
 local auraFilters = {"HELPFUL", "HARMFUL"};
 local DEFAULT_ICON = "Interface\\AddOns\\RaidFrameCustomization\\images\\rhomb";
 local _;
@@ -28,8 +27,8 @@ local centerAuras = {
     "Power Word: Shield"
 };
 
-local function configureIndicators(frame)
-    local frameName = frame:GetName();
+local function configureIndicators(frame, name)
+    local frameName = name or frame:GetName();
     if not f[frameName] then return end
 
     local config = RaidFrameCustomization.db.profile;
@@ -50,8 +49,7 @@ local function configureIndicators(frame)
 end
 
 -- Create the FontStrings used for indicators
-local function setupCompactUnitFrame(frame)
-    local name = frame:GetName();
+local function setupCompactUnitFrame(frame, name)
     f[name] = {};
     for _, pos in ipairs(positions) do
         f[name][pos] = {};
@@ -60,104 +58,63 @@ local function setupCompactUnitFrame(frame)
         f[name][pos].icon = frame:CreateTexture(nil, "OVERLAY");
         f[name][pos].icon:SetPoint(pos, frame, pos, paddings[pos][1], paddings[pos][2]);
     end
-    configureIndicators(frame);
-end
-
--- Get all unit auras TODO change to event driven, only remaining updating with timer
-local function getAuras(unit)
-    local unitAuras = {};
-    local auraName, icon, count, expires, caster, debuffType, spellId;
-    local filter;
-
-    for _, filter in ipairs(auraFilters) do
-        local i = 1;
-        while true do
-            auraName, _, icon, count, debuffType, _, expires, caster, _, _, spellId = UnitAura(unit, i, filter);
-            if not spellId then break end
-            if watchedAuras[auraName] or watchedAuras[spellId] or watchedAuras[debuffType] then
-                local aura = {};
-                aura.auraName = auraName;
-                aura.spellId = spellId;
-                aura.count = count;
-                aura.expires = expires;
-                aura.mine = UnitIsPlayer(caster);
-                aura.icon = icon;
-                aura.debuffType = debuffType;
-                table.insert(unitAuras, aura);
-            end
-            i = i + 1;
-        end
-    end
-    return unitAuras;
+    configureIndicators(frame, name);
 end
 
 -- Check the indicators on a frame and update the times on them
 local function updateIndicators(frame)
-    if not frame.unit then return end
-    local unit = frame.unit;
     local frameName = frame:GetName();
+    local unit = frame.unit;
 
-    -- Check if the indicator object exists, else create it
-    if not f[frameName] then setupCompactUnitFrame(frame) end
+    -- Create indicators if needed
+    if not f[frameName] then setupCompactUnitFrame(frame, frameName) end
+    -- Reset current
+    for _, ind in pairs(f[frameName]) do
+        ind.text:SetText("");
+        ind.icon:SetTexture("");
+    end
     -- Hide if unit is dead/disconnected
     if (not UnitIsConnected(unit)) or UnitIsDeadOrGhost(frame.displayedUnit) then
-        for _, ind in pairs(f[frameName]) do
-            ind.text:SetText("");
-            ind.icon:SetTexture("");
-        end
         return;
     end
 
-    local unitAuras = getAuras(unit);
-    for pos, ind in pairs(f[frameName]) do
-        -- try to find matching aura
-        local found, aura;
-        for _, aura in pairs(unitAuras) do
-            if indicatorAuras[pos][aura.auraName] or indicatorAuras[pos][aura.spellId] or
-               indicatorAuras[pos][aura.debuffType] then
-                found = aura;
-                -- break on first matching buff/debuff cast by me
-                -- otherwise continue through
-                if aura.mine then break end
-            end
-        end
-
-        local config = RaidFrameCustomization.db.profile[pos];
-        if found then
-            if config.mine and not found.mine then
-                -- don't show
-                ind.icon:SetTexture("");
-                ind.text:SetText("");
-            else
-                if config.showIcon and not config.useDefaultIcon then
-                    -- show icon TODO coloring
-                    ind.icon:SetTexture(found.icon);
-                end
-                if config.showText then
-                    -- show text
-                    local text;
-                    local remaining = found.expires - GetTime();
-                    if remaining > 60 then
-                        text = string.format("%dm", ceil(remaining/60));
-                    else
-                        text = string.format("%d", floor(remaining+0.5));
+    local name, icon, count, debuff, expires, caster, id;
+    local current = GetTime();
+    for _, filter in ipairs(auraFilters) do
+        local i = 1;
+        while true do
+            name, _, icon, count, debuff, _, expires, caster, _, _, id = UnitAura(unit, i, filter);
+            if not id then break end
+            local pos = watchedAuras[name] or watchedAuras[id] or watchedAuras[debuff];
+            if pos then
+                local config = RaidFrameCustomization.db.profile[pos];
+                if not config.mine or UnitIsPlayer(caster) then
+                    if config.showIcon and not config.useDefaultIcon then
+                        -- show icon TODO coloring
+                        ind.icon:SetTexture(icon);
                     end
-
-                    if config.stack and found.count > 0 then
-                        if text then
-                            text = found.count.."-"..text;
+                    if config.showText then
+                        -- show text
+                        local text;
+                        local remaining = expires - current;
+                        if remaining > 60 then
+                            text = string.format("%dm", ceil(remaining/60));
                         else
-                            text = found.count;
+                            text = string.format("%d", floor(remaining+0.5));
+                        end
+                        if count > 1 and config.stack then
+                            if text then
+                                text = count.."-"..text;
+                            else
+                                text = count;
+                            end
                         end
-                    end
 
-                    ind.text:SetText(text);
+                        ind.text:SetText(text);
+                    end
                 end
             end
-        else
-            -- not found, show nothing
-            ind.icon:SetTexture("");
-            ind.text:SetText("");
+            i = i + 1;
         end
     end
 end
@@ -174,12 +131,9 @@ function RaidFrameCustomization:RefreshConfig()
         CompactRaidFrameContainer_ApplyToFrames(CompactRaidFrameContainer, "normal", configureIndicators);
         -- Format aura strings
         watchedAuras = {};
-        indicatorAuras = {};
         for _, pos in ipairs(positions) do
-            indicatorAuras[pos] = {};
             for _, aura in ipairs(self.db.profile[pos]["auras"]) do
-                watchedAuras[aura] = true;
-                indicatorAuras[pos][aura] = true;
+                watchedAuras[aura] = pos; -- TODO single aura only in one position
             end
         end