6b823229423d1c7ddf62c912d9897a3077cda9b0
[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     end
60     if frame.raid then
61         frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
62         frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
63     end
64 end
65 local registerUnitEvents = addon.RegisterUnitEvents;
66
67 local function updateName(frame, unit)
68     local name = UnitName(unit);
69     if not name or not frame.name then return end
70     name = ssub(name, 1, 6);
71     if frame.unit == unit then
72         frame.name:SetText(name);
73     else
74         frame.name:SetFormattedText("-%s", name);
75     end
76
77     local _, class = UnitClass(unit);
78     local color = RAID_CLASS_COLORS[class];
79     if color then
80         if not frame.raid then
81             if UnitIsEnemy("player", unit) then
82                 frame.health:SetVertexColor(1, 0, 0);
83             elseif UnitIsPlayer(unit) then
84                 frame.health:SetVertexColor(color.r, color.g, color.b);
85             else
86                 frame.health:SetVertexColor(0, 1, 0);
87             end
88         else
89             frame.name:SetVertexColor(color.r, color.g, color.b);
90         end
91     end
92 end
93 addon.Events.UpdateName = updateName;
94
95 local function updateAggro(frame, unit)
96     local status = UnitThreatSituation(unit);
97     if status and status > 0 then
98         frame.base:SetVertexColor(GetThreatStatusColor(status));
99     else
100         frame.base:SetVertexColor(unpack(baseColor));
101     end
102 end
103 addon.Events.UpdateAggro = updateAggro;
104
105 local function updateVehicle(frame)
106     local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
107         UnitTargetsVehicleInRaidUI(frame.unit) and frame.vehicle and UnitExists(frame.vehicle);
108     if shouldTargetVehicle then
109         if not frame.inVehicle then
110             frame.inVehicle = true;
111             frame.displayed = frame.vehicle;
112             registerUnitEvents(frame);
113         end
114     elseif frame.inVehicle then
115         frame.inVehicle = false;
116         frame.displayed = frame.unit;
117         registerUnitEvents(frame);
118     end
119 end
120 addon.Events.UpdateVehicle = updateVehicle;
121
122 local function updateRole(frame, unit)
123     local role = UnitGroupRolesAssigned(unit);
124     if role == "HEALER" then
125         frame.role:SetTexCoord(0.75, 1, 0, 1);
126         frame.role:Show();
127     elseif role == "TANK" then
128         frame.role:SetTexCoord(0.5, 0.75, 0, 1);
129         frame.role:Show();
130     else
131         frame.role:Hide();
132     end
133 end
134 addon.Events.UpdateRole = updateRole;
135
136 local function updateReadyCheck(frame, unit)
137     local status = GetReadyCheckStatus(unit);
138     if status == "ready" then
139         frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
140         frame.ready:Show()
141     elseif status == "notready" then
142         frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
143         frame.ready:Show()
144     elseif status == "waiting" then
145         frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
146         frame.ready:Show()
147     else
148         frame.ready:Hide()
149     end
150 end
151 addon.Events.UpdateReadyCheck = updateReadyCheck;
152
153 local function updateRaidMarker(frame, unit)
154     local index = GetRaidTargetIndex(unit);
155     if index then
156         SetRaidTargetIconTexture(frame.targeticon, index);
157         frame.targeticon:Show();
158     else
159         frame.targeticon:Hide();
160     end
161 end
162 addon.Events.UpdateRaidMarker = updateRaidMarker;
163
164 local eventFuncs = {
165     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
166         updateAggro(frame, frame.displayed);
167     end,
168     ["UNIT_NAME_UPDATE"] = function(frame)
169         updateName(frame, frame.unit);
170         if frame.raid then
171             if frame.guid then
172                 guids[frame.guid] = nil;
173             end
174             frame.guid = UnitGUID(frame.unit);
175             if frame.guid then
176                 guids[frame.guid] = frame;
177             end
178         end
179     end,
180     ["PLAYER_ROLES_ASSIGNED"] = function(frame)
181         updateRole(frame, frame.unit);
182     end,
183     ["READY_CHECK"] = function(frame)
184         updateReadyCheck(frame, frame.unit);
185     end,
186     ["RAID_TARGET_UPDATE"] = function(frame)
187         updateRaidMarker(frame, frame.displayed);
188     end,
189     ["PLAYER_REGEN_DISABLED"] = function(frame)
190         if frame.raid then
191             -- clear buff status on entering combat, should also use UnitAura to re-fill
192             frame.tankcd = {};
193             frame.alert = {};
194             frame.stacks = {};
195             frame.heal = {};
196             frame.buff1 = {};
197             frame.buff2 = {};
198             addon.SetAuras(frame.unit, frame.guid);
199         end
200     end,
201     ["UPDATE_ALL_BARS"] = function(frame)
202         updateVehicle(frame);
203         updateRaidMarker(frame, frame.displayed);
204         if frame.raid or frame.unit ~= "player" then
205             updateName(frame, frame.unit);
206         end
207         if frame.raid then
208             updateRole(frame, frame.unit);
209             updateAggro(frame, frame.displayed);
210             updateReadyCheck(frame, frame.unit);
211             if frame.guid then
212                 guids[frame.guid] = nil;
213             end
214             frame.guid = UnitGUID(frame.unit);
215             if frame.guid then
216                 guids[frame.guid] = frame;
217             end
218             frame.tankcd = {};
219             frame.alert = {};
220             frame.stacks = {};
221             frame.heal = {};
222             frame.buff1 = {};
223             frame.buff2 = {};
224             addon.SetAuras(frame.unit, frame.guid);
225         else
226             frame.prev = {};
227         end
228     end,
229 };
230 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
231 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
232 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
233 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
234 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
235 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
236 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
237 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
238 eventFuncs["PLAYER_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
239 eventFuncs["UNIT_TARGETABLE_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
240 eventFuncs["INSTANCE_ENCOUNTER_ENGAGE_UNIT"] = eventFuncs["UPDATE_ALL_BARS"];
241 eventFuncs["PLAYER_ALIVE"] = eventFuncs["UPDATE_ALL_BARS"];
242
243 function addon.UnitEvent(self, event)
244     return eventFuncs[event](self);
245 end