refactor(map-editor): unify modal/menu state, drop cheatsheet_open

Adds menu_open, modal_open, show_layers_panel, show_palette to the
editor's state singleton; removes the now-redundant cheatsheet_open
flag. All cheatsheet-toggle paths route through the new generic
modal_open API. Visible behaviour unchanged.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Calic
2026-06-01 19:31:00 +02:00
parent 1bf5f46924
commit e373aa8a1f

View File

@@ -66,7 +66,10 @@ local state = (function()
}, },
dirty = false, dirty = false,
picker_open = false, -- atlas+tile picker expanded? (legacy 0.1.0) picker_open = false, -- atlas+tile picker expanded? (legacy 0.1.0)
cheatsheet_open = false, -- 0.2.0a: ?-modal toggled? menu_open = nil, -- 0.3.0: nil | <category_name> (e.g. "File")
modal_open = nil, -- 0.3.0: nil | <modal_id> (single-stack)
show_layers_panel = true, -- 0.3.0: Window > Layers Panel toggle
show_palette = true, -- 0.3.0: Window > Palette toggle
mouse_cell = nil, -- {x, y} or nil if mouse off-map mouse_cell = nil, -- {x, y} or nil if mouse off-map
snap_vertex = nil, -- 0.2.0b: {vx, vy} or nil (auto-tile mode snap target) snap_vertex = nil, -- 0.2.0b: {vx, vy} or nil (auto-tile mode snap target)
drag_paint = nil, -- 0.2.0c.1: nil | "paint" | "erase" (active drag-stroke mode) drag_paint = nil, -- 0.2.0c.1: nil | "paint" | "erase" (active drag-stroke mode)
@@ -136,11 +139,23 @@ local state = (function()
state.mode = (state.mode == "auto-tile") and "direct" or "auto-tile" state.mode = (state.mode == "auto-tile") and "direct" or "auto-tile"
end end
-- 0.2.0a: ?-modal cheatsheet visibility. function M.is_modal_open(id)
function M.is_cheatsheet_open() return state.cheatsheet_open end if id == nil then return state.modal_open ~= nil end
function M.toggle_cheatsheet() return state.modal_open == id
state.cheatsheet_open = not state.cheatsheet_open
end end
function M.open_modal(id) state.modal_open = id end
function M.close_modal() state.modal_open = nil end
function M.get_modal_open() return state.modal_open end
function M.set_menu_open(name) state.menu_open = name end
function M.get_menu_open() return state.menu_open end
function M.close_menu() state.menu_open = nil end
function M.is_layers_panel_shown() return state.show_layers_panel end
function M.toggle_layers_panel() state.show_layers_panel = not state.show_layers_panel end
function M.is_palette_shown() return state.show_palette end
function M.toggle_palette() state.show_palette = not state.show_palette end
function M.set_mouse_cell(x, y) function M.set_mouse_cell(x, y)
if x == nil then if x == nil then
@@ -181,10 +196,13 @@ local state = (function()
state.roof_mode = false state.roof_mode = false
state.dirty = false state.dirty = false
state.picker_open = false state.picker_open = false
state.cheatsheet_open = false
state.mouse_cell = nil state.mouse_cell = nil
state.snap_vertex = nil state.snap_vertex = nil
state.drag_paint = nil state.drag_paint = nil
state.menu_open = nil
state.modal_open = nil
state.show_layers_panel = true
state.show_palette = true
state.debug_overlay = false state.debug_overlay = false
state.world_overlay = true state.world_overlay = true
for name in pairs(state.layer_visible) do for name in pairs(state.layer_visible) do
@@ -310,7 +328,8 @@ local actions = (function()
-- 0.2.0a actions -- 0.2.0a actions
function M.toggle_mode() state.toggle_mode() end function M.toggle_mode() state.toggle_mode() end
function M.set_mode(m) state.set_mode(m) end function M.set_mode(m) state.set_mode(m) end
function M.toggle_cheatsheet() state.toggle_cheatsheet() end function M.open_cheatsheet() state.open_modal("cheatsheet") end
function M.close_modal() state.close_modal() end
-- 0.2.0c actions -- 0.2.0c actions
function M.toggle_flip() state.toggle_flip() end function M.toggle_flip() state.toggle_flip() end
@@ -724,7 +743,7 @@ local ui = (function()
end end
local function draw_cheatsheet(mx, my) local function draw_cheatsheet(mx, my)
if not state.is_cheatsheet_open() then return end if state.get_modal_open() ~= "cheatsheet" then return end
local c = cheatsheet_layout() local c = cheatsheet_layout()
-- Dim the whole screen behind the modal -- Dim the whole screen behind the modal
local screen_w, screen_h = engine.window.size() local screen_w, screen_h = engine.window.size()
@@ -843,19 +862,13 @@ local ui = (function()
elseif id == "rotate" then actions.cycle_rotation() elseif id == "rotate" then actions.cycle_rotation()
elseif id == "flip" then actions.toggle_flip() elseif id == "flip" then actions.toggle_flip()
elseif id == "reset_xform" then actions.reset_transform() elseif id == "reset_xform" then actions.reset_transform()
elseif id == "help" then actions.toggle_cheatsheet() elseif id == "help" then actions.open_cheatsheet()
end end
end end
-- Hit-test mouse click. Returns true if the click was consumed by UI. -- Hit-test mouse click. Returns true if the click was consumed by UI.
-- button: "left" | "right" -- button: "left" | "right"
function M.handle_click(mx, my, button) function M.handle_click(mx, my, button)
-- Cheatsheet modal absorbs all clicks while open (any click closes it)
if state.is_cheatsheet_open() then
actions.toggle_cheatsheet()
return true
end
-- Top toolbar buttons -- Top toolbar buttons
for _, b in ipairs(toolbar_buttons_layout()) do for _, b in ipairs(toolbar_buttons_layout()) do
if in_rect(mx, my, b.x, b.y, b.w, b.h) then if in_rect(mx, my, b.x, b.y, b.w, b.h) then
@@ -1004,7 +1017,7 @@ function update(ctx, dt)
actions.toggle_mode() actions.toggle_mode()
end end
if input.was_action_pressed("toggle_cheatsheet") then if input.was_action_pressed("toggle_cheatsheet") then
actions.toggle_cheatsheet() if state.is_modal_open("cheatsheet") then actions.close_modal() else actions.open_cheatsheet() end
end end
-- 0.2.0c hotkeys: transform controls (Direct-mode override paint) -- 0.2.0c hotkeys: transform controls (Direct-mode override paint)