Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local _G = _G
- local RAID_CLASS_COLORS = RAID_CLASS_COLORS
- local select = select
- local unpack = unpack
- local UnitPower = UnitPower
- local UnitPowerMax = UnitPowerMax
- local UnitHealth = UnitHealth
- local UnitHealthMax = UnitHealthMax
- local GREEN = { 0, 1, 0 }
- local RED = { 1, 0, 0 }
- local powerColors = {
- ["MANA"] = {0.1, 0.1, 0.98},
- --["MANA_BG"] = {0.0, 0.0, 0.2,},
- ["RAGE"] = {0.81, 0.01, 0.10},
- --["RAGE_BG"] = {0.2, 0, 0, 0.9},
- ["ENERGY"] = {0.90, 0.90, 0.0},
- --["ENERGY_BG"] = {0.2, 0.2, 0.0, 0.9},
- --["RUNIC_POWER"] = {0, 0.5 , 0.5 },
- --["RUNICPOWER_BG"] = {0.0, 0.16, 0.2, },
- }
- local unit_changed = {
- target = "PLAYER_TARGET_CHANGED",
- focus = "PLAYER_FOCUS_CHANGED",
- mouseover = "UPDATE_MOUSEOVER_UNIT",
- player = "",
- }
- local function frame_OnDragStart(self)
- if IsShiftKeyDown() and self:GetAttribute("drag") then
- self:StartMoving()
- end
- end
- local function frame_OnDragStop(self)
- self:StopMovingOrSizing()
- -- SavePosition(self) addons could save position here
- end
- local function UpdateStatus(self, event,...)
- local unit = ...
- if unit == self.unit then
- self:SetValue(self.status_current(self.unit) / self.status_max(self.unit)*100)
- local selfType = self.type
- if selfType == "Health" then
- self.text:SetText(UnitName(self.unit))
- elseif selfType == "Power" then
- self.text:SetText(self:GetValue())
- end
- end
- end
- local cache = {}
- local function UpdatePowerType(self)
- local integer, str = UnitPowerType(self.unit)
- self:UnregisterAllEvents()
- if str then
- local event = cache[str]
- if not event then
- event = "UNIT_"..str
- cache[str] = event
- end
- self:RegisterEvent(event)
- self:SetStatusBarColor(unpack(powerColors[str]))
- end
- end
- local function OnShow(self)
- local selfType = self.type
- if selfType == "Health" then
- self.text:SetText(UnitName(self.unit))
- elseif selfType == "Power" then
- self.text:SetText(self:GetValue())
- end
- if selfType == "Power" then
- UpdatePowerType(self)
- elseif selfType == "Health" then
- if self.classcolor then
- local _, CLASS = UnitClass(self.unit)
- if CLASS then
- local c = RAID_CLASS_COLORS[CLASS]
- self:SetStatusBarColor(c.r, c.g, c.b)
- else
- local color = UnitIsFriend(self.unit, "player") and GREEN or RED
- self:SetStatusBarColor(unpack(color))
- end
- else
- local color = UnitIsFriend(self.unit, "player") and GREEN or RED
- self:SetStatusBarColor(unpack(color))
- end
- end
- end
- local function OnUnitChanged(self, event, ...)
- OnShow(self.statusbar)
- UpdateStatus(self.statusbar,"", self.unit)
- end
- local function SetBorderColor(self, ...)
- self.statusbar.border:SetBackdropBorderColor(...)
- end
- local created_frames = {}
- function GetStatusbar(name, unit, statusbar_type)
- local event = statusbar_type=="Power" and "UNIT_POWER" or "UNIT_HEALTH"
- local f = CreateFrame("Button",name and "SimpleBars_"..name or nil,UIParent, "SecureUnitButtonTemplate,SecureHandlerDoubleClickTemplate") -- enable cool features
- -- cache frame for later use
- created_frames[f] = true
- f:SetSize(150,20)
- f.unit = unit
- f:SetPoint("CENTER",UIParent)
- f.statusbar = CreateFrame("Statusbar",nil,f)
- f.statusbar.type = statusbar_type
- f.statusbar.status_current = _G["Unit"..statusbar_type]
- f.statusbar.status_max = _G["Unit"..statusbar_type.."Max"]
- f.statusbar.unit = unit
- f.statusbar:SetMinMaxValues(0,100)
- f.statusbar:SetPoint("CENTER",f)
- f.statusbar:SetSize(150,20)
- f.statusbar:SetValue(100)
- f.statusbar:SetStatusBarColor(0.4, 0.4,0.4) -- pick some random color
- --f.statusbar:Lower()
- -- Statusbar texture
- f.statusbar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
- -- f.statusbarrame Border
- f.statusbar.border = CreateFrame("Frame", nil, f.statusbar)
- f.statusbar.border:SetBackdrop({
- bgFile=nil,
- edgeFile="Interface\\ChatFrame\\ChatFrameBackground",
- tile=true,
- tileSize=5,
- edgeSize= 2, -- border size
- })
- --f.statusbar.border:SetBackdropColor(0,1,0)
- f.statusbar.border:SetBackdropBorderColor(0,0,0)
- f.statusbar.border:SetAllPoints(f.statusbar)
- f.statusbar.text = f.statusbar:CreateFontString(nil,"ARTWORK")
- f.statusbar.text:SetFont(STANDARD_TEXT_FONT,12,"OUTLINE")
- f.statusbar.text:SetPoint("CENTER", f.statusbar)
- f.statusbar.text:SetText("")
- f.statusbar.text:SetTextColor(1, 1, 1)
- -- click support
- f:SetAttribute("unit", unit)
- f:SetAttribute("type1", "target")
- f:SetAttribute("type2", "focus")
- f:RegisterForClicks("AnyDown", "AnyUp")
- -- Enable Drag on double click
- f:SetAttribute("drag", false)
- f:SetAttribute("_ondoubleclick", [=[
- GREEN = GREEN or newtable(0,1,0)
- NORMAL = NORMAL or newtable(0,0,0)
- local enabled = not self:GetAttribute("drag")
- self:SetAttribute("drag",enabled)
- local newcolor = enabled and GREEN or NORMAL
- control:CallMethod("SetBorderColor", unpack(newcolor))
- ]=]);
- -- Handle updating statusbar with whatever
- f.statusbar:SetScript("OnEvent", UpdateStatus)
- f.statusbar:RegisterEvent(event)
- --f:SetScript("OnAttributeChanged", function(self, attribute, arg)
- --print("Attribute:", attribute, arg)
- --end)
- -- Handle dragging
- f:SetMovable(true)
- f:EnableMouse(true)
- f:RegisterForDrag("LeftButton")
- f:SetScript("OnDragStart", frame_OnDragStart)
- f:SetScript("OnDragStop", frame_OnDragStop)
- -- show / hide bar if unit exists
- RegisterUnitWatch(f) -- show / hide bar if unit exists
- -- Update other stuff on show, such as name if unit changes
- f.statusbar:SetScript("OnShow", OnShow)
- -- Handle unit changing
- f:RegisterEvent(unit_changed[unit] or "")
- f:SetScript("OnEvent", OnUnitChanged)
- f:Show()
- f.statusbar:Show()
- if UnitExists(f.unit) then
- UpdateStatus(f.statusbar, "", f.unit)
- end
- -- install some methods, they can be called from secure click handler
- f.SetBorderColor = SetBorderColor
- return f
- end
- local targethp = GetStatusbar("targethp","target", "Health") -- or GetStatusbar("player", "Health")
- targethp.statusbar.classcolor = true;
- local tpower = GetStatusbar("targetpower", "target", "Power") -- or GetStatusbar("player", "Health")
- -- update changes
- targethp:Hide()
- targethp:Show()
- local hp = GetStatusbar("playerhp", "player", "Health")
- hp.classcolor = true
- hp:Hide();hp:Show()
- local power = GetStatusbar("playerpower", "player", "Power")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement