fdbd89d - Refactor OmaUF for more flexible frames
[wowui.git] / OmaUF / Events.lua
index cbc8b0a..fa8965a 100644 (file)
@@ -1,12 +1,10 @@
 -- Events.lua
--- TODO -- recheck these, pvp functions not added yet
 local _;
 local unpack = unpack;
 local ssub = string.sub;
 local min = math.min;
 local ceil = math.ceil;
 local UnitName, UnitClass, UnitExists = UnitName, UnitClass, UnitExists;
-local UnitDebuff, UnitIsCharmed, UnitIsFriend = UnitDebuff, UnitIsCharmed, UnitIsFriend;
 local UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType;
 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
@@ -14,8 +12,14 @@ local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreat
 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
 local UnitHasVehicleUI, UnitTargetsVehicleInRaidUI = UnitHasVehicleUI, UnitTargetsVehicleInRaidUI;
-local UnitGroupRolesAssigned = UnitGroupRolesAssigned;
 local UnitLevel, UnitClassification = UnitLevel, UnitClassification;
+local UnitAffectingCombat, IsResting = UnitAffectingCombat, IsResting;
+local UnitIsPVPFreeForAll, UnitIsPVP = UnitIsPVPFreeForAll, UnitIsPVP;
+local UnitFactionGroup, UnitIsMercenary = UnitFactionGroup, UnitIsMercenary;
+local UnitIsGroupLeader, UnitIsGroupAssistant = UnitIsGroupLeader, UnitIsGroupAssistant;
+local HasLFGRestrictions = HasLFGRestrictions;
+local UnitPlayerControlled, UnitIsPlayer = UnitPlayerControlled, UnitIsPlayer;
+local UnitIsTapDenied, UnitSelectionColor = UnitIsTapDenied, UnitSelectionColor;
 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
 
 local updateAuraFrames = OmaUFAuras.UpdateAuras;
@@ -27,7 +31,7 @@ local powerColors = Settings.PowerColors;
 
 local M = {};
 OmaUFEvents = M;
-function M.RegisterEvents(frame)
+function M.RegisterUnitEvents(frame)
     -- events are taken from FrameXML/CompactUnitFrame.lua
     -- TODO raid marker support,
     -- player flags support (/afk, /dnd)
@@ -49,42 +53,34 @@ function M.RegisterEvents(frame)
     frame:RegisterUnitEvent("UNIT_CONNECTION", frame.unit, displayed);
     frame:RegisterUnitEvent("UNIT_FACTION", frame.unit, displayed);
 end
-local registerEvents = M.RegisterEvents;
+local registerUnitEvents = M.RegisterUnitEvents;
+
+local function updateMaxHealth(frame, unit)
+    frame.health.max = UnitHealthMax(unit);
+end
 
 local function updateHealth(frame, unit)
     local current, max = UnitHealth(unit), frame.health.max;
-    frame.health:Show();
-    -- sanity check, occasionally UnitHealthMax gives zero
-    if current > max then
+    if current > max or max <= 0 then
         -- somehow current health has gone over the maximum (missed maxhealth event)
-        frame.health.max = UnitHealthMax(unit);
-        max = frame.health.max;
-        if current > max then
-            -- error state, still over maximum
-            frame.health:SetWidth(frame.width);
-            return;
-        end
-    elseif max > 0 then
-        frame.health:SetWidth(current/frame.health.max*frame.width);
-    else
         frame.health:SetWidth(frame.width);
-        return;
-    end
-
-    if UnitIsDeadOrGhost(unit) then
+        updateMaxHealth(frame, unit);
+        frame.health:Show();
+    elseif current <= 0 or UnitIsDeadOrGhost(unit) then
         frame.health:Hide();
+    else
+        frame.health:SetWidth(current/max*frame.width);
+        frame.health:Show();
     end
 end
 
 local function updateHealthText(frame, unit)
-    local current, max = UnitHealth(unit), frame.health.max;
     if UnitIsDeadOrGhost(unit) then
         frame.healthText:SetText("Dead");
-        frame.healthText:Show();
     elseif not UnitIsConnected(unit) then
         frame.healthText:SetText("DC");
-        frame.healthText:Show();
-    elseif max > 0 then
+    else
+        local current = UnitHealth(unit);
         if current > 1200000000 then -- 1.2B
             frame.healthText:SetFormattedText("%.1fB", current / 1000000000);
         elseif current > 1200000 then -- 1.2M
@@ -94,37 +90,23 @@ local function updateHealthText(frame, unit)
         else
             frame.healthText:SetFormattedText("%d", current)
         end
-        frame.healthText:Show();
-    else
-        frame.healthText:Hide();
     end
 end
 
-local function updateMaxHealth(frame, unit)
-    frame.health.max = UnitHealthMax(unit);
+local function updateMaxPower(frame, unit)
+    frame.mana.max = UnitPowerMax(unit);
 end
 
 local function updatePower(frame, unit)
     local current, max = UnitPower(unit), frame.mana.max;
-    -- sanity check, occasionally UnitPowerMax gives zero
-    if current == 0 then
+    if current <= 0 then
         frame.mana:Hide();
-        return;
-    elseif current > max then
-        frame.mana:Show();
-        frame.mana.max = UnitPowerMax(unit);
-        max = frame.mana.max;
-        if current > max then
-            -- error
-            frame.mana:SetWidth(frame.width);
-            return;
-        end
-    end
-    if max > 0 then
-        frame.mana:SetWidth(UnitPower(unit)/max*frame.width);
+    elseif current > max or max <= 0 then
+        frame.mana:SetWidth(frame.width);
+        updateMaxPower(frame, unit);
         frame.mana:Show();
     else
-        frame.mana:SetWidth(frame.width);
+        frame.mana:SetWidth(current/max*frame.width);
         frame.mana:Show();
     end
 end
@@ -132,7 +114,7 @@ end
 local function updatePowerText(frame, unit)
     local current, max = UnitPower(unit), frame.mana.max;
     if UnitIsDeadOrGhost(unit) or not UnitIsConnected(unit) then
-        frame.healthText:Hide();
+        frame.manaText:Hide();
     elseif max > 0 and current > 0 and current < max then
         frame.manaText:SetText(ceil(current/max*100));
         frame.manaText:Show();
@@ -141,16 +123,11 @@ local function updatePowerText(frame, unit)
     end
 end
 
-local function updateMaxPower(frame, unit)
-    frame.mana.max = UnitPowerMax(unit);
-end
-
 local function updatePowerColor(frame, unit)
     frame.mana:SetVertexColor(unpack(powerColors[UnitPowerType(unit)]));
 end
 
 local function updateName(frame, unit)
-    if not frame.name then return end
     local name = UnitName(unit);
     if not name then return end
     frame.name:SetText(ssub(name, 1, 10));
@@ -159,10 +136,9 @@ end
 local function updateHealPred(frame, unit)
     local incoming = UnitGetIncomingHeals(unit) or 0;
     if incoming > 0 then
-        local max = frame.health.max;
         local space = frame.width - frame.health:GetWidth() + 1;
-        local pred = (incoming / max) * frame.width;
-        frame.healpred:SetWidth(min(space, pred));
+        incoming = (incoming / frame.health.max) * frame.width;
+        frame.healpred:SetWidth(min(space, incoming));
         frame.healpred:Show();
     else
         frame.healpred:Hide();
@@ -172,9 +148,8 @@ end
 local function updateShield(frame, unit)
     local shield = UnitGetTotalAbsorbs(unit) or 0;
     if shield > 0 then
-        local max = frame.health.max;
         local space = frame.width - frame.health:GetWidth();
-        shield = (shield / max) * frame.width;
+        shield = (shield / frame.health.max) * frame.width;
         if space == 0 then
             frame.shield:Hide();
             frame.shieldhl:Show();
@@ -196,9 +171,8 @@ end
 local function updateHealAbsorb(frame, unit)
     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
     if absorb > 0 then
-        local max = frame.health.max;
         local space = frame.health:GetWidth();
-        absorb = (absorb / max) * frame.width;
+        absorb = (absorb / frame.health.max) * frame.width;
         frame.healabsorb:SetWidth(min(space, absorb));
         frame.healabsorb:Show();
     else
@@ -207,7 +181,7 @@ local function updateHealAbsorb(frame, unit)
 end
 
 local function updateAuras(frame, unit)
-    updateAuraFrames(frame, unit);
+    if frame.auras then updateAuraFrames(frame, unit) end
 end
 
 local function updateAggro(frame, unit)
@@ -220,32 +194,22 @@ local function updateAggro(frame, unit)
 end
 
 local function updateVehicle(frame)
-    local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
+    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;
-            registerEvents(frame);
+            registerUnitEvents(frame);
         end
     elseif frame.inVehicle then
         frame.inVehicle = false;
         frame.displayed = frame.unit;
-        registerEvents(frame);
-    end
-end
-
-local function updateRole(frame, unit)
-    local role = UnitGroupRolesAssigned(unit);
-    if role == "HEALER" or role == "TANK" or role == "DAMAGER" then
-        frame.role:SetTexCoord(GetTexCoordsForRoleSmallCircle(role));
-        frame.role:Show();
-    else
-        frame.role:Hide();
+        registerUnitEvents(frame);
     end
 end
 
 local function updateLevelText(frame, unit, levelup)
-    if not frame.level then return end
     if levelup then
         -- PLAYER_LEVEL_UP
         frame.level:SetText(levelup);
@@ -276,7 +240,7 @@ local function updateLevelText(frame, unit, levelup)
 end
 
 local function updateStatus(frame, unit)
-    -- coords from PlayerFrame
+    -- coords from FrameXML/PlayerFrame.lua
     if frame.inCombat or UnitAffectingCombat(unit) then
         frame.status:SetTexCoord(0.5, 1, 0, 0.484375);
         frame.status:Show();
@@ -295,7 +259,7 @@ local function updatePVP(frame, unit)
     elseif UnitIsPVP(unit) then
         local faction = UnitFactionGroup(unit);
         if faction and faction ~= "Neutral" then
-            -- from PlayerFrame, mercenary checks
+            -- from FrameXML/PlayerFrame.lua, mercenary checks
             if UnitIsMercenary(unit) then
                 if faction == "Horde" then
                     faction = "Alliance";
@@ -356,7 +320,6 @@ local eventFuncs = {
         updateHealthText(frame, frame.displayed);
         updateShield(frame, frame.displayed);
         updateHealAbsorb(frame, frame.displayed);
-        -- no heal prediction update, that doesn't overflow too much
     end,
     ["UNIT_POWER"] = function(frame)
         updatePower(frame, frame.displayed);
@@ -396,42 +359,41 @@ local eventFuncs = {
         updatePowerText(frame, frame.displayed);
     end,
     ["UNIT_NAME_UPDATE"] = function(frame)
-        updateName(frame, frame.displayed);
+        if frame.name then updateName(frame, frame.displayed) end
         updateHealthColor(frame, frame.displayed);
     end,
     ["UNIT_CONNECTION"] = function(frame)
         updateHealthText(frame, frame.displayed);
         updatePowerText(frame, frame.displayed);
     end,
-    ["PLAYER_ROLES_ASSIGNED"] = function(frame)
-        updateRole(frame, frame.unit);
-    end,
     ["UNIT_LEVEL"] = function(frame)
+        -- if this is registered, frame has frame.level
         updateLevelText(frame, frame.unit);
     end,
     ["PLAYER_LEVEL_UP"] = function(frame, arg1)
         updateLevelText(frame, frame.unit, arg1);
     end,
     ["PLAYER_UPDATE_RESTING"] = function(frame)
+        -- player frame has frame.status
         updateStatus(frame, frame.unit);
     end,
     ["PLAYER_REGEN_DISABLED"] = function(frame)
         frame.inCombat = true;
-        updateStatus(frame, frame.unit);
+        if frame.status then updateStatus(frame, frame.unit) end
     end,
     ["PLAYER_REGEN_ENABLED"] = function(frame)
         frame.inCombat = false;
-        updateStatus(frame, frame.unit);
+        if frame.status then updateStatus(frame, frame.unit) end
     end,
     ["UNIT_FACTION"] = function(frame)
-        updatePVP(frame, frame.unit);
+        if frame.pvp then updatePVP(frame, frame.unit) end
         updateHealthColor(frame, frame.displayed);
     end,
     ["PARTY_LEADER_CHANGED"] = function(frame)
         updateLeaderIcon(frame, frame.unit);
     end,
     ["UPDATE_ALL_BARS"] = function(frame)
-        updateVehicle(frame);
+        if frame.vehicle then updateVehicle(frame) end
         updateMaxHealth(frame, frame.displayed);
         updateMaxPower(frame, frame.displayed);
         updateHealth(frame, frame.displayed);
@@ -444,12 +406,11 @@ local eventFuncs = {
         updateHealAbsorb(frame, frame.displayed);
         updatePowerColor(frame, frame.displayed);
         updateAggro(frame, frame.displayed);
-        updateName(frame, frame.displayed);
-        updateRole(frame, frame.unit);
-        updateLevelText(frame, frame.unit);
-        updateStatus(frame, frame.unit);
-        updatePVP(frame, frame.unit);
-        updateLeaderIcon(frame, frame.unit);
+        if frame.name then updateName(frame, frame.displayed) end
+        if frame.level then updateLevelText(frame, frame.unit) end
+        if frame.status then updateStatus(frame, frame.unit) end
+        if frame.pvp then updatePVP(frame, frame.unit) end
+        if frame.leader then updateLeaderIcon(frame, frame.unit) end
         updateHealthColor(frame, frame.displayed);
     end,
 };