bccab8e - Add castbars
authorAleksi Blinnikka <aleksi.blinnikka@gmail.com>
Thu, 7 May 2020 16:25:01 +0000
committerAleksi Blinnikka <aleksi.blinnikka@gmail.com>
Thu, 7 May 2020 16:25:01 +0000
loitsu/castbar.lua [new file with mode: 0644]
loitsu/loitsu.toc [new file with mode: 0644]

diff --git a/loitsu/castbar.lua b/loitsu/castbar.lua
new file mode 100644 (file)
index 0000000..a78e2fc
--- /dev/null
@@ -0,0 +1,217 @@
+-- castbar.lua
+local _, addon = ...;
+local unpack = unpack;
+local ssub = string.sub;
+local min = math.min;
+local ceil = math.ceil;
+local UnitCastingInfo, UnitChannelInfo = UnitCastingInfo, UnitChannelInfo;
+local GetTime = GetTime;
+local CTimerAfter = C_Timer.After;
+local init = CreateFrame("Frame", "LoitsuMain");
+
+local barTexture = "Interface\\AddOns\\kehys\\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 function onUpdate(bar)
+    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;
+    local percent;
+    if bar.channeling then
+        percent = remaining / (endTime - startTime);
+    else
+        percent = (currentClamped - startTime) / (endTime - startTime);
+    end
+
+    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.02, updaters[bar]);
+end
+
+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)
+    if bar.unit == "player" then return end
+    if nointr then
+        bar.cast:SetVertexColor(unpack(nointerruptColor));
+        bar.shield:Show();
+    else
+        bar.cast:SetVertexColor(unpack(bar.cast.color));
+        bar.shield:Hide();
+    end
+end
+
+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
+    bar.startTime = startTime / 1000;
+    bar.endTime = endTime / 1000;
+    -- don't show samwise for non-existent icons
+    if icon ~= "Interface\\Icons\\Temp" then
+        bar.icon:SetTexture(icon);
+        bar.icon:Show();
+        bar.cast:SetWidth(channeling and bar.cast.width or 0.1);
+    else
+        bar.icon:Hide();
+        bar.cast:SetWidth(channeling and bar.cast.width or 0.1); -- TODO use fullwidth
+    end
+    bar.spell:SetText(ssub(name, 1, bar.spell.count));
+    bar.time:SetFormattedText("%.1f", (endTime - startTime)/1000);
+    bar.cast:SetVertexColor(unpack(bar.cast.color));
+    showBar(bar);
+    toggleInterruptible(bar, noInterrupt);
+    return true;
+end
+
+local function applyDelay(bar, unit, channeling)
+    local startTime, endTime;
+    if channeling then
+        _, _, _, startTime, endTime = UnitChannelInfo(unit);
+    else
+        _, _, _, startTime, endTime = UnitCastingInfo(unit);
+    end
+    if not startTime or not endTime then return hideBar(bar) end
+    bar.startTime = startTime / 1000;
+    bar.endTime = endTime / 1000;
+end
+
+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)
+        hideBar(bar);
+    end,
+    ["UNIT_SPELLCAST_DELAYED"] = function(bar, unit)
+        applyDelay(bar, unit);
+    end,
+    ["UNIT_SPELLCAST_CHANNEL_UPDATE"] = function(bar, unit)
+        applyDelay(bar, unit, true);
+    end,
+    ["UNIT_SPELLCAST_INTERRUPTIBLE"] = function(bar)
+        toggleInterruptible(bar, false);
+    end,
+    ["UNIT_SPELLCAST_NOT_INTERRUPTIBLE"] = function(bar)
+        toggleInterruptible(bar, true);
+    end,
+};
+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
+        events[event](bar, unit);
+    elseif event == "PLAYER_TARGET_CHANGED" then
+        events[event](bar);
+    end
+end
+
+local function registerCastEvents(bar)
+    --bar:RegisterEvent("UNIT_SPELLCAST_SENT");
+    bar:RegisterEvent("UNIT_SPELLCAST_START");
+    bar:RegisterEvent("UNIT_SPELLCAST_STOP");
+    --bar:RegisterEvent("UNIT_SPELLCAST_FAILED");
+    --bar:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED");
+    bar:RegisterEvent("UNIT_SPELLCAST_DELAYED");
+    bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START");
+    bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP");
+    bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE");
+    bar:RegisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE");
+    bar:RegisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE");
+    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);
+    end
+end
+
+local function unregisterCastEvents(bar)
+    bar:UnregisterAllEvents();
+    hideBar(bar);
+end
+
+local barid = 1;
+local function createCastBar(unit, width, x, y)
+    local bar = CreateFrame("Frame", "loitsu"..barid, UIParent);
+    barid = barid + 1;
+    bar.unit = unit;
+    bar:SetPoint("CENTER", UIParent, "CENTER", x, y);
+    bar:SetWidth(width);
+    bar:SetHeight(22);
+    bar:Hide();
+    bar.back = bar:CreateTexture(nil, "BACKGROUND");
+    bar.back:SetAllPoints();
+    bar.back:SetColorTexture(0, 0, 0, 0.7);
+    bar.icon = bar:CreateTexture(nil, "ARTWORK", 1);
+    bar.icon:SetPoint("TOPLEFT", bar, "TOPLEFT", 1, -1);
+    bar.icon:SetPoint("BOTTOMRIGHT", bar, "BOTTOMLEFT", 21, 1);
+    bar.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93); -- remove borders (from Quartz)
+    bar.shield = bar:CreateTexture(nil, "ARTWORK");
+    bar.shield:SetPoint("CENTER", bar.icon, "CENTER", -2, -1);
+    bar.shield:SetWidth(28);
+    bar.shield:SetHeight(50);
+    bar.shield:SetTexture("Interface\\CastingBar\\UI-CastingBar-Small-Shield");
+    bar.shield:SetTexCoord(0, 36/256, 0, 1);
+    bar.shield:Hide();
+    bar.cast = bar:CreateTexture(nil, "ARTWORK");
+    bar.cast:SetPoint("TOPLEFT", bar.icon, "TOPRIGHT", 1, 0);
+    bar.cast:SetPoint("BOTTOMLEFT", bar.icon, "BOTTOMRIGHT", 1, 0);
+    bar.cast.width = bar:GetWidth() - bar.icon:GetWidth() - 3;
+    bar.cast.fullwidth = bar:GetWidth() - 2; -- for casts without icon
+    bar.cast:SetWidth(bar.cast.width);
+    bar.cast:SetTexture(barTexture);
+    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
+    registerCastEvents(bar);
+    return bar;
+end
+
+init:SetScript("OnEvent", function (self)
+    self:UnregisterAllEvents();
+    createCastBar("player", 160, -300, -140);
+    createCastBar("target", 160, 300, -140);
+end);
+init:RegisterEvent("PLAYER_LOGIN");
diff --git a/loitsu/loitsu.toc b/loitsu/loitsu.toc
new file mode 100644 (file)
index 0000000..ffa7f30
--- /dev/null
@@ -0,0 +1,7 @@
+## Interface: 80300
+## Title: Loitsu Cast Bar
+## Version: 1.0
+## Author: schyrio
+## Notes: My cast bars
+
+castbar.lua