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