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