001062c - Refector indicators to be more flexible
[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 UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType;
9 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
10 local UnitIsAFK, UnitIsDND = UnitIsAFK, UnitIsDND;
11 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
12 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
13 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
14 local UnitHasIncomingResurrection = UnitHasIncomingResurrection;
15 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
16 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
17 local GetReadyCheckTimeLeft, GetReadyCheckStatus = GetReadyCheckTimeLeft, GetReadyCheckStatus;
18 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
19 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
20 local READY_CHECK_READY_TEXTURE = READY_CHECK_READY_TEXTURE;
21 local READY_CHECK_NOT_READY_TEXTURE = READY_CHECK_NOT_READY_TEXTURE;
22 local READY_CHECK_WAITING_TEXTURE = READY_CHECK_WAITING_TEXTURE;
23
24 local checkIndicators = OmaRFIndicators.CheckIndicators;
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 powerColors = Settings.PowerColors;
32 local width = Settings.Width;
33
34 local M = {};
35 OmaRFEvents = M;
36 function M.RegisterEvents(frame)
37     frame:RegisterEvent("PLAYER_ENTERING_WORLD");
38     frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
39     frame:RegisterEvent("READY_CHECK");
40     frame:RegisterEvent("READY_CHECK_FINISHED");
41     frame:RegisterEvent("PARTY_MEMBER_ENABLE");
42     frame:RegisterEvent("PARTY_MEMBER_DISABLE");
43     frame:RegisterEvent("GROUP_ROSTER_UPDATE");
44     if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
45 end
46
47 local function unregisterPower(frame)
48     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
49     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT");
50     frame.mana:Hide();
51     frame:UnregisterEvent("UNIT_POWER");
52     frame:UnregisterEvent("UNIT_MAXPOWER");
53     frame:UnregisterEvent("UNIT_DISPLAYPOWER");
54     frame:UnregisterEvent("UNIT_POWER_BAR_SHOW");
55     frame:UnregisterEvent("UNIT_POWER_BAR_HIDE");
56 end
57
58 local function registerPower(frame)
59     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
60     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
61     frame.mana:Show();
62     frame:RegisterUnitEvent("UNIT_POWER", frame.unit);
63     frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit);
64     frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit);
65     frame:RegisterUnitEvent("UNIT_POWER_BAR_SHOW", frame.unit);
66     frame:RegisterUnitEvent("UNIT_POWER_BAR_HIDE", frame.unit);
67 end
68
69 function M.RegisterUnitEvents(frame)
70     -- events are taken from FrameXML/CompactUnitFrame.lua
71     -- TODO raid marker support
72     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
73     frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
74     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
75     frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
76     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
77     frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
78     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
79     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
80     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
81     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
82     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
83     frame:RegisterUnitEvent("INCOMING_RESURRECT_CHANGED", frame.unit, displayed);
84     frame:RegisterUnitEvent("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
85     frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
86     frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
87     frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
88     frame:RegisterUnitEvent("UNIT_PET", frame.unit, displayed);
89     frame:RegisterUnitEvent("RAID_TARGET_UPDATE", frame.unit, displayed);
90 end
91 local registerUnitEvents = M.RegisterUnitEvents;
92
93 local function updateHealth(frame, unit)
94     local current, max = UnitHealth(unit), frame.health.max;
95     frame.health:Show();
96     -- sanity check, occasionally UnitHealthMax gives zero
97     if current > max then
98         -- somehow current health has gone over the maximum (missed maxhealth event)
99         frame.health.max = UnitHealthMax(unit);
100         max = frame.health.max;
101         if current > max then
102             -- error state, still over maximum
103             frame.health:SetWidth(width);
104             return;
105         end
106     elseif max > 0 then
107         frame.health:SetWidth(current/frame.health.max*width);
108     else
109         frame.health:SetWidth(width);
110         return;
111     end
112
113     if UnitIsDeadOrGhost(unit) then
114         frame.health:Hide();
115     end
116 end
117
118 local function updateText(frame, unit)
119     local current, max = UnitHealth(unit), frame.health.max;
120     local healthLost = max - current;
121     if UnitIsDeadOrGhost(unit) then
122         frame.text:SetText("Dead");
123         frame.text:Show();
124     elseif not UnitIsConnected(unit) then
125         frame.text:SetText("DC");
126         frame.text:Show();
127     elseif UnitIsAFK(unit) then
128         frame.text:SetText("afk");
129         frame.text:Show();
130     elseif UnitIsDND(unit) and healthLost == 0 then
131         frame.text:SetText("dnd");
132         frame.text:Show();
133     elseif healthLost > 0 then
134         if healthLost > 1200000000 then -- 1.2B
135             frame.text:SetFormattedText("-%.1fB", healthLost / 1000000000);
136         elseif healthLost > 1200000 then -- 1.2M
137             frame.text:SetFormattedText("-%.1fM", healthLost / 1000000);
138         elseif healthLost > 1000 then -- 1K
139             frame.text:SetFormattedText("-%dK", healthLost / 1000);
140         else
141             frame.text:SetFormattedText("-%d", healthLost)
142         end
143         frame.text:Show();
144     else
145         frame.text:Hide();
146     end
147 end
148
149 local function updateIncomingRes(frame, unit)
150     if UnitHasIncomingResurrection(unit) then
151         frame.rez:Show();
152     else
153         frame.rez:Hide();
154     end
155 end
156
157 local function updateMaxHealth(frame, unit)
158     frame.health.max = UnitHealthMax(unit);
159 end
160
161 local function updatePower(frame, unit)
162     local current, max = UnitPower(unit), frame.mana.max;
163     -- sanity check, occasionally UnitPowerMax gives zero
164     if current > max then
165         frame.mana.max = UnitPowerMax(unit);
166         max = frame.mana.max;
167         if current > max then
168             -- error
169             frame.mana:SetWidth(width);
170             return;
171         end
172     elseif max > 0 then
173         frame.mana:SetWidth(UnitPower(unit)/max*width);
174     else
175         frame.mana:SetWidth(width);
176     end
177 end
178
179 local function updateMaxPower(frame, unit)
180     frame.mana.max = UnitPowerMax(unit);
181 end
182
183 local function updatePowerColor(frame, unit)
184     frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
185 end
186
187 local function updateName(frame, unit)
188     local name = UnitName(unit);
189     if not name then return end
190     frame.name:SetText(ssub(name, 1, 6));
191
192     local _, class = UnitClass(unit);
193     local color = RAID_CLASS_COLORS[class];
194     if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
195 end
196
197 local function updateHealPred(frame, unit)
198     local incoming = UnitGetIncomingHeals(unit) or 0;
199     if incoming > 0 then
200         local max = frame.health.max;
201         local space = width - frame.health:GetWidth() + 1;
202         local pred = (incoming / max) * width;
203         frame.healpred:SetWidth(min(space, pred));
204         frame.healpred:Show();
205     else
206         frame.healpred:Hide();
207     end
208 end
209
210 local function updateShield(frame, unit)
211     local shield = UnitGetTotalAbsorbs(unit) or 0;
212     if shield > 0 then
213         local max = frame.health.max;
214         local space = width - frame.health:GetWidth();
215         shield = (shield / max) * width;
216         if space < shield then
217             frame.shield:SetWidth(space);
218             frame.shieldhl:Show();
219         else
220             frame.shield:SetWidth(shield);
221             frame.shieldhl:Hide();
222         end
223         frame.shield:Show();
224     else
225         frame.shield:Hide();
226         frame.shieldhl:Hide();
227     end
228 end
229
230 local function updateHealAbsorb(frame, unit)
231     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
232     if absorb > 0 then
233         local max = frame.health.max;
234         local space = frame.health:GetWidth();
235         absorb = (absorb / max) * width;
236         frame.healabsorb:SetWidth(min(space, absorb));
237         frame.healabsorb:Show();
238     else
239         frame.healabsorb:Hide();
240     end
241 end
242
243 local function updateAuras(frame, unit)
244     local alert = checkIndicators(frame, unit);
245     if alert then
246         if frame.overlay.color ~= overlayColorAlert then
247             frame.overlay:SetVertexColor(unpack(overlayColorAlert));
248             frame.overlay:Show();
249             frame.overlay.color = overlayColorAlert;
250         end
251     elseif UnitDebuff(unit, 1, "RAID") ~= nil then
252         -- something dispellable
253         if frame.overlay.color ~= overlayColorDispel then
254             frame.overlay:SetVertexColor(unpack(overlayColorDispel));
255             frame.overlay:Show();
256             frame.overlay.color = overlayColorDispel;
257         end
258     -- don't overlay charmed when in vehicle
259     elseif UnitIsCharmed(unit) and unit == frame.unit then
260         if frame.overlay.color ~= overlayColorCharm then
261             frame.overlay:SetVertexColor(unpack(overlayColorCharm));
262             frame.overlay:Show();
263             frame.overlay.color = overlayColorCharm;
264         end
265     else
266         if frame.overlay.color ~= nil then
267             frame.overlay:Hide();
268             frame.overlay.color = nil;
269         end
270     end
271 end
272
273 local function updateAggro(frame, unit)
274     local status = UnitThreatSituation(unit);
275     if status and status > 0 then
276         frame.base:SetVertexColor(GetThreatStatusColor(status));
277     else
278         frame.base:SetVertexColor(unpack(baseColor));
279     end
280 end
281
282 local function updateVehicle(frame)
283     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
284     if shouldTargetVehicle then
285         if not frame.inVehicle then
286             frame.inVehicle = true;
287             frame.displayed = frame.vehicle;
288             registerUnitEvents(frame);
289         end
290     elseif frame.inVehicle then
291         frame.inVehicle = false;
292         frame.displayed = frame.unit;
293         registerUnitEvents(frame);
294     end
295 end
296
297 local function updateRole(frame, unit)
298     local role = UnitGroupRolesAssigned(unit);
299     if role == "HEALER" then
300         frame.role:SetTexCoord(0.75, 1, 0, 1);
301         frame.role:Show();
302         if not frame.role.healer then
303             registerPower(frame);
304             frame.role.healer = true;
305         end
306     elseif role == "TANK" then
307         frame.role:SetTexCoord(0.5, 0.75, 0, 1);
308         frame.role:Show();
309         if frame.role.healer then
310             unregisterPower(frame);
311             frame.role.healer = false;
312         end
313     else
314         frame.role:Hide();
315         if frame.role.healer then
316             unregisterPower(frame);
317             frame.role.healer = false;
318         end
319     end
320 end
321
322 local function updateReadyCheck(frame, unit)
323     local status = GetReadyCheckStatus(unit);
324     if status == "ready" then
325         frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
326         frame.ready:Show()
327     elseif status == "notready" then
328         frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
329         frame.ready:Show()
330     elseif status == "waiting" then
331         frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
332         frame.ready:Show()
333     else
334         frame.ready:Hide()
335     end
336 end
337
338 local function updateRaidMarker(frame, unit)
339     --print(unit, "marker");
340 end
341
342 local eventFuncs = {
343     ["UNIT_HEALTH"] = function(frame)
344         updateHealth(frame, frame.displayed);
345         updateText(frame, frame.displayed);
346         updateShield(frame, frame.displayed);
347         updateHealAbsorb(frame, frame.displayed);
348         -- no heal prediction update, that doesn't overflow too much
349         -- raid marker update here, because marker is removed when unit dies
350         -- without a RAID_TARGET_UPDATE event
351         updateRaidMarker(frame, frame.unit);
352     end,
353     ["UNIT_POWER"] = function(frame)
354         updatePower(frame, frame.displayed);
355     end,
356     ["UNIT_AURA"] = function(frame)
357         updateAuras(frame, frame.displayed);
358     end,
359     ["UNIT_HEAL_PREDICTION"] = function(frame)
360         updateHealPred(frame, frame.displayed);
361     end,
362     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
363         updateShield(frame, frame.displayed);
364     end,
365     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
366         updateHealAbsorb(frame, frame.displayed);
367     end,
368     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
369         updateAggro(frame, frame.displayed);
370     end,
371     ["UNIT_MAXHEALTH"] = function(frame)
372         updateMaxHealth(frame, frame.displayed);
373         updateHealth(frame, frame.displayed);
374         updateText(frame, frame.displayed);
375         updateShield(frame, frame.displayed);
376         updateHealAbsorb(frame, frame.displayed);
377     end,
378     ["UNIT_MAXPOWER"] = function(frame)
379         updateMaxPower(frame, frame.displayed);
380         updatePower(frame, frame.displayed);
381     end,
382     ["UNIT_DISPLAYPOWER"] = function(frame)
383         updatePowerColor(frame, frame.displayed);
384         updateMaxPower(frame, frame.displayed);
385         updatePower(frame, frame.displayed);
386     end,
387     ["UNIT_NAME_UPDATE"] = function(frame)
388         updateName(frame, frame.displayed);
389     end,
390     ["UNIT_CONNECTION"] = function(frame)
391         updateText(frame, frame.displayed);
392     end,
393     ["INCOMING_RESURRECT_CHANGED"] = function(frame)
394         updateIncomingRes(frame, frame.unit);
395     end,
396     ["PARTY_MEMBER_ENABLE"] = function(frame)
397         -- new power info possibly (FrameXML/CompactUnitFrame.lua)
398         updateMaxPower(frame, frame.displayed);
399         updatePowerColor(frame, frame.displayed);
400     end,
401     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
402         updateRole(frame, frame.unit);
403     end,
404     ["READY_CHECK"] = function(frame)
405         updateReadyCheck(frame, frame.unit);
406     end,
407     ["RAID_TARGET_UPDATE"] = function(frame)
408         updateRaidMarker(frame, frame.unit);
409     end,
410     ["UPDATE_ALL_BARS"] = function(frame)
411         updateRole(frame, frame.unit);
412         updateVehicle(frame);
413         updateMaxHealth(frame, frame.displayed);
414         updateMaxPower(frame, frame.displayed);
415         updateHealth(frame, frame.displayed);
416         updateText(frame, frame.displayed);
417         updatePower(frame, frame.displayed);
418         updateAuras(frame, frame.displayed);
419         updateShield(frame, frame.displayed);
420         updateHealPred(frame, frame.displayed);
421         updateHealAbsorb(frame, frame.displayed);
422         updatePowerColor(frame, frame.displayed);
423         updateAggro(frame, frame.displayed);
424         updateName(frame, frame.displayed);
425         updateIncomingRes(frame, frame.unit);
426         updateReadyCheck(frame, frame.unit);
427         updateRaidMarker(frame, frame.unit);
428     end,
429 };
430 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
431 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
432 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
433 eventFuncs["UNIT_POWER_BAR_SHOW"] = eventFuncs["UNIT_DISPLAYPOWER"];
434 eventFuncs["UNIT_POWER_BAR_HIDE"] = eventFuncs["UNIT_DISPLAYPOWER"];
435 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
436 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
437 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
438 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
439 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
440 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
441 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
442 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
443
444 function M.UnitEvent(self, event)
445     eventFuncs[event](self);
446 end