0051bb4 - add death strike tracking
[wowui.git] / kehys / events.lua
1 -- events.lua
2 -- 2019 Aleksi Blinnikka
3 local _;
4 local unpack = unpack;
5 local ssub = string.sub;
6 local min = math.min;
7 local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists;
8 local UnitDebuff, UnitIsCharmed = UnitDebuff, UnitIsCharmed;
9 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
10 local UnitIsAFK, UnitIsDND = UnitIsAFK, UnitIsDND;
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 GetReadyCheckTimeLeft, GetReadyCheckStatus = GetReadyCheckTimeLeft, GetReadyCheckStatus;
17 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
18 local GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture;
19 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
20 local READY_CHECK_READY_TEXTURE = READY_CHECK_READY_TEXTURE;
21 local READY_CHECK_NOT_READY_TEXTURE = READY_CHECK_NOT_READY_TEXTURE;
22 local READY_CHECK_WAITING_TEXTURE = READY_CHECK_WAITING_TEXTURE;
23
24 local _, addon = ...;
25 addon.Events = {};
26 local guids = addon.FrameGuids;
27 local baseColor = {0, 0, 0};
28 local overlayColorDispel = {1, 0.5, 0, 0.5};
29 local overlayColorCharm = {0.8, 0, 1, 0.5};
30 local overlayColorAlert = {1, 0, 0, 0.5};
31 local width = 80;
32
33 function addon.RegisterEvents(frame)
34     frame:RegisterEvent("PLAYER_ENTERING_WORLD");
35     frame:RegisterEvent("RAID_TARGET_UPDATE");
36     if frame.unit == "player" then frame:RegisterEvent("PLAYER_ALIVE") end
37     if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
38     if frame.unit == "target" then frame:RegisterEvent("PLAYER_TARGET_CHANGED") end
39     if frame.boss then frame:RegisterEvent("INSTANCE_ENCOUNTER_ENGAGE_UNIT") end
40     if frame.raid then
41         frame:RegisterEvent("PLAYER_REGEN_DISABLED");
42         frame:RegisterEvent("READY_CHECK");
43         frame:RegisterEvent("READY_CHECK_FINISHED");
44         frame:RegisterEvent("GROUP_ROSTER_UPDATE");
45     end
46 end
47
48 function addon.RegisterUnitEvents(frame)
49     -- events are taken from FrameXML/CompactUnitFrame.lua
50     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
51     frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
52     frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
53     frame:RegisterUnitEvent("UNIT_PET", frame.unit, displayed);
54     if frame.unit == "focus" or frame.unit == "target" or frame.boss then
55         frame:RegisterUnitEvent("UNIT_TARGETABLE_CHANGED", frame.unit, displayed);
56     end
57     if frame.raid or frame.unit ~= "player" then
58         frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
59         frame:RegisterUnitEvent("UNIT_FACTION", frame.unit, displayed);
60     end
61     if frame.raid then
62         frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
63         frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
64     end
65 end
66 local registerUnitEvents = addon.RegisterUnitEvents;
67
68 local function updateName(frame, unit)
69     local name = UnitName(unit);
70     if not name or not frame.name then return end
71     name = ssub(name, 1, 6);
72     if frame.unit == unit then
73         frame.name:SetText(name);
74     else
75         frame.name:SetFormattedText("-%s", name);
76     end
77
78     local _, class = UnitClass(unit);
79     local color = RAID_CLASS_COLORS[class];
80     if class == "DEATHKNIGHT" then
81         frame.isdk = true;
82     else
83         frame.isdk = nil;
84     end
85
86     if color then
87         if not frame.raid then
88             if not UnitPlayerControlled(unit) and UnitIsTapDenied(unit) then
89                 frame.health:SetVertexColor(0.5, 0.5, 0.5);
90             elseif UnitIsPlayer(unit) then
91                 frame.health:SetVertexColor(color.r, color.g, color.b);
92             elseif UnitPlayerControlled(unit) then
93                 frame.health:SetVertexColor(0, 1, 0);
94             else
95                 frame.health:SetVertexColor(UnitSelectionColor(unit));
96             end
97         else
98             frame.name:SetVertexColor(color.r, color.g, color.b);
99         end
100     end
101 end
102 addon.Events.UpdateName = updateName;
103
104 local function updateAggro(frame, unit)
105     local status = UnitThreatSituation(unit);
106     if status and status > 0 then
107         frame.base:SetVertexColor(GetThreatStatusColor(status));
108     else
109         frame.base:SetVertexColor(unpack(baseColor));
110     end
111 end
112 addon.Events.UpdateAggro = updateAggro;
113
114 local function updateVehicle(frame)
115     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
116         UnitTargetsVehicleInRaidUI(frame.unit) and frame.vehicle and UnitExists(frame.vehicle);
117     if shouldTargetVehicle then
118         if not frame.inVehicle then
119             frame.inVehicle = true;
120             frame.displayed = frame.vehicle;
121             registerUnitEvents(frame);
122         end
123     elseif frame.inVehicle then
124         frame.inVehicle = false;
125         frame.displayed = frame.unit;
126         registerUnitEvents(frame);
127     end
128 end
129 addon.Events.UpdateVehicle = updateVehicle;
130
131 local function updateRole(frame, unit)
132     frame.rolename = UnitGroupRolesAssigned(unit);
133     if frame.rolename == "HEALER" then
134         frame.role:SetTexCoord(0.75, 1, 0, 1);
135         frame.role:Show();
136     elseif frame.rolename == "TANK" then
137         frame.role:SetTexCoord(0.5, 0.75, 0, 1);
138         frame.role:Show();
139     else
140         frame.role:Hide();
141     end
142 end
143 addon.Events.UpdateRole = updateRole;
144
145 local function updateReadyCheck(frame, unit)
146     local status = GetReadyCheckStatus(unit);
147     if status == "ready" then
148         frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
149         frame.ready:Show()
150     elseif status == "notready" then
151         frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
152         frame.ready:Show()
153     elseif status == "waiting" then
154         frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
155         frame.ready:Show()
156     else
157         frame.ready:Hide()
158     end
159 end
160 addon.Events.UpdateReadyCheck = updateReadyCheck;
161
162 local function updateRaidMarker(frame, unit)
163     local index = GetRaidTargetIndex(unit);
164     if index then
165         SetRaidTargetIconTexture(frame.targeticon, index);
166         frame.targeticon:Show();
167     else
168         frame.targeticon:Hide();
169     end
170 end
171 addon.Events.UpdateRaidMarker = updateRaidMarker;
172
173 local eventFuncs = {
174     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
175         updateAggro(frame, frame.displayed);
176     end,
177     ["UNIT_NAME_UPDATE"] = function(frame)
178         updateName(frame, frame.unit);
179         if frame.raid then
180             if frame.guid then
181                 guids[frame.guid] = nil;
182             end
183             frame.guid = UnitGUID(frame.unit);
184             if frame.guid then
185                 guids[frame.guid] = frame;
186             end
187         end
188     end,
189     ["UNIT_FACTION"] = function(frame)
190         updateName(frame, frame.unit);
191     end,
192     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
193         updateRole(frame, frame.unit);
194     end,
195     ["READY_CHECK"] = function(frame)
196         updateReadyCheck(frame, frame.unit);
197     end,
198     ["RAID_TARGET_UPDATE"] = function(frame)
199         updateRaidMarker(frame, frame.displayed);
200     end,
201     ["PLAYER_REGEN_DISABLED"] = function(frame)
202         if frame.raid then
203             -- clear buff status on entering combat, should also use UnitAura to re-fill
204             frame.tankcd = {};
205             frame.alert = {};
206             frame.stacks = {};
207             frame.heal = {};
208             frame.buff1 = {};
209             frame.buff2 = {};
210             addon.SetAuras(frame.unit, frame.guid);
211         end
212     end,
213     ["UPDATE_ALL_BARS"] = function(frame)
214         updateVehicle(frame);
215         updateRaidMarker(frame, frame.displayed);
216         if frame.raid or frame.unit ~= "player" then
217             updateName(frame, frame.unit);
218         end
219         if frame.raid then
220             updateRole(frame, frame.unit);
221             updateAggro(frame, frame.displayed);
222             updateReadyCheck(frame, frame.unit);
223             if frame.guid then
224                 guids[frame.guid] = nil;
225             end
226             frame.guid = UnitGUID(frame.unit);
227             if frame.guid then
228                 guids[frame.guid] = frame;
229             end
230             frame.tankcd = {};
231             frame.alert = {};
232             frame.stacks = {};
233             frame.heal = {};
234             frame.buff1 = {};
235             frame.buff2 = {};
236             addon.SetAuras(frame.unit, frame.guid);
237         else
238             frame.prev = {};
239         end
240     end,
241 };
242 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
243 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
244 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
245 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
246 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
247 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
248 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
249 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
250 eventFuncs["PLAYER_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
251 eventFuncs["UNIT_TARGETABLE_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
252 eventFuncs["INSTANCE_ENCOUNTER_ENGAGE_UNIT"] = eventFuncs["UPDATE_ALL_BARS"];
253 eventFuncs["PLAYER_ALIVE"] = eventFuncs["UPDATE_ALL_BARS"];
254
255 function addon.UnitEvent(self, event)
256     return eventFuncs[event](self);
257 end