09b59f4 - Only have OnUpdate calls when necessary
[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 -- TODO maybe add a prefix when in vehicle
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 space = width - frame.health:GetWidth();
193         shield = (shield / frame.health.max) * width;
194         if space == 0 then
195             frame.shield:Hide();
196             frame.shieldhl:Show();
197         elseif space < shield then
198             frame.shield:SetWidth(space);
199             frame.shield:Show();
200             frame.shieldhl:Show();
201         else
202             frame.shield:SetWidth(shield);
203             frame.shield:Show();
204             frame.shieldhl:Hide();
205         end
206     else
207         frame.shield:Hide();
208         frame.shieldhl:Hide();
209     end
210 end
211
212 local function updateHealAbsorb(frame, unit)
213     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
214     if absorb > 0 then
215         local space = frame.health:GetWidth();
216         absorb = (absorb / frame.health.max) * width;
217         frame.healabsorb:SetWidth(min(space, absorb));
218         frame.healabsorb:Show();
219     else
220         frame.healabsorb:Hide();
221     end
222 end
223
224 local function updateAuras(frame, unit)
225     local alert = checkIndicators(frame, unit);
226     if alert then
227         if frame.overlay.color ~= overlayColorAlert then
228             frame.overlay:SetVertexColor(unpack(overlayColorAlert));
229             frame.overlay.color = overlayColorAlert;
230             frame.overlay:Show();
231         end
232     elseif UnitDebuff(unit, 1, "RAID") ~= nil then
233         -- something dispellable
234         if frame.overlay.color ~= overlayColorDispel then
235             frame.overlay:SetVertexColor(unpack(overlayColorDispel));
236             frame.overlay.color = overlayColorDispel;
237             frame.overlay:Show();
238         end
239     -- don't overlay charmed when in vehicle
240     elseif UnitIsCharmed(unit) and unit == frame.unit then
241         if frame.overlay.color ~= overlayColorCharm then
242             frame.overlay:SetVertexColor(unpack(overlayColorCharm));
243             frame.overlay.color = overlayColorCharm;
244             frame.overlay:Show();
245         end
246     else
247         if frame.overlay.color ~= nil then
248             frame.overlay.color = nil;
249             frame.overlay:Hide();
250         end
251     end
252 end
253
254 local function updateAggro(frame, unit)
255     local status = UnitThreatSituation(unit);
256     if status and status > 0 then
257         frame.base:SetVertexColor(GetThreatStatusColor(status));
258     else
259         frame.base:SetVertexColor(unpack(baseColor));
260     end
261 end
262
263 local function updateVehicle(frame)
264     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
265         UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
266     if shouldTargetVehicle then
267         if not frame.inVehicle then
268             frame.inVehicle = true;
269             frame.displayed = frame.vehicle;
270             registerUnitEvents(frame);
271         end
272     elseif frame.inVehicle then
273         frame.inVehicle = false;
274         frame.displayed = frame.unit;
275         registerUnitEvents(frame);
276     end
277 end
278
279 local function updateRole(frame, unit)
280     local role = UnitGroupRolesAssigned(unit);
281     if role == "HEALER" then
282         frame.role:SetTexCoord(0.75, 1, 0, 1);
283         frame.role:Show();
284         registerPower(frame);
285         frame.role.healer = true;
286     elseif role == "TANK" then
287         frame.role:SetTexCoord(0.5, 0.75, 0, 1);
288         frame.role:Show();
289         if frame.role.healer then
290             frame.role.healer = false;
291             unregisterPower(frame);
292         end
293     else
294         frame.role:Hide();
295         if frame.role.healer then
296             frame.role.healer = false;
297             unregisterPower(frame);
298         end
299     end
300 end
301
302 local function updateReadyCheck(frame, unit)
303     local status = GetReadyCheckStatus(unit);
304     if status == "ready" then
305         frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
306         frame.ready:Show()
307     elseif status == "notready" then
308         frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
309         frame.ready:Show()
310     elseif status == "waiting" then
311         frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
312         frame.ready:Show()
313     else
314         frame.ready:Hide()
315     end
316 end
317
318 local function updateRaidMarker(frame, unit)
319     local index = GetRaidTargetIndex(unit);
320     if index then
321         SetRaidTargetIconTexture(frame.targeticon, index);
322         frame.targeticon:Show();
323     else
324         frame.targeticon:Hide();
325     end
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, if needed
336         -- because marker is removed when unit dies
337         -- without a RAID_TARGET_UPDATE event
338         --updateRaidMarker(frame, frame.unit);
339     end,
340     ["UNIT_POWER"] = function(frame)
341         updatePower(frame, frame.displayed);
342     end,
343     ["UNIT_AURA"] = function(frame)
344         updateAuras(frame, frame.displayed);
345     end,
346     ["UNIT_HEAL_PREDICTION"] = function(frame)
347         updateHealPred(frame, frame.displayed);
348     end,
349     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
350         updateShield(frame, frame.displayed);
351     end,
352     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
353         updateHealAbsorb(frame, frame.displayed);
354     end,
355     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
356         updateAggro(frame, frame.displayed);
357     end,
358     ["UNIT_MAXHEALTH"] = function(frame)
359         updateMaxHealth(frame, frame.displayed);
360         updateHealth(frame, frame.displayed);
361         updateText(frame, frame.displayed);
362         updateShield(frame, frame.displayed);
363         updateHealAbsorb(frame, frame.displayed);
364     end,
365     ["UNIT_MAXPOWER"] = function(frame)
366         updateMaxPower(frame, frame.displayed);
367         updatePower(frame, frame.displayed);
368     end,
369     ["UNIT_DISPLAYPOWER"] = function(frame)
370         updatePowerColor(frame, frame.displayed);
371         updateMaxPower(frame, frame.displayed);
372         updatePower(frame, frame.displayed);
373     end,
374     ["UNIT_NAME_UPDATE"] = function(frame)
375         updateName(frame, frame.unit);
376     end,
377     ["UNIT_CONNECTION"] = function(frame)
378         updateText(frame, frame.displayed);
379     end,
380     ["PARTY_MEMBER_ENABLE"] = function(frame)
381         -- new power info possibly (FrameXML/CompactUnitFrame.lua)
382         updateMaxPower(frame, frame.displayed);
383         updatePowerColor(frame, frame.displayed);
384     end,
385     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
386         updateRole(frame, frame.unit);
387     end,
388     ["READY_CHECK"] = function(frame)
389         updateReadyCheck(frame, frame.unit);
390     end,
391     ["RAID_TARGET_UPDATE"] = function(frame)
392         updateRaidMarker(frame, frame.displayed);
393     end,
394     ["UPDATE_ALL_BARS"] = function(frame)
395         updateRole(frame, frame.unit);
396         updateVehicle(frame);
397         updateMaxHealth(frame, frame.displayed);
398         updateHealth(frame, frame.displayed);
399         updateText(frame, frame.displayed);
400         if frame.role.healer then
401             updateMaxPower(frame, frame.displayed);
402             updatePower(frame, frame.displayed);
403             updatePowerColor(frame, frame.displayed);
404         end
405         updateAuras(frame, frame.displayed);
406         updateShield(frame, frame.displayed);
407         updateHealPred(frame, frame.displayed);
408         updateHealAbsorb(frame, frame.displayed);
409         updateAggro(frame, frame.displayed);
410         updateName(frame, frame.unit);
411         updateReadyCheck(frame, frame.unit);
412         updateRaidMarker(frame, frame.displayed);
413     end,
414 };
415 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
416 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
417 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
418 eventFuncs["UNIT_POWER_BAR_SHOW"] = eventFuncs["UNIT_DISPLAYPOWER"];
419 eventFuncs["UNIT_POWER_BAR_HIDE"] = eventFuncs["UNIT_DISPLAYPOWER"];
420 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
421 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
422 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
423 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
424 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
425 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
426 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
427 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
428
429 function M.UnitEvent(self, event)
430     eventFuncs[event](self);
431 end