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