a296877 - Add option to iterate over UnitAura instead of aura list
[wowui.git] / OmaUF / CastBar.lua
index 7485a8a..de2544e 100644 (file)
@@ -17,7 +17,7 @@ OmaUFCastBar = M;
 
 -- TODO trade skill bar updates as well, check Quartz modules/Tradeskill.lua
 local function onUpdate(bar)
-    if not bar:IsShown() then return end -- TODO little fadeout possibly
+    if not bar:IsShown() then return end
     local width = bar.icon:IsShown() and bar.cast.width or bar.cast.width; -- TODO fullwidth
     local startTime, endTime = bar.startTime, bar.endTime;
     local currentClamped = min(GetTime(), endTime);
@@ -38,6 +38,16 @@ local function onUpdate(bar)
     bar.time:SetFormattedText("%.1f", remaining);
 end
 
+local function showBar(bar)
+    bar:Show();
+    bar:SetScript("OnUpdate", onUpdate);
+end
+
+local function hideBar(bar)
+    bar:Hide();
+    bar:SetScript("OnUpdate", nil);
+end
+
 local function toggleInterruptible(bar, nointr)
     if bar.unit == "player" then return end
     if nointr then
@@ -53,14 +63,15 @@ local function startCast(bar, unit, channeling)
     local name, icon, startTime, endTime, noInterrupt, id;
     if channeling then
         name, _, _, icon, startTime, endTime, _, noInterrupt = UnitChannelInfo(unit);
+        if not startTime or not endTime then return nil end
         bar.channeling = true;
         bar.cast.color = channelingColor;
     else
         _, _, name, icon, startTime, endTime, _, _, noInterrupt, id = UnitCastingInfo(unit);
+        if not startTime or not endTime then return nil end
         bar.channeling = nil;
         bar.cast.color = castingColor;
     end
-    if not startTime or not endTime then return nil end
     bar.startTime = startTime / 1000;
     bar.endTime = endTime / 1000;
     -- don't show samwise for non-existent icons
@@ -75,7 +86,7 @@ local function startCast(bar, unit, channeling)
     bar.spell:SetText(ssub(name, 1, bar.spell.count));
     bar.time:SetFormattedText("%.1f", (endTime - startTime)/1000);
     bar.cast:SetVertexColor(unpack(bar.cast.color));
-    bar:Show();
+    showBar(bar);
     toggleInterruptible(bar, noInterrupt);
     return true;
 end
@@ -87,7 +98,7 @@ local function applyDelay(bar, unit, channeling)
     else
         _, _, _, _, startTime, endTime = UnitCastingInfo(unit);
     end
-    if not startTime or not endTime then return bar:Hide() end
+    if not startTime or not endTime then return hideBar(bar) end
     bar.startTime = startTime / 1000;
     bar.endTime = endTime / 1000;
     -- TODO show delay text
@@ -97,11 +108,17 @@ local events = {
     ["UNIT_SPELLCAST_START"] = function(bar, unit)
         startCast(bar, unit);
     end,
+    ["PLAYER_TARGET_CHANGED"] = function(bar)
+        hideBar(bar);
+        if not startCast(bar, bar.unit) then
+            startCast(bar, bar.unit, true);
+        end
+    end,
     ["UNIT_SPELLCAST_CHANNEL_START"] = function(bar, unit)
         startCast(bar, unit, true);
     end,
-    ["UNIT_SPELLCAST_STOP"] = function(bar, unit)
-        bar:Hide();
+    ["UNIT_SPELLCAST_STOP"] = function(bar)
+        hideBar(bar);
     end,
     ["UNIT_SPELLCAST_DELAYED"] = function(bar, unit)
         applyDelay(bar, unit);
@@ -120,8 +137,9 @@ events["UNIT_SPELLCAST_CHANNEL_STOP"] = events["UNIT_SPELLCAST_STOP"];
 
 local function onEvent(bar, event, unit)
     if unit == bar.unit or (bar.unit == "player" and unit == "vehicle") then
-        --print(unit, event)
         events[event](bar, unit);
+    elseif event == "PLAYER_TARGET_CHANGED" then
+        events[event](bar);
     end
 end
 
@@ -137,7 +155,7 @@ function M.RegisterCastEvents(bar)
     bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE");
     bar:RegisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE");
     bar:RegisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE");
-    bar:SetScript("OnUpdate", onUpdate);
+    if bar.unit == "target" then bar:RegisterEvent("PLAYER_TARGET_CHANGED") end
     -- trigger initial check
     if not startCast(bar, bar.unit) then
         startCast(bar, bar.unit, true);
@@ -146,7 +164,6 @@ end
 
 function M.UnregisterCastEvents(bar)
     bar:UnregisterAllEvents();
-    bar:SetScript("OnUpdate", nil);
 end
 
 function M.CreateCastBar(parent, unit, yoffset)