4a66dd6 - Remove incoming res icon, slower periodic update rate
[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 UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
15 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
16 local GetReadyCheckTimeLeft, GetReadyCheckStatus = GetReadyCheckTimeLeft, GetReadyCheckStatus;
17 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
18 local GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture;
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     frame:RegisterEvent("RAID_TARGET_UPDATE");
43     if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
44 end
45
46 local function unregisterPower(frame)
47     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
48     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT");
49     frame.mana:Hide();
50     frame:UnregisterEvent("PARTY_MEMBER_ENABLE");
51     frame:UnregisterEvent("PARTY_MEMBER_DISABLE");
52     frame:UnregisterEvent("UNIT_POWER");
53     frame:UnregisterEvent("UNIT_MAXPOWER");
54     frame:UnregisterEvent("UNIT_DISPLAYPOWER");
55     frame:UnregisterEvent("UNIT_POWER_BAR_SHOW");
56     frame:UnregisterEvent("UNIT_POWER_BAR_HIDE");
57 end
58
59 local function registerPower(frame)
60     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
61     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
62     frame.mana:Show();
63     frame:RegisterEvent("PARTY_MEMBER_ENABLE");
64     frame:RegisterEvent("PARTY_MEMBER_DISABLE");
65     frame:RegisterUnitEvent("UNIT_POWER", frame.unit);
66     frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit);
67     frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit);
68     frame:RegisterUnitEvent("UNIT_POWER_BAR_SHOW", frame.unit);
69     frame:RegisterUnitEvent("UNIT_POWER_BAR_HIDE", frame.unit);
70 end
71
72 function M.RegisterUnitEvents(frame)
73     -- events are taken from FrameXML/CompactUnitFrame.lua
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("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
86     frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
87     frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
88     frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
89     frame:RegisterUnitEvent("UNIT_PET", 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 updateMaxPower(frame, unit)
144     frame.mana.max = UnitPowerMax(unit);
145 end
146
147 local function updatePower(frame, unit)
148     local current, max = UnitPower(unit), frame.mana.max;
149     if current <= 0 then
150         frame.mana:Hide();
151     elseif current > max or max <= 0 then
152         frame.mana:SetWidth(width);
153         updateMaxPower(frame, unit);
154         frame.mana:Show();
155     else
156         frame.mana:SetWidth(current/max*width);
157         frame.mana:Show();
158     end
159 end
160
161 local function updatePowerColor(frame, unit)
162     frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
163 end
164
165 local function updateName(frame, unit)
166     local name = UnitName(unit);
167     if not name then return end
168     frame.name:SetText(ssub(name, 1, 6));
169
170     local _, class = UnitClass(unit);
171     local color = RAID_CLASS_COLORS[class];
172     if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
173 end
174
175 local function updateHealPred(frame, unit)
176     local incoming = UnitGetIncomingHeals(unit) or 0;
177     if incoming > 0 then
178         -- always at least 1 pixel space for heal prediction
179         local space = width - frame.health:GetWidth() + 1;
180         incoming = (incoming / frame.health.max) * width;
181         frame.healpred:SetWidth(min(space, incoming));
182         frame.healpred:Show();
183     else
184         frame.healpred:Hide();
185     end
186 end
187
188 local function updateShield(frame, unit)
189     local shield = UnitGetTotalAbsorbs(unit) or 0;
190     if shield > 0 then
191         local space = width - frame.health:GetWidth();
192         shield = (shield / frame.health.max) * width;
193         if space == 0 then
194             frame.shield:Hide();
195             frame.shieldhl:Show();
196         elseif space < shield then
197             frame.shield:SetWidth(space);
198             frame.shield:Show();
199             frame.shieldhl:Show();
200         else
201             frame.shield:SetWidth(shield);
202             frame.shield:Show();
203             frame.shieldhl:Hide();
204         end
205     else
206         frame.shield:Hide();
207         frame.shieldhl:Hide();
208     end
209 end
210
211 local function updateHealAbsorb(frame, unit)
212     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
213     if absorb > 0 then
214         local space = frame.health:GetWidth();
215         absorb = (absorb / frame.health.max) * width;
216         frame.healabsorb:SetWidth(min(space, absorb));
217         frame.healabsorb:Show();
218     else
219         frame.healabsorb:Hide();
220     end
221 end
222
223 local function updateAuras(frame, unit)
224     local alert = checkIndicators(frame, unit);
225     if alert then
226         if frame.overlay.color ~= overlayColorAlert then
227             frame.overlay:SetVertexColor(unpack(overlayColorAlert));
228             frame.overlay.color = overlayColorAlert;
229             frame.overlay:Show();
230         end
231     elseif UnitDebuff(unit, 1, "RAID") ~= nil then
232         -- something dispellable
233         if frame.overlay.color ~= overlayColorDispel then
234             frame.overlay:SetVertexColor(unpack(overlayColorDispel));
235             frame.overlay.color = overlayColorDispel;
236             frame.overlay:Show();
237         end
238     -- don't overlay charmed when in vehicle
239     elseif UnitIsCharmed(unit) and unit == frame.unit then
240         if frame.overlay.color ~= overlayColorCharm then
241             frame.overlay:SetVertexColor(unpack(overlayColorCharm));
242             frame.overlay.color = overlayColorCharm;
243             frame.overlay:Show();
244         end
245     else
246         if frame.overlay.color ~= nil then
247             frame.overlay.color = nil;
248             frame.overlay:Hide();
249         end
250     end
251 end
252
253 local function updateAggro(frame, unit)
254     local status = UnitThreatSituation(unit);
255     if status and status > 0 then
256         frame.base:SetVertexColor(GetThreatStatusColor(status));
257     else
258         frame.base:SetVertexColor(unpack(baseColor));
259     end
260 end
261
262 local function updateVehicle(frame)
263     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
264         UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
265     if shouldTargetVehicle then
266         if not frame.inVehicle then
267             frame.inVehicle = true;
268             frame.displayed = frame.vehicle;
269             registerUnitEvents(frame);
270         end
271     elseif frame.inVehicle then
272         frame.inVehicle = false;
273         frame.displayed = frame.unit;
274         registerUnitEvents(frame);
275     end
276 end
277
278 local function updateRole(frame, unit)
279     local role = UnitGroupRolesAssigned(unit);
280     if role == "HEALER" then
281         frame.role:SetTexCoord(0.75, 1, 0, 1);
282         frame.role:Show();
283         registerPower(frame);
284         frame.role.healer = true;
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             frame.role.healer = false;
290             unregisterPower(frame);
291         end
292     else
293         frame.role:Hide();
294         if frame.role.healer then
295             frame.role.healer = false;
296             unregisterPower(frame);
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     local index = GetRaidTargetIndex(unit);
319     if index then
320         SetRaidTargetIconTexture(frame.targeticon, index);
321         frame.targeticon:Show();
322     else
323         frame.targeticon:Hide();
324     end
325 end
326
327 local eventFuncs = {
328     ["UNIT_HEALTH"] = function(frame)
329         updateHealth(frame, frame.displayed);
330         updateText(frame, frame.displayed);
331         updateShield(frame, frame.displayed);
332         updateHealAbsorb(frame, frame.displayed);
333         -- no heal prediction update, that doesn't overflow too much
334         -- raid marker update here, because marker is removed when unit dies
335         -- without a RAID_TARGET_UPDATE event
336         updateRaidMarker(frame, frame.unit);
337     end,
338     ["UNIT_POWER"] = function(frame)
339         updatePower(frame, frame.displayed);
340     end,
341     ["UNIT_AURA"] = function(frame)
342         updateAuras(frame, frame.displayed);
343     end,
344     ["UNIT_HEAL_PREDICTION"] = function(frame)
345         updateHealPred(frame, frame.displayed);
346     end,
347     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
348         updateShield(frame, frame.displayed);
349     end,
350     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
351         updateHealAbsorb(frame, frame.displayed);
352     end,
353     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
354         updateAggro(frame, frame.displayed);
355     end,
356     ["UNIT_MAXHEALTH"] = function(frame)
357         updateMaxHealth(frame, frame.displayed);
358         updateHealth(frame, frame.displayed);
359         updateText(frame, frame.displayed);
360         updateShield(frame, frame.displayed);
361         updateHealAbsorb(frame, frame.displayed);
362     end,
363     ["UNIT_MAXPOWER"] = function(frame)
364         updateMaxPower(frame, frame.displayed);
365         updatePower(frame, frame.displayed);
366     end,
367     ["UNIT_DISPLAYPOWER"] = function(frame)
368         updatePowerColor(frame, frame.displayed);
369         updateMaxPower(frame, frame.displayed);
370         updatePower(frame, frame.displayed);
371     end,
372     ["UNIT_NAME_UPDATE"] = function(frame)
373         updateName(frame, frame.displayed);
374     end,
375     ["UNIT_CONNECTION"] = function(frame)
376         updateText(frame, frame.displayed);
377     end,
378     ["PARTY_MEMBER_ENABLE"] = function(frame)
379         -- new power info possibly (FrameXML/CompactUnitFrame.lua)
380         updateMaxPower(frame, frame.displayed);
381         updatePowerColor(frame, frame.displayed);
382     end,
383     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
384         updateRole(frame, frame.unit);
385     end,
386     ["READY_CHECK"] = function(frame)
387         updateReadyCheck(frame, frame.unit);
388     end,
389     ["RAID_TARGET_UPDATE"] = function(frame)
390         updateRaidMarker(frame, frame.displayed);
391     end,
392     ["UPDATE_ALL_BARS"] = function(frame)
393         updateRole(frame, frame.unit);
394         updateVehicle(frame);
395         updateMaxHealth(frame, frame.displayed);
396         updateHealth(frame, frame.displayed);
397         updateText(frame, frame.displayed);
398         if frame.role.healer then
399             updateMaxPower(frame, frame.displayed);
400             updatePower(frame, frame.displayed);
401             updatePowerColor(frame, frame.displayed);
402         end
403         updateAuras(frame, frame.displayed);
404         updateShield(frame, frame.displayed);
405         updateHealPred(frame, frame.displayed);
406         updateHealAbsorb(frame, frame.displayed);
407         updateAggro(frame, frame.displayed);
408         updateName(frame, frame.displayed);
409         updateReadyCheck(frame, frame.unit);
410         updateRaidMarker(frame, frame.displayed);
411     end,
412 };
413 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
414 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
415 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
416 eventFuncs["UNIT_POWER_BAR_SHOW"] = eventFuncs["UNIT_DISPLAYPOWER"];
417 eventFuncs["UNIT_POWER_BAR_HIDE"] = eventFuncs["UNIT_DISPLAYPOWER"];
418 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
419 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
420 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
421 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
422 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
423 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
424 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
425 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
426
427 function M.UnitEvent(self, event)
428     eventFuncs[event](self);
429 end