+-- events.lua
+-- 2019 Aleksi Blinnikka
+local _;
+local unpack = unpack;
+local ssub = string.sub;
+local min = math.min;
+local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists;
+local UnitDebuff, UnitIsCharmed = UnitDebuff, UnitIsCharmed;
+local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
+local UnitIsAFK, UnitIsDND = UnitIsAFK, UnitIsDND;
+local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
+local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
+local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
+local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
+local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
+local GetReadyCheckTimeLeft, GetReadyCheckStatus = GetReadyCheckTimeLeft, GetReadyCheckStatus;
+local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
+local GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture;
+local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
+local READY_CHECK_READY_TEXTURE = READY_CHECK_READY_TEXTURE;
+local READY_CHECK_NOT_READY_TEXTURE = READY_CHECK_NOT_READY_TEXTURE;
+local READY_CHECK_WAITING_TEXTURE = READY_CHECK_WAITING_TEXTURE;
+
+local _, addon = ...;
+local baseColor = {0, 0, 0};
+local overlayColorDispel = {1, 0.5, 0, 0.5};
+local overlayColorCharm = {0.8, 0, 1, 0.5};
+local overlayColorAlert = {1, 0, 0, 0.5};
+local width = 80;
+
+function addon.RegisterEvents(frame)
+ frame:RegisterEvent("PLAYER_ENTERING_WORLD");
+ frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
+ frame:RegisterEvent("READY_CHECK");
+ frame:RegisterEvent("READY_CHECK_FINISHED");
+ frame:RegisterEvent("GROUP_ROSTER_UPDATE");
+ frame:RegisterEvent("RAID_TARGET_UPDATE");
+ if frame.unit == "focus" then frame:RegisterEvent("PLAYER_FOCUS_CHANGED") end
+end
+
+function addon.RegisterUnitEvents(frame)
+ -- events are taken from FrameXML/CompactUnitFrame.lua
+ local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
+ frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_NAME_UPDATE", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
+ frame:RegisterUnitEvent("PLAYER_FLAGS_CHANGED", frame.unit, displayed);
+ frame:RegisterUnitEvent("READY_CHECK_CONFIRM", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_ENTERED_VEHICLE", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_EXITED_VEHICLE", frame.unit, displayed);
+ frame:RegisterUnitEvent("UNIT_PET", frame.unit, displayed);
+end
+local registerUnitEvents = addon.RegisterUnitEvents;
+
+local function updateText(frame, unit)
+ if UnitIsDeadOrGhost(unit) then
+ frame.dead = true;
+ frame.text:SetText("Dead");
+ frame.text:Show();
+ elseif not UnitIsConnected(unit) then
+ frame.text:SetText("DC");
+ frame.text:Show();
+ elseif UnitIsAFK(unit) then
+ frame.text:SetText("afk");
+ frame.text:Show();
+ elseif UnitIsDND(unit) then
+ frame.text:SetText("dnd");
+ frame.text:Show();
+ else
+ frame.text:Hide();
+ end
+end
+
+local function updateMaxHealth(frame, unit)
+ frame.health.max = UnitHealthMax(unit);
+end
+
+local function updateHealth(frame, unit)
+ local current, max = UnitHealth(unit), frame.health.max;
+ if current > max or max <= 0 then
+ -- somehow current health has gone over the maximum (missed maxhealth event possibly)
+ -- just put health bar full and update max health for next event
+ frame.health:SetWidth(width);
+ frame.health.width = width;
+ updateMaxHealth(frame, unit);
+ frame.health:Show();
+ elseif current <= 0 or UnitIsDeadOrGhost(unit) then
+ frame.health:Hide();
+ return updateText(frame, unit); -- update death
+ else
+ local w = current/max*width;
+ frame.health:SetWidth(w);
+ frame.health.width = w;
+ frame.health:Show();
+ end
+
+ if frame.dead and current > 0 then
+ frame.dead = nil;
+ updateText(frame, unit); -- update revive
+ end
+end
+
+local function updateName(frame, unit)
+ local name = UnitName(unit);
+ if not name then return end
+ name = ssub(name, 1, 6);
+ if frame.unit == unit then
+ frame.name:SetText(name);
+ else
+ frame.name:SetFormattedText("-%s", name);
+ end
+
+ local _, class = UnitClass(unit);
+ local color = RAID_CLASS_COLORS[class];
+ if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
+end
+
+local function updateHealPred(frame, unit)
+ local incoming = UnitGetIncomingHeals(unit) or 0;
+ if incoming > 0 then
+ incoming = (incoming / frame.health.max) * width;
+ -- always at least 1 pixel space for heal prediction
+ frame.healpred:SetWidth(min(width - frame.health.width + 1, incoming));
+ frame.healpred:Show();
+ else
+ frame.healpred:Hide();
+ end
+end
+
+local function updateShield(frame, unit)
+ local shield = UnitGetTotalAbsorbs(unit) or 0;
+ if shield > 0 then
+ local space = width - frame.health.width;
+ shield = (shield / frame.health.max) * width;
+ if space == 0 then
+ frame.shield:Hide();
+ frame.shieldhl:Show();
+ elseif space < shield then
+ frame.shield:SetWidth(space);
+ frame.shield:Show();
+ frame.shieldhl:Show();
+ else
+ frame.shield:SetWidth(shield);
+ frame.shield:Show();
+ frame.shieldhl:Hide();
+ end
+ else
+ frame.shield:Hide();
+ frame.shieldhl:Hide();
+ end
+end
+
+local function updateHealAbsorb(frame, unit)
+ local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
+ if absorb > 0 then
+ absorb = (absorb / frame.health.max) * width;
+ frame.healabsorb:SetWidth(min(frame.health.width, absorb));
+ frame.healabsorb:Show();
+ else
+ frame.healabsorb:Hide();
+ end
+end
+
+local function updateAuras(frame, unit)
+ -- don't overlay charmed when in vehicle
+ if UnitIsCharmed(unit) and unit == frame.unit then
+ if frame.overlay.color ~= overlayColorCharm then
+ frame.overlay:SetVertexColor(unpack(overlayColorCharm));
+ frame.overlay.color = overlayColorCharm;
+ frame.overlay:Show();
+ end
+ elseif UnitDebuff(unit, 1, "RAID") ~= nil then
+ -- something dispellable
+ if frame.overlay.color ~= overlayColorDispel then
+ frame.overlay:SetVertexColor(unpack(overlayColorDispel));
+ frame.overlay.color = overlayColorDispel;
+ frame.overlay:Show();
+ end
+ else
+ if frame.overlay.color ~= nil then
+ frame.overlay.color = nil;
+ frame.overlay:Hide();
+ end
+ end
+end
+
+local function updateAggro(frame, unit)
+ local status = UnitThreatSituation(unit);
+ if status and status > 0 then
+ frame.base:SetVertexColor(GetThreatStatusColor(status));
+ else
+ frame.base:SetVertexColor(unpack(baseColor));
+ end
+end
+
+local function updateVehicle(frame)
+ local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and
+ UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
+ if shouldTargetVehicle then
+ if not frame.inVehicle then
+ frame.inVehicle = true;
+ frame.displayed = frame.vehicle;
+ registerUnitEvents(frame);
+ end
+ elseif frame.inVehicle then
+ frame.inVehicle = false;
+ frame.displayed = frame.unit;
+ registerUnitEvents(frame);
+ end
+end
+
+local function updateRole(frame, unit)
+ local role = UnitGroupRolesAssigned(unit);
+ if role == "HEALER" then
+ frame.role:SetTexCoord(0.75, 1, 0, 1);
+ frame.role:Show();
+ elseif role == "TANK" then
+ frame.role:SetTexCoord(0.5, 0.75, 0, 1);
+ frame.role:Show();
+ else
+ frame.role:Hide();
+ end
+end
+
+local function updateReadyCheck(frame, unit)
+ local status = GetReadyCheckStatus(unit);
+ if status == "ready" then
+ frame.ready:SetTexture(READY_CHECK_READY_TEXTURE);
+ frame.ready:Show()
+ elseif status == "notready" then
+ frame.ready:SetTexture(READY_CHECK_NOT_READY_TEXTURE);
+ frame.ready:Show()
+ elseif status == "waiting" then
+ frame.ready:SetTexture(READY_CHECK_WAITING_TEXTURE);
+ frame.ready:Show()
+ else
+ frame.ready:Hide()
+ end
+end
+
+local function updateRaidMarker(frame, unit)
+ local index = GetRaidTargetIndex(unit);
+ if index then
+ SetRaidTargetIconTexture(frame.targeticon, index);
+ frame.targeticon:Show();
+ else
+ frame.targeticon:Hide();
+ end
+end
+
+local eventFuncs = {
+ ["UNIT_HEALTH"] = function(frame)
+ updateHealth(frame, frame.displayed);
+ updateShield(frame, frame.displayed);
+ updateHealAbsorb(frame, frame.displayed);
+ -- no heal prediction update, that doesn't overflow too much
+ end,
+ ["UNIT_AURA"] = function(frame)
+ updateAuras(frame, frame.displayed);
+ end,
+ ["UNIT_HEAL_PREDICTION"] = function(frame)
+ updateHealPred(frame, frame.displayed);
+ end,
+ ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame)
+ updateShield(frame, frame.displayed);
+ end,
+ ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame)
+ updateHealAbsorb(frame, frame.displayed);
+ end,
+ ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame)
+ updateAggro(frame, frame.displayed);
+ end,
+ ["UNIT_MAXHEALTH"] = function(frame)
+ updateMaxHealth(frame, frame.displayed);
+ updateHealth(frame, frame.displayed);
+ updateShield(frame, frame.displayed);
+ updateHealAbsorb(frame, frame.displayed);
+ end,
+ ["UNIT_NAME_UPDATE"] = function(frame)
+ updateName(frame, frame.unit);
+ end,
+ ["UNIT_CONNECTION"] = function(frame)
+ updateText(frame, frame.displayed);
+ end,
+ ["PLAYER_ROLES_ASSIGNED"] = function(frame)
+ updateRole(frame, frame.unit);
+ end,
+ ["READY_CHECK"] = function(frame)
+ updateReadyCheck(frame, frame.unit);
+ end,
+ ["RAID_TARGET_UPDATE"] = function(frame)
+ updateRaidMarker(frame, frame.displayed);
+ end,
+ ["UPDATE_ALL_BARS"] = function(frame)
+ updateRole(frame, frame.unit);
+ updateVehicle(frame);
+ updateMaxHealth(frame, frame.displayed);
+ updateHealth(frame, frame.displayed);
+ updateText(frame, frame.displayed);
+ updateAuras(frame, frame.displayed);
+ updateShield(frame, frame.displayed);
+ updateHealPred(frame, frame.displayed);
+ updateHealAbsorb(frame, frame.displayed);
+ updateAggro(frame, frame.displayed);
+ updateName(frame, frame.unit);
+ updateReadyCheck(frame, frame.unit);
+ updateRaidMarker(frame, frame.displayed);
+ end,
+};
+eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
+eventFuncs["PLAYER_FLAGS_CHANGED"] = eventFuncs["UNIT_CONNECTION"];
+eventFuncs["READY_CHECK_CONFIRM"] = eventFuncs["READY_CHECK"];
+eventFuncs["READY_CHECK_FINISHED"] = eventFuncs["READY_CHECK"];
+eventFuncs["UNIT_ENTERED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
+eventFuncs["UNIT_EXITED_VEHICLE"] = eventFuncs["UPDATE_ALL_BARS"];
+eventFuncs["UNIT_PET"] = eventFuncs["UPDATE_ALL_BARS"];
+eventFuncs["GROUP_ROSTER_UPDATE"] = eventFuncs["UPDATE_ALL_BARS"];
+eventFuncs["PLAYER_ENTERING_WORLD"] = eventFuncs["UPDATE_ALL_BARS"];
+eventFuncs["PLAYER_FOCUS_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
+
+function addon.UnitEvent(self, event)
+ return eventFuncs[event](self);
+end