Files
ShaguTweaks-ClassicAPI/mods/xpbar-text.lua
T
OEVGandClaude Opus 5.5 a482808743 Add Hide Pet Unit Frame and show XP numbers on the XP bar
Hide Pet Unit Frame (Unit Frames, off by default) parents PetFrame to a
frame that is never shown. PetFrame_Update shows the frame again on
every pet change, so a plain Hide() would not last; with the hidden
parent the stock pet code runs unchanged and nothing draws.

XP Bar Text shows current/needed XP like the reputation bar
("1555/3000  51%"), followed by the rested bonus as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019W3rwusM4i1CYaHQAYixJ7
2026-11-02 01:25:13 +00:00

102 lines
3.2 KiB
Lua

local T = ShaguTweaks.T
local module = ShaguTweaks:register({
title = T["XP Bar Text"],
description = T["Shows current and needed XP, percentage and rested bonus while hovering over the experience bar."],
expansions = { ["vanilla"] = true },
category = T["Interface"],
enabled = true,
order = 70,
})
module.enable = function(self)
-- Reuse the overlay if enable() is called again instead of stacking another
-- frame and another event handler on top of the experience bar.
if not self.exp then
self.exp = CreateFrame("Frame", nil, UIParent)
self.exp:SetAllPoints(MainMenuExpBar)
self.exp:SetFrameStrata("HIGH")
self.exp.text = self.exp:CreateFontString(nil, "OVERLAY", "GameFontWhite")
self.exp.text:SetFont("Fonts\\ARIALN.TTF", 14, "OUTLINE")
self.exp.text:SetPoint("CENTER", MainMenuExpBar, "CENTER", 0, 1)
self.exp.text:SetJustifyH("CENTER")
self.exp.text:SetTextColor(1, 1, 1)
end
local exp = self.exp
exp.hovered = false
exp.text:Hide()
local function UpdateXPText()
local curr = UnitXP("player")
local max = UnitXPMax("player")
if not max or max == 0 then
if exp.text:IsShown() then exp.text:Hide() end
self.lastText = nil
return
end
local rest = GetXPExhaustion() or 0
local xpPct = math.floor(curr / max * 100)
-- "1555/3000 51%", like the reputation bar's current/max text.
local text = "|cffffffff" .. curr .. "/" .. max .. " " .. xpPct .. "%|r"
if rest > 0 then
-- Turtle WoW 1.18.1 reduced rested creature XP from +100% to +50%.
-- The effective GetXPExhaustion full-rest threshold is about 112.5% of
-- the current level XP, so normalize that pool to a 0-100% display.
local restPct = math.floor(math.min(rest / (max * 1.125) * 100, 100))
text = text .. " |cffaaaaaa(|cffa78aca" .. restPct .. "%|cffaaaaaa)|r"
end
if self.lastText ~= text then
exp.text:SetText(text)
self.lastText = text
end
if exp.hovered then
if not exp.text:IsShown() then exp.text:Show() end
elseif exp.text:IsShown() then
exp.text:Hide()
end
end
if not self.events then
self.events = CreateFrame("Frame", nil, UIParent)
self.events:RegisterEvent("PLAYER_ENTERING_WORLD")
self.events:RegisterEvent("PLAYER_XP_UPDATE")
self.events:RegisterEvent("UPDATE_EXHAUSTION")
self.events:RegisterEvent("PLAYER_LEVEL_UP")
end
self.events:SetScript("OnEvent", function()
if event == "PLAYER_ENTERING_WORLD" then
-- Hide Blizzard's default overlay that renders the original XP text.
MainMenuBarOverlayFrame:Hide()
end
UpdateXPText()
end)
-- Reuse the XP bar's existing mouse handling instead of adding a permanent
-- OnUpdate or a mouse-enabled overlay on top of it.
if not self.mouseoverHooked then
self.mouseoverHooked = true
ShaguTweaks.HookScript(MainMenuExpBar, "OnEnter", function()
exp.hovered = true
UpdateXPText()
end)
ShaguTweaks.HookScript(MainMenuExpBar, "OnLeave", function()
exp.hovered = false
if exp.text:IsShown() then exp.text:Hide() end
end)
end
-- Refresh immediately in case the module is enabled after entering the world.
MainMenuBarOverlayFrame:Hide()
UpdateXPText()
end