Files
ShaguTweaks-ClassicAPI/mods/actionbar-range-color.lua
T
2026-10-30 20:06:32 +00:00

87 lines
3.6 KiB
Lua

local _G = ShaguTweaks.GetGlobalEnv()
local T = ShaguTweaks.T
local hooksecurefunc = ShaguTweaks.hooksecurefunc
local API = ShaguTweaks.API
local module = ShaguTweaks:register({
title = T["Range Color"],
description = T["Action buttons will be colored red when out of range."],
expansions = { ["vanilla"] = true },
category = T["Action Bar"],
enabled = nil,
})
-- Modern APIs return booleans; legacy range APIs return 1/0/nil.
-- In Lua, numeric zero is truthy, so normalize both contracts explicitly.
local function NormalizeRange(value)
if value == true or value == 1 then return 1 end
if value == false or value == 0 then return 0 end
end
local function GetActionRange(action)
if not UnitExists("target") then return nil end
if API and API.GetActionInfo then
local actionType, id = API.GetActionInfo(action)
if actionType == "spell" and id and API.spellrange
and API.IsSpellInRange then
-- ClassicAPI's check is geometric: it does not reject the wrong
-- faction. A friendly buff should not turn red against an enemy.
local spells = _G.C_Spell
if spells then
if type(spells.IsSpellHelpful) == "function"
and spells.IsSpellHelpful(id) and type(UnitCanAssist) == "function"
and not UnitCanAssist("player", "target") then return nil end
if type(spells.IsSpellHarmful) == "function"
and spells.IsSpellHarmful(id) and type(UnitCanAttack) == "function"
and not UnitCanAttack("player", "target") then return nil end
end
-- nil is authoritative: self/rangeless spells have no range to
-- fail. Never turn it into an old action-slot false result.
return NormalizeRange(API.IsSpellInRange(id, "target"))
elseif actionType == "item" and id and API.itemrange
and API.IsItemInRange then
return NormalizeRange(API.IsItemInRange(id, "target"))
end
end
-- Macros, unresolved actions and clients without the native capability
-- retain the action-slot compatibility fallback.
return NormalizeRange(IsActionInRange(action))
end
module.enable = function(self)
hooksecurefunc("ActionButton_OnUpdate", function(elapsed)
if this.rangeTimer then
this.rangeTimer = this.rangeTimer - elapsed
if this.rangeTimer <= 0.2 then
local action = ActionButton_GetPagedID(this)
local inRange = GetActionRange(action)
local icon = _G[this:GetName() .. "Icon"]
if icon then
if inRange == 0 then
if not this.a then
this.r, this.g, this.b, this.a = 0.85, 0.18, 0.18, 1
end
icon:SetVertexColor(this.r, this.g, this.b, this.a)
else
-- Restore the normal usability tint even if the old
-- red action is now unusable or short of mana.
local usable, noMana = IsUsableAction(action)
if usable and usable ~= 0 then
icon:SetVertexColor(1, 1, 1, 1)
elseif noMana and noMana ~= 0 then
icon:SetVertexColor(0.5, 0.5, 1, 1)
else
icon:SetVertexColor(0.4, 0.4, 0.4, 1)
end
end
end
this.rangeTimer = TOOLTIP_UPDATE_TIME
end
end
end, true)
end