Files

237 lines
11 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Movable standard windows. Only frame methods and post-hooks (hooksecurefunc/HookScript):
-- no Blizzard globals are replaced. Protected frames are never moved in combat.
local ADDON = ...
-- Loot and gamepad menu frames run protected navigation when they close. Leave their scripts intact.
local FRAMES = {
"CharacterFrame", "BankFrame", "ContainerFrameCombinedBags", "WorldMapFrame",
"SpellBookFrame", "PlayerSpellsFrame", "PlayerTalentFrame", "TalentFrame", "ClassTalentFrame",
"QuestLogFrame", "QuestFrame", "FriendsFrame", "CommunitiesFrame", "GuildFrame", "RaidFrame",
"TradeSkillFrame", "ProfessionsFrame", "CraftFrame", "MerchantFrame", "MailFrame", "OpenMailFrame",
"AuctionHouseFrame", "AuctionFrame", "TradeFrame", "InspectFrame", "DressUpFrame",
"PVPFrame", "PVPUIFrame", "HonorFrame", "BattlefieldFrame", "LFGParentFrame", "PVEFrame",
"CollectionsJournal", "WardrobeFrame", "TabardFrame", "PetStableFrame", "ClassTrainerFrame",
"KeyRingFrame", "MacroFrame", "TimeManagerFrame",
"EncounterJournal", "AchievementFrame", "ItemSocketingFrame", "ItemTextFrame", "TaxiFrame",
}
for i = 1, 13 do FRAMES[#FRAMES + 1] = "ContainerFrame" .. i end
local db
local hooked = {}
-- Gamepad UI: closing a hooked window ends in a blocked SetPreferredGamepadInteractTarget() call, so
-- no game frame or function is hooked while it is on. Windows can be moved with mouse and keyboard UI.
local function isGamepadStyle(style)
return style == Enum.InputDeviceInterfaceType.Gamepad
end
local gamepad = false -- updated by the event handler below
local function canMove(frame)
if gamepad then return false end
return not (InCombatLockdown() and frame:IsProtected())
end
local function save(frame)
local point, _, relPoint, x, y = frame:GetPoint(1)
if point then db[frame:GetName()] = { point, relPoint, x, y } end
end
local function restore(frame)
local pos = db and db[frame:GetName()]
if not pos or not frame:IsShown() or not canMove(frame) then return end
-- saved offsets are in the window's own (saved) scale
local scale = db.__scale and db.__scale[frame:GetName()]
if scale and not (frame.IsMaximized and frame:IsMaximized()) then frame:SetScale(scale) end
frame:ClearAllPoints()
frame:SetPoint(pos[1], UIParent, pos[2], pos[3], pos[4])
end
local function handleFor(frame)
-- Drag by the title area when the template has one, else the frame itself.
return frame.TitleContainer or frame.TitleBar or frame.DragArea or (frame.BorderFrame and frame.BorderFrame.TitleContainer) or frame
end
local function hook(name)
local frame = _G[name]
if not frame or hooked[frame] or type(frame.SetMovable) ~= "function" then return end
if frame.IsForbidden and frame:IsForbidden() then return end
if InCombatLockdown() and frame:IsProtected() then return end
hooked[frame] = true
frame:SetMovable(true)
frame:SetClampedToScreen(true)
local handle = handleFor(frame)
if handle ~= frame and handle.EnableMouse then handle:EnableMouse(true) end
handle:RegisterForDrag("LeftButton")
handle:HookScript("OnDragStart", function()
if canMove(frame) then frame:StartMoving() end
end)
handle:HookScript("OnDragStop", function()
frame:StopMovingOrSizing()
if frame.SetUserPlaced then frame:SetUserPlaced(false) end -- we own persistence, not layout-cache
save(frame)
end)
handle:HookScript("OnMouseUp", function(_, button)
if button == "RightButton" and IsShiftKeyDown() and db[name] then
db[name] = nil
if not gamepad and not InCombatLockdown() and UpdateUIPanelPositions then UpdateUIPanelPositions(frame) end
end
end)
frame:HookScript("OnShow", function(self) C_Timer.After(0, function() restore(self) end) end)
end
-- Resizable windows: a grip in the bottom-right corner scales the whole window (map canvas + quest panel keep
-- their layout, the top-left corner stays put). Not while the window is maximized.
local RESIZABLE = { WorldMapFrame = true }
local MIN_SCALE, MAX_SCALE = 0.5, 1.5
local function isMaximized(frame)
return type(frame.IsMaximized) == "function" and frame:IsMaximized()
end
local function setScaleKeepTopLeft(frame, scale)
local left, top, eff = frame:GetLeft(), frame:GetTop(), frame:GetEffectiveScale()
frame:SetScale(scale)
if left and top then
local ne = frame:GetEffectiveScale()
frame:ClearAllPoints()
frame:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", left * eff / ne, top * eff / ne)
end
end
-- The quest panel's scrollbar ends in the window's bottom-right corner, under the grip: raise its bottom edge
-- by the grip height (same width). Blizzard's own anchors are kept and put back when maximized.
local GRIP_CLEARANCE = 32
local function liftQuestScrollBar(lift)
local sf = _G.QuestScrollFrame
local bar = sf and (sf.ScrollBar or _G.QuestScrollFrameScrollBar)
if not bar or (bar.IsForbidden and bar:IsForbidden()) then return end
if not bar.OctoOrigPoints then
local pts = {}
for i = 1, bar:GetNumPoints() do pts[i] = { bar:GetPoint(i) } end
if #pts == 0 then return end
bar.OctoOrigPoints = pts
bar.OctoOrigHeight = bar:GetHeight()
end
if bar.OctoLifted == lift then return end
bar.OctoLifted = lift
bar:ClearAllPoints()
local lowest = false
for _, p in ipairs(bar.OctoOrigPoints) do
local point, rel, relPoint, x, y = p[1], p[2], p[3], p[4], p[5]
if lift and point:find("^BOTTOM") then y = (y or 0) + GRIP_CLEARANCE; lowest = true end
bar:SetPoint(point, rel, relPoint, x, y)
end
-- anchored by top + height only: shorten instead
if lift and not lowest then bar:SetHeight(bar:GetHeight() - GRIP_CLEARANCE) end
if not lift and not lowest and bar.OctoOrigHeight then bar:SetHeight(bar.OctoOrigHeight) end
end
local function applyScale(frame)
if not canMove(frame) then return end
local want = (not isMaximized(frame) and db.__scale and db.__scale[frame:GetName()]) or 1
if math.abs(frame:GetScale() - want) > 0.001 then
if isMaximized(frame) then frame:SetScale(want) else setScaleKeepTopLeft(frame, want) end
end
local grip = frame.OctoSizeGrip
if grip then
-- the quest panel sits on a higher strata than the map window: keep the grip above it, or it gets no mouse
local top = _G.QuestMapFrame
if frame == WorldMapFrame and top then
grip:SetFrameStrata(top:GetFrameStrata())
grip:SetFrameLevel(top:GetFrameLevel() + 30)
end
grip:SetShown(not isMaximized(frame))
end
if frame == WorldMapFrame then liftQuestScrollBar(not isMaximized(frame)) end
end
local function addGrip(frame)
local name = frame:GetName()
local grip = CreateFrame("Button", nil, frame)
grip:SetSize(26, 26)
grip:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1)
grip:SetHitRectInsets(-10, -4, -10, -4) -- easier to grab than the art itself
grip:SetFrameLevel(frame:GetFrameLevel() + 50)
grip:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Up")
grip:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Down")
grip:SetHighlightTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Highlight")
grip:GetHighlightTexture():SetBlendMode("ADD")
frame.OctoSizeGrip = grip
local start
grip:SetScript("OnMouseDown", function(_, button)
if button ~= "LeftButton" or not canMove(frame) or isMaximized(frame) then return end
local cx, cy = GetCursorPosition()
start = { cx = cx, cy = cy, scale = frame:GetScale(), w = frame:GetWidth() * frame:GetEffectiveScale(),
h = frame:GetHeight() * frame:GetEffectiveScale() }
grip:SetScript("OnUpdate", function()
local x, y = GetCursorPosition()
-- grow by the larger of the horizontal / vertical drag, relative to the window's on-screen size
local f = math.max((start.w + x - start.cx) / start.w, (start.h + start.cy - y) / start.h)
local s = math.min(MAX_SCALE, math.max(MIN_SCALE, start.scale * f))
if canMove(frame) then setScaleKeepTopLeft(frame, s) end
end)
end)
grip:SetScript("OnMouseUp", function()
grip:SetScript("OnUpdate", nil)
if not start then return end
start = nil
db.__scale = db.__scale or {}
db.__scale[name] = frame:GetScale()
save(frame)
end)
grip:SetScript("OnEnter", function()
GameTooltip:SetOwner(grip, "ANCHOR_RIGHT")
GameTooltip:SetText("Drag to resize")
GameTooltip:AddLine("Shift+right-click: default size", 1, 1, 1)
GameTooltip:Show()
end)
grip:SetScript("OnLeave", function() GameTooltip:Hide() end)
grip:RegisterForClicks("RightButtonUp")
grip:SetScript("OnClick", function()
if not IsShiftKeyDown() or not canMove(frame) then return end
if db.__scale then db.__scale[name] = nil end
setScaleKeepTopLeft(frame, 1)
save(frame)
end)
frame:HookScript("OnShow", function(self) C_Timer.After(0, function() applyScale(self) end) end)
for _, m in ipairs({ "Maximize", "Minimize", "SetMaximized", "SetMinimized" }) do
if type(frame[m]) == "function" then hooksecurefunc(frame, m, function() applyScale(frame) end) end
end
applyScale(frame)
end
local function hookAll()
for _, name in ipairs(FRAMES) do
hook(name)
local frame = _G[name]
if RESIZABLE[name] and frame and hooked[frame] and not frame.OctoSizeGrip then addGrip(frame) end
end
end
-- Blizzard re-anchors panels and bags on open; put moved ones back afterwards.
local function reapplyAll()
for frame in pairs(hooked) do restore(frame) end
end
local loggedIn, globalsHooked, anyHooks, reloadAsked
local events = CreateFrame("Frame")
events:RegisterEvent("ADDON_LOADED")
events:RegisterEvent("PLAYER_LOGIN")
events:RegisterEvent("PLAYER_REGEN_ENABLED")
pcall(events.RegisterEvent, events, "INPUT_DEVICE_INTERFACE_TRANSITION") -- UI style switched mid-session
events:SetScript("OnEvent", function(_, event, arg1)
if event == "ADDON_LOADED" and arg1 == ADDON then
OctoMoveWindowsDB = OctoMoveWindowsDB or {}
db = OctoMoveWindowsDB
end
if event == "PLAYER_LOGIN" then loggedIn = true end
if event == "INPUT_DEVICE_INTERFACE_TRANSITION" then
gamepad = isGamepadStyle(arg1) -- arg1 is the new style
else
gamepad = isGamepadStyle(C_InputInterfaceStyle.GetCurrentStyle())
end
if not db then return end
if gamepad then
-- hooks cannot be removed; only a reload clears them
if anyHooks and not reloadAsked then
reloadAsked = true
print("OctoMoveWindows: type /reload to finish switching to the gamepad UI.")
end
return
end
reloadAsked = nil
hookAll()
anyHooks = anyHooks or next(hooked) ~= nil
if loggedIn and not globalsHooked then
globalsHooked, anyHooks = true, true
if UpdateUIPanelPositions then hooksecurefunc("UpdateUIPanelPositions", reapplyAll) end
if UpdateContainerFrameAnchors then hooksecurefunc("UpdateContainerFrameAnchors", reapplyAll) end
end
if event == "PLAYER_REGEN_ENABLED" or event == "INPUT_DEVICE_INTERFACE_TRANSITION" then reapplyAll() end
end)