Remember preview window size per mode (All/One/Sta), per preset

- Each mode stores its own window width/height in the preset file
- Switching modes saves current bounds and reopens with saved size
- Fit clears saved size to recalculate from content
- Button order: Mode → Fit → Setup; "Sta" renamed to "Static"
This commit is contained in:
Cidwel Highwind 2026-04-04 13:29:21 +02:00
parent df9a2a3dcf
commit 222a36623e
1 changed files with 68 additions and 30 deletions

View File

@ -118,6 +118,10 @@ local S = {
previewLayoutValue = 2, -- number for fixed cols/rows previewLayoutValue = 2, -- number for fixed cols/rows
previewMode = "all", -- "all", "single", or "static" previewMode = "all", -- "all", "single", or "static"
previewSingleIdx = 1, -- which animation to show in single mode previewSingleIdx = 1, -- which animation to show in single mode
-- Per-mode preview window size (0 = auto/not set yet)
pvWinWAll = 0, pvWinHAll = 0,
pvWinWOne = 0, pvWinHOne = 0,
pvWinWSta = 0, pvWinHSta = 0,
-- Static preview scroll/pan -- Static preview scroll/pan
previewStaScrollX = 0, previewStaScrollX = 0,
previewStaScrollY = 0, previewStaScrollY = 0,
@ -310,6 +314,12 @@ local function savePreset(presetName)
f:write("gbAlwaysOverwrite=" .. tostring(S.gbAlwaysOverwrite) .. "\n") f:write("gbAlwaysOverwrite=" .. tostring(S.gbAlwaysOverwrite) .. "\n")
f:write("gbTileW=" .. S.gbTileW .. "\n") f:write("gbTileW=" .. S.gbTileW .. "\n")
f:write("gbTileH=" .. S.gbTileH .. "\n") f:write("gbTileH=" .. S.gbTileH .. "\n")
f:write("pvWinWAll=" .. S.pvWinWAll .. "\n")
f:write("pvWinHAll=" .. S.pvWinHAll .. "\n")
f:write("pvWinWOne=" .. S.pvWinWOne .. "\n")
f:write("pvWinHOne=" .. S.pvWinHOne .. "\n")
f:write("pvWinWSta=" .. S.pvWinWSta .. "\n")
f:write("pvWinHSta=" .. S.pvWinHSta .. "\n")
for i, name in ipairs(S.animNames) do for i, name in ipairs(S.animNames) do
f:write("anim_" .. (i - 1) .. "=" .. serializeFrames(S.anims[name] or {}) .. "\n") f:write("anim_" .. (i - 1) .. "=" .. serializeFrames(S.anims[name] or {}) .. "\n")
end end
@ -378,6 +388,12 @@ local function loadPreset(presetName)
S.gbTileW = ts S.gbTileW = ts
S.gbTileH = ts S.gbTileH = ts
end end
if k == "pvWinWAll" then S.pvWinWAll = tonumber(v) or 0 end
if k == "pvWinHAll" then S.pvWinHAll = tonumber(v) or 0 end
if k == "pvWinWOne" then S.pvWinWOne = tonumber(v) or 0 end
if k == "pvWinHOne" then S.pvWinHOne = tonumber(v) or 0 end
if k == "pvWinWSta" then S.pvWinWSta = tonumber(v) or 0 end
if k == "pvWinHSta" then S.pvWinHSta = tonumber(v) or 0 end
-- Dynamic anim frames: anim_0, anim_1, ... -- Dynamic anim frames: anim_0, anim_1, ...
local animIdx = k and string.match(k, "^anim_(%d+)$") local animIdx = k and string.match(k, "^anim_(%d+)$")
if animIdx then if animIdx then
@ -1314,21 +1330,15 @@ openPreviewWindow = function()
S.previewSingleIdx = 1 S.previewSingleIdx = 1
end end
-- calcPreviewSize helper (Sta mode excluded — uses last All/One size) -- calcPreviewSize: compute "natural" content size for Fit (All/One only)
local function calcPreviewSize() local function calcPreviewSize()
local z local z = pvZoom()
if S.previewMode == "static" then
-- Don't try to auto-size for Sta; fall through to All sizing
z = S.previewZoomAll
else
z = pvZoom()
end
local ptw = S.tileW * z local ptw = S.tileW * z
local pth = S.tileH * z local pth = S.tileH * z
local cellW = ptw + PV_MARGIN * 2 local cellW = ptw + PV_MARGIN * 2
local cellH = pth + PV_MARGIN * 2 local cellH = pth + PV_MARGIN * 2
if S.previewMode == "single" then if S.previewMode == "single" then
return cellW, cellH + 14 -- extra space for name label return cellW, cellH + 14
else else
local numAnims = #S.animNames local numAnims = #S.animNames
local cols, rows = getPreviewGrid(numAnims) local cols, rows = getPreviewGrid(numAnims)
@ -1336,14 +1346,41 @@ openPreviewWindow = function()
end end
end end
-- Calculate initial size -- Save current window bounds into S for the given mode
local pvWidth, pvHeight = calcPreviewSize() local function saveBoundsForMode(mode)
if not previewDlg then return end
local ok, b = pcall(function() return previewDlg.bounds end)
if not ok or not b then return end
if mode == "all" then S.pvWinWAll = b.width; S.pvWinHAll = b.height
elseif mode == "single" then S.pvWinWOne = b.width; S.pvWinHOne = b.height
elseif mode == "static" then S.pvWinWSta = b.width; S.pvWinHSta = b.height
end
end
-- Get saved size for current mode, or fallback to calcPreviewSize
local function getWinSize()
local w, h = 0, 0
if S.previewMode == "all" then w = S.pvWinWAll; h = S.pvWinHAll
elseif S.previewMode == "single" then w = S.pvWinWOne; h = S.pvWinHOne
elseif S.previewMode == "static" then w = S.pvWinWSta; h = S.pvWinHSta
end
if w > 0 and h > 0 then return w, h end
-- No saved size: use content fit (Sta defaults to 200x200)
if S.previewMode == "static" then return 200, 200 end
local cw, ch = calcPreviewSize()
return math.max(cw, 120), math.max(ch, 40)
end
-- Calculate initial canvas size from saved window size
local pvWidth, pvHeight = getWinSize()
-- Canvas is smaller than window (buttons/decorations), but we approximate
pvWidth = math.max(pvWidth, 120) pvWidth = math.max(pvWidth, 120)
pvHeight = math.max(pvHeight, 40) pvHeight = math.max(pvHeight, 40)
previewDlg = Dialog{ previewDlg = Dialog{
title = "AniPhallow (" .. S.currentPreset .. ")", title = "AniPhallow (" .. S.currentPreset .. ")",
onclose = function() onclose = function()
saveBoundsForMode(S.previewMode)
if previewTimer then pcall(function() previewTimer:stop() end) end if previewTimer then pcall(function() previewTimer:stop() end) end
if pvRefreshTimer then pcall(function() pvRefreshTimer:stop() end) end if pvRefreshTimer then pcall(function() pvRefreshTimer:stop() end) end
previewTimer = nil previewTimer = nil
@ -1356,18 +1393,10 @@ openPreviewWindow = function()
end end
} }
-- All buttons on the same row: Setup, toggle mode, Fit -- Buttons: Mode, Fit, Setup
previewDlg:button{
id = "pvSetup",
text = "Setup",
onclick = function()
openMainDialog()
end
}
local function getModeLabel() local function getModeLabel()
if S.previewMode == "single" then return "One" if S.previewMode == "single" then return "One"
elseif S.previewMode == "static" then return "Sta" elseif S.previewMode == "static" then return "Static"
else return "All" end else return "All" end
end end
@ -1375,6 +1404,8 @@ openPreviewWindow = function()
id = "pvToggleMode", id = "pvToggleMode",
text = getModeLabel(), text = getModeLabel(),
onclick = function() onclick = function()
-- Save current bounds before switching
saveBoundsForMode(S.previewMode)
if S.previewMode == "all" then if S.previewMode == "all" then
S.previewMode = "single" S.previewMode = "single"
if #S.animNames > 0 then if #S.animNames > 0 then
@ -1383,17 +1414,13 @@ openPreviewWindow = function()
end end
elseif S.previewMode == "single" then elseif S.previewMode == "single" then
S.previewMode = "static" S.previewMode = "static"
refreshSource() -- ensure source image is ready refreshSource()
else else
S.previewMode = "all" S.previewMode = "all"
end end
-- Update button text, label visibility, and Fit enabled state -- Close and reopen with new mode's saved size
pcall(function() pcall(function() previewDlg:close() end)
previewDlg:modify{ id = "pvToggleMode", text = getModeLabel() } openPreviewWindow()
previewDlg:modify{ id = "pvAnimName", visible = (S.previewMode == "single") }
previewDlg:modify{ id = "pvFit", enabled = (S.previewMode ~= "static") }
end)
previewDlg:repaint()
end end
} }
@ -1403,12 +1430,23 @@ openPreviewWindow = function()
enabled = (S.previewMode ~= "static"), enabled = (S.previewMode ~= "static"),
onclick = function() onclick = function()
if S.previewMode == "static" then return end if S.previewMode == "static" then return end
-- Close and reopen to refit -- Clear saved size so it recalculates from content
if S.previewMode == "all" then S.pvWinWAll = 0; S.pvWinHAll = 0
elseif S.previewMode == "single" then S.pvWinWOne = 0; S.pvWinHOne = 0
end
pcall(function() previewDlg:close() end) pcall(function() previewDlg:close() end)
openPreviewWindow() openPreviewWindow()
end end
} }
previewDlg:button{
id = "pvSetup",
text = "Setup",
onclick = function()
openMainDialog()
end
}
previewDlg:canvas{ previewDlg:canvas{
id = "pvCanvas", id = "pvCanvas",
width = pvWidth, width = pvWidth,