Files
ShaguTweaks-ClassicAPI/mods/castbar-move.lua
T

137 lines
4.0 KiB
Lua

-- Movable Cast Bar
--
-- Hold Ctrl+Shift+Alt to show a marker over the player's cast bar and drag
-- it anywhere; right-click the marker to put it back. The position is saved
-- per character. The Swing Timer, Wand Timer and the Improved Castbar icon
-- are anchored to the cast bar, so they follow it.
local _G = ShaguTweaks.GetGlobalEnv()
local T = ShaguTweaks.T
local module = ShaguTweaks:register({
title = T["Movable Cast Bar"],
description = T["Hold <Ctrl>, <Shift> and <Alt> to move the cast bar; right-click to reset."],
expansions = { ["vanilla"] = true },
category = T["Action Bar"],
enabled = true,
})
local POSITION_KEY = "CastBarPosition"
-- One table for state: Lua 5.0 allows only 32 upvalues per function.
local S = { preview = false }
local function ModifiersDown()
local API = ShaguTweaks.API or {}
local shift = API.IsShiftKeyDown or IsShiftKeyDown
local ctrl = API.IsControlKeyDown or IsControlKeyDown
local alt = API.IsAltKeyDown or IsAltKeyDown
return shift() and ctrl() and alt() and true or false
end
local function SavedPosition()
local pos = ShaguTweaks_config and ShaguTweaks_config[POSITION_KEY]
if pos and pos[1] and pos[2] then return pos end
end
-- Put the bar at the saved spot. Goes through the original SetPoint because
-- the wrapper below ignores everybody else while a saved spot exists.
local function Apply()
local pos = SavedPosition()
if not pos then return end
CastingBarFrame:ClearAllPoints()
S.orig(CastingBarFrame, "BOTTOMLEFT", UIParent, "BOTTOMLEFT", pos[1], pos[2])
end
-- Back to the stock spot; Reduced Actionbar re-anchors it on the next layout pass.
local function Reset()
if ShaguTweaks_config then ShaguTweaks_config[POSITION_KEY] = nil end
CastingBarFrame:ClearAllPoints()
S.orig(CastingBarFrame, "BOTTOM", UIParent, "BOTTOM", 0, 60)
if _G.UIParent_ManageFramePositions then _G.UIParent_ManageFramePositions() end
end
local function SetPreview(on)
if S.preview == on then return end
S.preview = on
if on then
S.mover:Show()
else
CastingBarFrame:StopMovingOrSizing()
S.mover:Hide()
end
end
local function CreateMover()
local m = CreateFrame("Frame", "ShaguTweaksCastBarMover", UIParent)
m:SetFrameStrata("DIALOG")
m:SetAllPoints(CastingBarFrame)
m:EnableMouse(true)
m:Hide()
m.bg = m:CreateTexture(nil, "BACKGROUND")
m.bg:SetTexture("Interface\\Buttons\\WHITE8X8")
m.bg:SetAllPoints(m)
m.bg:SetVertexColor(1, .7, 0, .6)
m.text = m:CreateFontString(nil, "OVERLAY")
m.text:SetFont("Fonts\\FRIZQT__.TTF", 11, "OUTLINE")
m.text:SetPoint("CENTER", m, "CENTER", 0, 0)
m.text:SetText(T["Cast Bar"])
m:SetScript("OnMouseDown", function()
if arg1 == "LeftButton" then CastingBarFrame:StartMoving() end
end)
m:SetScript("OnMouseUp", function()
CastingBarFrame:StopMovingOrSizing()
ShaguTweaks_config = ShaguTweaks_config or {}
if arg1 == "RightButton" then
Reset()
else
ShaguTweaks_config[POSITION_KEY] = { CastingBarFrame:GetLeft(), CastingBarFrame:GetBottom() }
Apply()
end
end)
S.mover = m
end
module.enable = function(self)
if not CastingBarFrame then return end
if not S.mover then
S.orig = CastingBarFrame.SetPoint
CreateMover()
CastingBarFrame:SetMovable(true)
CastingBarFrame:SetClampedToScreen(true)
S.events = CreateFrame("Frame")
S.events:SetScript("OnEvent", function()
if event == "MODIFIER_STATE_CHANGED" then
SetPreview(ModifiersDown())
else
Apply()
end
end)
end
-- Reduced Actionbar and others re-anchor the bar on every layout pass.
-- While a saved spot exists their SetPoint calls are ignored.
CastingBarFrame.SetPoint = function(frame, a, b, c, d, e)
if SavedPosition() then return end
return S.orig(frame, a, b, c, d, e)
end
S.events:RegisterEvent("PLAYER_ENTERING_WORLD")
S.events:RegisterEvent("MODIFIER_STATE_CHANGED")
Apply()
end
module.disable = function(self)
if not S.mover then return end
SetPreview(false)
S.events:UnregisterAllEvents()
CastingBarFrame.SetPoint = nil
end