ba69982 - Fix raid aura throttling and throttle unit frame auras as well
[wowui.git] / OmaRF / Events.lua
1 -- Events.lua
2 local _;
3 local unpack = unpack;
4 local ssub = string.sub;
5 local min = math.min;
6 local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists;
7 local UnitDebuff, UnitIsCharmed = UnitDebuff, UnitIsCharmed;
8 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
9 local UnitIsAFK, UnitIsDND = UnitIsAFK, UnitIsDND;
10 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
11 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
12 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
13 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
14 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
15 local GetReadyCheckTimeLeft, GetReadyCheckStatus = GetReadyCheckTimeLeft, GetReadyCheckStatus;
16 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
17 local GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture;
18 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
19 local READY_CHECK_READY_TEXTURE = READY_CHECK_READY_TEXTURE;
20 local READY_CHECK_NOT_READY_TEXTURE = READY_CHECK_NOT_READY_TEXTURE;
21 local READY_CHECK_WAITING_TEXTURE = READY_CHECK_WAITING_TEXTURE;
22
23 local updateIndicatorAuras = OmaRFIndicators.UpdateAuras;
24 local updateMajorAuras = OmaRFIndicators.UpdateMajorAuras;
25
26 local Settings = OmaRFSettings;
27 local baseColor = Settings.BaseColor;
28 local overlayColorDispel = Settings.OverlayColorDispel;
29 local overlayColorCharm = Settings.OverlayColorCharm;
30 local overlayColorAlert = Settings.OverlayColorAlert;
31 local width = Settings.Width;
32
33 local M = {};
34 OmaRFEvents = M;
35 function M.RegisterEvents(frame)
36     frame:RegisterEvent("PLAYER_ENTERING_WORLD");
37     frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
38     frame:RegisterEvent("READY_CHECK");
39     frame:RegisterEvent("READY_CHECK_FINISHED");
40     frame:RegisterEvent("GROUP_ROSTER_UPDATE");
41     frame:RegisterEvent("RAID_TARGET_UPDATE");
42     if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
43 end
44
45 function M.RegisterUnitEvents(frame)
46     -- events are taken from FrameXML/CompactUnitFrame.lua
47     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
48     frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
49     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
50     frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
51     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
52     frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
53     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
54     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
55     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
56     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
57     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
58     frame:RegisterUnitEvent("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
59     frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
60     frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
61     frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
62     frame:RegisterUnitEvent("UNIT_PET", frame.unit, displayed);
63 end
64 local registerUnitEvents = M.RegisterUnitEvents;
65
66 local function updateText(frame, unit)
67     if UnitIsDeadOrGhost(unit) then
68         frame.dead = true;
69         frame.text:SetText("Dead");
70         frame.text:Show();
71     elseif not UnitIsConnected(unit) then
72         frame.text:SetText("DC");
73         frame.text:Show();
74     elseif UnitIsAFK(unit) then
75         frame.text:SetText("afk");
76         frame.text:Show();
77     elseif UnitIsDND(unit) then
78         frame.text:SetText("dnd");
79         frame.text:Show();
80     else
81         frame.text:Hide();
82     end
83 end
84 M.UpdateText = updateText;
85
86 local function updateMaxHealth(frame, unit)
87     frame.health.max = UnitHealthMax(unit);
88 end
89 M.UpdateMaxHealth = updateMaxHealth;
90
91 local function updateHealth(frame, unit)
92     local current, max = UnitHealth(unit), frame.health.max;
93     if current > max or max <= 0 then
94         -- somehow current health has gone over the maximum (missed maxhealth event possibly)
95         -- just put health bar full and update max health for next event
96         frame.health:SetWidth(width);
97         frame.health.width = width;
98         updateMaxHealth(frame, unit);
99         frame.health:Show();
100     elseif current <= 0 or UnitIsDeadOrGhost(unit) then
101         frame.health:Hide();
102         return updateText(frame, unit); -- update death
103     else
104         local w = current/max*width;
105         frame.health:SetWidth(w);
106         frame.health.width = w;
107         frame.health:Show();
108     end
109
110     if frame.dead and current > 0 then
111         frame.dead = nil;
112         updateText(frame, unit); -- update revive
113     end
114 end
115 M.UpdateHealth = updateHealth;
116
117 local function updateName(frame, unit)
118     local name = UnitName(unit);
119     if not name then return end
120     name = ssub(name, 1, 6);
121     if frame.unit == unit then
122         frame.name:SetText(name);
123     else
124         frame.name:SetFormattedText("-%s", name);
125     end
126
127     local _, class = UnitClass(unit);
128     local color = RAID_CLASS_COLORS[class];
129     if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
130 end
131 M.UpdateName = updateName;
132
133 local function updateHealPred(frame, unit)
134     local incoming = UnitGetIncomingHeals(unit) or 0;
135     if incoming > 0 then
136         incoming = (incoming / frame.health.max) * width;
137         -- always at least 1 pixel space for heal prediction
138         frame.healpred:SetWidth(min(width - frame.health.width + 1, incoming));
139         frame.healpred:Show();
140     else
141         frame.healpred:Hide();
142     end
143 end
144 M.UpdateHealPred = updateHealPred;
145
146 local function updateShield(frame, unit)
147     local shield = UnitGetTotalAbsorbs(unit) or 0;
148     if shield > 0 then
149         local space = width - frame.health.width;
150         shield = (shield / frame.health.max) * width;
151         if space == 0 then
152             frame.shield:Hide();
153             frame.shieldhl:Show();
154         elseif space < shield then
155             frame.shield:SetWidth(space);
156             frame.shield:Show();
157             frame.shieldhl:Show();
158         else
159             frame.shield:SetWidth(shield);
160             frame.shield:Show();
161             frame.shieldhl:Hide();
162         end
163     else
164         frame.shield:Hide();
165         frame.shieldhl:Hide();
166     end
167 end
168 M.UpdateShield = updateShield;
169
170 local function updateHealAbsorb(frame, unit)
171     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
172     if absorb > 0 then
173         absorb = (absorb / frame.health.max) * width;
174         frame.healabsorb:SetWidth(min(frame.health.width, absorb));
175         frame.healabsorb:Show();
176     else
177         frame.healabsorb:Hide();
178     end
179 end
180 M.UpdateHealAbsorb = updateHealAbsorb;
181
182 local function updateAuras(frame, unit)
183     updateIndicatorAuras(frame, unit); -- this is throttled
184     if updateMajorAuras(frame, unit) then
185         if frame.overlay.color ~= overlayColorAlert then
186             frame.overlay:SetVertexColor(unpack(overlayColorAlert));
187             frame.overlay.color = overlayColorAlert;
188             frame.overlay:Show();
189         end
190     elseif UnitDebuff(unit, 1, "RAID") ~= nil then
191         -- something dispellable
192         if frame.overlay.color ~= overlayColorDispel then
193             frame.overlay:SetVertexColor(unpack(overlayColorDispel));
194             frame.overlay.color = overlayColorDispel;
195             frame.overlay:Show();
196         end
197     -- don't overlay charmed when in vehicle
198     elseif UnitIsCharmed(unit) and unit == frame.unit then
199         if frame.overlay.color ~= overlayColorCharm then
200             frame.overlay:SetVertexColor(unpack(overlayColorCharm));
201             frame.overlay.color = overlayColorCharm;
202             frame.overlay:Show();
203         end
204     else
205         if frame.overlay.color ~= nil then
206             frame.overlay.color = nil;
207             frame.overlay:Hide();
208         end
209     end
210 end
211 M.UpdateAuras = updateAuras;
212
213 local function updateAggro(frame, unit)
214     local status = UnitThreatSituation(unit);
215     if status and status > 0 then
216         frame.base:SetVertexColor(GetThreatStatusColor(status));
217     else
218         frame.base:SetVertexColor(unpack(baseColor));
219     end
220 end
221 M.UpdateAggro = updateAggro;
222
223 local function updateVehicle(frame)
224     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
225         UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
226     if shouldTargetVehicle then
227         if not frame.inVehicle then
228             frame.inVehicle = true;
229             frame.displayed = frame.vehicle;
230             registerUnitEvents(frame);
231         end
232     elseif frame.inVehicle then
233         frame.inVehicle = false;
234         frame.displayed = frame.unit;
235         registerUnitEvents(frame);
236     end
237 end
238 M.UpdateVehicle = updateVehicle;
239
240 local function updateRole(frame, unit)
241     local role = UnitGroupRolesAssigned(unit);
242     if role == "HEALER" then
243         frame.role:SetTexCoord(0.75, 1, 0, 1);
244         frame.role:Show();
245     elseif role == "TANK" then
246         frame.role:SetTexCoord(0.5, 0.75, 0, 1);
247         frame.role:Show();
248     else
249         frame.role:Hide();
250     end
251 end
252 M.UpdateRole = updateRole;
253
254 local function updateReadyCheck(frame, unit)
255     local status = GetReadyCheckStatus(unit);
256     if status == "ready" then
257         frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
258         frame.ready:Show()
259     elseif status == "notready" then
260         frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
261         frame.ready:Show()
262     elseif status == "waiting" then
263         frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
264         frame.ready:Show()
265     else
266         frame.ready:Hide()
267     end
268 end
269 M.UpdateReadyCheck = updateReadyCheck;
270
271 local function updateRaidMarker(frame, unit)
272     local index = GetRaidTargetIndex(unit);
273     if index then
274         SetRaidTargetIconTexture(frame.targeticon, index);
275         frame.targeticon:Show();
276     else
277         frame.targeticon:Hide();
278     end
279 end
280 M.UpdateRaidMarker = updateRaidMarker;
281
282 local eventFuncs = {
283     ["UNIT_HEALTH"] = function(frame)
284         updateHealth(frame, frame.displayed);
285         updateShield(frame, frame.displayed);
286         updateHealAbsorb(frame, frame.displayed);
287         -- no heal prediction update, that doesn't overflow too much
288     end,
289     ["UNIT_AURA"] = function(frame)
290         updateAuras(frame, frame.displayed);
291     end,
292     ["UNIT_HEAL_PREDICTION"] = function(frame)
293         updateHealPred(frame, frame.displayed);
294     end,
295     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
296         updateShield(frame, frame.displayed);
297     end,
298     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
299         updateHealAbsorb(frame, frame.displayed);
300     end,
301     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
302         updateAggro(frame, frame.displayed);
303     end,
304     ["UNIT_MAXHEALTH"] = function(frame)
305         updateMaxHealth(frame, frame.displayed);
306         updateHealth(frame, frame.displayed);
307         updateShield(frame, frame.displayed);
308         updateHealAbsorb(frame, frame.displayed);
309     end,
310     ["UNIT_NAME_UPDATE"] = function(frame)
311         updateName(frame, frame.unit);
312     end,
313     ["UNIT_CONNECTION"] = function(frame)
314         updateText(frame, frame.displayed);
315     end,
316     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
317         updateRole(frame, frame.unit);
318     end,
319     ["READY_CHECK"] = function(frame)
320         updateReadyCheck(frame, frame.unit);
321     end,
322     ["RAID_TARGET_UPDATE"] = function(frame)
323         updateRaidMarker(frame, frame.displayed);
324     end,
325     ["UPDATE_ALL_BARS"] = function(frame)
326         updateRole(frame, frame.unit);
327         updateVehicle(frame);
328         updateMaxHealth(frame, frame.displayed);
329         updateHealth(frame, frame.displayed);
330         updateText(frame, frame.displayed);
331         updateAuras(frame, frame.displayed);
332         updateShield(frame, frame.displayed);
333         updateHealPred(frame, frame.displayed);
334         updateHealAbsorb(frame, frame.displayed);
335         updateAggro(frame, frame.displayed);
336         updateName(frame, frame.unit);
337         updateReadyCheck(frame, frame.unit);
338         updateRaidMarker(frame, frame.displayed);
339     end,
340 };
341 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
342 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
343 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
344 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
345 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
346 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
347 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
348 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
349 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
350 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
351
352 function M.UnitEvent(self, event)
353     return eventFuncs[event](self);
354 end