From ef755243440913804538169afcd21a9f0ea30a08 Mon Sep 17 00:00:00 2001 From: shagu Date: Mon, 6 Dec 2021 14:52:11 +0100 Subject: [PATCH] cooldown-numbers: show cooldown animation numbers --- ShaguTweaks-tbc.toc | 1 + ShaguTweaks.toc | 1 + mods/cooldown-numbers.lua | 93 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 mods/cooldown-numbers.lua diff --git a/ShaguTweaks-tbc.toc b/ShaguTweaks-tbc.toc index aaa14d5..3b2ff88 100644 --- a/ShaguTweaks-tbc.toc +++ b/ShaguTweaks-tbc.toc @@ -62,3 +62,4 @@ mods\hide-gryphons.lua mods\worldmap-colors.lua mods\unitframes-bighealth.lua mods\unitframes-classportrait.lua +mods\cooldown-numbers.lua diff --git a/ShaguTweaks.toc b/ShaguTweaks.toc index 2aa0d83..df25656 100644 --- a/ShaguTweaks.toc +++ b/ShaguTweaks.toc @@ -62,3 +62,4 @@ mods\hide-gryphons.lua mods\worldmap-colors.lua mods\unitframes-bighealth.lua mods\unitframes-classportrait.lua +mods\cooldown-numbers.lua diff --git a/mods/cooldown-numbers.lua b/mods/cooldown-numbers.lua new file mode 100644 index 0000000..bdbc7a7 --- /dev/null +++ b/mods/cooldown-numbers.lua @@ -0,0 +1,93 @@ +local _G = _G or getfenv(0) +local hooksecurefunc = ShaguTweaks.hooksecurefunc +local GetExpansion = ShaguTweaks.GetExpansion +local AddBorder = ShaguTweaks.AddBorder +local TimeConvert = ShaguTweaks.TimeConvert + +local module = ShaguTweaks:register({ + title = "Cooldown Numbers", + description = "Display the remaining duration as text on every cooldown.", + expansions = { ["vanilla"] = true, ["tbc"] = true }, + enabled = nil, + color = { r = .3, g = .3, b = .3, a = .9} +}) + +local function CooldownOnUpdate() + -- hide frames without parent + local parent = this:GetParent() + if not parent then this:Hide() end + + if not this.tick then this.tick = GetTime() + .1 end + if this.tick > GetTime() then return end + this.tick = GetTime() + .1 + + -- fix own alpha value (should be inherited, but somehow isn't always) + this:SetAlpha(parent:GetAlpha()) + + local remaining = this.duration - (GetTime() - this.start) + if remaining > 0 then + this.text:SetText(TimeConvert(remaining)) + else + this:Hide() + end +end + +local function CreateCoolDown(cooldown, start, duration) + local parent = cooldown:GetParent() + if not parent then return end + + -- skip already set debuff timers + if cooldown.readable then return end + + local parentname = parent and parent.GetName and parent:GetName() + parentname = parentname or "UnknownCooldownFrame" + + cooldown.cooldowntext = CreateFrame("Frame", parentname .. "CooldownText", cooldown) + cooldown.cooldowntext:SetAllPoints(cooldown) + cooldown.cooldowntext:SetFrameLevel(parent:GetFrameLevel() + 1) + cooldown.cooldowntext.text = cooldown.cooldowntext:CreateFontString(parentname .. "CooldownTextFont", "OVERLAY") + + -- detect dynamic font size + local size = parent:GetHeight() or 0 + size = size > 0 and size * .64 or 12 + size = size > 14 and 14 or size + + -- set fonts + cooldown.cooldowntext.text:SetFont(STANDARD_TEXT_FONT, size, "OUTLINE") + cooldown.cooldowntext.text:SetPoint("CENTER", cooldown.cooldowntext, "CENTER", 0, 0) + cooldown.cooldowntext:SetScript("OnUpdate", CooldownOnUpdate) +end + +local function SetCooldown(this, start, duration, enable) + -- add support for omnicc's disable flag + if this.noCooldownCount then return end + + -- don't draw global cooldowns + if duration < 2 then return end + + if not this.cooldowntext then + CreateCoolDown(this, start, duration) + end + + if start > 0 and duration > 0 and (not enable or enable > 0) then + this.cooldowntext:Show() + else + this.cooldowntext:Hide() + end + + this.cooldowntext.start = start + this.cooldowntext.duration = duration +end + +module.enable = function(self) + if GetExpansion() == "vanilla" then + -- vanilla does not have a cooldown frame type, so we hook the + -- regular SetTimer function that each one is calling. + hooksecurefunc("CooldownFrame_SetTimer", SetCooldown) + else + -- tbc and later expansion have a cooldown frametype, so we can + -- hook directly into the frame creation and add our function there. + local methods = getmetatable(CreateFrame('Cooldown', nil, nil, 'CooldownFrameTemplate')).__index + hooksecurefunc(methods, 'SetCooldown', SetCooldown) + end +end