X-Git-Url: https://www.aleksib.fi/git/wowui.git/blobdiff_plain/82ce2fc4623897a0b8ca99baac27f7c433226366..c2e174c52f3aa1eed78946009486d04c72447d11:/OmaUF/CastBar.lua diff --git a/OmaUF/CastBar.lua b/OmaUF/CastBar.lua index 1cb98bc..6170513 100644 --- a/OmaUF/CastBar.lua +++ b/OmaUF/CastBar.lua @@ -6,18 +6,20 @@ local min = math.min; local ceil = math.ceil; local UnitCastingInfo, UnitChannelInfo = UnitCastingInfo, UnitChannelInfo; local GetTime = GetTime; +local CTimerAfter = C_Timer.After; local barTexture = "Interface\\AddOns\\OmaRF\\images\\minimalist"; local castingColor = {1, 0.49, 0}; -- from Quartz defaults local nointerruptColor = {0.6, 0.6, 0.6}; local channelingColor = {0.32, 0.3, 1}; +local updaters = {}; local M = {}; OmaUFCastBar = M; local function onUpdate(bar) - if not bar:IsShown() then return end -- TODO little fadeout possibly - local width = bar.icon:IsShown() and bar.cast.width or bar.cast.width; -- TODO fullwidth + if not bar.updating 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); local remaining = endTime - currentClamped; @@ -28,13 +30,26 @@ local function onUpdate(bar) percent = (currentClamped - startTime) / (endTime - startTime); end - width = percent*width; + local width = percent*bar.cast.width; if width <= 0 then bar.cast:SetWidth(0.1); else bar.cast:SetWidth(width); end bar.time:SetFormattedText("%.1f", remaining); + CTimerAfter(0.03, updaters[bar]); +end +M.OnUpdate = onUpdate; + +local function showBar(bar) + bar:Show(); + bar.updating = true; + onUpdate(bar); +end + +local function hideBar(bar) + bar:Hide(); + bar.updating = nil; end local function toggleInterruptible(bar, nointr) @@ -43,7 +58,7 @@ local function toggleInterruptible(bar, nointr) bar.cast:SetVertexColor(unpack(nointerruptColor)); bar.shield:Show(); else - bar.cast:SetVertexColor(unpack(castingColor)); + bar.cast:SetVertexColor(unpack(bar.cast.color)); bar.shield:Hide(); end end @@ -51,13 +66,16 @@ end local function startCast(bar, unit, channeling) local name, icon, startTime, endTime, noInterrupt, id; if channeling then - name, _, _, icon, startTime, endTime, _, noInterrupt = UnitChannelInfo(unit); + 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); + _, 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 @@ -71,10 +89,12 @@ local function startCast(bar, unit, channeling) end bar.spell:SetText(ssub(name, 1, bar.spell.count)); bar.time:SetFormattedText("%.1f", (endTime - startTime)/1000); - bar:Show(); + bar.cast:SetVertexColor(unpack(bar.cast.color)); + showBar(bar); toggleInterruptible(bar, noInterrupt); return true; end +M.StartCast = startCast; local function applyDelay(bar, unit, channeling) local startTime, endTime; @@ -83,21 +103,28 @@ 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 end +M.ApplyDelay = applyDelay; 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); @@ -116,8 +143,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 @@ -133,7 +161,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); @@ -142,7 +170,7 @@ end function M.UnregisterCastEvents(bar) bar:UnregisterAllEvents(); - bar:SetScript("OnUpdate", nil); + hideBar(bar); end function M.CreateCastBar(parent, unit, yoffset) @@ -173,12 +201,13 @@ function M.CreateCastBar(parent, unit, yoffset) bar.cast.fullwidth = bar:GetWidth() - 2; -- for casts without icon bar.cast:SetWidth(bar.cast.width); bar.cast:SetTexture(barTexture); - bar.cast:SetVertexColor(unpack(castingColor)); + bar.cast.color = castingColor; bar.spell = bar:CreateFontString(nil, "OVERLAY", "GameFontHighlight"); bar.spell:SetPoint("LEFT", bar.icon, "RIGHT", 2, 0); bar.spell.count = ceil(bar.cast:GetWidth()/10); bar.time = bar:CreateFontString(nil, "OVERLAY", "GameFontHighlight"); bar.time:SetPoint("RIGHT", bar, "RIGHT", -2, 0); bar:SetScript("OnEvent", onEvent); + updaters[bar] = function() onUpdate(bar) end return bar; end