view KBF.lua @ 16:87131aeedc3f

more tooltip refactoring/fixes
author Chris Mellon <arkanes@gmai.com>
date Wed, 13 Oct 2010 12:29:17 -0500
parents 0c5e210221c9
children 233745d4774b
line wrap: on
line source
local _, kbf = ...

KBF = kbf -- make global for debugging

local kbf = LibStub("AceAddon-3.0"):NewAddon(kbf, "KBF", "AceEvent-3.0", "AceConsole-3.0")


function kbf:OnInitialize()
    self.debuffFrames = {}
    self.anchor = self:CreateAnchorFrame()
    self.anchor:Show()
    self:RegisterEvent("UNIT_AURA")
    -- set up the countdown timer
    -- TODO: Fancy enable/disable based on whether you have any timed buffs.
    -- Not a big deal, how often do you care about that
    -- also TODO: Maybe should bucket OnUpdates somehow
    self.anchor:SetScript("OnUpdate", function() self:OnUpdate() end)
    self.dirty = true -- force an immediate scan on login
    self:HideBlizzardBuffFrames()
end
-- naming convention
-- "frame" is the secure aura button created by the group handler
-- "bar" is the set of icon + status bars that we create to show the buff time

function kbf:HideBlizzardBuffFrames()
    local function HideBlizFrame(frame)
        if not frame then return end
        frame:UnregisterAllEvents()
        frame:SetScript("OnUpdate", nil)
        frame:Hide()
        frame.Show = function() end
    end
    HideBlizFrame(BuffFrame)
    HideBlizFrame(ConsolidatedBuffs)
    --HideBlizFrame(TemporaryEnchantFrame)
    
end

function kbf:OnUpdate()
    local unit = self.secureFrame:GetAttribute("unit")
    local buffCount = 0
    for idx=1,99 do
        local frame = self.secureFrame:GetAttribute("child"..idx)
        if not (frame and frame:IsShown()) then break end
        buffCount = buffCount + 1
        if self.dirty then 
            if self:BindBarToBuff(frame, unit) then break end
        end
        self:UpdateBarExpirationTime(frame)
        -- Don't forget to refresh shown tooltips
        if ( GameTooltip:IsOwned(frame) ) then
            self:OnEnter(frame)
        end
    end
    -- temporary enchants
    -- TODO: The blizz secure aura header binds both temp enchants
    -- to the main hand. No support for cancelling weapon enchants
    -- until this gets fixed up
    
    -- debuffs
    -- Since debuffs aren't cancellable, don't need to use the secure header
    -- for them. This could be rewritten to support useful features like
    -- sorting & scaling and stuff. Honestly, should at least be alphabetical.
    for idx=1,99 do
        local frame = self.debuffFrames[idx]
        if self.dirty then
            local name, rank, icon, stacks, debuffType, duration, expirationTime = UnitAura(unit, idx, "HARMFUL")
            if not name then 
                -- out of debuffs, hide all the rest of them
                for jdx = idx, 99 do
                    local bar = self.debuffFrames[jdx]
                    if bar then bar:Hide() else break end
                end
                break 
            end
            if not frame then
                frame = self:ConstructBar(nil, 1, 0, 0)
                self.debuffFrames[idx] = frame
            end
            self:SetBarAppearance(frame, name, icon, stacks, duration, expirationTime)
            frame:ClearAllPoints()
            -- position it under all the buffs, with a half-bar spacing
            frame:SetPoint("TOP", self.anchor, "BOTTOM", 0, (buffCount * -16) - 8)
            frame:Show()
            frame.filter = "HARMFUL"
            frame.unit = unit
            frame.index = idx
            frame:SetScript("OnEnter", function() kbf:OnEnter(frame) end)
            frame:SetScript("OnLeave", function() GameTooltip:Hide() end)
            buffCount = buffCount + 1
        else
            -- not dirty, so no frame means we're done
            if not frame then break end
        end
        self:UpdateBarExpirationTime(frame)
        if ( GameTooltip:IsOwned(frame) ) then
            self:OnEnter(frame)
        end
    end
    self.dirty = nil
end

function kbf:UNIT_AURA(event, unit)
    if unit ~= self.secureFrame:GetAttribute("unit") then return end
    self.dirty = true
end

function kbf:UpdateBarExpirationTime(frame)
    if frame.expirationTime then
        local remaining = frame.expirationTime - GetTime()
        remaining = math.max(0, remaining)
        local perc = remaining / frame.duration
        frame.timertext:SetText(self:FormatTimeText(remaining))
        frame.statusbar:SetValue(remaining)
    end
end

function kbf:BindBarToBuff(parentFrame, unit)
    local index = parentFrame:GetAttribute("index")
    local filter = parentFrame:GetAttribute("filter")
    local name, rank, icon, stacks, debuffType, duration, expirationTime, 
        unitCaster, isStealable, shouldConsolidate, spellId = UnitAura(unit, index, filter)
    if not name then return end
    if not parentFrame.icon then
        self:ConstructBar(parentFrame)
    end
    self:SetBarAppearance(parentFrame, name, icon, stacks, duration, expirationTime)
end

function kbf:SetBarAppearance(parentFrame, name, icon, stacks, duration, expirationTime)
    parentFrame.icon:SetNormalTexture(icon)
    if stacks and stacks > 0 then
        parentFrame.text:SetText(string.format("%s(%d)", name, stacks))
    else
        parentFrame.text:SetText(name)
    end
    parentFrame.timertext:SetText(self:FormatTimeText(duration))
    -- store duration information
    if duration and duration > 0 then
        parentFrame.expirationTime = expirationTime
        parentFrame.duration = duration
        parentFrame.statusbar:SetMinMaxValues(0, duration)
    else
        parentFrame.expirationTime = nil
        parentFrame.duration = 0
        parentFrame.statusbar:SetMinMaxValues(0,1)
        parentFrame.statusbar:SetValue(1)
    end
end

function kbf:FormatTimeText(time)
    if not time or time == 0 then return "" end
    local timetext
    local h = floor(time/3600)
    local m = time - (h*3600)
    m = floor(m/60)
    local s = time - ((h*3600) + (m*60))
    if h > 0 then
        timetext = ("%d:%02d"):format(h, m)
    elseif m > 0 then
        timetext = string.format("%d:%02d", m, floor(s))
    elseif s < 10 then
        timetext = string.format("%1.1f", s)
    else
        timetext = string.format("%.0f", floor(s))
    end
    return timetext
end

function KBF:OnEnter(button, motion)
    -- this is for the secure buttons, so use the attributes
    -- I'd like a better place to position this but it's funky for right now, handle it later
    local unit = button.unit or button:GetAttribute("unit")
    local filter = button.filter or button:GetAttribute("filter")
    local index = button:GetAttribute("index") or button.index
    GameTooltip:SetOwner(button, "ANCHOR_BOTTOMLEFT");
    GameTooltip:SetFrameLevel(button:GetFrameLevel() + 2);
    GameTooltip:SetUnitAura(unit, index, filter);
end

-- creates a icon + statusbar bar
function kbf:ConstructBar(frame, r, g, b)
    local texture = "Interface\\TargetingFrame\\UI-StatusBar"
    -- Because of secureframe suckiness, these height & width numbers
    -- have to be consistent with the stuff in KBF.xml
    local height = 16
    local width = 200 -- this is the width *without* the icon
    local font, _ style = GameFontHighlight:GetFont()
    local r = r or 0
    local g = g or 1
    local b = b or 0
    local bgcolor = {r, g, b, 0.5}
    local color = {r, g, b, 1}
    local fontsize = 11
    local timertextwidth = fontsize * 3.6
    local textcolor = {1, 1, 1, 1}
    local timertextcolor = {1, 1, 1, 1}
    if not frame then
        frame = CreateFrame("Button", nil, UIParent) -- the "top level" frame that represents the bar as a whole
        frame:SetHeight(16)
        frame:SetWidth(200 + 16)
    end
    local bar = frame
    bar.icon = CreateFrame("Button", nil, bar) -- the icon
    bar.statusbarbg = CreateFrame("StatusBar", nil, bar) -- the bars background
    bar.statusbar = CreateFrame("StatusBar", nil, bar) -- and the bars foreground
    bar.text = bar.statusbar:CreateFontString(nil, "OVERLAY") -- the label text
    bar.timertext = bar.statusbar:CreateFontString(nil, "OVERLAY") -- and the timer text

    -- the icon
    bar.icon:ClearAllPoints()
    bar.icon:SetPoint("LEFT", bar, "LEFT", 0, 0)
    -- icons are square
    bar.icon:SetWidth(height)
    bar.icon:SetHeight(height)
    --bar.icon:EnableMouse(false)
    -- the status bar background & foreground
    local function setupStatusBar(sb, color)
        sb:ClearAllPoints()
        sb:SetHeight(height)
        sb:SetWidth(width)
        -- offset the height of the frame on the x-axis for the icon.
        sb:SetPoint("TOPLEFT", bar, "TOPLEFT", height, 0)
        sb:SetStatusBarTexture(texture)
        sb:GetStatusBarTexture():SetVertTile(false)
        sb:GetStatusBarTexture():SetHorizTile(false)
        sb:SetStatusBarColor(unpack(color))
        sb:SetMinMaxValues(0,1)
        sb:SetValue(1)
    end
    setupStatusBar(bar.statusbarbg, bgcolor)
    setupStatusBar(bar.statusbar, color)
    bar.statusbarbg:SetFrameLevel(bar.statusbarbg:GetFrameLevel()-1) -- make sure the bg frame stays in the back
    -- timer text
    bar.timertext:SetFontObject(GameFontHighlight)
    bar.timertext:SetFont(GameFontHighlight:GetFont())
    bar.timertext:SetHeight(height)
    bar.timertext:SetWidth(timertextwidth)
    bar.timertext:SetPoint("LEFT", bar.statusbar, "LEFT", 0, 0)
    bar.timertext:SetJustifyH("RIGHT")
    bar.timertext:SetText("time")
    bar.timertext:SetTextColor(timertextcolor[1], timertextcolor[2], timertextcolor[3], timertextcolor[4])

    -- and the label text
    bar.text:SetFontObject(GameFontHighlight)
    bar.text:SetFont(GameFontHighlight:GetFont())
    bar.text:SetHeight(height)
    bar.text:SetWidth((width - timertextwidth) *.9)
    bar.text:SetPoint("RIGHT", bar.statusbar, "RIGHT", 0, 0)
    bar.text:SetJustifyH("LEFT")
    bar.text:SetText("text")
    bar.text:SetTextColor(textcolor[1], textcolor[2], textcolor[3], textcolor[4])

    bar:EnableMouse(true)
    return bar
end

function kbf:CreateAnchorFrame()
    -- give it a name so it'll remember its position
    local anchor = CreateFrame("FRAME", "KBFAnchorFrame", UIParent)
    anchor:SetClampedToScreen(true)
    anchor:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background", 
                         edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
                         tile = true, tileSize = 16, edgeSize = 16,
                         insets = { left = 4, right = 4, top = 4, bottom = 4 },
                         })
    local text = anchor:CreateFontString(nil, "OVERLAY") -- the label text
    text:SetFontObject(GameFontHighlight)
    text:SetFont(GameFontHighlight:GetFont())
    text:SetPoint("TOPLEFT", anchor, "TOPLEFT", 0, 0)
    text:SetPoint("BOTTOMRIGHT", anchor, "BOTTOMRIGHT", 0, 0)
    text:SetText("KBF ANCHOR")
    anchor:SetWidth(200 + 16 + 8)
    anchor:SetHeight(16 + 8)
    -- movability
    anchor:EnableMouse(true)
    anchor:SetMovable(true)
    anchor:RegisterForDrag("LeftButton")
    anchor:SetScript("OnDragStart", anchor.StartMoving)
    anchor:SetScript("OnDragStop",  anchor.StopMovingOrSizing)
    anchor:ClearAllPoints()
    anchor:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
    anchor:Hide()
    
    local frame = CreateFrame("FRAME", "KBFBuffFrame", anchor, "SecureAuraHeaderTemplate")
    --local frame = anchor
    frame:SetAttribute("filter", "HELPFUL")
    frame:SetAttribute("template", "KBFSecureUnitAuraTemplate")
    frame:SetAttribute("point", "TOP")
    frame:SetAttribute("wrapAfter", 100) -- required due to bugs in secure header
    frame:SetAttribute("consolidateTo", nil)
    frame:SetAttribute("xOffset", 0)
    frame:SetAttribute("yOffset", -16)
    frame:SetAttribute("minWidth",  216)
    frame:SetAttribute("minHeight", 16)
    frame:SetAttribute("unit", "player") -- TODO: figure out the vehicle swapping stuff
    frame:SetAttribute("sortMethod", "NAME")
    frame:SetAttribute("sortOrder", "-")
    -- TODO: SecureAuraHeader doesn't correcltly implement the temp enchants
    --frame:SetAttribute("weaponTemplate", "KBFSecureUnitAuraTemplate")
    --frame:SetAttribute("includeWeapons", 1)
    frame:SetPoint("TOP", anchor, "BOTTOM", 0, 0)
    frame:Show() -- has to be shown, otherwise the child frames don't show
    self.secureFrame = frame
    return anchor
end