PageRenderTime 31ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Tukui/modules/unitframes/plugins/oUF_Experience/oUF_Experience.lua

http://github.com/Asphyxia/Tukui
Lua | 152 lines | 109 code | 28 blank | 15 comment | 29 complexity | 78917a1132a3fae6f95ac12228c8f717 MD5 | raw file
  1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
  2if C.unitframes.enable ~= true then return end
  3
  4local _, ns = ...
  5local oUF = ns.oUF or oUF
  6
  7if not oUF then return end
  8--[[
  9
 10	Elements handled:
 11	 .Experience [statusbar]
 12	 .Experience.Rested [statusbar] (optional, must be parented to Experience)
 13	 .Experience.Text [fontstring] (optional)
 14
 15	Booleans:
 16	 - noTooltip
 17
 18	Functions that can be overridden from within a layout:
 19	 - PostUpdate(element unit, min, max)
 20
 21--]]
 22
 23local _, ns = ...
 24local oUF = ns.oUF or oUF
 25assert(oUF, 'oUF Experience was unable to locate oUF install')
 26
 27local hunterPlayer = select(2, UnitClass('player')) == 'HUNTER'
 28
 29local function GetXP(unit)
 30	if(unit == 'pet') then
 31		return GetPetExperience()
 32	else
 33		return UnitXP(unit), UnitXPMax(unit)
 34	end
 35end
 36
 37local function SetTooltip(self)
 38	local unit = self:GetParent().unit
 39	local min, max = GetXP(unit)
 40
 41	local bars = unit == 'pet' and 6 or 20
 42
 43	GameTooltip:SetOwner(self, 'ANCHOR_BOTTOM', 0, -5)
 44	GameTooltip:AddLine(string.format(XP..": %d / %d (%d%% - %d/%d)", min, max, min/max * 100, bars - (bars * (max - min) / max), bars))
 45	GameTooltip:AddLine(string.format(LEVEL_ABBR..": %d (%d%% - %d/%d)", max - min, (max - min) / max * 100, 1 + bars * (max - min) / max, bars))
 46
 47	if(self.rested) then
 48		GameTooltip:AddLine(string.format("|cff0090ff"..TUTORIAL_TITLE26..": +%d (%d%%)", self.rested, self.rested / max * 100))
 49	end
 50
 51	GameTooltip:Show()
 52end
 53
 54local function Update(self, event, owner)
 55	if(event == 'UNIT_PET' and owner ~= 'player') then return end
 56
 57	local experience = self.Experience
 58	-- Conditional hiding
 59	if(self.unit == 'player') then
 60		if(UnitLevel('player') == MAX_PLAYER_LEVEL) then
 61			return experience:Hide()
 62		end
 63	elseif(self.unit == 'pet') then
 64		local _, hunterPet = HasPetUI()
 65		if(not self.disallowVehicleSwap and UnitHasVehicleUI('player')) then
 66			return experience:Hide()
 67		elseif(not hunterPet or (UnitLevel('pet') == UnitLevel('player'))) then
 68			return experience:Hide()
 69		end
 70	else
 71		return experience:Hide()
 72	end
 73
 74	local unit = self.unit
 75	local min, max = GetXP(unit)
 76	experience:SetMinMaxValues(0, max)
 77	experience:SetValue(min)
 78	experience:Show()
 79
 80	if(experience.Text) then
 81		experience.Text:SetFormattedText('%d / %d', min, max)
 82	end
 83
 84	if(experience.Rested) then
 85		local rested = GetXPExhaustion()
 86		if(unit == 'player' and rested and rested > 0) then
 87			experience.Rested:SetMinMaxValues(0, max)
 88			experience.Rested:SetValue(math.min(min + rested, max))
 89			experience.rested = rested
 90		else
 91			experience.Rested:SetMinMaxValues(0, 1)
 92			experience.Rested:SetValue(0)
 93			experience.rested = nil
 94		end
 95	end
 96
 97	if(experience.PostUpdate) then
 98		return experience:PostUpdate(unit, min, max)
 99	end
100end
101
102local function Enable(self, unit)
103	local experience = self.Experience
104	if(experience) then
105		local Update = experience.Update or Update
106
107		self:RegisterEvent('PLAYER_XP_UPDATE', Update)
108		self:RegisterEvent('PLAYER_LEVEL_UP', Update)
109		self:RegisterEvent('UNIT_PET', Update)
110
111		if(experience.Rested) then
112			self:RegisterEvent('UPDATE_EXHAUSTION', Update)
113		end
114
115		if(hunterPlayer) then
116			self:RegisterEvent('UNIT_PET_EXPERIENCE', Update)
117		end
118
119		if(not experience.noTooltip) then
120			experience:EnableMouse()
121			experience:HookScript('OnLeave', GameTooltip_Hide)
122			experience:HookScript('OnEnter', SetTooltip)
123		end
124
125		if(not experience:GetStatusBarTexture()) then
126			experience:SetStatusBarTexture([=[Interface\TargetingFrame\UI-StatusBar]=])
127		end
128
129		return true
130	end
131end
132
133local function Disable(self)
134	local experience = self.Experience
135	if(experience) then
136		local Update = experience.Update or Update
137
138		self:UnregisterEvent('PLAYER_XP_UPDATE', Update)
139		self:UnregisterEvent('PLAYER_LEVEL_UP', Update)
140		self:UnregisterEvent('UNIT_PET', Update)
141
142		if(experience.Rested) then
143			self:UnregisterEvent('UPDATE_EXHAUSTION', Update)
144		end
145
146		if(hunterPlayer) then
147			self:UnregisterEvent('UNIT_PET_EXPERIENCE', Update)
148		end
149	end
150end
151
152oUF:AddElement('Experience', Update, Enable, Disable)