2 -- 2019 Aleksi Blinnikka
5 local ssub = string.sub;
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;
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};
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
41 frame:RegisterEvent("PLAYER_REGEN_DISABLED");
42 frame:RegisterEvent("READY_CHECK");
43 frame:RegisterEvent("READY_CHECK_FINISHED");
44 frame:RegisterEvent("GROUP_ROSTER_UPDATE");
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);
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);
62 frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
63 frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
66 local registerUnitEvents = addon.RegisterUnitEvents;
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);
75 frame.name:SetFormattedText("-%s", name);
78 local _, class = UnitClass(unit);
79 local color = RAID_CLASS_COLORS[class];
80 frame.classname = class;
83 if not frame.raid then
84 if not UnitPlayerControlled(unit) and UnitIsTapDenied(unit) then
85 frame.health:SetVertexColor(0.5, 0.5, 0.5);
86 elseif UnitIsPlayer(unit) then
87 frame.health:SetVertexColor(color.r, color.g, color.b);
88 elseif UnitPlayerControlled(unit) then
89 frame.health:SetVertexColor(0, 1, 0);
91 frame.health:SetVertexColor(UnitSelectionColor(unit));
94 frame.name:SetVertexColor(color.r, color.g, color.b);
98 addon.Events.UpdateName = updateName;
100 local function updateAggro(frame, unit)
101 local status = UnitThreatSituation(unit);
102 if status and status > 0 then
103 frame.base:SetVertexColor(GetThreatStatusColor(status));
105 frame.base:SetVertexColor(unpack(baseColor));
108 addon.Events.UpdateAggro = updateAggro;
110 local function updateVehicle(frame)
111 local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
112 UnitTargetsVehicleInRaidUI(frame.unit) and frame.vehicle and UnitExists(frame.vehicle);
113 if shouldTargetVehicle then
114 if not frame.inVehicle then
115 frame.inVehicle = true;
116 frame.displayed = frame.vehicle;
117 registerUnitEvents(frame);
119 elseif frame.inVehicle then
120 frame.inVehicle = false;
121 frame.displayed = frame.unit;
122 registerUnitEvents(frame);
125 addon.Events.UpdateVehicle = updateVehicle;
127 local function updateRole(frame, unit)
128 frame.rolename = UnitGroupRolesAssigned(unit);
129 if frame.rolename == "HEALER" then
130 frame.role:SetTexCoord(0.75, 1, 0, 1);
132 elseif frame.rolename == "TANK" then
133 frame.role:SetTexCoord(0.5, 0.75, 0, 1);
139 addon.Events.UpdateRole = updateRole;
141 local function updateReadyCheck(frame, unit)
142 local status = GetReadyCheckStatus(unit);
143 if status == "ready" then
144 frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
146 elseif status == "notready" then
147 frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
149 elseif status == "waiting" then
150 frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
156 addon.Events.UpdateReadyCheck = updateReadyCheck;
158 local function updateRaidMarker(frame, unit)
159 local index = GetRaidTargetIndex(unit);
161 SetRaidTargetIconTexture(frame.targeticon, index);
162 frame.targeticon:Show();
164 frame.targeticon:Hide();
167 addon.Events.UpdateRaidMarker = updateRaidMarker;
170 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
171 updateAggro(frame, frame.displayed);
173 ["UNIT_NAME_UPDATE"] = function(frame)
174 updateName(frame, frame.unit);
177 guids[frame.guid] = nil;
179 frame.guid = UnitGUID(frame.unit);
181 guids[frame.guid] = frame;
185 ["UNIT_FACTION"] = function(frame)
186 updateName(frame, frame.unit);
188 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
189 updateRole(frame, frame.unit);
191 ["READY_CHECK"] = function(frame)
192 updateReadyCheck(frame, frame.unit);
194 ["RAID_TARGET_UPDATE"] = function(frame)
195 updateRaidMarker(frame, frame.displayed);
197 ["PLAYER_REGEN_DISABLED"] = function(frame)
199 -- clear buff status on entering combat, should also use UnitAura to re-fill
207 addon.SetAuras(frame.unit, frame.guid);
210 ["UPDATE_ALL_BARS"] = function(frame)
211 updateVehicle(frame);
212 updateRaidMarker(frame, frame.displayed);
213 if frame.raid or frame.unit ~= "player" then
214 updateName(frame, frame.unit);
217 updateRole(frame, frame.unit);
218 updateAggro(frame, frame.displayed);
219 updateReadyCheck(frame, frame.unit);
221 guids[frame.guid] = nil;
223 frame.guid = UnitGUID(frame.unit);
225 guids[frame.guid] = frame;
233 addon.SetAuras(frame.unit, frame.guid);
239 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
240 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
241 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
242 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
243 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
244 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
245 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
246 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
247 eventFuncs["PLAYER_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
248 eventFuncs["UNIT_TARGETABLE_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
249 eventFuncs["INSTANCE_ENCOUNTER_ENGAGE_UNIT"] = eventFuncs["UPDATE_ALL_BARS"];
250 eventFuncs["PLAYER_ALIVE"] = eventFuncs["UPDATE_ALL_BARS"];
252 function addon.UnitEvent(self, event)
253 return eventFuncs[event](self);