From 9fefe7619ac70b58b9165b56f5d3f93eab803e5b Mon Sep 17 00:00:00 2001 From: Calic Date: Sun, 24 May 2026 01:11:35 +0200 Subject: [PATCH] Add editor actions module composing maps write APIs paint_cell_at, erase_cell_at, cycle_rotation, save plus thin wrappers for the UI hit-tests (set_active_layer/_atlas/_tile, toggle_layer_visible/_roof_mode/_picker). Each action updates state and delegates the actual map mutation to lib-core.maps. --- src/actions.lua | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/actions.lua diff --git a/src/actions.lua b/src/actions.lua new file mode 100644 index 0000000..6efedba --- /dev/null +++ b/src/actions.lua @@ -0,0 +1,86 @@ +-- src/actions.lua +-- High-level edit operations. Combines state mutations with lib-core.maps writes. + +local maps = require("lib-core.maps") +local state = require("src.state") + +local M = {} + +-- ===================================================================== +-- Cell painting +-- ===================================================================== + +function M.paint_cell_at(cell_x, cell_y) + if state.is_roof_mode() then + maps.set_roof(cell_x, cell_y, 1) + else + local gid = maps.encode_gid( + state.get_active_atlas(), + state.get_active_tile(), + state.get_active_rot() + ) + maps.set_cell_gid(state.get_active_layer(), cell_x, cell_y, gid) + end + state.mark_dirty() +end + +function M.erase_cell_at(cell_x, cell_y) + if state.is_roof_mode() then + maps.set_roof(cell_x, cell_y, 0) + else + maps.set_cell_gid(state.get_active_layer(), cell_x, cell_y, 0) + end + state.mark_dirty() +end + +-- ===================================================================== +-- Hotkey-driven actions +-- ===================================================================== + +function M.cycle_rotation() + state.cycle_rotation() +end + +function M.save() + local id = state.get_map_id() + local path = state.get_map_path() + if not id or not path then + engine.print("map-editor: cannot save — no map loaded") + return + end + maps.save_to_disk(id, path) + state.mark_clean() + engine.print(string.format("map-editor: saved '%s' to %s", id, path)) +end + +-- ===================================================================== +-- UI-driven actions (called by ui.lua hit-tests) +-- ===================================================================== + +function M.set_active_layer(layer_name) + state.set_active_layer(layer_name) +end + +function M.toggle_layer_visible(layer_name) + state.toggle_layer_visible(layer_name) +end + +function M.toggle_roof_mode() + state.toggle_roof_mode() +end + +function M.set_active_atlas(atlas_idx) + state.set_active_atlas(atlas_idx) + -- Reset to first tile of the new atlas + state.set_active_tile(1) +end + +function M.set_active_tile(tile_id) + state.set_active_tile(tile_id) +end + +function M.toggle_picker() + state.toggle_picker_open() +end + +return M