c6843f3 - Refactor updateIndicators to get auras when they're applied
[wowui.git] / OmaRF / Indicators.lua
index 36d03cc..9557258 100644 (file)
@@ -29,6 +29,50 @@ local unpack = unpack;
 local floor = floor;
 local ceil = ceil;
 
+-- update current auras
+hooksecurefunc("CompactUnitFrame_UpdateAuras", function(frame)
+    local frameName = frame:GetName();
+    if f[frameName] then
+        for _, ind in pairs(f[frameName]) do ind.expires = nil end
+        for _, ind in ipairs(majorFrames[frameName]) do ind.expires = nil end
+
+        local name, icon, count, expires, caster, id;
+        local unit = frame.displayedUnit;
+        local majorI = 1;
+        for _, filter in ipairs(auraFilters) do
+            local i = 1;
+            while true do
+                name, _, icon, count, _, _, expires, caster, _, _, id = UnitAura(unit, i, filter);
+                if not id then break end
+                local pos = watchedAuras[id] or watchedAuras[name];
+                if pos then
+                    local ind = f[frameName][pos];
+                    local config = OmaRF.db.profile.indicators[pos];
+                    if not config.mine or UnitIsPlayer(caster) then
+                        if config.useDefaultIcon then
+                            ind.icon:SetTexture(DEFAULT_ICON);
+                        else
+                            ind.icon:SetTexture(icon);
+                        end
+                        ind.expires = expires;
+                    end
+                end
+
+                if (majorAuras[id] or majorAuras[name]) and majorI <= majorMax then
+                    local ind = majorFrames[frameName][majorI];
+                    ind.icon:SetTexture(icon);
+                    ind.expires = expires;
+                    if count > 1 then
+                        ind.stackText:SetText(count);
+                    end
+                    majorI = majorI + 1;
+                end
+                i = i + 1;
+            end
+        end
+    end
+end);
+
 local function configureIndicators(frame, name)
     local frameName = name or frame:GetName();
     if not f[frameName] then return end
@@ -51,8 +95,8 @@ local function configureIndicators(frame, name)
         end
         ind.icon:SetWidth(config.iconSize);
         ind.icon:SetHeight(config.iconSize);
-        ind.expire:SetFont(STANDARD_TEXT_FONT, config["textSize"], "OUTLINE");
-        ind.stack:SetFont(STANDARD_TEXT_FONT, config["textSize"], "OUTLINE");
+        ind.expireText:SetFont(STANDARD_TEXT_FONT, config["textSize"], "OUTLINE");
+        ind.stackText:SetFont(STANDARD_TEXT_FONT, config["textSize"], "OUTLINE");
     end
 end
 
@@ -75,16 +119,17 @@ local function setupCompactUnitFrame(frame, name)
         if i > 1 then
             majorFrames[name][i].icon:SetPoint("TOPLEFT", majorFrames[name][i-1].icon, "TOPRIGHT");
         end
-        majorFrames[name][i].expire = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
-        majorFrames[name][i].expire:SetPoint("BOTTOMRIGHT", majorFrames[name][i].icon, "BOTTOMRIGHT");
-        majorFrames[name][i].stack = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
-        majorFrames[name][i].stack:SetPoint("TOPLEFT", majorFrames[name][i].icon, "TOPLEFT");
+        majorFrames[name][i].expireText = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
+        majorFrames[name][i].expireText:SetPoint("BOTTOMRIGHT", majorFrames[name][i].icon, "BOTTOMRIGHT");
+        majorFrames[name][i].stackText = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall");
+        majorFrames[name][i].stackText:SetPoint("TOPLEFT", majorFrames[name][i].icon, "TOPLEFT");
     end
 
     configureIndicators(frame, name);
 end
 
 local function remaining(expires, current)
+    if expires == 0 then return "" end
     local remain = expires - current;
     if remain > 60 then
         return format("%dm", ceil(remain/60));
@@ -92,72 +137,64 @@ local function remaining(expires, current)
     return floor(remain+0.5);
 end
 
--- Check the indicators on a frame and update the times on them
-local function updateIndicators(frame)
-    local frameName = frame:GetName();
-    local unit = frame.unit;
-    if not unit then return end -- possible if the frame is just being hidden
-
-    -- Create indicators if needed
-    if not f[frameName] then setupCompactUnitFrame(frame, frameName) end
-    -- Reset current
+local function hide(frameName)
     for _, ind in pairs(f[frameName]) do
         ind.text:Hide();
         ind.icon:Hide();
     end
     for _, ind in ipairs(majorFrames[frameName]) do
         ind.icon:Hide();
-        ind.expire:Hide();
-        ind.stack:Hide();
+        ind.expireText:Hide();
+        ind.stackText:Hide();
     end
+end
+
+-- Check the indicators on a frame and update the times on them
+local function updateIndicators(frame)
+    local frameName = frame:GetName();
+    local unit = frame.unit;
+    if not unit then return end -- possible if the frame is just being hidden
+
+    -- Create indicators if needed, out of combat if forbidden
+    if not f[frameName] then
+        if frame:IsForbidden() then
+            if not OmaRF.ooc_queue[frameName] then
+                OmaRF.ooc_queue[frameName] = {
+                    func = setupCompactUnitFrame,
+                    args = { frame, frameName }
+                };
+            end
+            return;
+        else
+            setupCompactUnitFrame(frame, frameName);
+        end
+    end
+    -- Reset current
+    hide(frameName);
     -- Hide if unit is dead/disconnected
     if (not UnitIsConnected(unit)) or UnitIsDeadOrGhost(frame.displayedUnit) then
         return;
     end
 
-    local name, icon, count, expires, caster, id;
     local current = GetTime();
-    local majorI = 1;
-    for _, filter in ipairs(auraFilters) do
-        local i = 1;
-        while true do
-            name, _, icon, count, _, _, expires, caster, _, _, id = UnitAura(unit, i, filter);
-            if not id then break end
-            local pos = watchedAuras[id] or watchedAuras[name];
-            if pos then
-                local ind = f[frameName][pos];
-                local config = OmaRF.db.profile.indicators[pos];
-                if not config.mine or UnitIsPlayer(caster) then
-                    if config.showIcon then
-                        -- show icon
-                        if config.useDefaultIcon then
-                            ind.icon:SetTexture(DEFAULT_ICON);
-                        else
-                            ind.icon:SetTexture(icon);
-                        end
-                        ind.icon:Show()
-                    end
-                    if config.showText then
-                        -- show text
-                        ind.text:SetText(remaining(expires, current));
-                        ind.text:Show();
-                    end
-                end
-            end
-
-            if (majorAuras[id] or majorAuras[name]) and majorI <= majorMax then
-                local ind = majorFrames[frameName][majorI];
-                ind.icon:SetTexture(icon);
+    for pos, ind in pairs(f[frameName]) do
+        if ind.expires ~= nil then
+            local config = OmaRF.db.profile.indicators[pos];
+            if config.showIcon then
                 ind.icon:Show();
-                ind.expire:SetText(remaining(expires, current));
-                ind.expire:Show();
-                if count > 1 then
-                    ind.stack:SetText(count);
-                    ind.stack:Show();
-                end
-                majorI = majorI + 1;
             end
-            i = i + 1;
+            if config.showText then
+                ind.text:SetText(remaining(ind.expires, current));
+                ind.text:Show();
+            end
+        end
+    end
+    for _, ind in ipairs(majorFrames[frameName]) do
+        if ind.expires ~= nil then
+            ind.icon:Show();
+            ind.expireText:SetText(remaining(ind.expires, current));
+            ind.expireText:Show();
+            ind.stackText:Show();
         end
     end
 end