PageRenderTime 143ms CodeModel.GetById 117ms app.highlight 6ms RepoModel.GetById 14ms app.codeStats 0ms

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

http://github.com/Asphyxia/Tukui
Lua | 82 lines | 62 code | 20 blank | 0 comment | 9 complexity | 5980bb27c8e31cb9ac6959bdb88709e0 MD5 | raw file
 1local parent, ns = ...
 2local oUF = ns.oUF
 3
 4local ALTERNATE_POWER_INDEX = ALTERNATE_POWER_INDEX
 5
 6local UpdatePower = function(self, event, unit, powerType)
 7	if(self.unit ~= unit or powerType ~= 'ALTERNATE') then return end
 8
 9	local altpowerbar = self.AltPowerBar
10
11	if(altpowerbar.PreUpdate) then
12		altpowerbar:PreUpdate()
13	end
14
15	local barType, min = UnitAlternatePowerInfo(unit)
16	local cur = UnitPower(unit, ALTERNATE_POWER_INDEX)
17	local max = UnitPowerMax(unit, ALTERNATE_POWER_INDEX)
18
19	altpowerbar.barType = barType
20	altpowerbar:SetMinMaxValues(min, max)
21	altpowerbar:SetValue(cur)
22
23	if(altpowerbar.PostUpdate) then
24		return altpowerbar:PostUpdate(min, cur, max)
25	end
26end
27
28local ForceUpdate = function(element)
29	return UpdatePower(element.__owner, 'ForceUpdate', element.__owner.unit, 'ALTERNATE')
30end
31
32local Toggler = function(self, event, unit)
33	if(unit ~= self.unit) then return end
34	local altpowerbar = self.AltPowerBar
35
36	local barType, minPower, _, _, _, hideFromOthers = UnitAlternatePowerInfo(unit)
37	if(barType and (not hideFromOthers or unit == 'player' or self.realUnit == 'player')) then
38		self:RegisterEvent('UNIT_POWER', UpdatePower)
39		self:RegisterEvent('UNIT_MAXPOWER', UpdatePower)
40
41		ForceUpdate(altpowerbar)
42		altpowerbar:Show()
43	else
44		self:UnregisterEvent('UNIT_POWER', UpdatePower)
45		self:UnregisterEvent('UNIT_MAXPOWER', UpdatePower)
46
47		altpowerbar:Hide()
48	end
49end
50
51local Enable = function(self, unit)
52	local altpowerbar = self.AltPowerBar
53	if(altpowerbar) then
54		altpowerbar.__owner = self
55		altpowerbar.ForceUpdate = ForceUpdate
56
57		self:RegisterEvent('UNIT_POWER_BAR_SHOW', Toggler)
58		self:RegisterEvent('UNIT_POWER_BAR_HIDE', Toggler)
59
60		altpowerbar:Hide()
61
62		PlayerPowerBarAlt:UnregisterEvent'UNIT_POWER_BAR_SHOW'
63		PlayerPowerBarAlt:UnregisterEvent'UNIT_POWER_BAR_HIDE'
64		PlayerPowerBarAlt:UnregisterEvent'PLAYER_ENTERING_WORLD'
65
66		return true
67	end
68end
69
70local Disable = function(self, unit)
71	local altpowerbar = self.AltPowerBar
72	if(altpowerbar) then
73		self:UnregisterEvent('UNIT_POWER_BAR_SHOW', Toggler)
74		self:UnregisterEvent('UNIT_POWER_BAR_HIDE', Toggler)
75
76		PlayerPowerBarAlt:RegisterEvent'UNIT_POWER_BAR_SHOW'
77		PlayerPowerBarAlt:RegisterEvent'UNIT_POWER_BAR_HIDE'
78		PlayerPowerBarAlt:RegisterEvent'PLAYER_ENTERING_WORLD'
79	end
80end
81
82oUF:AddElement('AltPowerBar', Toggler, Enable, Disable)