WoWInterface SVN Bigtank

[/] [trunk/] [Event.lua] - Rev 2

Compare with Previous | Blame | View Log

local frame = nil
local buffs = {}
local on_begins = {}
local on_updates = {}
local on_ends = {}
local on_power_update = nil
local event_handler = {}

local function Hide(frame)
    frame.Show = frame.Hide
    frame:Hide()
end

local function SetConfig()
    Hide(UIErrorsFrame)
    Hide(PlayerStatusGlow)
    Hide(PlayerStatusTexture)
    Hide(PlayerFrameFlash)
    Hide(PlayerHitIndicator)
    Hide(PlayerFrameMyHealPredictionBar)
    Hide(PlayerFrameOtherHealPredictionBar)
    Hide(PlayerFrameTotalAbsorbBar)
    Hide(PlayerFrameTotalAbsorbBarOverlay)
    Hide(PlayerFrameOverAbsorbGlow)
    Hide(PlayerFrameOverHealAbsorbGlow)
    Hide(PlayerFrameHealAbsorbBar)
    Hide(PlayerFrameHealAbsorbBarLeftShadow)
    Hide(PlayerFrameHealAbsorbBarRightShadow)
end

local function CheckBuff(spell_name)
    if UnitBuff("player", spell_name) then
        local value    = select(15, UnitBuff("player", spell_name))
        local duration = select(6,  UnitBuff("player", spell_name))
        local expire   = select(7,  UnitBuff("player", spell_name))
        if value == nil then
            value = 0
        end
        if buffs[spell_name] == 0 then
            if on_begins[spell_name] then
                on_begins[spell_name](spell_name, duration, expire, value)
            end
        elseif buffs[spell_name] ~= expire + duration + value then
            if on_updates[spell_name] then
                on_updates[spell_name](spell_name, duration, expire, value)
            end
        end
        buffs[spell_name] = expire + duration + value
    else
        if buffs[spell_name] ~= 0 then
            buffs[spell_name] = 0
            if on_ends[spell_name] then
                on_ends[spell_name](spell_name)
            end
        end
    end
end

event_handler["UNIT_AURA"] = function(event, ...)
    if ... == "player" then
        for spell_name, obj in pairs(buffs) do
            CheckBuff(spell_name)
        end
    end
end

event_handler["UNIT_DEFENSE"] = function(event, ...)
    for spell_name, obj in pairs(buffs) do
        CheckBuff(spell_name)
    end
end

event_handler["UNIT_POWER"] = function(event, ...)
    if ... == "player" then
        if on_power_update then
            on_power_update()
        end
    end
end

event_handler["PLAYER_ENTERING_WORLD"] = function(event, ...)
    frame:UnregisterEvent("PLAYER_ENTERING_WORLD")
    frame:RegisterEvent("UNIT_AURA")
    frame:RegisterEvent("UNIT_DEFENSE")
    frame:RegisterEvent("UNIT_POWER")
    SetConfig()
end

local function OnEvent(frame, event, ...)
    if event_handler[event] then
        event_handler[event](event, ...)
    end
end

frame = CreateFrame("Frame")
frame:SetScript("OnEvent", OnEvent)
frame:RegisterEvent("PLAYER_ENTERING_WORLD")

function BuffEvent(spell_name, on_begin, on_update, on_end)
    buffs[spell_name] = 0
    on_begins[spell_name] = on_begin
    on_updates[spell_name] = on_update
    on_ends[spell_name] = on_end
end

function PowerEvent(on_update)
    on_power_update = on_update
end

Compare with Previous | Blame