ruen-ui/RuenUI/ButtonStyle.lua

78 lines
4.1 KiB
Lua

local _, P = ...
-- Presentation only: never replace Blizzard's secure button mixins or scripts.
-- Modern action templates expose named regions even when a texture getter is
-- absent. Copying only the getter left the pet extensions with a generic glow.
local defaults = {
Normal = "UI-HUD-ActionBar-IconFrame",
Pushed = "UI-HUD-ActionBar-IconFrame-Down",
Highlight = "UI-HUD-ActionBar-IconFrame-Mouseover",
Checked = "UI-HUD-ActionBar-IconFrame-Mouseover",
}
local function region(frame, kind)
if not frame then return end
local named = frame[kind .. "Texture"]
if named then return named end
local getter = frame["Get" .. kind .. "Texture"]
return type(getter) == "function" and getter(frame) or nil
end
function P.StyleActionButton(button, prototype)
for _, kind in ipairs({"Normal", "Pushed", "Highlight", "Checked"}) do
local source = region(prototype, kind)
local atlas = source and source:GetAtlas() or defaults[kind]
local setter = button["Set" .. kind .. "Atlas"]
if atlas and type(setter) == "function" then setter(button, atlas)
elseif source and source:GetTexture() then button["Set" .. kind .. "Texture"](button, source:GetTexture())
end
local target = region(button, kind)
if target then
target:ClearAllPoints()
-- Native small buttons anchor the art at TOPLEFT, not CENTER.
-- Match its size relative to the icon at any pet/UI scale.
local point, relative, relativePoint, x, y
if source and source.GetPoint then point, relative, relativePoint, x, y = source:GetPoint(1) end
if point and (relative == prototype or relative == prototype.icon) then
local ratio = button:GetWidth() / prototype:GetWidth()
target:SetPoint(point, button, relativePoint, (x or 0) * ratio, (y or 0) * ratio)
else target:SetPoint("TOPLEFT", button, "TOPLEFT", 0, 0) end
local w, h = button:GetWidth(), button:GetHeight()
if source and prototype:GetWidth() > 0 and prototype:GetHeight() > 0 then
target:SetSize(source:GetWidth() / prototype:GetWidth() * w,
source:GetHeight() / prototype:GetHeight() * h)
target:SetTexCoord(source:GetTexCoord())
if source.GetBlendMode and target.SetBlendMode then target:SetBlendMode(source:GetBlendMode()) end
if source.GetDrawLayer and target.SetDrawLayer then target:SetDrawLayer(source:GetDrawLayer()) end
else target:SetSize(w * 46 / 45, h) end
-- Do not inherit the prototype's transient attack-flash alpha.
if target.SetAlpha then target:SetAlpha(1) end
end
end
if prototype and prototype.Flash then
local source = prototype.Flash
if not button.Flash then button.Flash = button:CreateTexture(nil, "OVERLAY"); button.Flash:Hide() end
local flash = button.Flash
if source:GetAtlas() and flash.SetAtlas then flash:SetAtlas(source:GetAtlas()) else flash:SetTexture(source:GetTexture()) end
flash:ClearAllPoints(); flash:SetPoint("TOPLEFT", button, "TOPLEFT", 0, 0)
flash:SetSize(source:GetWidth() / prototype:GetWidth() * button:GetWidth(),
source:GetHeight() / prototype:GetHeight() * button:GetHeight())
end
end
function P.SetActionButtonSelected(button, selected, attack)
-- Pass an engine value straight to the widget; do not inspect secret values.
button:SetChecked(selected)
local checked = region(button, "Checked")
if checked and checked.SetAlpha then checked:SetAlpha(attack and .5 or 1) end
if button.Flash and UIFrameFlash and UIFrameFlashStop then
local flashing = false
if not (issecretvalue and issecretvalue(selected)) then flashing = attack and selected or false end
if flashing ~= button.ruenAttackFlashing then
if flashing then UIFrameFlash(button.Flash, .5, .5, -1, false)
else UIFrameFlashStop(button.Flash); button.Flash:Hide() end
button.ruenAttackFlashing = flashing
end
end
end