7585f81 - Fix coords display for Shadowlands
[wowui.git] / loitsu / castbar.lua
1 -- castbar.lua
2 local _, addon = ...;
3 local unpack = unpack;
4 local ssub = string.sub;
5 local min = math.min;
6 local ceil = math.ceil;
7 local UnitCastingInfo, UnitChannelInfo = UnitCastingInfo, UnitChannelInfo;
8 local GetTime = GetTime;
9 local CTimerAfter = C_Timer.After;
10 local init = CreateFrame("Frame", "LoitsuMain");
11
12 local barTexture = "Interface\\AddOns\\kehys\\images\\minimalist";
13 local castingColor = {1, 0.49, 0}; -- from Quartz defaults
14 local nointerruptColor = {0.6, 0.6, 0.6};
15 local channelingColor = {0.32, 0.3, 1};
16 local updaters = {};
17
18 local function onUpdate(bar)
19     if not bar.updating then return end
20     --local width = bar.icon:IsShown() and bar.cast.width or bar.cast.width; -- TODO fullwidth
21     local startTime, endTime = bar.startTime, bar.endTime;
22     local currentClamped = min(GetTime(), endTime);
23     local remaining = endTime - currentClamped;
24     local percent;
25     if bar.channeling then
26         percent = remaining / (endTime - startTime);
27     else
28         percent = (currentClamped - startTime) / (endTime - startTime);
29     end
30
31     local width = percent*bar.cast.width;
32     if width <= 0 then
33         bar.cast:SetWidth(0.1);
34     else
35         bar.cast:SetWidth(width);
36     end
37     bar.time:SetFormattedText("%.1f", remaining);
38     CTimerAfter(0.02, updaters[bar]);
39 end
40
41 local function showBar(bar)
42     bar:Show();
43     bar.updating = true;
44     onUpdate(bar);
45 end
46
47 local function hideBar(bar)
48     bar:Hide();
49     bar.updating = nil;
50 end
51
52 local function toggleInterruptible(bar, nointr)
53     if bar.unit == "player" then return end
54     if nointr then
55         bar.cast:SetVertexColor(unpack(nointerruptColor));
56         bar.shield:Show();
57     else
58         bar.cast:SetVertexColor(unpack(bar.cast.color));
59         bar.shield:Hide();
60     end
61 end
62
63 local function startCast(bar, unit, channeling)
64     local name, icon, startTime, endTime, noInterrupt, id;
65     if channeling then
66         name, _, icon, startTime, endTime, _, noInterrupt = UnitChannelInfo(unit);
67         if not startTime or not endTime then return nil end
68         bar.channeling = true;
69         bar.cast.color = channelingColor;
70     else
71         _, name, icon, startTime, endTime, _, _, noInterrupt, id = UnitCastingInfo(unit);
72         if not startTime or not endTime then return nil end
73         bar.channeling = nil;
74         bar.cast.color = castingColor;
75     end
76     bar.startTime = startTime / 1000;
77     bar.endTime = endTime / 1000;
78     -- don't show samwise for non-existent icons
79     if icon ~= "Interface\\Icons\\Temp" then
80         bar.icon:SetTexture(icon);
81         bar.icon:Show();
82         bar.cast:SetWidth(channeling and bar.cast.width or 0.1);
83     else
84         bar.icon:Hide();
85         bar.cast:SetWidth(channeling and bar.cast.width or 0.1); -- TODO use fullwidth
86     end
87     bar.spell:SetText(ssub(name, 1, bar.spell.count));
88     bar.time:SetFormattedText("%.1f", (endTime - startTime)/1000);
89     bar.cast:SetVertexColor(unpack(bar.cast.color));
90     showBar(bar);
91     toggleInterruptible(bar, noInterrupt);
92     return true;
93 end
94
95 local function applyDelay(bar, unit, channeling)
96     local startTime, endTime;
97     if channeling then
98         _, _, _, startTime, endTime = UnitChannelInfo(unit);
99     else
100         _, _, _, startTime, endTime = UnitCastingInfo(unit);
101     end
102     if not startTime or not endTime then return hideBar(bar) end
103     bar.startTime = startTime / 1000;
104     bar.endTime = endTime / 1000;
105 end
106
107 local events = {
108     ["UNIT_SPELLCAST_START"] = function(bar, unit)
109         startCast(bar, unit);
110     end,
111     ["PLAYER_TARGET_CHANGED"] = function(bar)
112         hideBar(bar);
113         if not startCast(bar, bar.unit) then
114             startCast(bar, bar.unit, true);
115         end
116     end,
117     ["UNIT_SPELLCAST_CHANNEL_START"] = function(bar, unit)
118         startCast(bar, unit, true);
119     end,
120     ["UNIT_SPELLCAST_STOP"] = function(bar)
121         hideBar(bar);
122     end,
123     ["UNIT_SPELLCAST_DELAYED"] = function(bar, unit)
124         applyDelay(bar, unit);
125     end,
126     ["UNIT_SPELLCAST_CHANNEL_UPDATE"] = function(bar, unit)
127         applyDelay(bar, unit, true);
128     end,
129     ["UNIT_SPELLCAST_INTERRUPTIBLE"] = function(bar)
130         toggleInterruptible(bar, false);
131     end,
132     ["UNIT_SPELLCAST_NOT_INTERRUPTIBLE"] = function(bar)
133         toggleInterruptible(bar, true);
134     end,
135 };
136 events["UNIT_SPELLCAST_CHANNEL_STOP"] = events["UNIT_SPELLCAST_STOP"];
137
138 local function onEvent(bar, event, unit)
139     if unit == bar.unit or (bar.unit == "player" and unit == "vehicle") then
140         events[event](bar, unit);
141     elseif event == "PLAYER_TARGET_CHANGED" then
142         events[event](bar);
143     end
144 end
145
146 local function registerCastEvents(bar)
147     --bar:RegisterEvent("UNIT_SPELLCAST_SENT");
148     bar:RegisterEvent("UNIT_SPELLCAST_START");
149     bar:RegisterEvent("UNIT_SPELLCAST_STOP");
150     --bar:RegisterEvent("UNIT_SPELLCAST_FAILED");
151     --bar:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED");
152     bar:RegisterEvent("UNIT_SPELLCAST_DELAYED");
153     bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START");
154     bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP");
155     bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE");
156     bar:RegisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE");
157     bar:RegisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE");
158     if bar.unit == "target" then bar:RegisterEvent("PLAYER_TARGET_CHANGED") end
159     -- trigger initial check
160     if not startCast(bar, bar.unit) then
161         startCast(bar, bar.unit, true);
162     end
163 end
164
165 local function unregisterCastEvents(bar)
166     bar:UnregisterAllEvents();
167     hideBar(bar);
168 end
169
170 local barid = 1;
171 local function createCastBar(unit, width, x, y)
172     local bar = CreateFrame("Frame", "loitsu"..barid, UIParent);
173     barid = barid + 1;
174     bar.unit = unit;
175     bar:SetPoint("CENTER", UIParent, "CENTER", x, y);
176     bar:SetWidth(width);
177     bar:SetHeight(22);
178     bar:Hide();
179     bar.back = bar:CreateTexture(nil, "BACKGROUND");
180     bar.back:SetAllPoints();
181     bar.back:SetColorTexture(0, 0, 0, 0.7);
182     bar.icon = bar:CreateTexture(nil, "ARTWORK", 1);
183     bar.icon:SetPoint("TOPLEFT", bar, "TOPLEFT", 1, -1);
184     bar.icon:SetPoint("BOTTOMRIGHT", bar, "BOTTOMLEFT", 21, 1);
185     bar.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93); -- remove borders (from Quartz)
186     bar.shield = bar:CreateTexture(nil, "ARTWORK");
187     bar.shield:SetPoint("CENTER", bar.icon, "CENTER", -2, -1);
188     bar.shield:SetWidth(28);
189     bar.shield:SetHeight(50);
190     bar.shield:SetTexture("Interface\\CastingBar\\UI-CastingBar-Small-Shield");
191     bar.shield:SetTexCoord(0, 36/256, 0, 1);
192     bar.shield:Hide();
193     bar.cast = bar:CreateTexture(nil, "ARTWORK");
194     bar.cast:SetPoint("TOPLEFT", bar.icon, "TOPRIGHT", 1, 0);
195     bar.cast:SetPoint("BOTTOMLEFT", bar.icon, "BOTTOMRIGHT", 1, 0);
196     bar.cast.width = bar:GetWidth() - bar.icon:GetWidth() - 3;
197     bar.cast.fullwidth = bar:GetWidth() - 2; -- for casts without icon
198     bar.cast:SetWidth(bar.cast.width);
199     bar.cast:SetTexture(barTexture);
200     bar.cast.color = castingColor;
201     bar.spell = bar:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
202     bar.spell:SetPoint("LEFT", bar.icon, "RIGHT", 2, 0);
203     bar.spell.count = ceil(bar.cast:GetWidth()/10);
204     bar.time = bar:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
205     bar.time:SetPoint("RIGHT", bar, "RIGHT", -2, 0);
206     bar:SetScript("OnEvent", onEvent);
207     updaters[bar] = function() onUpdate(bar) end
208     registerCastEvents(bar);
209     return bar;
210 end
211
212 init:SetScript("OnEvent", function (self)
213     self:UnregisterAllEvents();
214     createCastBar("player", 160, -300, -140);
215     createCastBar("target", 160, 290, -140);
216 end);
217 init:RegisterEvent("PLAYER_LOGIN");