diff --git a/mods/health-color.lua b/mods/health-color.lua index 27a3823..b2f9e18 100644 --- a/mods/health-color.lua +++ b/mods/health-color.lua @@ -1,8 +1,6 @@ -local _G = ShaguTweaks.GetGlobalEnv() local T = ShaguTweaks.T -local Abbreviate = ShaguTweaks.Abbreviate local GetColorGradient = ShaguTweaks.GetColorGradient -local vanilla = ShaguTweaks.GetExpansion() == "vanilla" or nil +local hooksecurefunc = hooksecurefunc or ShaguTweaks.hooksecurefunc local module = ShaguTweaks:register({ title = T["Unit Frame Health Colors"], @@ -12,36 +10,44 @@ local module = ShaguTweaks:register({ enabled = nil, }) -module.enable = function(self) - local HookUnitFrame_UpdateManaType = UnitFrame_UpdateManaType - function UnitFrame_UpdateManaType(uf) - HookUnitFrame_UpdateManaType(uf) - if not uf then uf = this end - local string = uf.manabar and uf.manabar.TextString - if not string then return end - - if not strfind(uf.manabar:GetName(), "Health") then - local r, g, b = uf.manabar:GetStatusBarColor() - string:SetTextColor((r + 2) / 3, (g + 2) / 3, (b + 2) / 3, 1) - end - end - - local HookTextStatusBar_UpdateTextString = TextStatusBar_UpdateTextString - function TextStatusBar_UpdateTextString(sb) - if not sb then sb = this end - - HookTextStatusBar_UpdateTextString(sb) - local string = sb.TextString - - if string and sb.unit then - local min, max = sb:GetMinMaxValues() - local cur = sb:GetValue() - local percent = max > 0 and floor(cur/max*100) or 0 - - if strfind(sb:GetName(), "Health") then - local r, g, b = GetColorGradient(percent/100) - string:SetTextColor((r + 1) / 2, (g + 1) / 2, (b + 1) / 2, .75) - end - end +local function SetTextColorIfChanged(fontString, r, g, b, a) + local cr, cg, cb, ca = fontString:GetTextColor() + if cr ~= r or cg ~= g or cb ~= b or ca ~= a then + fontString:SetTextColor(r, g, b, a) end end + +module.enable = function(self) + local function UpdateManaTextColor(uf) + if not uf then uf = this end + if not uf or not uf.manabar or not uf.manabar.TextString then return end + + local name = uf.manabar:GetName() + if name and not strfind(name, "Health") then + local r, g, b = uf.manabar:GetStatusBarColor() + SetTextColorIfChanged(uf.manabar.TextString, + (r + 2) / 3, (g + 2) / 3, (b + 2) / 3, 1) + end + end + + local function UpdateHealthTextColor(sb) + if not sb then sb = this end + if not sb or not sb.TextString or not sb.unit then return end + + local name = sb:GetName() + if not name or not strfind(name, "Health") then return end + + local _, max = sb:GetMinMaxValues() + local cur = sb:GetValue() + local percent = max > 0 and floor(cur / max * 100) or 0 + local r, g, b = GetColorGradient(percent / 100) + + SetTextColorIfChanged(sb.TextString, + (r + 1) / 2, (g + 1) / 2, (b + 1) / 2, .75) + end + + -- Append our color adjustments instead of replacing Blizzard's global + -- functions. This keeps the module cooperative with other UI addons. + hooksecurefunc("UnitFrame_UpdateManaType", UpdateManaTextColor) + hooksecurefunc("TextStatusBar_UpdateTextString", UpdateHealthTextColor) +end