121eb4e - Hide Blizzard raid frames
[wowui.git] / OmaUF / Events.lua
1 -- Events.lua
2 local _;
3 local unpack = unpack;
4 local ssub = string.sub;
5 local min = math.min;
6 local ceil = math.ceil;
7 local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists;
8 local UnitDebuff, UnitIsCharmed = UnitDebuff, UnitIsCharmed;
9 local UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType;
10 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
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 UnitGroupRolesAssigned = UnitGroupRolesAssigned;
17 local UnitLevel, UnitClassification = UnitLevel, UnitClassification;
18 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
19
20 local Settings = OmaUFSettings;
21 local baseColor = Settings.BaseColor;
22 local overlayColorDispel = Settings.OverlayColorDispel;
23 local overlayColorCharm = Settings.OverlayColorCharm;
24 local overlayColorAlert = Settings.OverlayColorAlert;
25 local powerColors = Settings.PowerColors;
26 local width = 10;
27
28 local M = {};
29 OmaUFEvents = M;
30 function M.RegisterEvents(frame)
31     -- events are taken from FrameXML/CompactUnitFrame.lua
32     -- TODO raid marker support,
33     -- player flags support (/afk, /dnd)
34     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
35     frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
36     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
37     frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
38     frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
39     frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
40     frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
41     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
42     frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
43     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
44     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
45     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
46     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
47     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
48     frame:RegisterUnitEvent("UNIT_FACTION", frame.unit, displayed);
49 end
50 local registerEvents = M.RegisterEvents;
51
52 local function updateHealth(frame, unit)
53     local current, max = UnitHealth(unit), frame.health.max;
54     frame.health:Show();
55     -- sanity check, occasionally UnitHealthMax gives zero
56     if current > max then
57         -- somehow current health has gone over the maximum (missed maxhealth event)
58         frame.health.max = UnitHealthMax(unit);
59         max = frame.health.max;
60         if current > max then
61             -- error state, still over maximum
62             frame.health:SetWidth(width);
63             return;
64         end
65     elseif max > 0 then
66         frame.health:SetWidth(current/frame.health.max*width);
67     else
68         frame.health:SetWidth(width);
69         return;
70     end
71
72     if UnitIsDeadOrGhost(unit) then
73         frame.health:Hide();
74     end
75 end
76
77 local function updateHealthText(frame, unit)
78     local current, max = UnitHealth(unit), frame.health.max;
79     if UnitIsDeadOrGhost(unit) then
80         frame.healthText:SetText("Dead");
81         frame.healthText:Show();
82     elseif not UnitIsConnected(unit) then
83         frame.healthText:SetText("DC");
84         frame.healthText:Show();
85     elseif max > 0 and current < max then
86         frame.healthText:SetText(ceil(current/max*100));
87         frame.healthText:Show();
88     else
89         frame.healthText:Hide();
90     end
91 end
92
93 local function updateMaxHealth(frame, unit)
94     frame.health.max = UnitHealthMax(unit);
95 end
96
97 local function updatePower(frame, unit)
98     local current, max = UnitPower(unit), frame.mana.max;
99     -- sanity check, occasionally UnitPowerMax gives zero
100     if current == 0 then
101         frame.mana:Hide();
102         return;
103     elseif current > max then
104         frame.mana:Show();
105         frame.mana.max = UnitPowerMax(unit);
106         max = frame.mana.max;
107         if current > max then
108             -- error
109             frame.mana:SetWidth(width);
110             return;
111         end
112     end
113     if max > 0 then
114         frame.mana:SetWidth(UnitPower(unit)/max*width);
115         frame.mana:Show();
116     else
117         frame.mana:SetWidth(width);
118         frame.mana:Show();
119     end
120 end
121
122 local function updatePowerText(frame, unit)
123     local current, max = UnitPower(unit), frame.mana.max;
124     if UnitIsDeadOrGhost(unit) or not UnitIsConnected(unit) then
125         frame.healthText:Hide();
126     elseif max > 0 and current > 0 and current < max then
127         frame.manaText:SetText(ceil(current/max*100));
128         frame.manaText:Show();
129     else
130         frame.manaText:Hide();
131     end
132 end
133
134 local function updateMaxPower(frame, unit)
135     frame.mana.max = UnitPowerMax(unit);
136 end
137
138 local function updatePowerColor(frame, unit)
139     frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
140 end
141
142 local function updateName(frame, unit)
143     local name = UnitName(unit);
144     if not name then return end
145     frame.name:SetText(ssub(name, 1, 10));
146
147     local _, class = UnitClass(unit);
148     local color = RAID_CLASS_COLORS[class];
149     if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
150 end
151
152 local function updateHealPred(frame, unit)
153     local incoming = UnitGetIncomingHeals(unit) or 0;
154     if incoming > 0 then
155         local max = frame.health.max;
156         local space = width - frame.health:GetWidth() + 1;
157         local pred = (incoming / max) * width;
158         frame.healpred:SetWidth(min(space, pred));
159         frame.healpred:Show();
160     else
161         frame.healpred:Hide();
162     end
163 end
164
165 local function updateShield(frame, unit)
166     local shield = UnitGetTotalAbsorbs(unit) or 0;
167     if shield > 0 then
168         local max = frame.health.max;
169         local space = width - frame.health:GetWidth();
170         shield = (shield / max) * width;
171         if space == 0 then
172             frame.shield:Hide();
173             frame.shieldhl:Show();
174         elseif space < shield then
175             frame.shield:SetWidth(space);
176             frame.shield:Show();
177             frame.shieldhl:Show();
178         else
179             frame.shield:SetWidth(shield);
180             frame.shield:Show();
181             frame.shieldhl:Hide();
182         end
183     else
184         frame.shield:Hide();
185         frame.shieldhl:Hide();
186     end
187 end
188
189 local function updateHealAbsorb(frame, unit)
190     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
191     if absorb > 0 then
192         local max = frame.health.max;
193         local space = frame.health:GetWidth();
194         absorb = (absorb / max) * width;
195         frame.healabsorb:SetWidth(min(space, absorb));
196         frame.healabsorb:Show();
197     else
198         frame.healabsorb:Hide();
199     end
200 end
201
202 local function updateAuras(frame, unit)
203     if UnitDebuff(unit, 1, "RAID") ~= nil then
204         -- something dispellable
205         if frame.overlay.color ~= overlayColorDispel then
206             frame.overlay:SetVertexColor(unpack(overlayColorDispel));
207             frame.overlay:Show();
208             frame.overlay.color = overlayColorDispel;
209         end
210     -- don't overlay charmed when in vehicle
211     elseif UnitIsCharmed(unit) and unit == frame.unit then
212         if frame.overlay.color ~= overlayColorCharm then
213             frame.overlay:SetVertexColor(unpack(overlayColorCharm));
214             frame.overlay:Show();
215             frame.overlay.color = overlayColorCharm;
216         end
217     else
218         if frame.overlay.color ~= nil then
219             frame.overlay:Hide();
220             frame.overlay.color = nil;
221         end
222     end
223 end
224
225 local function updateAggro(frame, unit)
226     local status = UnitThreatSituation(unit);
227     if status and status > 0 then
228         frame.base:SetVertexColor(GetThreatStatusColor(status));
229     else
230         frame.base:SetVertexColor(unpack(baseColor));
231     end
232 end
233
234 local function updateVehicle(frame)
235     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
236     if shouldTargetVehicle then
237         if not frame.inVehicle then
238             frame.inVehicle = true;
239             frame.displayed = frame.vehicle;
240             registerEvents(frame);
241         end
242     elseif frame.inVehicle then
243         frame.inVehicle = false;
244         frame.displayed = frame.unit;
245         registerEvents(frame);
246     end
247 end
248
249 local function updateRole(frame, unit)
250     local role = UnitGroupRolesAssigned(unit);
251     if role == "HEALER" or role == "TANK" or role == "DAMAGER" then
252         frame.role:SetTexCoord(GetTexCoordsForRole(role));
253         frame.role:Show();
254     else
255         frame.role:Hide();
256     end
257 end
258
259 local function updateLevelText(frame, unit, levelup)
260     if levelup then
261         -- PLAYER_LEVEL_UP
262         frame.level:SetText(levelup);
263     else
264         local level = UnitLevel(unit);
265         local class = UnitClassification(unit);
266         local leveltext, classtext;
267         if level < 0 then
268             if class == "worldboss" then
269                 leveltext = "Boss";
270             else
271                 leveltext = "??";
272             end
273         else
274             leveltext = level;
275         end
276         if class == "rareelite" then
277             classtext = " Rare Elite";
278         elseif class == "elite" then
279             classtext = " Elite";
280         elseif class == "rare" then
281             classtext = " Rare";
282         else
283             classtext = "";
284         end
285         frame.level:SetFormattedText("%s%s", leveltext, classtext);
286     end
287 end
288
289 local function updateStatus(frame, unit)
290     -- coords from PlayerFrame
291     if frame.inCombat or UnitAffectingCombat(unit) then
292         frame.status:SetTexCoord(0.5, 1, 0, 0.484375);
293         frame.status:Show();
294     elseif unit == "player" and IsResting() then
295         frame.status:SetTexCoord(0, 0.5, 0, 0.421875);
296         frame.status:Show();
297     else
298         frame.status:Hide();
299     end
300 end
301
302 local function updatePVP(frame, unit)
303     if UnitIsPVPFreeForAll(unit) then
304         frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-FFA");
305         frame.pvp:Show();
306     elseif UnitIsPVP(unit) then
307         local faction = UnitFactionGroup(unit);
308         if faction and faction ~= "Neutral" then
309             -- from PlayerFrame, mercenary checks
310             if UnitIsMercenary(unit) then
311                 if faction == "Horde" then
312                     faction = "Alliance";
313                 elseif faction == "Alliance" then
314                     faction = "Horde";
315                 end
316             end
317             frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-"..faction);
318             frame.pvp:Show();
319         else
320             frame.pvp:Hide();
321         end
322     else
323         frame.pvp:Hide();
324     end
325 end
326
327 local eventFuncs = {
328     ["UNIT_HEALTH"] = function(frame)
329         updateHealth(frame, frame.displayed);
330         updateHealthText(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     end,
335     ["UNIT_POWER"] = function(frame)
336         updatePower(frame, frame.displayed);
337         updatePowerText(frame, frame.displayed);
338     end,
339     ["UNIT_AURA"] = function(frame)
340         updateAuras(frame, frame.displayed);
341     end,
342     ["UNIT_HEAL_PREDICTION"] = function(frame)
343         updateHealPred(frame, frame.displayed);
344     end,
345     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
346         updateShield(frame, frame.displayed);
347     end,
348     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
349         updateHealAbsorb(frame, frame.displayed);
350     end,
351     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
352         updateAggro(frame, frame.displayed);
353     end,
354     ["UNIT_MAXHEALTH"] = function(frame)
355         updateMaxHealth(frame, frame.displayed);
356         updateHealth(frame, frame.displayed);
357         updateHealthText(frame, frame.displayed);
358         updateShield(frame, frame.displayed);
359         updateHealAbsorb(frame, frame.displayed);
360     end,
361     ["UNIT_MAXPOWER"] = function(frame)
362         updateMaxPower(frame, frame.displayed);
363         updatePower(frame, frame.displayed);
364         updatePowerText(frame, frame.displayed);
365     end,
366     ["UNIT_DISPLAYPOWER"] = function(frame)
367         updatePowerColor(frame, frame.displayed);
368     end,
369     ["UNIT_NAME_UPDATE"] = function(frame)
370         updateName(frame, frame.displayed);
371     end,
372     ["UNIT_CONNECTION"] = function(frame)
373         updateHealthText(frame, frame.displayed);
374         updatePowerText(frame, frame.displayed);
375     end,
376     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
377         updateRole(frame, frame.unit);
378     end,
379     ["UNIT_LEVEL"] = function(frame)
380         updateLevelText(frame, frame.unit);
381     end,
382     ["PLAYER_LEVEL_UP"] = function(frame, arg1)
383         updateLevelText(frame, frame.unit, arg1);
384     end,
385     ["PLAYER_UPDATE_RESTING"] = function(frame)
386         updateStatus(frame, frame.unit);
387     end,
388     ["PLAYER_REGEN_DISABLED"] = function(frame)
389         frame.inCombat = true;
390         updateStatus(frame, frame.unit);
391     end,
392     ["PLAYER_REGEN_ENABLED"] = function(frame)
393         frame.inCombat = false;
394         updateStatus(frame, frame.unit);
395     end,
396     ["UNIT_FACTION"] = function(frame)
397         updatePVP(frame, frame.unit);
398     end,
399     ["UPDATE_ALL_BARS"] = function(frame)
400         updateVehicle(frame);
401         updateMaxHealth(frame, frame.displayed);
402         updateMaxPower(frame, frame.displayed);
403         updateHealth(frame, frame.displayed);
404         updateHealthText(frame, frame.displayed);
405         updatePower(frame, frame.displayed);
406         updatePowerText(frame, frame.displayed);
407         updateAuras(frame, frame.displayed);
408         updateShield(frame, frame.displayed);
409         updateHealPred(frame, frame.displayed);
410         updateHealAbsorb(frame, frame.displayed);
411         updatePowerColor(frame, frame.displayed);
412         updateAggro(frame, frame.displayed);
413         updateName(frame, frame.displayed);
414         updateRole(frame, frame.unit);
415         updateLevelText(frame, frame.unit);
416         updateStatus(frame, frame.unit);
417         updatePVP(frame, frame.unit);
418     end,
419 };
420 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
421 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
422 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
423 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
424 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
425 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
426 eventFuncs["PLAYER_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
427
428 function M.UnitEvent(self, event, arg1)
429     eventFuncs[event](self, arg1);
430 end
431
432 function M.LoadChar()
433     width = Settings.Character.Width;
434 end