Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 240 lines (195 sloc) 7.833 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
--[[ Element: Power Bar

Handles updating of `self.Power` based upon the units power.

Widget

Power - A StatusBar used to represent mana.

Sub-Widgets

.bg - A Texture which functions as a background. It will inherit the color of
the main StatusBar.

Notes

The default StatusBar texture will be applied if the UI widget doesn't have a
status bar texture or color defined.

Options

The following options are listed by priority. The first check that returns
true decides the color of the bar.

.colorTapping - Use `self.colors.tapping` to color the bar if the unit
isn't tapped by the player.
.colorDisconnected - Use `self.colors.disconnected` to color the bar if the
unit is offline.
.colorPower - Use `self.colors.power[token]` to color the bar based on
the unit's power type. This method will fall-back to
`:GetAlternativeColor()` if it can't find a color matching
the token. If this function isn't defined, then it will
attempt to color based upon the alternative power colors
returned by [UnitPowerType](http://wowprogramming.com/docs/api/UnitPowerType).
Finally, if these aren't defined, then it will attempt to
color the bar based upon `self.colors.power[type]`.
.colorClass - Use `self.colors.class[class]` to color the bar based on
unit class. `class` is defined by the second return of
[UnitClass](http://wowprogramming.com/docs/api/UnitClass).
.colorClassNPC - Use `self.colors.class[class]` to color the bar if the
unit is a NPC.
.colorClassPet - Use `self.colors.class[class]` to color the bar if the
unit is player controlled, but not a player.
.colorReaction - Use `self.colors.reaction[reaction]` to color the bar
based on the player's reaction towards the unit.
`reaction` is defined by the return value of
[UnitReaction](http://wowprogramming.com/docs/api/UnitReaction).
.colorSmooth - Use `self.colors.smooth` to color the bar with a smooth
gradient based on the player's current health percentage.

Sub-Widget Options

.multiplier - Defines a multiplier, which is used to tint the background based
on the main widgets R, G and B values. Defaults to 1 if not
present.

Examples

-- Position and size
local Power = CreateFrame("StatusBar", nil, self)
Power:SetHeight(20)
Power:SetPoint('BOTTOM')
Power:SetPoint('LEFT')
Power:SetPoint('RIGHT')
-- Add a background
local Background = Power:CreateTexture(nil, 'BACKGROUND')
Background:SetAllPoints(Power)
Background:SetTexture(1, 1, 1, .5)
-- Options
Power.frequentUpdates = true
Power.colorTapping = true
Power.colorDisconnected = true
Power.colorPower = true
Power.colorClass = true
Power.colorReaction = true
-- Make the background darker.
Background.multiplier = .5
-- Register it with oUF
self.Power = Power
self.Power.bg = Background

Hooks

Override(self) - Used to completely override the internal update function.
Removing the table key entry will make the element fall-back
to its internal function again.
]]

local parent, ns = ...
local oUF = ns.oUF

oUF.colors.power = {}
for power, color in next, PowerBarColor do
if (type(power) == "string") then
oUF.colors.power[power] = {color.r, color.g, color.b}
end
end

oUF.colors.power[0] = oUF.colors.power["MANA"]
oUF.colors.power[1] = oUF.colors.power["RAGE"]
oUF.colors.power[2] = oUF.colors.power["FOCUS"]
oUF.colors.power[3] = oUF.colors.power["ENERGY"]
oUF.colors.power[4] = oUF.colors.power["UNUSED"]
oUF.colors.power[5] = oUF.colors.power["RUNES"]
oUF.colors.power[6] = oUF.colors.power["RUNIC_POWER"]
oUF.colors.power[7] = oUF.colors.power["SOUL_SHARDS"]
oUF.colors.power[8] = oUF.colors.power["ECLIPSE"]
oUF.colors.power[9] = oUF.colors.power["HOLY_POWER"]

local GetDisplayPower = function(power, unit)
local _, _, _, _, _, _, showOnRaid = UnitAlternatePowerInfo(unit)
if(power.displayAltPower and showOnRaid) then
return ALTERNATE_POWER_INDEX
else
return (UnitPowerType(unit))
end
end

local Update = function(self, event, unit)
if(self.unit ~= unit) then return end
local power = self.Power

if(power.PreUpdate) then power:PreUpdate(unit) end

local displayType = GetDisplayPower(power, unit)
local min, max = UnitPower(unit, displayType), UnitPowerMax(unit, displayType)
local disconnected = not UnitIsConnected(unit)
power:SetMinMaxValues(0, max)

if(disconnected) then
power:SetValue(max)
else
power:SetValue(min)
end

power.disconnected = disconnected

local r, g, b, t
if(power.colorTapping and UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
t = self.colors.tapped
elseif(power.colorDisconnected and not UnitIsConnected(unit)) then
t = self.colors.disconnected
elseif(power.colorPower) then
local ptype, ptoken, altR, altG, altB = UnitPowerType(unit)

t = self.colors.power[ptoken]
if(not t) then
if(power.GetAlternativeColor) then
r, g, b = power:GetAlternativeColor(unit, ptype, ptoken, altR, altG, altB)
elseif(altR) then
r, g, b = altR, altG, altB
else
t = self.colors.power[ptype]
end
end
elseif(power.colorClass and UnitIsPlayer(unit)) or
(power.colorClassNPC and not UnitIsPlayer(unit)) or
(power.colorClassPet and UnitPlayerControlled(unit) and not UnitIsPlayer(unit)) then
local _, class = UnitClass(unit)
t = self.colors.class[class]
elseif(power.colorReaction and UnitReaction(unit, 'player')) then
t = self.colors.reaction[UnitReaction(unit, "player")]
elseif(power.colorSmooth) then
r, g, b = self.ColorGradient(min, max, unpack(power.smoothGradient or self.colors.smooth))
end

if(t) then
r, g, b = t[1], t[2], t[3]
end

if(b) then
power:SetStatusBarColor(r, g, b)

local bg = power.bg
if(bg) then
local mu = bg.multiplier or 1
bg:SetVertexColor(r * mu, g * mu, b * mu)
end
end

if(power.PostUpdate) then
return power:PostUpdate(unit, min, max)
end
end

local Path = function(self, ...)
return (self.Power.Override or Update) (self, ...)
end

local ForceUpdate = function(element)
return Path(element.__owner, 'ForceUpdate', element.__owner.unit)
end

local Enable = function(self, unit)
local power = self.Power
if(power) then
power.__owner = self
power.ForceUpdate = ForceUpdate

if(power.frequentUpdates and (unit == 'player' or unit == 'pet')) then
self:RegisterEvent('UNIT_POWER_FREQUENT', Path)
else
self:RegisterEvent('UNIT_POWER', Path)
end

self:RegisterEvent('UNIT_POWER_BAR_SHOW', Path)
self:RegisterEvent('UNIT_POWER_BAR_HIDE', Path)
self:RegisterEvent('UNIT_DISPLAYPOWER', Path)
self:RegisterEvent('UNIT_CONNECTION', Path)
self:RegisterEvent('UNIT_MAXPOWER', Path)

-- For tapping.
self:RegisterEvent('UNIT_FACTION', Path)

if(power:IsObjectType'StatusBar' and not power:GetStatusBarTexture()) then
power:SetStatusBarTexture[[Interface\TargetingFrame\UI-StatusBar]]
end

return true
end
end

local Disable = function(self)
local power = self.Power
if(power) then
self:UnregisterEvent('UNIT_POWER_FREQUENT', Path)
self:UnregisterEvent('UNIT_POWER', Path)
self:UnregisterEvent('UNIT_POWER_BAR_SHOW', Path)
self:UnregisterEvent('UNIT_POWER_BAR_HIDE', Path)
self:UnregisterEvent('UNIT_DISPLAYPOWER', Path)
self:UnregisterEvent('UNIT_CONNECTION', Path)
self:UnregisterEvent('UNIT_MAXPOWER', Path)
self:UnregisterEvent('UNIT_FACTION', Path)
end
end

oUF:AddElement('Power', Path, Enable, Disable)
Something went wrong with that request. Please try again.