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