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