Quantcast

Clean up and update to 1.4 standard

Adrian L Lange [02-02-10 - 19:39]
Clean up and update to 1.4 standard
- Also updated to work with the new vehicle element
Filename
oUF_Experience.lua
diff --git a/oUF_Experience.lua b/oUF_Experience.lua
index d98b225..7503c4d 100644
--- a/oUF_Experience.lua
+++ b/oUF_Experience.lua
@@ -2,24 +2,24 @@

 	Elements handled:
 	 .Experience [statusbar]
+	 .Experience.Rested [statusbar] (optional, must be parented to Experience)
 	 .Experience.Text [fontstring] (optional)
-	 .Experience.Rested [statusbar] (optional)

 	Booleans:
-	 - Tooltip
+	 - noTooltip

 	Functions that can be overridden from within a layout:
-	 - PostUpdate(self, event, unit, bar, min, max)
-	 - OverrideText(bar, unit, min, max)
+	 - PostUpdate(element unit, min, max)

 --]]
+
 local _, ns = ...
 local oUF = ns.oUF or oUF
 assert(oUF, 'oUF Experience was unable to locate oUF install')

-local hunter = select(2, UnitClass('player')) == 'HUNTER'
+local hunterPlayer = select(2, UnitClass('player')) == 'HUNTER'

-local function xp(unit)
+local function GetXP(unit)
 	if(unit == 'pet') then
 		return GetPetExperience()
 	else
@@ -27,158 +27,120 @@ local function xp(unit)
 	end
 end

-local function tooltip(self)
+local function SetTooltip(self)
 	local unit = self:GetParent().unit
-	local min, max = xp(unit)
+	local min, max = GetXP(unit)
+
 	local bars = unit == 'pet' and 6 or 20

 	GameTooltip:SetOwner(self, 'ANCHOR_BOTTOMRIGHT', 5, -5)
-	GameTooltip:AddLine(format('XP: %d / %d (%d%% - %d bars)', min, max, min / max * 100, bars))
-	GameTooltip:AddLine(format('Left: %d (%d%% - %d bars)', max - min, (max - min) / max * 100, bars * (max - min) / max))
+	GameTooltip:AddLine(string.format('XP: %d / %d (%d%% - %d bars)', min, max, min/max * 100, bars))
+	GameTooltip:AddLine(string.format('Remaining: %d (%d%% - %d bars)', max - min, (max - min) / max * 100, bars * (max - min) / max))

-	if(self.exhaustion) then
-		GameTooltip:AddLine(format('|cff0090ffRested: +%d (%d%%)', self.exhaustion, self.exhaustion / max * 100))
+	if(self.rested) then
+		GameTooltip:AddLine(string.format('|cff0090ffRested: +%d (%d%%)', self.rested, self.rested / max * 100))
 	end

 	GameTooltip:Show()
 end

-local function update(self)
-	local bar, unit = self.Experience, self.unit
+local function Update(self, event, owner)
+	if(event == 'UNIT_PET' and owner ~= 'player') then return end

-	local exhaustion = GetXPExhaustion()
-	local min, max = xp(unit)
-	bar:SetMinMaxValues(0, max)
-	bar:SetValue(min)
-	bar:Show()
-
-	if(bar.Text) then
-		if(bar.OverrideText) then
-			bar:OverrideText(unit, min, max)
-		else
-			bar.Text:SetFormattedText('%d / %d', min, max)
+	local experience = self.Experience
+	-- Conditional hiding
+	if(self.unit == 'player') then
+		if(UnitLevel('player') == MAX_PLAYER_LEVEL) then
+			return experience:Hide()
 		end
-	end
-
-	if(bar.Rested) then
-		if(unit == 'player' and exhaustion and exhaustion > 0) then
-			bar.Rested:SetMinMaxValues(0, max)
-			bar.Rested:SetValue(math.min(min + exhaustion, max))
-			bar.exhaustion = exhaustion
-		else
-			bar.Rested:SetMinMaxValues(0, 1)
-			bar.Rested:SetValue(0)
-			bar.exhaustion = nil
+	elseif(self.unit == 'pet') then
+		local _, hunterPet = HasPetUI()
+		if(not self.disallowVehicleSwap and UnitHasVehicleUI('player')) then
+			return experience:Hide()
+		elseif(not hunterPet or (UnitLevel('pet') == UnitLevel('player'))) then
+			return experience:Hide()
 		end
+	else
+		return experience:Hide()
 	end

-	if(bar.PostUpdate) then
-		bar.PostUpdate(self, event, unit, bar, min, max)
-	end
-end
+	local unit = self.unit
+	local min, max = GetXP(unit)
+	experience:SetMinMaxValues(0, max)
+	experience:SetValue(min)
+	experience:Show()

-local function argcheck(self)
-	local bar = self.Experience
+	if(experience.Text) then
+		experience.Text:SetFormattedText('%d / %d', min, max)
+	end

-	if(self.unit == 'player') then
-		if(IsXPUserDisabled()) then
-			self:DisableElement('Experience')
-			self:RegisterEvent('ENABLE_XP_GAIN', function(self)
-				self:EnableElement('Experience')
-				self:UpdateElement('Experience')
-			end)
-		elseif(UnitLevel('player') == MAX_PLAYER_LEVEL) then
-			bar:Hide()
-		else
-			update(self)
-		end
-	elseif(self.unit == 'pet') then
-		if(not self.disallowVehicleSwap and UnitHasVehicleUI('player')) then
-			update(self)
-			bar:Hide()
-		elseif(UnitExists('pet') and UnitLevel('pet') ~= UnitLevel('player') and hunter) then
-			update(self)
+	if(experience.Rested) then
+		local rested = GetXPExhaustion()
+		if(unit == 'player' and rested and rested > 0) then
+			experience.Rested:SetMinMaxValues(0, max)
+			experience.Rested:SetValue(math.min(min + rested, max))
+			experience.rested = rested
 		else
-			bar:Hide()
+			experience.Rested:SetMinMaxValues(0, 1)
+			experience.Rested:SetValue(0)
+			experience.rested = nil
 		end
 	end
-end

-local function petcheck(self, event, unit)
-	if(unit == 'player') then
-		argcheck(self)
+	if(experience.PostUpdate) then
+		return experience:PostUpdate(unit, min, max)
 	end
 end

-local function enable(self, unit)
-	local bar = self.Experience
-
-	if(bar) then
-		if(not bar:GetStatusBarTexture()) then
-			bar:SetStatusBarTexture([=[Interface\TargetingFrame\UI-StatusBar]=])
-		end
+local function Enable(self, unit)
+	local experience = self.Experience
+	if(experience) then
+		local Update = experience.Update or Update

-		self:RegisterEvent('PLAYER_XP_UPDATE', argcheck)
-		self:RegisterEvent('PLAYER_LEVEL_UP', argcheck)
-		self:RegisterEvent('UNIT_PET', petcheck)
+		self:RegisterEvent('PLAYER_XP_UPDATE', Update)
+		self:RegisterEvent('PLAYER_LEVEL_UP', Update)
+		self:RegisterEvent('UNIT_PET', Update)

-		if(bar.Rested) then
-			self:RegisterEvent('UPDATE_EXHAUSTION', argcheck)
-			bar.Rested:SetFrameLevel(1)
+		if(experience.Rested) then
+			self:RegisterEvent('UPDATE_EXHAUSTION', Update)
+			experience.Rested:SetFrameLevel(1)
 		end

-		if(hunter) then
-			self:RegisterEvent('UNIT_PET_EXPERIENCE', argcheck)
+		if(hunterPlayer) then
+			self:RegisterEvent('UNIT_PET_EXPERIENCE', Update)
 		end

-		if(not self.disallowVehicleSwap) then
-			self:RegisterEvent('UNIT_ENTERED_VEHICLE', argcheck)
-			self:RegisterEvent('UNIT_EXITED_VEHICLE', argcheck)
+		if(not experience.noTooltip) then
+			experience:EnableMouse()
+			experience:HookScript('OnLeave', GameTooltip_Hide)
+			experience:HookScript('OnEnter', SetTooltip)
 		end

-		if(bar.Tooltip) then
-			bar:EnableMouse()
-			bar:HookScript('OnLeave', GameTooltip_Hide)
-			bar:HookScript('OnEnter', tooltip)
+		if(not experience:GetStatusBarTexture()) then
+			experience:SetStatusBarTexture([=[Interface\TargetingFrame\UI-StatusBar]=])
 		end

-		bar:HookScript('OnHide', function(self)
-			if(self.Rested) then
-				self.Rested:Hide()
-			end
-		end)
-
-		bar:HookScript('OnShow', function(self)
-			if(self.Rested) then
-				self.Rested:Show()
-			end
-		end)
-
 		return true
 	end
 end

-local function disable(self)
-	local bar = self.Experience
-	if(bar) then
-		bar:Hide()
-		self:UnregisterEvent('PLAYER_XP_UPDATE', argcheck)
-		self:UnregisterEvent('PLAYER_LEVEL_UP', argcheck)
-		self:UnregisterEvent('UNIT_PET', petcheck)
+local function Disable(self)
+	local experience = self.Experience
+	if(experience) then
+		local Update = experience.Update or Update

-		if(bar.Rested) then
-			self:UnregisterEvent('UPDATE_EXHAUSTION', argcheck)
-		end
+		self:UnregisterEvent('PLAYER_XP_UPDATE', Update)
+		self:UnregisterEvent('PLAYER_LEVEL_UP', Update)
+		self:UnregisterEvent('UNIT_PET', Update)

-		if(hunter) then
-			self:UnregisterEvent('UNIT_PET_EXPERIENCE', argcheck)
+		if(experience.Rested) then
+			self:UnregisterEvent('UPDATE_EXHAUSTION', Update)
 		end

-		if(not self.disallowVehicleSwap) then
-			self:UnregisterEvent('UNIT_ENTERED_VEHICLE', argcheck)
-			self:UnregisterEvent('UNIT_EXITED_VEHICLE', argcheck)
+		if(hunterPlayer) then
+			self:UnregisterEvent('UNIT_PET_EXPERIENCE', Update)
 		end
 	end
 end

-oUF:AddElement('Experience', argcheck, enable, disable)
+oUF:AddElement('Experience', Update, Enable, Disable)