4 local ssub = string.sub;
6 local ceil = math.ceil;
7 local UnitCastingInfo, UnitChannelInfo = UnitCastingInfo, UnitChannelInfo;
8 local GetTime = GetTime;
9 local CTimerAfter = C_Timer.After;
11 local barTexture = "Interface\\AddOns\\OmaRF\\images\\minimalist";
12 local castingColor = {1, 0.49, 0}; -- from Quartz defaults
13 local nointerruptColor = {0.6, 0.6, 0.6};
14 local channelingColor = {0.32, 0.3, 1};
20 local function onUpdate(bar)
21 if not bar.updating then return end
22 --local width = bar.icon:IsShown() and bar.cast.width or bar.cast.width; -- TODO fullwidth
23 local startTime, endTime = bar.startTime, bar.endTime;
24 local currentClamped = min(GetTime(), endTime);
25 local remaining = endTime - currentClamped;
27 if bar.channeling then
28 percent = remaining / (endTime - startTime);
30 percent = (currentClamped - startTime) / (endTime - startTime);
33 local width = percent*bar.cast.width;
35 bar.cast:SetWidth(0.1);
37 bar.cast:SetWidth(width);
39 bar.time:SetFormattedText("%.1f", remaining);
40 CTimerAfter(0.03, updaters[bar]);
42 M.OnUpdate = onUpdate;
44 local function showBar(bar)
50 local function hideBar(bar)
55 local function toggleInterruptible(bar, nointr)
56 if bar.unit == "player" then return end
58 bar.cast:SetVertexColor(unpack(nointerruptColor));
61 bar.cast:SetVertexColor(unpack(bar.cast.color));
66 local function startCast(bar, unit, channeling)
67 local name, icon, startTime, endTime, noInterrupt, id;
69 name, _, icon, startTime, endTime, _, noInterrupt = UnitChannelInfo(unit);
70 if not startTime or not endTime then return nil end
71 bar.channeling = true;
72 bar.cast.color = channelingColor;
74 _, name, icon, startTime, endTime, _, _, noInterrupt, id = UnitCastingInfo(unit);
75 if not startTime or not endTime then return nil end
77 bar.cast.color = castingColor;
79 bar.startTime = startTime / 1000;
80 bar.endTime = endTime / 1000;
81 -- don't show samwise for non-existent icons
82 if icon ~= "Interface\\Icons\\Temp" then
83 bar.icon:SetTexture(icon);
85 bar.cast:SetWidth(channeling and bar.cast.width or 0.1);
88 bar.cast:SetWidth(channeling and bar.cast.width or 0.1); -- TODO use fullwidth
90 bar.spell:SetText(ssub(name, 1, bar.spell.count));
91 bar.time:SetFormattedText("%.1f", (endTime - startTime)/1000);
92 bar.cast:SetVertexColor(unpack(bar.cast.color));
94 toggleInterruptible(bar, noInterrupt);
97 M.StartCast = startCast;
99 local function applyDelay(bar, unit, channeling)
100 local startTime, endTime;
102 _, _, _, _, startTime, endTime = UnitChannelInfo(unit);
104 _, _, _, _, startTime, endTime = UnitCastingInfo(unit);
106 if not startTime or not endTime then return hideBar(bar) end
107 bar.startTime = startTime / 1000;
108 bar.endTime = endTime / 1000;
109 -- TODO show delay text
111 M.ApplyDelay = applyDelay;
114 ["UNIT_SPELLCAST_START"] = function(bar, unit)
115 startCast(bar, unit);
117 ["PLAYER_TARGET_CHANGED"] = function(bar)
119 if not startCast(bar, bar.unit) then
120 startCast(bar, bar.unit, true);
123 ["UNIT_SPELLCAST_CHANNEL_START"] = function(bar, unit)
124 startCast(bar, unit, true);
126 ["UNIT_SPELLCAST_STOP"] = function(bar)
129 ["UNIT_SPELLCAST_DELAYED"] = function(bar, unit)
130 applyDelay(bar, unit);
132 ["UNIT_SPELLCAST_CHANNEL_UPDATE"] = function(bar, unit)
133 applyDelay(bar, unit, true);
135 ["UNIT_SPELLCAST_INTERRUPTIBLE"] = function(bar)
136 toggleInterruptible(bar, false);
138 ["UNIT_SPELLCAST_NOT_INTERRUPTIBLE"] = function(bar)
139 toggleInterruptible(bar, true);
142 events["UNIT_SPELLCAST_CHANNEL_STOP"] = events["UNIT_SPELLCAST_STOP"];
144 local function onEvent(bar, event, unit)
145 if unit == bar.unit or (bar.unit == "player" and unit == "vehicle") then
146 events[event](bar, unit);
147 elseif event == "PLAYER_TARGET_CHANGED" then
152 function M.RegisterCastEvents(bar)
153 --bar:RegisterEvent("UNIT_SPELLCAST_SENT");
154 bar:RegisterEvent("UNIT_SPELLCAST_START");
155 bar:RegisterEvent("UNIT_SPELLCAST_STOP");
156 --bar:RegisterEvent("UNIT_SPELLCAST_FAILED");
157 --bar:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED");
158 bar:RegisterEvent("UNIT_SPELLCAST_DELAYED");
159 bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START");
160 bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP");
161 bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE");
162 bar:RegisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE");
163 bar:RegisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE");
164 if bar.unit == "target" then bar:RegisterEvent("PLAYER_TARGET_CHANGED") end
165 -- trigger initial check
166 if not startCast(bar, bar.unit) then
167 startCast(bar, bar.unit, true);
171 function M.UnregisterCastEvents(bar)
172 bar:UnregisterAllEvents();
176 function M.CreateCastBar(parent, unit, yoffset)
177 local bar = CreateFrame("Frame", parent:GetName().."CastBar", parent);
179 bar:SetPoint("BOTTOMLEFT", parent, "TOPLEFT", 0, yoffset);
180 bar:SetPoint("BOTTOMRIGHT", parent, "TOPRIGHT", 0, yoffset);
183 bar.back = bar:CreateTexture(nil, "BACKGROUND");
184 bar.back:SetAllPoints();
185 bar.back:SetColorTexture(0, 0, 0, 0.7);
186 bar.icon = bar:CreateTexture(nil, "ARTWORK", 1);
187 bar.icon:SetPoint("TOPLEFT", bar, "TOPLEFT", 1, -1);
188 bar.icon:SetPoint("BOTTOMRIGHT", bar, "BOTTOMLEFT", 21, 1);
189 bar.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93); -- remove borders (from Quartz)
190 bar.shield = bar:CreateTexture(nil, "ARTWORK");
191 bar.shield:SetPoint("CENTER", bar.icon, "CENTER", -2, -1);
192 bar.shield:SetWidth(28);
193 bar.shield:SetHeight(50);
194 bar.shield:SetTexture("Interface\\CastingBar\\UI-CastingBar-Small-Shield");
195 bar.shield:SetTexCoord(0, 36/256, 0, 1);
197 bar.cast = bar:CreateTexture(nil, "ARTWORK");
198 bar.cast:SetPoint("TOPLEFT", bar.icon, "TOPRIGHT", 1, 0);
199 bar.cast:SetPoint("BOTTOMLEFT", bar.icon, "BOTTOMRIGHT", 1, 0);
200 bar.cast.width = bar:GetWidth() - bar.icon:GetWidth() - 3;
201 bar.cast.fullwidth = bar:GetWidth() - 2; -- for casts without icon
202 bar.cast:SetWidth(bar.cast.width);
203 bar.cast:SetTexture(barTexture);
204 bar.cast.color = castingColor;
205 bar.spell = bar:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
206 bar.spell:SetPoint("LEFT", bar.icon, "RIGHT", 2, 0);
207 bar.spell.count = ceil(bar.cast:GetWidth()/10);
208 bar.time = bar:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
209 bar.time:SetPoint("RIGHT", bar, "RIGHT", -2, 0);
210 bar:SetScript("OnEvent", onEvent);
211 updaters[bar] = function() onUpdate(bar) end