Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 185 lines (151 sloc) 4.389 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
--[[

Elements handled:
.Experience [statusbar]
.Experience.Text [fontstring] (optional)
.Experience.Rested [statusbar] (optional)

Booleans:
- Tooltip

Functions that can be overridden from within a layout:
- PostUpdate(self, event, unit, bar, min, max)
- OverrideText(bar, 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 function xp(unit)
if(unit == 'pet') then
return GetPetExperience()
else
return UnitXP(unit), UnitXPMax(unit)
end
end

local function tooltip(self)
local unit = self:GetParent().unit
local min, max = xp(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))

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

GameTooltip:Show()
end

local function update(self)
local bar, unit = self.Experience, self.unit

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)
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
end
end

if(bar.PostUpdate) then
bar.PostUpdate(self, event, unit, bar, min, max)
end
end

local function argcheck(self)
local bar = self.Experience

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)
else
bar:Hide()
end
end
end

local function petcheck(self, event, unit)
if(unit == 'player') then
argcheck(self)
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

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

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

if(hunter) then
self:RegisterEvent('UNIT_PET_EXPERIENCE', argcheck)
end

if(not self.disallowVehicleSwap) then
self:RegisterEvent('UNIT_ENTERED_VEHICLE', argcheck)
self:RegisterEvent('UNIT_EXITED_VEHICLE', argcheck)
end

if(bar.Tooltip) then
bar:EnableMouse()
bar:HookScript('OnLeave', GameTooltip_Hide)
bar:HookScript('OnEnter', tooltip)
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)

if(bar.Rested) then
self:UnregisterEvent('UPDATE_EXHAUSTION', argcheck)
end

if(hunter) then
self:UnregisterEvent('UNIT_PET_EXPERIENCE', argcheck)
end

if(not self.disallowVehicleSwap) then
self:UnregisterEvent('UNIT_ENTERED_VEHICLE', argcheck)
self:UnregisterEvent('UNIT_EXITED_VEHICLE', argcheck)
end
end
end

oUF:AddElement('Experience', argcheck, enable, disable)
Something went wrong with that request. Please try again.