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