Quantcast

Added rested support

p3lim-52096 [05-20-09 - 16:47]
Added rested support
Added OverrideTooltip function
Rather big cleanup

git-svn-id: svn://svn.wowinterface.com/oUF_Experience-227/trunk@30 f81be506-7a38-4956-9b9f-d707db2888a5
Filename
oUF_Experience/oUF_Experience.lua
diff --git a/oUF_Experience/oUF_Experience.lua b/oUF_Experience/oUF_Experience.lua
index 418bc7e..c5ed755 100644
--- a/oUF_Experience/oUF_Experience.lua
+++ b/oUF_Experience/oUF_Experience.lua
@@ -3,6 +3,7 @@
 	Elements handled:
 	 .Experience [statusbar]
 	 .Experience.Text [fontstring] (optional)
+	 .Experience.Rested [statusbar] (optional)

 	Shared:
 	 - MouseOver [boolean]
@@ -10,26 +11,29 @@

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

 --]]
-local localized, class = UnitClass('player')

-local function Tooltip(self, unit, min, max, num)
-	if(self.MouseOver) then self:SetAlpha(1) end
+local function showTooltip(self, unit, min, max, bars)
+	if(self.MouseOver) then
+		self:SetAlpha(1)
+	end

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

-	if(unit == 'player' and GetXPExhaustion()) then
-		GameTooltip:AddLine(string.format('|cff0090ffRested: +%d (%.1f%%)', GetXPExhaustion(), GetXPExhaustion()/max*100))
+	local rested = GetXPExhaustion()
+	if(unit == 'player' and rested > 0) then
+		GameTooltip:AddLine(format('|cff0090ffRested: +%d (%.1f%%)', rested, rested / max * 100))
 	end

 	GameTooltip:Show()
 end

-local function GetXP(unit)
+local function getXP(unit)
 	if(unit == 'pet') then
 		return GetPetExperience()
 	else
@@ -37,77 +41,108 @@ local function GetXP(unit)
 	end
 end

-local function Update(self, event, unit)
-	if(event == 'UNIT_PET' and unit ~= 'player') then return end
+local function Update(self, event)
+	local bar, unit = self.Experience, self.unit
+
+	if(UnitLevel(unit) == MAX_PLAYER_LEVEL) then
+		self:DisableElement('Experience')
+
+		if(bar.Rested) then
+			bar.Rested:Hide()
+		end

-	local bar = self.Experience
-	if(self.unit == 'player' and UnitLevel('player') == MAX_PLAYER_LEVEL) then return bar:Hide() end
-	if(self.unit == 'pet' and class ~= 'HUNTER') then return bar:Hide() end
-	if(self.unit == 'pet' and UnitLevel('pet') >= UnitLevel('player')) then bar:Hide() end
+		return bar:Hide()
+	end

-	local min, max = GetXP(self.unit)
-	bar:SetMinMaxValues(0, max)
+	if(UnitLevel('pet') == 0 and unit == 'pet') then
+		bar:Hide()
+	else
+		bar:Show()
+	end
+
+	local min, max = getXP(unit)
+	bar:SetMinMaxValues(math.min(0, min), max)
 	bar:SetValue(min)
-	bar:Show()

 	if(bar.Text) then
 		if(bar.OverrideText) then
-			bar:OverrideText(min, max)
+			bar:OverrideText(unit, min, max)
 		else
 			bar.Text:SetFormattedText('%d / %d', min, max)
 		end
 	end

+	if(bar.Rested and unit == 'player') then
+		if(GetXPExhaustion() > 0) then
+			bar.Rested:SetMinMaxValues(min, max)
+			bar.Rested:SetValue(math.min(min + GetXPExhaustion(), max))
+		else
+			bar.Rested:SetMinMaxValues(0, 1)
+			bar.Rested:SetValue(0)
+		end
+	end
+
+	if(bar.PostUpdate) then
+		bar.PostUpdate(self, event, unit, bar, min, max)
+	end
+
 	if(bar.Tooltip) then
 		bar:SetScript('OnEnter', function()
-			Tooltip(bar, self.unit, min, max, self.unit == 'pet' and 6 or 20)
+			return (bar.OverrideTooltip or showTooltip) (bar, unit, min, max, unit == 'pet' and 6 or 20)
 		end)
 	end
-
-	if(bar.PostUpdate) then bar.PostUpdate(self, event, unit, bar, min, max) end
 end

 local function Enable(self, unit)
-	local experience = self.Experience
-	if(experience and (unit == 'pet' or unit == 'player')) then
-		self:RegisterEvent('PLAYER_XP_UPDATE', Update)
-		self:RegisterEvent('UNIT_PET', Update)
-
-		if(class == 'HUNTER') then
-			self:RegisterEvent('UNIT_PET_EXPERIENCE', Update)
+	local xp = self.Experience
+	if(xp) then
+		if(not xp:GetStatusBarTexture()) then
+			xp:SetStatusBarTexture([=[Interface\TargetingFrame\UI-StatusBar]=])
 		end

-		if(experience.MouseOver or experience.Tooltip) then
-			experience:EnableMouse()
+		if(unit == 'player') then
+			self:RegisterEvent('PLAYER_XP_UPDATE', Update)
+			self:RegisterEvent('PLAYER_LEVEL_UP', Update)
+
+			if(xp.Rested) then
+				self:RegisterEvent('UPDATE_EXHAUSTION', Update)
+			end
+		elseif(unit == 'pet' and select(2, UnitClass('player')) == 'HUNTER') then -- only called once so select is "ok"
+			self:RegisterEvent('UNIT_PET_EXPERIENCE', Update)
 		end

-		if(experience.Tooltip and experience.MouseOver) then
-			experience:SetAlpha(0)
-			experience:SetScript('OnLeave', function(self) self:SetAlpha(0); GameTooltip:Hide() end)
-		elseif(experience.MouseOver and not experience.Tooltip) then
-			experience:SetAlpha(0)
-			experience:SetScript('OnEnter', function(self) self:SetAlpha(1) end)
-			experience:SetScript('OnLeave', function(self) self:SetAlpha(0) end)
-		elseif(experience.Tooltip and not experience.MouseOver) then
-			experience:SetScript('OnLeave', function() GameTooltip:Hide() end)
+		if(xp.MouseOver or xp.Tooltip) then
+			xp:EnableMouse()
 		end

-		if(not experience:GetStatusBarTexture()) then
-			experience:SetStatusBarTexture([=[Interface\TargetingFrame\UI-StatusBar]=])
+		if(xp.MouseOver and xp.Tooltip) then
+			xp:SetAlpha(0)
+			xp:SetScript('OnLeave', function() xp:SetAlpha(0); GameTooltip:Hide() end)
+		elseif(xp.MouseOver and not xp.Tooltip) then
+			xp:SetAlpha(0)
+			xp:SetScript('OnEnter', function() xp:SetAlpha(1) end)
+			xp:SetScript('OnLeave', function() xp:SetAlpha(0) end)
+		elseif(not xp.MouseOver and xp.Tooltip) then
+			xp:SetScript('OnLeave', function() GameTooltip:Hide() end)
 		end

 		return true
-	end
+	end
 end

-local function Disable(self)
-	if(self.Experience) then
-		self:UnregisterEvent('PLAYER_XP_UPDATE', Update)
-		self:UnregisterEvent('UNIT_PET', Update)
-
-		if(class == 'HUNTER') then
+local function Disable(self, unit)
+	local xp = self.Experience
+	if(xp) then
+		if(unit == 'player') then
+			self:UnregisterEvent('PLAYER_XP_UPDATE', Update)
+			self:UnregisterEvent('PLAYER_LEVEL_UP', Update)
+
+			if(xp.Rested) then
+				self:UnregisterEvent('UPDATE_EXHAUSTION', Update)
+			end
+		elseif(unit == 'pet') then
 			self:UnregisterEvent('UNIT_PET_EXPERIENCE', Update)
-		end
+		end
 	end
 end