fa6bbd0 - Add Cloying Shadows debuff
[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 UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
9 local UnitIsAFK, UnitIsDND = UnitIsAFK, UnitIsDND;
10 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
11 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
12 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
13 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
14 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
15 local GetReadyCheckTimeLeft, GetReadyCheckStatus = GetReadyCheckTimeLeft, GetReadyCheckStatus;
16 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
17 local GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture;
18 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
19 local READY_CHECK_READY_TEXTURE = READY_CHECK_READY_TEXTURE;
20 local READY_CHECK_NOT_READY_TEXTURE = READY_CHECK_NOT_READY_TEXTURE;
21 local READY_CHECK_WAITING_TEXTURE = READY_CHECK_WAITING_TEXTURE;
22
23 local checkIndicators = OmaRFIndicators.CheckIndicators;
24
25 local Settings = OmaRFSettings;
26 local baseColor = Settings.BaseColor;
27 local overlayColorDispel = Settings.OverlayColorDispel;
28 local overlayColorCharm = Settings.OverlayColorCharm;
29 local overlayColorAlert = Settings.OverlayColorAlert;
30 local width = Settings.Width;
31
32 local M = {};
33 OmaRFEvents = M;
34 function M.RegisterEvents(frame)
35     frame:RegisterEvent("PLAYER_ENTERING_WORLD");
36     frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
37     frame:RegisterEvent("READY_CHECK");
38     frame:RegisterEvent("READY_CHECK_FINISHED");
39     frame:RegisterEvent("GROUP_ROSTER_UPDATE");
40     frame:RegisterEvent("RAID_TARGET_UPDATE");
41     if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
42 end
43
44 function M.RegisterUnitEvents(frame)
45     -- events are taken from FrameXML/CompactUnitFrame.lua
46     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
47     frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
48     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
49     frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
50     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
51     frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
52     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
53     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
54     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
55     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
56     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
57     frame:RegisterUnitEvent("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
58     frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
59     frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
60     frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
61     frame:RegisterUnitEvent("UNIT_PET", frame.unit, displayed);
62 end
63 local registerUnitEvents = M.RegisterUnitEvents;
64
65 local function updateText(frame, unit)
66     if UnitIsDeadOrGhost(unit) then
67         frame.dead = true;
68         frame.text:SetText("Dead");
69         frame.text:Show();
70     elseif not UnitIsConnected(unit) then
71         frame.text:SetText("DC");
72         frame.text:Show();
73     elseif UnitIsAFK(unit) then
74         frame.text:SetText("afk");
75         frame.text:Show();
76     elseif UnitIsDND(unit) then
77         frame.text:SetText("dnd");
78         frame.text:Show();
79     else
80         frame.text:Hide();
81     end
82 end
83 M.UpdateText = updateText;
84
85 local function updateMaxHealth(frame, unit)
86     frame.health.max = UnitHealthMax(unit);
87 end
88 M.UpdateMaxHealth = updateMaxHealth;
89
90 local function updateHealth(frame, unit)
91     local current, max = UnitHealth(unit), frame.health.max;
92     if current > max or max <= 0 then
93         -- somehow current health has gone over the maximum (missed maxhealth event possibly)
94         -- just put health bar full and update max health for next event
95         frame.health:SetWidth(width);
96         frame.health.width = width;
97         updateMaxHealth(frame, unit);
98         frame.health:Show();
99     elseif current <= 0 or UnitIsDeadOrGhost(unit) then
100         frame.health:Hide();
101         return updateText(frame, unit); -- update death
102     else
103         local w = current/max*width;
104         frame.health:SetWidth(w);
105         frame.health.width = w;
106         frame.health:Show();
107     end
108
109     if frame.dead and current > 0 then
110         frame.dead = nil;
111         updateText(frame, unit); -- update revive
112     end
113 end
114 M.UpdateHealth = updateHealth;
115
116 local function updateName(frame, unit)
117     local name = UnitName(unit);
118     if not name then return end
119     name = ssub(name, 1, 6);
120     if frame.unit == unit then
121         frame.name:SetText(name);
122     else
123         frame.name:SetFormattedText("-%s", name);
124     end
125
126     local _, class = UnitClass(unit);
127     local color = RAID_CLASS_COLORS[class];
128     if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
129 end
130 M.UpdateName = updateName;
131
132 local function updateHealPred(frame, unit)
133     local incoming = UnitGetIncomingHeals(unit) or 0;
134     if incoming > 0 then
135         incoming = (incoming / frame.health.max) * width;
136         -- always at least 1 pixel space for heal prediction
137         frame.healpred:SetWidth(min(width - frame.health.width + 1, incoming));
138         frame.healpred:Show();
139     else
140         frame.healpred:Hide();
141     end
142 end
143 M.UpdateHealPred = updateHealPred;
144
145 local function updateShield(frame, unit)
146     local shield = UnitGetTotalAbsorbs(unit) or 0;
147     if shield > 0 then
148         local space = width - frame.health.width;
149         shield = (shield / frame.health.max) * width;
150         if space == 0 then
151             frame.shield:Hide();
152             frame.shieldhl:Show();
153         elseif space < shield then
154             frame.shield:SetWidth(space);
155             frame.shield:Show();
156             frame.shieldhl:Show();
157         else
158             frame.shield:SetWidth(shield);
159             frame.shield:Show();
160             frame.shieldhl:Hide();
161         end
162     else
163         frame.shield:Hide();
164         frame.shieldhl:Hide();
165     end
166 end
167 M.UpdateShield = updateShield;
168
169 local function updateHealAbsorb(frame, unit)
170     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
171     if absorb > 0 then
172         absorb = (absorb / frame.health.max) * width;
173         frame.healabsorb:SetWidth(min(frame.health.width, absorb));
174         frame.healabsorb:Show();
175     else
176         frame.healabsorb:Hide();
177     end
178 end
179 M.UpdateHealAbsorb = updateHealAbsorb;
180
181 local function updateAuras(frame, unit)
182     local alert = checkIndicators(frame, unit);
183     if alert then
184         if frame.overlay.color ~= overlayColorAlert then
185             frame.overlay:SetVertexColor(unpack(overlayColorAlert));
186             frame.overlay.color = overlayColorAlert;
187             frame.overlay:Show();
188         end
189     elseif UnitDebuff(unit, 1, "RAID") ~= nil then
190         -- something dispellable
191         if frame.overlay.color ~= overlayColorDispel then
192             frame.overlay:SetVertexColor(unpack(overlayColorDispel));
193             frame.overlay.color = overlayColorDispel;
194             frame.overlay:Show();
195         end
196     -- don't overlay charmed when in vehicle
197     elseif UnitIsCharmed(unit) and unit == frame.unit then
198         if frame.overlay.color ~= overlayColorCharm then
199             frame.overlay:SetVertexColor(unpack(overlayColorCharm));
200             frame.overlay.color = overlayColorCharm;
201             frame.overlay:Show();
202         end
203     else
204         if frame.overlay.color ~= nil then
205             frame.overlay.color = nil;
206             frame.overlay:Hide();
207         end
208     end
209 end
210 M.UpdateAuras = updateAuras;
211
212 local function updateAggro(frame, unit)
213     local status = UnitThreatSituation(unit);
214     if status and status > 0 then
215         frame.base:SetVertexColor(GetThreatStatusColor(status));
216     else
217         frame.base:SetVertexColor(unpack(baseColor));
218     end
219 end
220 M.UpdateAggro = updateAggro;
221
222 local function updateVehicle(frame)
223     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
224         UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
225     if shouldTargetVehicle then
226         if not frame.inVehicle then
227             frame.inVehicle = true;
228             frame.displayed = frame.vehicle;
229             registerUnitEvents(frame);
230         end
231     elseif frame.inVehicle then
232         frame.inVehicle = false;
233         frame.displayed = frame.unit;
234         registerUnitEvents(frame);
235     end
236 end
237 M.UpdateVehicle = updateVehicle;
238
239 local function updateRole(frame, unit)
240     local role = UnitGroupRolesAssigned(unit);
241     if role == "HEALER" then
242         frame.role:SetTexCoord(0.75, 1, 0, 1);
243         frame.role:Show();
244     elseif role == "TANK" then
245         frame.role:SetTexCoord(0.5, 0.75, 0, 1);
246         frame.role:Show();
247     else
248         frame.role:Hide();
249     end
250 end
251 M.UpdateRole = updateRole;
252
253 local function updateReadyCheck(frame, unit)
254     local status = GetReadyCheckStatus(unit);
255     if status == "ready" then
256         frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
257         frame.ready:Show()
258     elseif status == "notready" then
259         frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
260         frame.ready:Show()
261     elseif status == "waiting" then
262         frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
263         frame.ready:Show()
264     else
265         frame.ready:Hide()
266     end
267 end
268 M.UpdateReadyCheck = updateReadyCheck;
269
270 local function updateRaidMarker(frame, unit)
271     local index = GetRaidTargetIndex(unit);
272     if index then
273         SetRaidTargetIconTexture(frame.targeticon, index);
274         frame.targeticon:Show();
275     else
276         frame.targeticon:Hide();
277     end
278 end
279 M.UpdateRaidMarker = updateRaidMarker;
280
281 local eventFuncs = {
282     ["UNIT_HEALTH"] = function(frame)
283         updateHealth(frame, frame.displayed);
284         updateShield(frame, frame.displayed);
285         updateHealAbsorb(frame, frame.displayed);
286         -- no heal prediction update, that doesn't overflow too much
287     end,
288     ["UNIT_AURA"] = function(frame)
289         updateAuras(frame, frame.displayed);
290     end,
291     ["UNIT_HEAL_PREDICTION"] = function(frame)
292         updateHealPred(frame, frame.displayed);
293     end,
294     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
295         updateShield(frame, frame.displayed);
296     end,
297     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
298         updateHealAbsorb(frame, frame.displayed);
299     end,
300     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
301         updateAggro(frame, frame.displayed);
302     end,
303     ["UNIT_MAXHEALTH"] = function(frame)
304         updateMaxHealth(frame, frame.displayed);
305         updateHealth(frame, frame.displayed);
306         updateShield(frame, frame.displayed);
307         updateHealAbsorb(frame, frame.displayed);
308     end,
309     ["UNIT_NAME_UPDATE"] = function(frame)
310         updateName(frame, frame.unit);
311     end,
312     ["UNIT_CONNECTION"] = function(frame)
313         updateText(frame, frame.displayed);
314     end,
315     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
316         updateRole(frame, frame.unit);
317     end,
318     ["READY_CHECK"] = function(frame)
319         updateReadyCheck(frame, frame.unit);
320     end,
321     ["RAID_TARGET_UPDATE"] = function(frame)
322         updateRaidMarker(frame, frame.displayed);
323     end,
324     ["UPDATE_ALL_BARS"] = function(frame)
325         updateRole(frame, frame.unit);
326         updateVehicle(frame);
327         updateMaxHealth(frame, frame.displayed);
328         updateHealth(frame, frame.displayed);
329         updateText(frame, frame.displayed);
330         updateAuras(frame, frame.displayed);
331         updateShield(frame, frame.displayed);
332         updateHealPred(frame, frame.displayed);
333         updateHealAbsorb(frame, frame.displayed);
334         updateAggro(frame, frame.displayed);
335         updateName(frame, frame.unit);
336         updateReadyCheck(frame, frame.unit);
337         updateRaidMarker(frame, frame.displayed);
338     end,
339 };
340 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
341 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
342 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
343 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
344 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
345 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
346 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
347 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
348 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
349 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
350
351 function M.UnitEvent(self, event)
352     return eventFuncs[event](self);
353 end