PageRenderTime 240ms CodeModel.GetById 100ms app.highlight 60ms RepoModel.GetById 72ms app.codeStats 0ms

/Tukui/modules/unitframes/core/oUF/elements/power.lua

http://github.com/Asphyxia/Tukui
Lua | 151 lines | 119 code | 29 blank | 3 comment | 26 complexity | de23f0924e0825f966ffda946c72466d MD5 | raw file
  1local WoW41 = select(4, GetBuildInfo()) == 40100
  2
  3local parent, ns = ...
  4local oUF = ns.oUF
  5
  6oUF.colors.power = {}
  7for power, color in next, PowerBarColor do
  8	if(type(power) == 'string') then
  9		oUF.colors.power[power] = {color.r, color.g, color.b}
 10	end
 11end
 12
 13local Update = function(self, event, unit)
 14	if(self.unit ~= unit) then return end
 15	local power = self.Power
 16
 17	if(power.PreUpdate) then power:PreUpdate(unit) end
 18
 19	local min, max = UnitPower(unit), UnitPowerMax(unit)
 20	local disconnected = not UnitIsConnected(unit)
 21	power:SetMinMaxValues(0, max)
 22
 23	if(disconnected) then
 24		power:SetValue(max)
 25	else
 26		power:SetValue(min)
 27	end
 28
 29	power.disconnected = disconnected
 30
 31	local r, g, b, t
 32	if(power.colorTapping and UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit)) then
 33		t = self.colors.tapped
 34	elseif(power.colorDisconnected and not UnitIsConnected(unit)) then
 35		t = self.colors.disconnected
 36	elseif(power.colorPower) then
 37		local ptype, ptoken, altR, altG, altB = UnitPowerType(unit)
 38
 39		t = self.colors.power[ptoken]
 40		if(not t and altR) then
 41			r, g, b = altR, altG, altB
 42		end
 43	elseif(power.colorClass and UnitIsPlayer(unit)) or
 44		(power.colorClassNPC and not UnitIsPlayer(unit)) or
 45		(power.colorClassPet and UnitPlayerControlled(unit) and not UnitIsPlayer(unit)) then
 46		local _, class = UnitClass(unit)
 47		t = self.colors.class[class]
 48	elseif(power.colorReaction and UnitReaction(unit, 'player')) then
 49		t = self.colors.reaction[UnitReaction(unit, "player")]
 50	elseif(power.colorSmooth) then
 51		r, g, b = self.ColorGradient(min / max, unpack(power.smoothGradient or self.colors.smooth))
 52	end
 53
 54	if(t) then
 55		r, g, b = t[1], t[2], t[3]
 56	end
 57
 58	if(b) then
 59		power:SetStatusBarColor(r, g, b)
 60
 61		local bg = power.bg
 62		if(bg) then
 63			local mu = bg.multiplier or 1
 64			bg:SetVertexColor(r * mu, g * mu, b * mu)
 65		end
 66	end
 67
 68	if(power.PostUpdate) then
 69		return power:PostUpdate(unit, min, max)
 70	end
 71end
 72
 73local Path = function(self, ...)
 74	return (self.Power.Override or Update) (self, ...)
 75end
 76
 77local UNIT_HAPPINESS = function(self, event, unit, powerType, ...)
 78	if(powerType == 'HAPPINESS') then
 79		return Path(self, event, unit, powerType, ...)
 80	end
 81end
 82
 83local ForceUpdate = function(element)
 84	return Path(element.__owner, 'ForceUpdate', element.__owner.unit)
 85end
 86
 87local OnPowerUpdate
 88do
 89	local UnitPower = UnitPower
 90	OnPowerUpdate = function(self)
 91		if(self.disconnected) then return end
 92		local unit = self.__owner.unit
 93		local power = UnitPower(unit)
 94
 95		if(power ~= self.min) then
 96			self.min = power
 97
 98			return Path(self.__owner, 'OnPowerUpdate', unit)
 99		end
100	end
101end
102
103local Enable = function(self, unit)
104	local power = self.Power
105	if(power) then
106		power.__owner = self
107		power.ForceUpdate = ForceUpdate
108
109		if(power.frequentUpdates and (unit == 'player' or unit == 'pet')) then
110			power:SetScript("OnUpdate", OnPowerUpdate)
111
112			-- Without this happiness won't really update corretly when using
113			-- frequentUpdates
114			if(unit == 'pet') then
115				power:RegisterEvent('UNIT_POWER', UNIT_HAPPINESS)
116			end
117		else
118			self:RegisterEvent('UNIT_POWER', Path)
119		end
120
121		self:RegisterEvent('UNIT_CONNECTION', Path)
122		self:RegisterEvent('UNIT_MAXPOWER', Path)
123
124		-- For tapping.
125		self:RegisterEvent('UNIT_FACTION', Path)
126
127		if(not power:GetStatusBarTexture()) then
128			power:SetStatusBarTexture[[Interface\TargetingFrame\UI-StatusBar]]
129		end
130
131		return true
132	end
133end
134
135local Disable = function(self)
136	local power = self.Power
137	if(power) then
138		if(power:GetScript'OnUpdate') then
139			power:SetScript("OnUpdate", nil)
140			self:UnregisterEvent('UNIT_POWER', UNIT_HAPPINESS)
141		else
142			self:UnregisterEvent('UNIT_POWER', Path)
143		end
144
145		self:UnregisterEvent('UNIT_CONNECTION', Path)
146		self:UnregisterEvent('UNIT_MAXPOWER', Path)
147		self:UnregisterEvent('UNIT_FACTION', Path)
148	end
149end
150
151oUF:AddElement('Power', Path, Enable, Disable)