class_name EditorUndoRedoStack extends RefCounted ## Command pattern over ChunkEditorModel mutations ## (docs/architecture/editor.md ยง7.1 `UndoRedoStack`). ## ## Thin wrapper around Godot's built-in `UndoRedo` -- the general-purpose ## class meant for use in game code (unlike the editor-only ## `EditorUndoRedoManager`, which only exists inside `EditorPlugin`s and ## isn't available at runtime). Every EditorCore tool (scripts/editor/tools/ ## *.gd) opens exactly one action per user operation and adds a do/undo ## Callable pair that calls the *same* ChunkEditorModel raw setter in both ## directions (e.g. `set_tile_raw` with the new id, then again with the old ## one) -- so undo genuinely reverts the mutation instead of re-deriving it. ## ## `UndoRedo` extends `Object`, not `RefCounted`, so it is not automatically ## freed; `_notification(NOTIFICATION_PREDELETE)` frees it when this wrapper ## (a RefCounted) goes out of scope. var _ur := UndoRedo.new() func _notification(what: int) -> void: if what == NOTIFICATION_PREDELETE and _ur != null: _ur.free() _ur = null ## Begin a new undoable action. Must be paired with `commit()`. func begin(action_name: String) -> void: _ur.create_action(action_name) ## Register the forward step of the current action. Called immediately by ## `commit()`, and again on every future `redo()`. func add_do(callable: Callable) -> void: _ur.add_do_method(callable) ## Register the reverse step of the current action. Called on every ## `undo()`. func add_undo(callable: Callable) -> void: _ur.add_undo_method(callable) ## Close the current action and execute its do-step once. func commit() -> void: _ur.commit_action() ## Close the current action without executing anything (used when a tool ## decides mid-way that there is nothing worth recording). func cancel() -> void: _ur.commit_action(false) func undo() -> bool: return _ur.undo() func redo() -> bool: return _ur.redo() func can_undo() -> bool: return _ur.has_undo() func can_redo() -> bool: return _ur.has_redo() func clear_history() -> void: _ur.clear_history()