cba0622 - Hide Blizzard main bar frames
[wowui.git] / OmaUF / Events.lua
index d68affe..8121ae6 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,15 @@ 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 GetRaidTargetIndex, SetRaidTargetIconTexture = GetRaidTargetIndex, SetRaidTargetIconTexture;
 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
 
 local updateAuraFrames = OmaUFAuras.UpdateAuras;
@@ -23,102 +28,95 @@ local updateAuraFrames = OmaUFAuras.UpdateAuras;
 local Settings = OmaUFSettings;
 local baseColor = Settings.BaseColor;
 local healthColor = Settings.HealthColor;
-local overlayColorDispel = Settings.OverlayColorDispel;
-local overlayColorCharm = Settings.OverlayColorCharm;
-local overlayColorAlert = Settings.OverlayColorAlert;
 local powerColors = Settings.PowerColors;
-local width = 10;
 
 local M = {};
 OmaUFEvents = M;
-function M.RegisterEvents(frame)
-    -- events are taken from FrameXML/CompactUnitFrame.lua
-    -- TODO raid marker support,
-    -- player flags support (/afk, /dnd)
+function M.RegisterUnitEvents(frame)
+    -- events are taken from FrameXML/CompactUnitFrame.lua and FrameXML/TargetFrame.lua
+    -- TODO player flags support (/afk, /dnd)
+    frame:RegisterEvent("RAID_TARGET_UPDATE"); -- have to register all and just check
     local displayed = frame.unit ~= frame.displayed and frame.displayed or nil;
     frame:RegisterUnitEvent("UNIT_HEALTH", frame.unit, displayed);
     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", frame.unit, displayed);
     frame:RegisterUnitEvent("UNIT_MAXHEALTH", frame.unit, displayed);
-    frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
-    frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
-    frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
+    if frame.mana then
+        frame:RegisterUnitEvent("UNIT_POWER", frame.unit, displayed);
+        frame:RegisterUnitEvent("UNIT_MAXPOWER", frame.unit, displayed);
+        frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", frame.unit, displayed);
+        frame:RegisterUnitEvent("UNIT_POWER_BAR_SHOW", frame.unit, displayed);
+        frame:RegisterUnitEvent("UNIT_POWER_BAR_HIDE", frame.unit, displayed);
+    end
+    if frame.shield then
+        frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
+    end
+    if frame.healabsorb then
+        frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", frame.unit, displayed);
+    end
+    if frame.auras then
+        frame:RegisterUnitEvent("UNIT_AURA", frame.unit, displayed);
+    end
     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("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(width);
-            return;
-        end
-    elseif max > 0 then
-        frame.health:SetWidth(current/frame.health.max*width);
-    else
-        frame.health:SetWidth(width);
-        return;
-    end
-
-    if UnitIsDeadOrGhost(unit) then
+        frame.health:SetWidth(frame.width);
+        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 and current < max then
-        frame.healthText:SetText(ceil(current/max*100));
-        frame.healthText:Show();
+    elseif frame.healthText.percent then
+        frame.healthText:SetFormattedText("%.1f", UnitHealth(unit)/frame.health.max*100);
     else
-        frame.healthText:Hide();
+        local current = UnitHealth(unit);
+        if current > 1000000000 then -- 1.0B
+            frame.healthText:SetFormattedText("%.2fB", current / 1000000000);
+        elseif current > 1000000 then -- 1.0M
+            frame.healthText:SetFormattedText("%.2fM", current / 1000000);
+        elseif current > 1000 then -- 1K
+            frame.healthText:SetFormattedText("%.1fK", current / 1000);
+        else
+            frame.healthText:SetFormattedText("%d", current)
+        end
     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(width);
-            return;
-        end
-    end
-    if max > 0 then
-        frame.mana:SetWidth(UnitPower(unit)/max*width);
+    elseif current > max or max <= 0 then
+        frame.mana:SetWidth(frame.width);
+        updateMaxPower(frame, unit);
         frame.mana:Show();
     else
-        frame.mana:SetWidth(width);
+        frame.mana:SetWidth(current/max*frame.width);
         frame.mana:Show();
     end
 end
@@ -126,7 +124,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();
@@ -135,10 +133,6 @@ 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
@@ -146,28 +140,14 @@ end
 local function updateName(frame, unit)
     local name = UnitName(unit);
     if not name then return end
-    frame.name:SetText(ssub(name, 1, 10));
-end
-
-local function updateHealPred(frame, unit)
-    local incoming = UnitGetIncomingHeals(unit) or 0;
-    if incoming > 0 then
-        local max = frame.health.max;
-        local space = width - frame.health:GetWidth() + 1;
-        local pred = (incoming / max) * width;
-        frame.healpred:SetWidth(min(space, pred));
-        frame.healpred:Show();
-    else
-        frame.healpred:Hide();
-    end
+    frame.name:SetText(ssub(name, 1, frame.name.count));
 end
 
 local function updateShield(frame, unit)
     local shield = UnitGetTotalAbsorbs(unit) or 0;
     if shield > 0 then
-        local max = frame.health.max;
-        local space = width - frame.health:GetWidth();
-        shield = (shield / max) * width;
+        local space = frame.width - frame.health:GetWidth();
+        shield = (shield / frame.health.max) * frame.width;
         if space == 0 then
             frame.shield:Hide();
             frame.shieldhl:Show();
@@ -189,9 +169,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) * width;
+        absorb = (absorb / frame.health.max) * frame.width;
         frame.healabsorb:SetWidth(min(space, absorb));
         frame.healabsorb:Show();
     else
@@ -200,27 +179,7 @@ local function updateHealAbsorb(frame, unit)
 end
 
 local function updateAuras(frame, unit)
-    updateAuraFrames(frame, unit);
-    if UnitIsFriend("player", unit) and UnitDebuff(unit, 1, "RAID") ~= nil then
-        -- something dispellable
-        if frame.overlay.color ~= overlayColorDispel then
-            frame.overlay:SetVertexColor(unpack(overlayColorDispel));
-            frame.overlay:Show();
-            frame.overlay.color = overlayColorDispel;
-        end
-    -- don't overlay charmed when in vehicle
-    elseif UnitIsCharmed(unit) and not frame.inVehicle then
-        if frame.overlay.color ~= overlayColorCharm then
-            frame.overlay:SetVertexColor(unpack(overlayColorCharm));
-            frame.overlay:Show();
-            frame.overlay.color = overlayColorCharm;
-        end
-    else
-        if frame.overlay.color ~= nil then
-            frame.overlay:Hide();
-            frame.overlay.color = nil;
-        end
-    end
+    if frame.auras then updateAuraFrames(frame, unit) end
 end
 
 local function updateAggro(frame, unit)
@@ -232,28 +191,20 @@ local function updateAggro(frame, unit)
     end
 end
 
+-- only works for player frame
 local function updateVehicle(frame)
-    local shouldTargetVehicle = UnitHasVehicleUI(frame.unit) and UnitTargetsVehicleInRaidUI(frame.unit) and UnitExists(frame.vehicle);
+    local shouldTargetVehicle = UnitHasVehicleUI("player") and
+        UnitTargetsVehicleInRaidUI("player") and UnitExists("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
 
@@ -288,7 +239,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();
@@ -307,7 +258,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";
@@ -362,13 +313,22 @@ local function updateHealthColor(frame, unit)
     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);
         updateHealthText(frame, frame.displayed);
-        updateShield(frame, frame.displayed);
-        updateHealAbsorb(frame, frame.displayed);
-        -- no heal prediction update, that doesn't overflow too much
+        if frame.shield then updateShield(frame, frame.displayed) end
+        if frame.healabsorb then updateHealAbsorb(frame, frame.displayed) end
     end,
     ["UNIT_POWER"] = function(frame)
         updatePower(frame, frame.displayed);
@@ -377,9 +337,6 @@ local eventFuncs = {
     ["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,
@@ -393,8 +350,8 @@ local eventFuncs = {
         updateMaxHealth(frame, frame.displayed);
         updateHealth(frame, frame.displayed);
         updateHealthText(frame, frame.displayed);
-        updateShield(frame, frame.displayed);
-        updateHealAbsorb(frame, frame.displayed);
+        if frame.shield then updateShield(frame, frame.displayed) end
+        if frame.healabsorb then updateHealAbsorb(frame, frame.displayed) end
     end,
     ["UNIT_MAXPOWER"] = function(frame)
         updateMaxPower(frame, frame.displayed);
@@ -403,77 +360,83 @@ local eventFuncs = {
     end,
     ["UNIT_DISPLAYPOWER"] = function(frame)
         updatePowerColor(frame, frame.displayed);
+        updateMaxPower(frame, frame.displayed);
+        updatePower(frame, frame.displayed);
+        updatePowerText(frame, frame.displayed);
     end,
     ["UNIT_NAME_UPDATE"] = function(frame)
-        updateName(frame, frame.displayed);
-        updateHealthColor(frame, frame.unit);
+        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);
-        updateHealthColor(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,
+    ["RAID_TARGET_UPDATE"] = function(frame)
+        updateRaidMarker(frame, frame.displayed);
+    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);
         updateHealthText(frame, frame.displayed);
-        updatePower(frame, frame.displayed);
-        updatePowerText(frame, frame.displayed);
-        updateAuras(frame, frame.displayed);
-        updateShield(frame, frame.displayed);
-        updateHealPred(frame, frame.displayed);
-        updateHealAbsorb(frame, frame.displayed);
-        updatePowerColor(frame, frame.displayed);
+        updateHealthColor(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);
-        updateHealthColor(frame, frame.unit);
+        updateRaidMarker(frame, frame.displayed);
+        if frame.mana then
+            updateMaxPower(frame, frame.displayed);
+            updatePower(frame, frame.displayed);
+            updatePowerText(frame, frame.displayed);
+            updatePowerColor(frame, frame.displayed);
+        end
+        if frame.auras then updateAuras(frame, frame.displayed) end
+        if frame.shield then updateShield(frame, frame.displayed) end
+        if frame.healabsorb then updateHealAbsorb(frame, frame.displayed) end
+        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
     end,
 };
 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
+eventFuncs["UNIT_POWER_BAR_SHOW"] = eventFuncs["UNIT_DISPLAYPOWER"];
+eventFuncs["UNIT_POWER_BAR_HIDE"] = eventFuncs["UNIT_DISPLAYPOWER"];
 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_TARGET_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
+eventFuncs["INSTANCE_ENCOUNTER_ENGAGE_UNIT"] = eventFuncs["UPDATE_ALL_BARS"];
+eventFuncs["UNIT_TARGETABLE_CHANGED"] = eventFuncs["UPDATE_ALL_BARS"];
 
 function M.UnitEvent(self, event, arg1)
     eventFuncs[event](self, arg1);
 end
-
-function M.LoadChar()
-    width = Settings.Character.Width;
-end