4 local ssub = string.sub;
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;
23 local checkIndicators = OmaRFIndicators.CheckIndicators;
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;
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
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);
63 local registerUnitEvents = M.RegisterUnitEvents;
65 local function updateText(frame, unit)
66 if UnitIsDeadOrGhost(unit) then
67 frame.text:SetText("Dead");
69 elseif not UnitIsConnected(unit) then
70 frame.text:SetText("DC");
72 elseif UnitIsAFK(unit) then
73 frame.text:SetText("afk");
75 elseif UnitIsDND(unit) then
76 frame.text:SetText("dnd");
82 M.UpdateText = updateText;
84 local function updateMaxHealth(frame, unit)
85 frame.health.max = UnitHealthMax(unit);
87 M.UpdateMaxHealth = updateMaxHealth;
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);
97 elseif current <= 0 or UnitIsDeadOrGhost(unit) then
100 updateText(frame, unit); -- update death
102 frame.health:SetWidth(current/max*width);
106 if frame.dead and current > 0 then
108 updateText(frame, unit); -- update revive
111 M.UpdateHealth = updateHealth;
113 -- TODO maybe add a prefix when in vehicle
114 local function updateName(frame, unit)
115 local name = UnitName(unit);
116 if not name then return end
117 frame.name:SetText(ssub(name, 1, 6));
119 local _, class = UnitClass(unit);
120 local color = RAID_CLASS_COLORS[class];
121 if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
123 M.UpdateName = updateName;
125 local function updateHealPred(frame, unit)
126 local incoming = UnitGetIncomingHeals(unit) or 0;
128 -- always at least 1 pixel space for heal prediction
129 local space = width - frame.health:GetWidth() + 1;
130 incoming = (incoming / frame.health.max) * width;
131 frame.healpred:SetWidth(min(space, incoming));
132 frame.healpred:Show();
134 frame.healpred:Hide();
137 M.UpdateHealPred = updateHealPred;
139 local function updateShield(frame, unit)
140 local shield = UnitGetTotalAbsorbs(unit) or 0;
142 local space = width - frame.health:GetWidth();
143 shield = (shield / frame.health.max) * width;
146 frame.shieldhl:Show();
147 elseif space < shield then
148 frame.shield:SetWidth(space);
150 frame.shieldhl:Show();
152 frame.shield:SetWidth(shield);
154 frame.shieldhl:Hide();
158 frame.shieldhl:Hide();
161 M.UpdateShield = updateShield;
163 local function updateHealAbsorb(frame, unit)
164 local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
166 local space = frame.health:GetWidth();
167 absorb = (absorb / frame.health.max) * width;
168 frame.healabsorb:SetWidth(min(space, absorb));
169 frame.healabsorb:Show();
171 frame.healabsorb:Hide();
174 M.UpdateHealAbsorb = updateHealAbsorb;
176 local function updateAuras(frame, unit)
177 local alert = checkIndicators(frame, unit);
179 if frame.overlay.color ~= overlayColorAlert then
180 frame.overlay:SetVertexColor(unpack(overlayColorAlert));
181 frame.overlay.color = overlayColorAlert;
182 frame.overlay:Show();
184 elseif UnitDebuff(unit, 1, "RAID") ~= nil then
185 -- something dispellable
186 if frame.overlay.color ~= overlayColorDispel then
187 frame.overlay:SetVertexColor(unpack(overlayColorDispel));
188 frame.overlay.color = overlayColorDispel;
189 frame.overlay:Show();
191 -- don't overlay charmed when in vehicle
192 elseif UnitIsCharmed(unit) and unit == frame.unit then
193 if frame.overlay.color ~= overlayColorCharm then
194 frame.overlay:SetVertexColor(unpack(overlayColorCharm));
195 frame.overlay.color = overlayColorCharm;
196 frame.overlay:Show();
199 if frame.overlay.color ~= nil then
200 frame.overlay.color = nil;
201 frame.overlay:Hide();
205 M.UpdateAuras = updateAuras;
207 local function updateAggro(frame, unit)
208 local status = UnitThreatSituation(unit);
209 if status and status > 0 then
210 frame.base:SetVertexColor(GetThreatStatusColor(status));
212 frame.base:SetVertexColor(unpack(baseColor));
215 M.UpdateAggro = updateAggro;
217 local function updateVehicle(frame)
218 local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
219 UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
220 if shouldTargetVehicle then
221 if not frame.inVehicle then
222 frame.inVehicle = true;
223 frame.displayed = frame.vehicle;
224 registerUnitEvents(frame);
226 elseif frame.inVehicle then
227 frame.inVehicle = false;
228 frame.displayed = frame.unit;
229 registerUnitEvents(frame);
232 M.UpdateVehicle = updateVehicle;
234 local function updateRole(frame, unit)
235 local role = UnitGroupRolesAssigned(unit);
236 if role == "HEALER" then
237 frame.role:SetTexCoord(0.75, 1, 0, 1);
239 elseif role == "TANK" then
240 frame.role:SetTexCoord(0.5, 0.75, 0, 1);
246 M.UpdateRole = updateRole;
248 local function updateReadyCheck(frame, unit)
249 local status = GetReadyCheckStatus(unit);
250 if status == "ready" then
251 frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
253 elseif status == "notready" then
254 frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
256 elseif status == "waiting" then
257 frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
263 M.UpdateReadyCheck = updateReadyCheck;
265 local function updateRaidMarker(frame, unit)
266 local index = GetRaidTargetIndex(unit);
268 SetRaidTargetIconTexture(frame.targeticon, index);
269 frame.targeticon:Show();
271 frame.targeticon:Hide();
274 M.UpdateRaidMarker = updateRaidMarker;
277 ["UNIT_HEALTH"] = function(frame)
278 updateHealth(frame, frame.displayed);
279 updateShield(frame, frame.displayed);
280 updateHealAbsorb(frame, frame.displayed);
281 -- no heal prediction update, that doesn't overflow too much
282 -- raid marker update here, if needed
283 -- because marker is removed when unit dies
284 -- without a RAID_TARGET_UPDATE event
285 --updateRaidMarker(frame, frame.unit);
287 ["UNIT_AURA"] = function(frame)
288 updateAuras(frame, frame.displayed);
290 ["UNIT_HEAL_PREDICTION"] = function(frame)
291 updateHealPred(frame, frame.displayed);
293 ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
294 updateShield(frame, frame.displayed);
296 ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
297 updateHealAbsorb(frame, frame.displayed);
299 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
300 updateAggro(frame, frame.displayed);
302 ["UNIT_MAXHEALTH"] = function(frame)
303 updateMaxHealth(frame, frame.displayed);
304 updateHealth(frame, frame.displayed);
305 updateShield(frame, frame.displayed);
306 updateHealAbsorb(frame, frame.displayed);
308 ["UNIT_NAME_UPDATE"] = function(frame)
309 updateName(frame, frame.unit);
311 ["UNIT_CONNECTION"] = function(frame)
312 updateText(frame, frame.displayed);
314 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
315 updateRole(frame, frame.unit);
317 ["READY_CHECK"] = function(frame)
318 updateReadyCheck(frame, frame.unit);
320 ["RAID_TARGET_UPDATE"] = function(frame)
321 updateRaidMarker(frame, frame.displayed);
323 ["UPDATE_ALL_BARS"] = function(frame)
324 updateRole(frame, frame.unit);
325 updateVehicle(frame);
326 updateMaxHealth(frame, frame.displayed);
327 updateHealth(frame, frame.displayed);
328 updateText(frame, frame.displayed);
329 updateAuras(frame, frame.displayed);
330 updateShield(frame, frame.displayed);
331 updateHealPred(frame, frame.displayed);
332 updateHealAbsorb(frame, frame.displayed);
333 updateAggro(frame, frame.displayed);
334 updateName(frame, frame.unit);
335 updateReadyCheck(frame, frame.unit);
336 updateRaidMarker(frame, frame.displayed);
339 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
340 eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
341 eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
342 eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
343 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
344 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
345 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
346 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
347 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
348 eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
350 function M.UnitEvent(self, event)
351 eventFuncs[event](self);