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.
This commit is contained in:
Calic
2026-05-24 01:11:35 +02:00
parent 0b2eb20199
commit 9fefe7619a

86
src/actions.lua Normal file
View File

@@ -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