All pastes #82681 Raw Edit

Industrial

public text v1 · immutable
·published 2006-07-08 20:44 UTC
rendered paste body
idUnitframes = AceLibrary('AceAddon-2.0'):new('AceDB-2.0', 'AceEvent-2.0', 'AceHook-2.0')
idUnitframes.Frames = {}

function idUnitframes:OnEnable()
    self.Constants = {
        Global = {
            Scale = 1,
            Alpha = 1
        },

        Background = {
            Padding = 5,
        },

        Bar = {
            Width = 150,
            Height = 15,
            Texture = 'Interface\\AddOns\\idUnitframes\\textures\\bar'
        },

        Healthbar = {
            Color = {
                r = 49/255,
                g = 227/255,
                b = 37/255
            }
        },

        Powerbar = {
            Color = {
                Druid   = {r = 49/255,  g = 37/255, b = 227/255},
                Hunter  = {r = 1.00,    g = 1.00,   b = 34/255},
                Mage    = {r = 49/255,  g = 37/255, b = 227/255},
                Paladin = {r = 49/255,  g = 37/255, b = 227/255},
                Priest  = {r = 49/255,  g = 37/255, b = 227/255},
                Rogue   = {r = 1.00,    g = 1.00,   b = 34/255},
                Shaman  = {r = 49/255,  g = 37/255, b = 227/255},
                Warlock = {r = 49/255,  g = 37/255, b = 227/255},
                Warrior = {r = 255/255, g = 0,      b = 0}
            }
        },

        Nametext = {
            Class = {
                Druid = 'Dru',
                Hunter = 'Hun',
                Mage = 'Mag',
                Paladin = 'Pal',
                Priest = 'Pri',
                Rogue = 'Rog',
                Shaman = 'Shm',
                Warlock = 'Wlk',
                Warrior = 'War'
            }
        },

        Buff = {
            Width = 40,
            Height = 40
        }
    }

    PlayerFrame:Hide()
    TargetFrame:Hide()
    PlayerFrame:SetScript('OnShow', function() PlayerFrame:Hide() end)
    TargetFrame:SetScript('OnShow', function() TargetFrame:Hide() end)

    self:createPlayerframe()
    self:createTargetframe()

    self:RegisterEvent('UNIT_HEALTH', function()
        if arg1 == 'player' then
            self:updateHealth('player')
        elseif arg1 == 'target' then
            self:updateHealth('target')
        end
    end)
    self:RegisterEvent('UNIT_MAX_HEALTH', function()
        if arg1 == 'player' then
            self:updateHealth('player')
        elseif arg1 == 'target' then
            self:updateHealth('target')
        end
    end)

    self:RegisterEvent('UNIT_MANA', function()
        if arg1 == 'player' then
            self:updatePower('player')
        elseif arg1 == 'target' then
            self:updatePower('target')
        end
    end)
    self:RegisterEvent('UNIT_MAXMANA', function()
        if arg1 == 'player' then
            self:updatePower('player')
        elseif arg1 == 'target' then
            self:updatePower('target')
        end
    end)
    self:RegisterEvent('UNIT_RAGE', function()
        if arg1 == 'player' then
            self:updatePower('player')
        elseif arg1 == 'target' then
            self:updatePower('target')
        end
    end)
    self:RegisterEvent('UNIT_MAXRAGE', function()
        if arg1 == 'player' then
            self:updatePower('player')
        elseif arg1 == 'target' then
            self:updatePower('target')
        end
    end)

    self:RegisterEvent('UNIT_AURA', function()
        if arg1 == 'player' then
            self:updateBuffs('player')
            self:updateDebuffs('player')
        elseif arg1 == 'target' then
            self:updateBuffs('target')
            self:updateDebuffs('target')
        end
    end)

    self:RegisterEvent('PLAYER_TARGET_CHANGED', function()
        if UnitExists('target') then
            self:updateHealth('target')
            self:updatePower('target')
            self.Frames.target.Healthbar.Background:SetVertexColor(self.Constants.Healthbar.Color.r/5, self.Constants.Healthbar.Color.g/5, self.Constants.Healthbar.Color.b/5, 1)
            self.Frames.target.Powerbar.Background:SetVertexColor(self.Constants.Powerbar.Color[UnitClass('target')].r/5, self.Constants.Powerbar.Color[UnitClass('target')].g/5, self.Constants.Powerbar.Color[UnitClass('target')].b/5, 1)
            self:updateBuffs('target')
            self:updateDebuffs('target')
            self:updateName('target')
            self.Frames.target:Show()
        else
            self.Frames.target:Hide()
        end
    end)

    self.Frames.player:SetPoint('BOTTOM', UIParent, 'BOTTOM', 0, 150)
    self.Frames.target:SetPoint('BOTTOM', self.Frames.player, 'TOP', 0, 1)

    self.Frames.player:Show()
end

function idUnitframes:OnDisable()
    self.Constants = nil

    PlayerFrame:Show()
    TargetFrame:Show()
    PlayerFrame:SetScript('OnShow', nil)
    TargetFrame:SetScript('OnShow', nil)
end

function idUnitframes:createPlayerframe()
    self.Frames.player = self:newFrame()
    self.Frames.player.unit = 'player'

    self:updateHealth('player')
    self:updatePower('player')
    self.Frames.player.Healthbar.Background:SetVertexColor(self.Constants.Healthbar.Color.r/5, self.Constants.Healthbar.Color.g/5, self.Constants.Healthbar.Color.b/5, 1)
    self.Frames.player.Powerbar.Background:SetVertexColor(self.Constants.Powerbar.Color[UnitClass('player')].r/5, self.Constants.Powerbar.Color[UnitClass('player')].g/5, self.Constants.Powerbar.Color[UnitClass('player')].b/5, 1)
    self:updateBuffs('player')
    self:updateDebuffs('player')
    self:updateName('player')
end

function idUnitframes:createTargetframe()
    self.Frames.target = self:newFrame()
    self.Frames.target.unit = 'target'
end

function idUnitframes:newFrame()
    local Font, _, _ = GameFontHighlightSmall:GetFont()
    local Background = CreateFrame('Button', name, UIParent)
    local Healthbar = CreateFrame('StatusBar', nil, Background)
    Healthbar.Background = Healthbar:CreateTexture(nil, 'BACKGROUND')
    local Powerbar = CreateFrame('StatusBar', nil, Background)
    Powerbar.Background = Powerbar:CreateTexture(nil, 'BACKGROUND')
    local Name = Background:CreateFontString(nil, 'OVERLAY')
    local Debuffs = CreateFrame('Frame', nil, Background)
    local Buffs = CreateFrame('Frame', nil, Background)

    Healthbar:SetWidth(self.Constants.Bar.Width)
    Healthbar:SetHeight(self.Constants.Bar.Height)
    Healthbar:SetScale(self.Constants.Global.Scale)
    Healthbar:SetAlpha(self.Constants.Global.Alpha)
    Healthbar:SetStatusBarTexture(self.Constants.Bar.Texture)
    Healthbar:SetStatusBarColor(self.Constants.Healthbar.Color.r, self.Constants.Healthbar.Color.g, self.Constants.Healthbar.Color.b)
    Healthbar.Background:SetTexture('Interface\\Tooltips\\UI-Tooltip-Background')
    Healthbar.Background:SetAllPoints(Healthbar)

    Powerbar:SetWidth(self.Constants.Bar.Width)
    Powerbar:SetHeight(self.Constants.Bar.Height)
    Powerbar:SetScale(self.Constants.Global.Scale)
    Powerbar:SetAlpha(self.Constants.Global.Alpha)
    Powerbar:SetStatusBarTexture(self.Constants.Bar.Texture)
    Powerbar.Background:SetTexture('Interface\\Tooltips\\UI-Tooltip-Background')
    Powerbar.Background:SetAllPoints(Powerbar)

    Background:SetWidth(self.Constants.Background.Padding*3 + self.Constants.Bar.Width + self.Constants.Bar.Width)
    Background:SetHeight(self.Constants.Background.Padding*2 + self.Constants.Bar.Height)
    Background:SetScale(self.Constants.Global.Scale)
    Background:SetAlpha(self.Constants.Global.Alpha)
    Background:SetBackdrop({
        bgFile = 'Interface\\Tooltips\\UI-Tooltip-Background',
        edgeFile = '',
        edgeSize = 0,
        tile = true,
        tileSize = 16,
        insets = {left = 0, right = 0, top = 0, bottom = 0}
    })
    Background:SetBackdropColor(0,0,0,0.6)

    Background:EnableMouse(true)
    --Background:SetMovable(true)
    --Background:RegisterForDrag('LeftButton')
    --Background:SetScript('OnDragStart', function() if IsAltKeyDown() then self.frames[u]:StartMoving() end end)
    --Background:SetScript('OnDragStop', function() self.frames[u]:StopMovingOrSizing() self:savePosition() end)
    Background:SetScript('OnEnter', function() UnitFrame_OnEnter() end)
    Background:SetScript('OnLeave', function() UnitFrame_OnLeave() end)
    Background:RegisterForClicks('LeftButtonUp','RightButtonUp','MiddleButtonUp','Button4Up','Button5Up')
    Background:SetScript('OnClick', self.OnClick)

    Name:SetFontObject(GameFontNormal)
    Name:SetFont(Font, 11)
    Name:SetHeight(Background:GetHeight())
    Name:SetWidth(Healthbar:GetWidth())
    Name:SetTextColor(1, 1, 1)
    Name:SetJustifyH('RIGHT')
    Name:SetJustifyV('CENTER')

    for i=1,16 do
        Debuffs[i] = CreateFrame('Button', nil, Debuffs)
        Debuffs[i].Icon = Debuffs[i]:CreateTexture(nil, 'BACKGROUND')
        Debuffs[i]:SetWidth(0) -- buffs fill in from the right
        Debuffs[i]:SetHeight(Background:GetHeight())
        Debuffs[i]:ClearAllPoints()
        if i>1 then Debuffs[i]:SetPoint('LEFT', Debuffs[i-1], 'RIGHT') end
        Debuffs[i].Icon:SetAllPoints(Debuffs[i])
        Debuffs[i]:SetScript('OnEnter', self.OnAuraEnter)
        Debuffs[i]:SetScript('OnLeave', function() GameTooltip:Hide() end)
        Debuffs[i]:Show()

        Buffs[i] = CreateFrame('Button', nil, Buffs)
        Buffs[i].Icon = Buffs[i]:CreateTexture(nil, 'BACKGROUND')
        Buffs[i]:SetWidth(0) -- buffs fill in from the right
        Buffs[i]:SetHeight(Background:GetHeight())
        Buffs[i]:ClearAllPoints()
        if i>1 then Buffs[i]:SetPoint('LEFT', Buffs[i-1], 'RIGHT') end
        Buffs[i].Icon:SetAllPoints(Buffs[i])
        Buffs[i]:SetScript('OnEnter', self.OnAuraEnter)
        Buffs[i]:SetScript('OnLeave', function() GameTooltip:Hide() end)
        Buffs[i]:Show()
    end
    Debuffs[1]:SetPoint('LEFT', Debuffs, 'LEFT')
    Buffs[1]:SetPoint('LEFT', Buffs, 'LEFT')

    Debuffs:SetHeight(Background:GetHeight())
    Debuffs:ClearAllPoints()
    Debuffs:SetPoint('LEFT', Background, 'RIGHT', self.Constants.Background.Padding, 0)

    Buffs:SetHeight(Background:GetHeight())
    Buffs:ClearAllPoints()
    Buffs:SetPoint('LEFT', Debuffs, 'RIGHT', self.Constants.Background.Padding, 0)

    Healthbar:SetPoint('TOPLEFT', Background, 'TOPLEFT', self.Constants.Background.Padding, -self.Constants.Background.Padding)
    Powerbar:SetPoint('LEFT', Healthbar, 'RIGHT', self.Constants.Background.Padding, 0)
    Name:SetPoint('RIGHT', Background, 'LEFT', -self.Constants.Background.Padding, 0)

    Background.Healthbar = Healthbar
    Background.Powerbar = Powerbar
    Background.Name = Name
    Background.Debuffs = Debuffs
    Background.Buffs = Buffs

    Background:Hide()
    return Background
end

function idUnitframes:updateHealth(Unit)
    if not UnitExists(Unit) then return end
    local Health = UnitHealth(Unit)
    local Maxhealth = UnitHealthMax(Unit)
    local r,g,b = self.Constants.Healthbar.Color.r, self.Constants.Healthbar.Color.g, self.Constants.Healthbar.Color.b

    self.Frames[Unit].Healthbar:SetStatusBarColor(r, g, b, 1)
    self.Frames[Unit].Healthbar:SetMinMaxValues(0, Maxhealth)
    self.Frames[Unit].Healthbar:SetValue(Health)
end

function idUnitframes:updatePower(Unit)
    if not UnitExists(Unit) then return end
    local Health = UnitMana(Unit)
    local Maxhealth = UnitManaMax(Unit)
    local r = self.Constants.Powerbar.Color[UnitClass(Unit)].r
    local g = self.Constants.Powerbar.Color[UnitClass(Unit)].g
    local b = self.Constants.Powerbar.Color[UnitClass(Unit)].b

    self.Frames[Unit].Powerbar:SetStatusBarColor(r, g, b, 1)
    self.Frames[Unit].Powerbar:SetMinMaxValues(0, Maxhealth)
    self.Frames[Unit].Powerbar:SetValue(Health)
end

function idUnitframes:updateBuffs(Unit)
    if not UnitExists(Unit) then return end
    local Buff
    local Buffslength = 0
    for i = 1, 16 do
        Buff = UnitBuff(Unit, i)
        if (Buff) then
            self.Frames[Unit].Buffs[i]:SetWidth(self.Frames[Unit]:GetHeight())
            self.Frames[Unit].Buffs[i].Icon:SetTexture(Buff)
            self.Frames[Unit].Buffs[i].index = i
            Buffslength = Buffslength + self.Frames[Unit].Buffs[i]:GetWidth()
        else
            self.Frames[Unit].Buffs[i].Icon:SetTexture(nil)
            self.Frames[Unit].Buffs[i].index = nil
            self.Frames[Unit].Buffs[i]:SetWidth(0)
        end

        Buff = nil
    end

    self.Frames[Unit].Buffs:SetWidth(self.Constants.Background.Padding+Buffslength)
end

function idUnitframes:updateDebuffs(Unit)
    if not UnitExists(Unit) then return end
    local Buff
    local Buffslength = 0
    for i = 1, 16 do
        Buff = UnitDebuff(Unit, i)
        if (Buff) then
            self.Frames[Unit].Debuffs[i]:SetWidth(self.Frames[Unit]:GetHeight())
            self.Frames[Unit].Debuffs[i].Icon:SetTexture(Buff)
            self.Frames[Unit].Debuffs[i].index = i
            Buffslength = Buffslength + self.Frames[Unit].Debuffs[i]:GetWidth()
        else
            self.Frames[Unit].Debuffs[i].Icon:SetTexture(nil)
            self.Frames[Unit].Debuffs[i].index = nil
            self.Frames[Unit].Debuffs[i]:SetWidth(0)
        end

        Buff = nil
    end

    if Buffslength > 0 then
        self.Frames[Unit].Buffs:ClearAllPoints()
        self.Frames[Unit].Buffs:SetPoint('LEFT', self.Frames[Unit].Debuffs, 'RIGHT', self.Constants.Background.Padding, 0)

        self.Frames[Unit].Debuffs:Show()
        self.Frames[Unit].Debuffs:SetWidth(Buffslength)
    else
        self.Frames[Unit].Buffs:ClearAllPoints()
        self.Frames[Unit].Buffs:SetPoint('LEFT', self.Frames[Unit], 'RIGHT', self.Constants.Background.Padding, 0)

        self.Frames[Unit].Debuffs:Hide()
    end
end

function idUnitframes:updateName(Unit)
    if not UnitExists(Unit) then return end
    self.Frames[Unit].Name:SetText(string.format('%s %s %s', UnitName(Unit), UnitLevel(Unit), self.Constants.Nametext.Class[UnitClass(Unit)]))
end

function idUnitframes:OnClick()
    local b,u = arg1, this.unit

    if not UnitExists(u) then return end
    if UnitIsUnit(u, 'player') then u = 'player' end

    if b == 'LeftButton' then
        if SpellIsTargeting() then
            SpellTargetUnit(u)
        elseif CursorHasItem() then
            if UnitIsUnit(u, 'player') then
                AutoEquipCursorItem()
            elseif UnitIsFriend(u, 'player') and UnitIsPlayer(u) then
                DropItemOnUnit('target')
            end
        else
            TargetUnit(u)
        end
    elseif b == 'RightButton' then
        u = oUF:strcap(u)
        if string.find(u,'Party') then
            ToggleDropDownMenu(1, nil, getglobal('PartyMemberFrame'..this:GetID()..'DropDown'), 'cursor', 0, 0)
        else
            ToggleDropDownMenu(1, nil, getglobal(u..'FrameDropDown'), 'cursor', 0, 0)
        end
    end
end

function idUnitframes:OnAuraEnter()
    local u, i = this:GetParent():GetParent().unit, this.id
    if not this:IsVisible() then return end

    GameTooltip:SetOwner(this, 'ANCHOR_BOTTOMRIGHT')

    local unit = this:GetParent().u

    if (this.isdebuff) then
        GameTooltip:SetUnitDebuff(u, i)
    else
        GameTooltip:SetUnitBuff(u, i)
    end
end