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