9f42c24 - Use constant time jump table
[wowui.git] / OmaRF / Indicators.lua
index c408815..8dd2ba9 100644 (file)
@@ -1,8 +1,10 @@
 -- Indicators.lua
 local pairs, ipairs = pairs, ipairs;
 -- Indicators.lua
 local pairs, ipairs = pairs, ipairs;
+local floor = math.floor;
 local GetTime = GetTime;
 local UnitAura = UnitAura;
 local UnitIsPlayer = UnitIsPlayer;
 local GetTime = GetTime;
 local UnitAura = UnitAura;
 local UnitIsPlayer = UnitIsPlayer;
+local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
 local CTimerAfter = C_Timer.After;
 
 local watchedAuras = {
 local CTimerAfter = C_Timer.After;
 
 local watchedAuras = {
@@ -27,80 +29,65 @@ local majorAuras = {
 };
 
 local updaters = {};
 };
 
 local updaters = {};
+local updating = {};
 local positions = {"TOPRIGHT", "BOTTOMLEFT"};
 local auraFilters = {"HELPFUL", "HARMFUL"};
 
 local positions = {"TOPRIGHT", "BOTTOMLEFT"};
 local auraFilters = {"HELPFUL", "HARMFUL"};
 
--- TODO text
---[[local function remaining(expires, current)
-    if expires == 0 then return "" end
+local function remaining(text, expires, current)
+    if expires == 0 then
+        text:SetText("");
+        return false;
+    end
     local remain = expires - current;
     local remain = expires - current;
-    if remain > 60 then
-        return format("%dm", ceil(remain/60));
+    if remain > 8 then
+        text:SetText("");
+    else
+        text:SetText(floor(remain+0.5));
     end
     end
-    return floor(remain+0.5);
-end]]
+    return true;
+end
 
 
---[[local function updateIndicators(frame, unit)
-    if not frame:IsShown() then
+local function updateIndicators(frame, unit)
+    if not frame:IsShown() or not UnitIsConnected(unit) or UnitIsDeadOrGhost(unit) then
+        updating[frame] = nil;
         return;
         return;
-    elseif not UnitIsConnected(unit) or UnitIsDeadOrGhost(unit) then
-        if frame.inds:IsShown() then frame.inds:Hide() end
-        return;
-    elseif not frame.inds:IsShown() then
-        frame.inds:Show();
     end
 
     end
 
+    local needUpdate = false;
     local current = GetTime();
     local current = GetTime();
-    for pos, ind in pairs(frame.inds) do
+    for _, pos in pairs(positions) do
+        local ind = frame.inds[pos];
         if ind.expires ~= nil then
         if ind.expires ~= nil then
-            if OmaRF.db.profile.indicators[pos].showText then
-                ind.text:SetText(remaining(ind.expires, current));
-            end
-        else
-            if ind.icon:IsShown() then ind.icon:Hide() end
-            if ind.text:IsShown() then ind.text:Hide() end
+            needUpdate = remaining(ind.text, ind.expires, current) or needUpdate;
         end
     end
         end
     end
-    for _, ind in ipairs(frame.majorInds) do
+    for i = 1,3 do
+        local ind = frame.major[i];
         if ind.expires ~= nil then
         if ind.expires ~= nil then
-            ind.expireText:SetText(remaining(ind.expires, current));
-        else
-            if ind.icon:IsShown() then ind.icon:Hide() end
-            if ind.expireText:IsShown() then ind.expireText:Hide() end
-            if ind.stackText:IsShown() then ind.stackText:Hide() end
+            needUpdate = remaining(ind.text, ind.expires, current) or needUpdate;
         end
     end
         end
     end
-end]]
-
---local function showIndicator(ind, caster, expires, current, config)
-local function showIndicator(ind, caster)
-    --if not config.mine or UnitIsPlayer(caster) then
-    if UnitIsPlayer(caster) then
-        --if config.showIcon and not ind:IsShown() then
-        if not ind:IsShown() then
-            ind:Show();
-        end
-        --ind.expires = expires;
-        --[[if config.showText then
-            ind.text:SetText(remaining(expires, current));
-            if not ind.text:IsShown() then ind.text:Show() end
-        end]]
+    if needUpdate then
+        CTimerAfter(0.16, updaters[frame]);
+    else
+        updating[frame] = nil;
     end
 end
 
 function OmaCheckIndicators(frame, unit)
     for _, pos in pairs(positions) do
     end
 end
 
 function OmaCheckIndicators(frame, unit)
     for _, pos in pairs(positions) do
-        --ind.expires = nil;
-        if frame.inds[pos]:IsShown() then frame.inds[pos]:Hide() end
+        frame.inds[pos].expires = nil;
+        frame.inds[pos]:Hide();
+        frame.inds[pos].text:Hide();
     end
     for i = 1,3 do
     end
     for i = 1,3 do
-        --ind.expires = nil;
-        if frame.major[1]:IsShown() then frame.major[1]:Hide() end
+        frame.major[i].expires = nil;
+        frame.major[i]:Hide();
+        frame.major[i].text:Hide();
+        frame.major[i].stack:Hide();
     end
     end
-    frame.showInds = false;
-    frame.showMajors = false;
-
     local name, icon, count, expires, caster, id;
     local name, icon, count, expires, caster, id;
+    local showInds, showMajors, needUpdate = false, false, false;
     local majorPos = 1;
     local current = GetTime();
     for _, filter in ipairs(auraFilters) do
     local majorPos = 1;
     local current = GetTime();
     for _, filter in ipairs(auraFilters) do
@@ -109,37 +96,44 @@ function OmaCheckIndicators(frame, unit)
             name, _, icon, count, _, _, expires, caster, _, _, id = UnitAura(unit, i, filter);
             if not id then break end
             local pos = watchedAuras[id] or watchedAuras[name];
             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
-                frame.showInds = true;
-                if UnitIsPlayer(caster) and not frame.inds[pos]:IsShown() then
-                    frame.inds[pos]:Show();
-                end
+            if pos and UnitIsPlayer(caster) then
+                needUpdate = remaining(frame.inds[pos].text, expires, current);
+                frame.inds[pos].expires = expires;
+                frame.inds[pos]:Show();
+                frame.inds[pos].text:Show();
+                showInds = true;
             end
             if (majorAuras[id] or majorAuras[name]) and majorPos <= 3 then
             end
             if (majorAuras[id] or majorAuras[name]) and majorPos <= 3 then
+                needUpdate = remaining(frame.major[majorPos].text, expires, current);
+                frame.major[majorPos].expires = expires;
                 frame.major[majorPos]:SetTexture(icon);
                 frame.major[majorPos]:Show();
                 frame.major[majorPos]:SetTexture(icon);
                 frame.major[majorPos]:Show();
-                frame.showMajors = true;
+                frame.major[majorPos].text:Show();
+                if count > 1 then
+                    frame.major[majorPos].stack:SetText(count);
+                    frame.major[majorPos].stack:Show();
+                end
+                showMajors = true;
                 majorPos = majorPos + 1;
             end
             i = i + 1;
         end
     end
                 majorPos = majorPos + 1;
             end
             i = i + 1;
         end
     end
-    if frame.showInds then
-        if not frame.inds:IsShown() then frame.inds:Show() end
-        -- create a function for updating the indicator each second
-        --[[local func = updaters[frame];
-        if not func then
-            func = function() updateIndicators(frame, unit) end;
-            updaters[frame] = func;
+    if showInds or showMajors then
+        frame.inds:Show();
+        frame.major:Show();
+        if needUpdate and not updating[frame] then
+            updating[frame] = true; -- race?
+            -- create a function for updating the indicator
+            local func = updaters[frame];
+            if not func then
+                func = function() updateIndicators(frame, unit) end;
+                updaters[frame] = func;
+            end
+            CTimerAfter(0.16, func);
         end
         end
-        CTimerAfter(1, func);]]
-    elseif frame.inds:IsShown() then
+    else
         frame.inds:Hide();
         frame.inds:Hide();
-    end
-    if frame.showMajors then
-        if not frame.major:IsShown() then frame.major:Show() end
-    elseif frame.major:IsShown() then
         frame.major:Hide();
     end
 end
         frame.major:Hide();
     end
 end
-