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