4 local ssub = string.sub;
6 local ceil = math.ceil;
7 local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists;
8 local UnitDebuff, UnitIsCharmed = UnitDebuff, UnitIsCharmed;
9 local UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType;
10 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
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 UnitGroupRolesAssigned = UnitGroupRolesAssigned;
17 local UnitLevel, UnitClassification = UnitLevel, UnitClassification;
18 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
20 local Settings = OmaUFSettings;
21 local baseColor = Settings.BaseColor;
22 local overlayColorDispel = Settings.OverlayColorDispel;
23 local overlayColorCharm = Settings.OverlayColorCharm;
24 local overlayColorAlert = Settings.OverlayColorAlert;
25 local powerColors = Settings.PowerColors;
30 function M.RegisterEvents(frame)
31 -- events are taken from FrameXML/CompactUnitFrame.lua
32 -- TODO raid marker support,
33 -- player flags support (/afk, /dnd)
34 local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
35 frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
36 frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
37 frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
38 frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
39 frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
40 frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
41 frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
42 frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
43 frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
44 frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
45 frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
46 frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
47 frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
48 frame:RegisterUnitEvent("UNIT_FACTION", frame.unit, displayed);
50 local registerEvents = M.RegisterEvents;
52 local function updateHealth(frame, unit)
53 local current, max = UnitHealth(unit), frame.health.max;
55 -- sanity check, occasionally UnitHealthMax gives zero
57 -- somehow current health has gone over the maximum (missed maxhealth event)
58 frame.health.max = UnitHealthMax(unit);
59 max = frame.health.max;
61 -- error state, still over maximum
62 frame.health:SetWidth(width);
66 frame.health:SetWidth(current/frame.health.max*width);
68 frame.health:SetWidth(width);
72 if UnitIsDeadOrGhost(unit) then
77 local function updateHealthText(frame, unit)
78 local current, max = UnitHealth(unit), frame.health.max;
79 if UnitIsDeadOrGhost(unit) then
80 frame.healthText:SetText("Dead");
81 frame.healthText:Show();
82 elseif not UnitIsConnected(unit) then
83 frame.healthText:SetText("DC");
84 frame.healthText:Show();
85 elseif max > 0 and current < max then
86 frame.healthText:SetText(ceil(current/max*100));
87 frame.healthText:Show();
89 frame.healthText:Hide();
93 local function updateMaxHealth(frame, unit)
94 frame.health.max = UnitHealthMax(unit);
97 local function updatePower(frame, unit)
98 local current, max = UnitPower(unit), frame.mana.max;
99 -- sanity check, occasionally UnitPowerMax gives zero
103 elseif current > max then
105 frame.mana.max = UnitPowerMax(unit);
106 max = frame.mana.max;
107 if current > max then
109 frame.mana:SetWidth(width);
114 frame.mana:SetWidth(UnitPower(unit)/max*width);
117 frame.mana:SetWidth(width);
122 local function updatePowerText(frame, unit)
123 local current, max = UnitPower(unit), frame.mana.max;
124 if UnitIsDeadOrGhost(unit) or not UnitIsConnected(unit) then
125 frame.healthText:Hide();
126 elseif max > 0 and current > 0 and current < max then
127 frame.manaText:SetText(ceil(current/max*100));
128 frame.manaText:Show();
130 frame.manaText:Hide();
134 local function updateMaxPower(frame, unit)
135 frame.mana.max = UnitPowerMax(unit);
138 local function updatePowerColor(frame, unit)
139 frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
142 local function updateName(frame, unit)
143 local name = UnitName(unit);
144 if not name then return end
145 frame.name:SetText(ssub(name, 1, 10));
147 local _, class = UnitClass(unit);
148 local color = RAID_CLASS_COLORS[class];
149 if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
152 local function updateHealPred(frame, unit)
153 local incoming = UnitGetIncomingHeals(unit) or 0;
155 local max = frame.health.max;
156 local space = width - frame.health:GetWidth() + 1;
157 local pred = (incoming / max) * width;
158 frame.healpred:SetWidth(min(space, pred));
159 frame.healpred:Show();
161 frame.healpred:Hide();
165 local function updateShield(frame, unit)
166 local shield = UnitGetTotalAbsorbs(unit) or 0;
168 local max = frame.health.max;
169 local space = width - frame.health:GetWidth();
170 shield = (shield / max) * width;
173 frame.shieldhl:Show();
174 elseif space < shield then
175 frame.shield:SetWidth(space);
177 frame.shieldhl:Show();
179 frame.shield:SetWidth(shield);
181 frame.shieldhl:Hide();
185 frame.shieldhl:Hide();
189 local function updateHealAbsorb(frame, unit)
190 local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
192 local max = frame.health.max;
193 local space = frame.health:GetWidth();
194 absorb = (absorb / max) * width;
195 frame.healabsorb:SetWidth(min(space, absorb));
196 frame.healabsorb:Show();
198 frame.healabsorb:Hide();
202 local function updateAuras(frame, unit)
203 if UnitDebuff(unit, 1, "RAID") ~= nil then
204 -- something dispellable
205 if frame.overlay.color ~= overlayColorDispel then
206 frame.overlay:SetVertexColor(unpack(overlayColorDispel));
207 frame.overlay:Show();
208 frame.overlay.color = overlayColorDispel;
210 -- don't overlay charmed when in vehicle
211 elseif UnitIsCharmed(unit) and unit == frame.unit then
212 if frame.overlay.color ~= overlayColorCharm then
213 frame.overlay:SetVertexColor(unpack(overlayColorCharm));
214 frame.overlay:Show();
215 frame.overlay.color = overlayColorCharm;
218 if frame.overlay.color ~= nil then
219 frame.overlay:Hide();
220 frame.overlay.color = nil;
225 local function updateAggro(frame, unit)
226 local status = UnitThreatSituation(unit);
227 if status and status > 0 then
228 frame.base:SetVertexColor(GetThreatStatusColor(status));
230 frame.base:SetVertexColor(unpack(baseColor));
234 local function updateVehicle(frame)
235 local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
236 if shouldTargetVehicle then
237 if not frame.inVehicle then
238 frame.inVehicle = true;
239 frame.displayed = frame.vehicle;
240 registerEvents(frame);
242 elseif frame.inVehicle then
243 frame.inVehicle = false;
244 frame.displayed = frame.unit;
245 registerEvents(frame);
249 local function updateRole(frame, unit)
250 local role = UnitGroupRolesAssigned(unit);
251 if role == "HEALER" or role == "TANK" or role == "DAMAGER" then
252 frame.role:SetTexCoord(GetTexCoordsForRole(role));
259 local function updateLevelText(frame, unit, levelup)
262 frame.level:SetText(levelup);
264 local level = UnitLevel(unit);
265 local class = UnitClassification(unit);
266 local leveltext, classtext;
268 if class == "worldboss" then
276 if class == "rareelite" then
277 classtext = " Rare Elite";
278 elseif class == "elite" then
279 classtext = " Elite";
280 elseif class == "rare" then
285 frame.level:SetFormattedText("%s%s", leveltext, classtext);
289 local function updateStatus(frame, unit)
290 -- coords from PlayerFrame
291 if frame.inCombat or UnitAffectingCombat(unit) then
292 frame.status:SetTexCoord(0.5, 1, 0, 0.484375);
294 elseif unit == "player" and IsResting() then
295 frame.status:SetTexCoord(0, 0.5, 0, 0.421875);
302 local function updatePVP(frame, unit)
303 if UnitIsPVPFreeForAll(unit) then
304 frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-FFA");
306 elseif UnitIsPVP(unit) then
307 local faction = UnitFactionGroup(unit);
308 if faction and faction ~= "Neutral" then
309 -- from PlayerFrame, mercenary checks
310 if UnitIsMercenary(unit) then
311 if faction == "Horde" then
312 faction = "Alliance";
313 elseif faction == "Alliance" then
317 frame.pvp:SetTexture("Interface\\TARGETINGFRAME\\UI-PVP-"..faction);
328 ["UNIT_HEALTH"] = function(frame)
329 updateHealth(frame, frame.displayed);
330 updateHealthText(frame, frame.displayed);
331 updateShield(frame, frame.displayed);
332 updateHealAbsorb(frame, frame.displayed);
333 -- no heal prediction update, that doesn't overflow too much
335 ["UNIT_POWER"] = function(frame)
336 updatePower(frame, frame.displayed);
337 updatePowerText(frame, frame.displayed);
339 ["UNIT_AURA"] = function(frame)
340 updateAuras(frame, frame.displayed);
342 ["UNIT_HEAL_PREDICTION"] = function(frame)
343 updateHealPred(frame, frame.displayed);
345 ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
346 updateShield(frame, frame.displayed);
348 ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
349 updateHealAbsorb(frame, frame.displayed);
351 ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
352 updateAggro(frame, frame.displayed);
354 ["UNIT_MAXHEALTH"] = function(frame)
355 updateMaxHealth(frame, frame.displayed);
356 updateHealth(frame, frame.displayed);
357 updateHealthText(frame, frame.displayed);
358 updateShield(frame, frame.displayed);
359 updateHealAbsorb(frame, frame.displayed);
361 ["UNIT_MAXPOWER"] = function(frame)
362 updateMaxPower(frame, frame.displayed);
363 updatePower(frame, frame.displayed);
364 updatePowerText(frame, frame.displayed);
366 ["UNIT_DISPLAYPOWER"] = function(frame)
367 updatePowerColor(frame, frame.displayed);
369 ["UNIT_NAME_UPDATE"] = function(frame)
370 updateName(frame, frame.displayed);
372 ["UNIT_CONNECTION"] = function(frame)
373 updateHealthText(frame, frame.displayed);
374 updatePowerText(frame, frame.displayed);
376 ["PLAYER_ROLES_ASSIGNED"] = function(frame)
377 updateRole(frame, frame.unit);
379 ["UNIT_LEVEL"] = function(frame)
380 updateLevelText(frame, frame.unit);
382 ["PLAYER_LEVEL_UP"] = function(frame, arg1)
383 updateLevelText(frame, frame.unit, arg1);
385 ["PLAYER_UPDATE_RESTING"] = function(frame)
386 updateStatus(frame, frame.unit);
388 ["PLAYER_REGEN_DISABLED"] = function(frame)
389 frame.inCombat = true;
390 updateStatus(frame, frame.unit);
392 ["PLAYER_REGEN_ENABLED"] = function(frame)
393 frame.inCombat = false;
394 updateStatus(frame, frame.unit);
396 ["UNIT_FACTION"] = function(frame)
397 updatePVP(frame, frame.unit);
399 ["UPDATE_ALL_BARS"] = function(frame)
400 updateVehicle(frame);
401 updateMaxHealth(frame, frame.displayed);
402 updateMaxPower(frame, frame.displayed);
403 updateHealth(frame, frame.displayed);
404 updateHealthText(frame, frame.displayed);
405 updatePower(frame, frame.displayed);
406 updatePowerText(frame, frame.displayed);
407 updateAuras(frame, frame.displayed);
408 updateShield(frame, frame.displayed);
409 updateHealPred(frame, frame.displayed);
410 updateHealAbsorb(frame, frame.displayed);
411 updatePowerColor(frame, frame.displayed);
412 updateAggro(frame, frame.displayed);
413 updateName(frame, frame.displayed);
414 updateRole(frame, frame.unit);
415 updateLevelText(frame, frame.unit);
416 updateStatus(frame, frame.unit);
417 updatePVP(frame, frame.unit);
420 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
421 eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
422 eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
423 eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
424 eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
425 eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
426 eventFuncs["PLAYER_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
428 function M.UnitEvent(self, event, arg1)
429 eventFuncs[event](self, arg1);
432 function M.LoadChar()
433 width = Settings.Character.Width;