From e373aa8a1fd4c5d623030ff5100f74b49b2adff2 Mon Sep 17 00:00:00 2001 From: Calic Date: Mon, 1 Jun 2026 19:31:00 +0200 Subject: [PATCH] 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) --- init.lua | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/init.lua b/init.lua index b6a8d7c..b379f39 100644 --- a/init.lua +++ b/init.lua @@ -66,7 +66,10 @@ local state = (function() }, dirty = false, 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 | (e.g. "File") + modal_open = nil, -- 0.3.0: nil | (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 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) @@ -136,11 +139,23 @@ local state = (function() state.mode = (state.mode == "auto-tile") and "direct" or "auto-tile" end - -- 0.2.0a: ?-modal cheatsheet visibility. - function M.is_cheatsheet_open() return state.cheatsheet_open end - function M.toggle_cheatsheet() - state.cheatsheet_open = not state.cheatsheet_open + function M.is_modal_open(id) + if id == nil then return state.modal_open ~= nil end + return state.modal_open == id 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) if x == nil then @@ -181,10 +196,13 @@ local state = (function() state.roof_mode = false state.dirty = false state.picker_open = false - state.cheatsheet_open = false state.mouse_cell = nil state.snap_vertex = 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.world_overlay = true for name in pairs(state.layer_visible) do @@ -310,7 +328,8 @@ local actions = (function() -- 0.2.0a actions function M.toggle_mode() state.toggle_mode() 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 function M.toggle_flip() state.toggle_flip() end @@ -724,7 +743,7 @@ local ui = (function() end 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() -- Dim the whole screen behind the modal local screen_w, screen_h = engine.window.size() @@ -843,19 +862,13 @@ local ui = (function() elseif id == "rotate" then actions.cycle_rotation() elseif id == "flip" then actions.toggle_flip() elseif id == "reset_xform" then actions.reset_transform() - elseif id == "help" then actions.toggle_cheatsheet() + elseif id == "help" then actions.open_cheatsheet() end end -- Hit-test mouse click. Returns true if the click was consumed by UI. -- button: "left" | "right" 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 for _, b in ipairs(toolbar_buttons_layout()) do 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() end 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 -- 0.2.0c hotkeys: transform controls (Direct-mode override paint)