4 local ssub = string.sub;
6 local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists;
7 local UnitDebuff, UnitIsCharmed = UnitDebuff, UnitIsCharmed;
8 local UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType;
9 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
10 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
11 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
12 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
13 local UnitHasIncomingResurrection = UnitHasIncomingResurrection;
14 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
15 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
16 local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
17 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
19 local checkIndicators = OmaRFIndicators.CheckIndicators;
21 local Settings = OmaRFSettings;
22 local width = Settings.Character.Width;
23 local baseColor = Settings.BaseColor;
24 local overlayColorDispel = Settings.OverlayColorDispel;
25 local overlayColorCharm = Settings.OverlayColorCharm;
26 local overlayColorAlert = Settings.OverlayColorAlert;
27 local powerColors = Settings.PowerColors;
31 function M.RegisterEvents(frame)
32 -- events are taken from FrameXML/CompactUnitFrame.lua
33 -- TODO ready check support, raid marker support,
34 -- player flags support (/afk, /dnd)
35 local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
36 frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
37 frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
38 frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
39 frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
40 frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
41 frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
42 frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
43 frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
44 frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
45 frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
46 frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
47 frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
48 frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
49 frame:RegisterUnitEvent("INCOMING_RESURRECT_CHANGED", frame.unit, displayed);
51 local registerEvents = M.RegisterEvents;
53 local function updateHealth(frame, unit)
54 local current, max = UnitHealth(unit), frame.health.max;
56 -- sanity check, occasionally UnitHealthMax gives zero
58 -- somehow current health has gone over the maximum (missed maxhealth event)
59 frame.health.max = UnitHealthMax(unit);
60 max = frame.health.max;
62 -- error state, still over maximum
63 frame.health:SetWidth(width);
67 frame.health:SetWidth(current/frame.health.max*width);
69 frame.health:SetWidth(width);
73 if UnitIsDeadOrGhost(unit) then
78 local function updateText(frame, unit)
79 local current, max = UnitHealth(unit), frame.health.max;
80 local healthLost = max - current;
81 if UnitIsDeadOrGhost(unit) then
82 frame.text:SetText("Dead");
83 elseif not UnitIsConnected(unit) then
84 frame.text:SetText("DC");
85 elseif healthLost > 0 then
86 if healthLost > 1200000000 then -- 1.2B
87 frame.text:SetFormattedText("-%.1fB", healthLost / 1000000000);
88 elseif healthLost > 1200000 then -- 1.2M
89 frame.text:SetFormattedText("-%.1fM", healthLost / 1000000);
90 elseif healthLost > 1000 then -- 1K
91 frame.text:SetFormattedText("-%dK", healthLost / 1000);
93 frame.text:SetFormattedText("-%d", healthLost)
101 local function updateIncomingRes(frame, unit)
102 if UnitHasIncomingResurrection(unit) then
109 local function updateMaxHealth(frame, unit)
110 frame.health.max = UnitHealthMax(unit);
113 local function updatePower(frame, unit)
114 local current, max = UnitPower(unit), frame.mana.max;
115 -- sanity check, occasionally UnitPowerMax gives zero
116 if current > max then
117 frame.mana.max = UnitPowerMax(unit);
118 max = frame.mana.max;
119 if current > max then
121 frame.mana:SetWidth(width);
125 frame.mana:SetWidth(UnitPower(unit)/max*width);
127 frame.mana:SetWidth(width);
131 local function updateMaxPower(frame, unit)
132 frame.mana.max = UnitPowerMax(unit);
135 local function updatePowerColor(frame, unit)
136 frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
139 local function updateName(frame, unit)
140 local name = UnitName(unit);
141 if not name then return end
142 frame.name:SetText(ssub(name, 1, 6));
144 local _, class = UnitClass(unit);
145 local color = RAID_CLASS_COLORS[class];
146 if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
149 local function updateHealPred(frame, unit)
150 local incoming = UnitGetIncomingHeals(unit) or 0;
152 local max = frame.health.max;
153 local space = width - frame.health:GetWidth() + 1;
154 local pred = (incoming / max) * width;
155 frame.healpred:SetWidth(min(space, pred));
156 frame.healpred:Show();
158 frame.healpred:Hide();
162 local function updateShield(frame, unit)
163 local shield = UnitGetTotalAbsorbs(unit) or 0;
165 local max = frame.health.max;
166 local space = width - frame.health:GetWidth();
167 shield = (shield / max) * width;
168 if space < shield then
169 frame.shield:SetWidth(space);
170 frame.shieldhl:Show();
172 frame.shield:SetWidth(shield);
173 frame.shieldhl:Hide();
178 frame.shieldhl:Hide();
182 local function updateHealAbsorb(frame, unit)
183 local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
185 local max = frame.health.max;
186 local space = frame.health:GetWidth();
187 absorb = (absorb / max) * width;
188 frame.healabsorb:SetWidth(min(space, absorb));
189 frame.healabsorb:Show();
191 frame.healabsorb:Hide();
195 local function updateAuras(frame, unit)
196 checkIndicators(frame, unit);
197 if UnitDebuff(unit, 1, "RAID") ~= nil then
198 -- something dispellable
199 if frame.overlay.color ~= overlayColorDispel then
200 frame.overlay:SetVertexColor(unpack(overlayColorDispel));
201 frame.overlay:Show();
202 frame.overlay.color = overlayColorDispel;
204 -- don't overlay charmed when in vehicle
205 elseif UnitIsCharmed(unit) and unit == frame.unit then
206 if frame.overlay.color ~= overlayColorCharm then
207 frame.overlay:SetVertexColor(unpack(overlayColorCharm));
208 frame.overlay:Show();
209 frame.overlay.color = overlayColorCharm;
212 if frame.overlay.color ~= nil then
213 frame.overlay:Hide();
214 frame.overlay.color = nil;
219 local function updateAggro(frame, unit)
220 local status = UnitThreatSituation(unit);
221 if status and status > 0 then
222 frame.base:SetVertexColor(GetThreatStatusColor(status));
224 frame.base:SetVertexColor(unpack(baseColor));
228 local function updateVehicle(frame)
229 local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
230 if shouldTargetVehicle then
231 if not frame.inVehicle then
232 frame.inVehicle = true;
233 frame.displayed = frame.vehicle;
234 registerEvents(frame);
236 elseif frame.inVehicle then
237 frame.inVehicle = false;
238 frame.displayed = frame.unit;
239 registerEvents(frame);
243 local function updateRole(frame, unit)
244 local role = UnitGroupRolesAssigned(unit);
245 if role == "HEALER" then
246 frame.role:SetTexCoord(0.75, 1, 0, 1);
248 elseif role == "TANK" then
249 frame.role:SetTexCoord(0.5, 0.75, 0, 1);
257 ["UNIT_HEALTH"] = function(frame)
258 updateHealth(frame, frame.displayed);
259 updateText(frame, frame.displayed);
260 updateShield(frame, frame.displayed);
261 updateHealAbsorb(frame, frame.displayed);
262 -- no heal prediction update, that doesn't overflow too much
264 ["UNIT_POWER"] = function(frame)
265 updatePower(frame, frame.displayed);
267 ["UNIT_AURA"] = function(frame)
268 updateAuras(frame, frame.displayed);
270 ["UNIT_HEAL_PREDICTION"] = function(frame)
271 updateHealPred(frame, frame.displayed);
273 ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
274 updateShield(frame, frame.displayed);
276 ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
277 updateHealAbsorb(frame, frame.displayed);
279 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
280 updateAggro(frame, frame.displayed);
282 ["UNIT_MAXHEALTH"] = function(frame)
283 updateMaxHealth(frame, frame.displayed);
284 updateHealth(frame, frame.displayed);
285 updateText(frame, frame.displayed);
286 updateShield(frame, frame.displayed);
287 updateHealAbsorb(frame, frame.displayed);
289 ["UNIT_MAXPOWER"] = function(frame)
290 updateMaxPower(frame, frame.displayed);
291 updatePower(frame, frame.displayed);
293 ["UNIT_DISPLAYPOWER"] = function(frame)
294 updatePowerColor(frame, frame.displayed);
296 ["UNIT_NAME_UPDATE"] = function(frame)
297 updateName(frame, frame.displayed);
299 ["UNIT_CONNECTION"] = function(frame)
300 updateText(frame, frame.displayed);
302 ["INCOMING_RESURRECT_CHANGED"] = function(frame)
303 updateIncomingRes(frame, frame.unit);
305 ["PARTY_MEMBER_ENABLE"] = function(frame)
306 -- new power info possibly (FrameXML/CompactUnitFrame.lua)
307 updateMaxPower(frame, frame.displayed);
308 updatePowerColor(frame, frame.displayed);
310 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
311 updateRole(frame, frame.unit);
313 ["UPDATE_ALL_BARS"] = function(frame)
314 updateVehicle(frame);
315 updateMaxHealth(frame, frame.displayed);
316 updateMaxPower(frame, frame.displayed);
317 updateHealth(frame, frame.displayed);
318 updateText(frame, frame.displayed);
319 updatePower(frame, frame.displayed);
320 updateAuras(frame, frame.displayed);
321 updateShield(frame, frame.displayed);
322 updateHealPred(frame, frame.displayed);
323 updateHealAbsorb(frame, frame.displayed);
324 updatePowerColor(frame, frame.displayed);
325 updateIncomingRes(frame, frame.unit);
326 updateAggro(frame, frame.displayed);
327 updateName(frame, frame.displayed);
328 updateRole(frame, frame.unit);
331 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
332 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
333 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
334 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
335 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
336 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
337 function M.UnitEvent(self, event)
338 eventFuncs[event](self);