4a66dd6 - Remove incoming res icon, slower periodic update rate
[wowui.git] / OmaRF / RaidFrame.lua
index cb2c3a2..ebadfef 100644 (file)
@@ -1,17 +1,21 @@
 -- RaidFrame.lua
 local _;
-local unpack, pairs, rawget = unpack, pairs, rawget;
+local unpack, pairs = unpack, pairs;
 local format = string.format;
-local UnitHealthMax, UnitPowerMax = UnitHealthMax, UnitPowerMax;
 local UnitInRange = UnitInRange;
+local InCombatLockdown = InCombatLockdown;
 local CreateFrame, RegisterStateDriver, RegisterUnitWatch = CreateFrame, RegisterStateDriver, RegisterUnitWatch;
+local CTimerAfter = C_Timer.After;
 local STANDARD_TEXT_FONT = STANDARD_TEXT_FONT;
+local GameTooltip = GameTooltip;
+local GameTooltip_SetDefaultAnchor = GameTooltip_SetDefaultAnchor;
 
 local registerEvents = OmaRFEvents.RegisterEvents;
+local registerUnitEvents = OmaRFEvents.RegisterUnitEvents;
 local unitEvent = OmaRFEvents.UnitEvent;
+local setupIndicators = OmaRFIndicators.SetupIndicators;
 
 local Settings = OmaRFSettings;
-local indSize = Settings.IndSize;
 local baseColor = Settings.BaseColor;
 local bgColor = Settings.BgColor;
 local healthColor = Settings.HealthColor;
@@ -19,86 +23,59 @@ local shieldColor = Settings.ShieldColor;
 local shieldhlColor = Settings.ShieldhlColor;
 local healpredColor = Settings.HealpredColor;
 local healabsorbColor = Settings.HealabsorbColor;
+local width, height = Settings.Width, Settings.Height;
 -- placeholders with visible values when error happens
-local positions = {};
-local width, height = 10, 10;
 local anchorX, anchorY = 10, 10;
 local attributes = {};
 
 local CFrame = CreateFrame("Frame", "OmaRFFrame", UIParent);
 local inheritedFrames = "SecureUnitButtonTemplate,SecureHandlerStateTemplate";
+local barTexture = "Interface\\AddOns\\OmaRF\\images\\minimalist";
+local class = nil;
 local party = {};
 local raid = {};
+local updaters = {};
 
 local M = {};
 OmaRaidFrame = M;
 
-local function setupIndicators(frame)
-    frame.inds = CreateFrame("Frame", nil, frame);
-    frame.inds:SetAllPoints();
-    frame.inds:Hide();
-    for _, pos in pairs(positions) do
-        frame.inds[pos] = frame.inds:CreateTexture(nil, "OVERLAY");
-        frame.inds[pos]:SetPoint(pos, frame.inds, pos);
-        frame.inds[pos]:SetWidth(indSize);
-        frame.inds[pos]:SetHeight(indSize);
-        frame.inds[pos]:SetTexture("Interface\\AddOns\\OmaRF\\images\\rhomb");
-        frame.inds[pos]:SetVertexColor(1, 0, 0);
-        frame.inds[pos]:Hide();
-        frame.inds[pos].text = frame.inds:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
-        frame.inds[pos].text:SetPoint("BOTTOMRIGHT", frame.inds[pos], "BOTTOMRIGHT");
-        frame.inds[pos].text:Hide();
-    end
-    frame.major = CreateFrame("Frame", nil, frame);
-    frame.major:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, -indSize + 4);
-    frame.major:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT");
-    for i = 1,3 do
-        frame.major[i] = frame.major:CreateTexture(nil, "OVERLAY");
-        if i == 1 then
-            frame.major[i]:SetPoint("TOPLEFT", frame.major, "TOPLEFT");
-        else
-            frame.major[i]:SetPoint("TOPLEFT", frame.major[i-1], "TOPRIGHT");
-        end
-        frame.major[i]:SetWidth(indSize*2);
-        frame.major[i]:SetHeight(indSize*2);
-        frame.major[i]:Hide();
-        frame.major[i].text = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
-        frame.major[i].text:SetPoint("BOTTOMRIGHT", frame.major[i], "BOTTOMRIGHT");
-        frame.major[i].text:Hide();
-        frame.major[i].stack = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
-        frame.major[i].stack:SetPoint("TOPLEFT", frame.major[i], "TOPLEFT");
-        frame.major[i].stack:Hide();
-    end
-end
-
-local function unitUpdate(self, elapsed)
+local function unitUpdate(frame)
     -- there's no in/out of range event, have to check each frame
     -- from FrameXML/CompactUnitFrame.lua
-    local inRange, checked = UnitInRange(self.displayed);
-    if checked and not inRange then
-        self:SetAlpha(0.55);
-    else
-        self:SetAlpha(1);
+    if frame.updating then
+        local inRange, checked = UnitInRange(frame.displayed);
+        if checked and not inRange then
+            frame:SetAlpha(0.55);
+        else
+            frame:SetAlpha(1);
+        end
+        CTimerAfter(0.20, updaters[frame]);
     end
 end
 
 local function frameShow(frame)
-    frame:RegisterEvent("PARTY_MEMBER_ENABLE");
-    frame:RegisterEvent("PARTY_MEMBER_DISABLE");
-    frame:RegisterEvent("UNIT_ENTERED_VEHICLE");
-    frame:RegisterEvent("UNIT_EXITED_VEHICLE");
-    frame:RegisterEvent("UNIT_PET");
-    frame:RegisterEvent("PLAYER_ROLES_ASSIGNED");
-    frame:RegisterEvent("GROUP_ROSTER_UPDATE");
-    frame:RegisterEvent("PLAYER_ENTERING_WORLD");
     registerEvents(frame);
-    frame:SetScript("OnUpdate", unitUpdate);
-    unitEvent(frame, "UPDATE_ALL_BARS", frame.displayed);
+    registerUnitEvents(frame);
+    frame.updating = true;
+    CTimerAfter(0.20, updaters[frame]);
+    unitEvent(frame, "UPDATE_ALL_BARS");
 end
 
 local function frameHide(frame)
     frame:UnregisterAllEvents();
-    frame:SetScript("OnUpdate", nil);
+    frame.updating = nil;
+end
+
+local function showTooltip(secure)
+    -- only show raid frame tooltips out of combat
+    if not InCombatLockdown() then
+        GameTooltip_SetDefaultAnchor(GameTooltip, secure);
+        GameTooltip:SetUnit(secure:GetAttribute("unit"));
+    end
+end
+
+local function hideTooltip(secure)
+    if GameTooltip:IsOwned(secure) then GameTooltip:FadeOut() end
 end
 
 local function setupFrame(frame, secure, unit)
@@ -108,11 +85,8 @@ local function setupFrame(frame, secure, unit)
     frame.displayed = unit;
     -- hide frame to get initial frameShow call
     frame:Hide();
-    if unit == "player" then
-        frame.vehicle = "vehicle";
-    else
-        frame.vehicle = unit.."pet";
-    end
+    if unit == "player" then frame.vehicle = "vehicle"
+    else frame.vehicle = unit.."pet" end
     -- create visuals
     secure:SetWidth(width+2);
     secure:SetHeight(height+2);
@@ -127,22 +101,20 @@ local function setupFrame(frame, secure, unit)
     frame.background:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1);
     frame.background:SetColorTexture(unpack(bgColor));
     frame.health = frame:CreateTexture(nil, "BORDER");
-    frame.health:SetTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill");
+    frame.health:SetTexture(barTexture);
     frame.health:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
-    frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
+    frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
     frame.health:SetVertexColor(unpack(healthColor));
-    frame.health:SetWidth(width);
-    frame.health.max = UnitHealthMax(unit);
     frame.mana = frame:CreateTexture(nil, "BORDER");
     frame.mana:SetPoint("TOPLEFT", frame.background, "BOTTOMLEFT", 0, 2);
     frame.mana:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
     frame.mana:SetColorTexture(1, 1, 1);
-    frame.mana:SetWidth(width);
-    frame.mana.max = UnitPowerMax(unit);
+    frame.mana:Hide();
     frame.shield = frame:CreateTexture(nil, "BORDER");
     frame.shield:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
     frame.shield:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
-    frame.shield:SetColorTexture(unpack(shieldColor));
+    frame.shield:SetTexture(barTexture);
+    frame.shield:SetVertexColor(unpack(shieldColor));
     frame.shield:Hide();
     frame.shieldhl = frame:CreateTexture(nil, "ARTWORK");
     frame.shieldhl:SetPoint("TOPLEFT", frame.background, "TOPRIGHT", -1, 0);
@@ -159,34 +131,42 @@ local function setupFrame(frame, secure, unit)
     frame.healabsorb:SetPoint("BOTTOMRIGHT", frame.health, "BOTTOMRIGHT");
     frame.healabsorb:SetColorTexture(unpack(healabsorbColor));
     frame.healabsorb:Hide();
-    frame.role = frame:CreateTexture(nil, "ARTWORK");
-    frame.role:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", -2, 2);
-    frame.role:SetPoint("TOPLEFT", frame.background, "BOTTOMRIGHT", -14, 14);
-    frame.role:SetTexCoord(0.75, 1, 0, 1);
-    frame.role:SetTexture("Interface\\LFGFRAME\\LFGROLE");
-    frame.role:Hide();
     frame.overlay = frame:CreateTexture(nil, "ARTWORK", nil, 1);
     frame.overlay:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
-    frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
+    frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT");
     frame.overlay:SetColorTexture(1, 1, 1);
     frame.overlay:Hide();
+    frame.role = frame:CreateTexture(nil, "ARTWORK", nil, 2);
+    frame.role:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", -2, 2);
+    frame.role:SetPoint("TOPLEFT", frame.background, "BOTTOMRIGHT", -14, 14);
+    frame.role:SetTexture("Interface\\LFGFRAME\\LFGROLE");
+    frame.role:Hide();
     frame.name = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
     frame.name:SetPoint("CENTER", frame.background, "CENTER", 0, 11);
     frame.text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
     frame.text:SetFont(STANDARD_TEXT_FONT, 13);
     frame.text:SetPoint("CENTER", frame.background, "CENTER", 0, -1);
     frame.text:Hide();
-    frame.rez = frame:CreateTexture(nil, "OVERLAY");
-    frame.rez:SetPoint("TOPLEFT", frame.background, "CENTER", -12, 12);
-    frame.rez:SetPoint("BOTTOMRIGHT", frame.background, "CENTER", 12, -12);
-    frame.rez:SetTexture("Interface\\RaidFrame\\Raid-Icon-Rez");
-    frame.rez:Hide();
+    frame.ready = frame:CreateTexture(nil, "OVERLAY");
+    frame.ready:SetPoint("TOPLEFT", frame.background, "BOTTOMLEFT", 0, 14);
+    frame.ready:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMLEFT", 14, 0);
+    frame.ready:Hide();
+    frame.targeticon = frame:CreateTexture(nil, "OVERLAY");
+    frame.targeticon:SetPoint("CENTER", frame.background, "TOP");
+    frame.targeticon:SetWidth(12);
+    frame.targeticon:SetHeight(12);
+    frame.targeticon:SetTexture("Interface\\TARGETINGFRAME\\UI-RaidTargetingIcons");
+    frame.targeticon:Hide();
     -- set up indicators
-    setupIndicators(frame);
+    setupIndicators(frame, class);
     -- set scripts
     frame:SetScript("OnShow", frameShow);
     frame:SetScript("OnHide", frameHide);
     frame:SetScript("OnEvent", unitEvent);
+    secure:SetScript("OnEnter", showTooltip);
+    secure:SetScript("OnLeave", hideTooltip);
+    -- set up periodic updates
+    updaters[frame] = function() return unitUpdate(frame) end
     -- set attributes
     secure:RegisterForClicks("AnyDown");
     for attr, val in pairs(attributes) do
@@ -283,30 +263,98 @@ local function initializeRaid(parent)
     end
 end
 
+local function initializeFocus(parent)
+    local secure = CreateFrame("Button", "OmaFocusSecure", parent, inheritedFrames);
+    local frame = CreateFrame("Frame", "OmaFocus", parent);
+    local unit = "focus";
+    secure:SetPoint("BOTTOMLEFT", parent, "TOPLEFT");
+    frame:SetPoint("BOTTOMLEFT", parent, "TOPLEFT");
+    setupFrame(frame, secure, unit);
+    RegisterUnitWatch(frame);
+    RegisterUnitWatch(secure);
+    local vehicle = format("[@%s,unithasvehicleui] vehicle; no", unit);
+    RegisterStateDriver(secure, "vehicleui", vehicle);
+    secure:SetAttribute("_onstate-vehicleui", vehicletoggle);
+end
+
 local function initialize()
+    _, class = UnitClass("player");
+    anchorX, anchorY = Settings.Character.AnchorX, Settings.Character.AnchorY;
+    attributes = Settings.Character.Clickheal;
+    CFrame:SetFrameStrata("LOW");
     CFrame:SetPoint("CENTER", nil, "CENTER", anchorX, anchorY);
     CFrame:SetHeight((height+2)*8);
-    CFrame:SetWidth((width+2)*5+1);
+    CFrame:SetWidth((width+2)*5);
     initializeParty(CFrame);
     initializeRaid(CFrame);
+    initializeFocus(CFrame);
 end
 
-local function loadCharSettings()
-    width, height = Settings.Character.Width, Settings.Character.Height;
-    anchorX, anchorY = Settings.Character.AnchorX, Settings.Character.AnchorY;
-    attributes = Settings.Character.Clickheal;
-    positions = Settings.Character.Positions;
+-- hiding Blizzard frames somewhat based on ShadowedUF
+local function hideBlizzardRaidButton()
+    if InCombatLockdown() then return end
+    local manager = CompactRaidFrameManager;
+    -- remove show and unlock buttons from manager
+    manager.displayFrame.hiddenModeToggle:Hide();
+    manager.displayFrame.lockedModeToggle:Hide();
+end
+
+local hidden = false;
+local function hideBlizzardRaid()
+    if hidden then return end
+    hidden = true;
+    local function hide()
+        if InCombatLockdown() then return end
+        CompactRaidFrameContainer:Hide();
+        local shown = CompactRaidFrameManager_GetSetting("IsShown");
+        if shown and shown ~= "0" then
+            CompactRaidFrameManager_SetSetting("IsShown", "0");
+        end
+    end
+
+    CompactRaidFrameContainer:UnregisterAllEvents();
+    -- there may still be frames created at startup getting events
+    for i=1,MAX_RAID_MEMBERS do
+        local frame = _G["CompactRaidFrame"..i];
+        if not frame then break end
+        frame:UnregisterAllEvents();
+    end
+    hooksecurefunc("CompactRaidFrameManager_UpdateShown", hide);
+    hide();
+    CompactRaidFrameContainer:HookScript("OnShow", hide);
+    hooksecurefunc("CompactRaidFrameManager_UpdateOptionsFlowContainer", hideBlizzardRaidButton);
+    hideBlizzardRaidButton();
+
+    -- hide focus frame
+    for _, frame in pairs({FocusFrame, FocusFrameToT, FocusFrameSpellBar}) do
+        frame:UnregisterAllEvents();
+        if frame.healthbar then frame.healthbar:UnregisterAllEvents() end
+        if frame.manabar then frame.manabar:UnregisterAllEvents() end
+        if frame.spellbar then frame.spellbar:UnregisterAllEvents() end
+        if frame.powerBarAlt then frame.powerBarAlt:UnregisterAllEvents() end
+        frame:Hide();
+    end
+end
+
+local function updateTooltipFuncs()
+    -- let MoveAnything hook these to anchor tooltip elsewhere
+    GameTooltip = _G["GameTooltip"];
+    GameTooltip_SetDefaultAnchor = _G["GameTooltip_SetDefaultAnchor"];
 end
 
-CFrame:RegisterEvent("ADDON_LOADED");
 CFrame:RegisterEvent("PLAYER_LOGIN");
-CFrame:SetScript("OnEvent", function(self, event)
-    if event == "PLAYER_LOGIN" then
-        initialize();
-    elseif event == "ADDON_LOADED" then
+CFrame:RegisterEvent("PLAYER_ENTERING_WORLD");
+CFrame:RegisterEvent("PLAYER_REGEN_ENABLED");
+CFrame:SetScript("OnEvent", function(self, event, addon)
+    if event == "PLAYER_REGEN_ENABLED" then
+        -- if CompactRaidFrameManager_UpdateOptionsFlowContainer was called in combat
+        -- couldn't hide the button
+        hideBlizzardRaidButton();
+    elseif event == "PLAYER_ENTERING_WORLD" then
+        CTimerAfter(0.01, updateTooltipFuncs);
+    elseif event == "PLAYER_LOGIN" then
         OmaRFLoadChar();
-        loadCharSettings();
-        OmaRFEvents.LoadChar();
-        OmaRFIndicators.LoadChar();
+        hideBlizzardRaid();
+        initialize();
     end
 end);