From 3ac33af60515620502f1eed95f9513af24f13687 Mon Sep 17 00:00:00 2001 From: Pierre-Thomas Meisels Date: Mon, 8 Apr 2024 14:07:00 +0200 Subject: [PATCH] fix: Add callable when executing command and use it to refresh layer editor when add or remove layer --- src/editor/commands/add_layer_command.cpp | 8 +++-- src/editor/commands/add_layer_command.h | 2 +- .../commands/add_positionable_command.cpp | 2 ++ src/editor/commands/command.h | 34 +++++++++++++++++-- src/editor/commands/composite_command.h | 2 ++ .../commands/move_editor_plane_command.cpp | 2 ++ src/editor/commands/revert_command.h | 2 ++ .../commands/rotate_editor_plane_command.cpp | 2 ++ .../commands/select_positionable_command.cpp | 4 +++ .../commands/set_layer_visibility_command.cpp | 4 +++ .../set_positionable_visibility_command.cpp | 1 + src/editor/inspector/layers_editor.cpp | 23 +++---------- src/editor/inspector/layers_editor.h | 4 --- 13 files changed, 62 insertions(+), 28 deletions(-) diff --git a/src/editor/commands/add_layer_command.cpp b/src/editor/commands/add_layer_command.cpp index dc334f8..daeba7d 100644 --- a/src/editor/commands/add_layer_command.cpp +++ b/src/editor/commands/add_layer_command.cpp @@ -7,15 +7,17 @@ using namespace editor::commands; void AddLayerCommand::redo() { - context_node->add_layer(layer_name, layer_id); + layer_id = context_node->add_layer(layer_name, layer_id); + Command::redo(); } void AddLayerCommand::undo() { if (layer_id == node::IsometricMap::NO_LAYER_ID) { context_node->remove_layer(layer_name); - return; + } else { + context_node->remove_layer(layer_id); } - context_node->remove_layer(layer_id); + Command::undo(); } void AddLayerCommand::set_layer_id(uint32_t p_layer_id) { diff --git a/src/editor/commands/add_layer_command.h b/src/editor/commands/add_layer_command.h index 906b8ec..a8d5fa9 100644 --- a/src/editor/commands/add_layer_command.h +++ b/src/editor/commands/add_layer_command.h @@ -21,8 +21,8 @@ namespace editor { ~AddLayerCommand() override = default; private: - uint32_t layer_id; String layer_name; + uint32_t layer_id; }; } } diff --git a/src/editor/commands/add_positionable_command.cpp b/src/editor/commands/add_positionable_command.cpp index e1e58e1..40edbf0 100644 --- a/src/editor/commands/add_positionable_command.cpp +++ b/src/editor/commands/add_positionable_command.cpp @@ -11,10 +11,12 @@ void AddPositionableCommand::redo() { positionable_id, layer_id ); + Command::redo(); } void AddPositionableCommand::undo() { context_node->remove_positionable(aabb); + Command::undo(); } void AddPositionableCommand::set_aabb(const AABB& p_aabb) { diff --git a/src/editor/commands/command.h b/src/editor/commands/command.h index be09fc1..427dc19 100644 --- a/src/editor/commands/command.h +++ b/src/editor/commands/command.h @@ -8,6 +8,8 @@ #include "editor/editor_undo_redo_manager.h" #include "scene/main/node.h" +constexpr const char* undo_or_redo_triggered { "undo_or_redo_triggered" }; + namespace editor { namespace commands { @@ -21,16 +23,32 @@ namespace editor { static void _bind_methods(); public: - virtual void redo() = 0; - virtual void undo() = 0; + virtual void redo(); + virtual void undo(); virtual void set_context_node(TContextNode* p_context_node); + void add_hook(const Callable& p_hook); void append_to_undoredo(); Command(); ~Command() override = default; + + private: + Vector hooks; + + void _execute_hooks() const; }; + template + void Command::redo() { + _execute_hooks(); + } + + template + void Command::undo() { + _execute_hooks(); + } + template void Command::append_to_undoredo() { EditorUndoRedoManager::get_singleton()->add_do_method(this, "redo"); @@ -42,6 +60,18 @@ namespace editor { context_node = p_context_node; } + template + void Command::add_hook(const Callable& p_hook) { + hooks.append(p_hook); + } + + template + void Command::_execute_hooks() const { + for (const Callable& hook : hooks) { + hook.call(); + } + } + template Command::Command() : context_node(nullptr) {} diff --git a/src/editor/commands/composite_command.h b/src/editor/commands/composite_command.h index fa58ca5..1f47962 100644 --- a/src/editor/commands/composite_command.h +++ b/src/editor/commands/composite_command.h @@ -28,6 +28,7 @@ namespace editor { for (int i = 0; i < commands.size(); ++i) { commands.get(i)->redo(); } + Command::redo(); } template @@ -35,6 +36,7 @@ namespace editor { for (int i = commands.size() - 1; i >= 0; --i) { commands.get(i)->undo(); } + Command::undo(); } template diff --git a/src/editor/commands/move_editor_plane_command.cpp b/src/editor/commands/move_editor_plane_command.cpp index 7bc330e..5027f71 100644 --- a/src/editor/commands/move_editor_plane_command.cpp +++ b/src/editor/commands/move_editor_plane_command.cpp @@ -12,12 +12,14 @@ void MoveEditorPlaneCommand::redo() { IsometricEditorPlugin* isometric_editor_plugin = IsometricEditorPlugin::get_instance(); isometric_editor_plugin->get_editor_plane_for_map(context_node, plane_type).set_position(new_position); isometric_editor_plugin->refresh(plane_type); + Command::redo(); } void MoveEditorPlaneCommand::undo() { IsometricEditorPlugin* isometric_editor_plugin = IsometricEditorPlugin::get_instance(); isometric_editor_plugin->get_editor_plane_for_map(context_node, plane_type).set_position(old_position); isometric_editor_plugin->refresh(plane_type); + Command::undo(); } void MoveEditorPlaneCommand::set_new_position(int p_new_position) { diff --git a/src/editor/commands/revert_command.h b/src/editor/commands/revert_command.h index ee7f407..cf42c12 100644 --- a/src/editor/commands/revert_command.h +++ b/src/editor/commands/revert_command.h @@ -26,11 +26,13 @@ namespace editor { template void RevertCommand::redo() { reverse_command->undo(); + Command::redo(); } template void RevertCommand::undo() { reverse_command->redo(); + Command::undo(); } template diff --git a/src/editor/commands/rotate_editor_plane_command.cpp b/src/editor/commands/rotate_editor_plane_command.cpp index 246a01e..d96ea15 100644 --- a/src/editor/commands/rotate_editor_plane_command.cpp +++ b/src/editor/commands/rotate_editor_plane_command.cpp @@ -8,10 +8,12 @@ using namespace editor::commands; void RotateEditorPlaneCommand::redo() { set_axis_and_position(new_axis, 0); + Command::redo(); } void RotateEditorPlaneCommand::undo() { set_axis_and_position(former_axis, former_position); + Command::undo(); } void RotateEditorPlaneCommand::set_new_axis(Vector3::Axis p_axis) { diff --git a/src/editor/commands/select_positionable_command.cpp b/src/editor/commands/select_positionable_command.cpp index 35485c2..c62fba0 100644 --- a/src/editor/commands/select_positionable_command.cpp +++ b/src/editor/commands/select_positionable_command.cpp @@ -17,6 +17,8 @@ void SelectPositionableCommand::redo() { } else { editor::PositionableSelectorManager::get_instance().select_positionable_at(context_node, positionable); } + + Command::redo(); } void SelectPositionableCommand::undo() { @@ -30,6 +32,8 @@ void SelectPositionableCommand::undo() { if (should_deselect_first) { editor::PositionableSelectorManager::get_instance().set_selected_for_map(context_node, selected_cache); } + + Command::undo(); } void SelectPositionableCommand::set_position(const Vector3& p_position) { diff --git a/src/editor/commands/set_layer_visibility_command.cpp b/src/editor/commands/set_layer_visibility_command.cpp index e636e61..1054334 100644 --- a/src/editor/commands/set_layer_visibility_command.cpp +++ b/src/editor/commands/set_layer_visibility_command.cpp @@ -7,10 +7,14 @@ using namespace editor::commands; void SetLayerVisibilityCommand::redo() { context_node->set_layer_visible(layer_id, is_visible); + + Command::redo(); } void SetLayerVisibilityCommand::undo() { context_node->set_layer_visible(layer_id, !is_visible); + + Command::undo(); } void SetLayerVisibilityCommand::set_layer_id(uint32_t p_layer_id) { diff --git a/src/editor/commands/set_positionable_visibility_command.cpp b/src/editor/commands/set_positionable_visibility_command.cpp index 60ad61c..75c9399 100644 --- a/src/editor/commands/set_positionable_visibility_command.cpp +++ b/src/editor/commands/set_positionable_visibility_command.cpp @@ -9,6 +9,7 @@ void SetPositionableVisibilityCommand::redo() { if (node::IsometricPositionable * positionable {context_node->get_positionable_at(position)}) { positionable->set_visible(!positionable->is_visible()); } + Command::redo(); } void SetPositionableVisibilityCommand::undo() { diff --git a/src/editor/inspector/layers_editor.cpp b/src/editor/inspector/layers_editor.cpp index 189f079..9fbe192 100644 --- a/src/editor/inspector/layers_editor.cpp +++ b/src/editor/inspector/layers_editor.cpp @@ -12,8 +12,6 @@ constexpr const char add_layer_action_name[] = "add_layer"; constexpr const char remove_layer_action_name[] = "remove_layer"; constexpr const char set_layer_visible_action_name[] = "set_layer_visible"; -constexpr const char layer_removed_signal[] = "layer_removed"; - LayersEditor::LayersEditor() { HBoxContainer* top_bar {memnew(HBoxContainer)}; layer_line_edit->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); @@ -128,12 +126,13 @@ void LayersEditor::_add_layer() { Ref add_layer_command; add_layer_command.instantiate(); add_layer_command->set_layer_name(layer_line_edit->get_text()); + + add_layer_command->add_hook(callable_mp(this, &LayersEditor::refresh)); + commands.push_back(add_layer_command); commands::emitters::CommandToActionTransformer action_transformer; action_transformer.transform(commands, IsometricEditorPlugin::get_instance()->get_selected_map()); - - refresh(); } void LayersEditor::_on_remove_layer_button(const uint32_t p_layer_id, const String& p_layer_name) { @@ -225,25 +224,13 @@ void LayersEditor::_remove_layer(uint32_t p_layer_id, const String& p_layer_name remove_layer_command.instantiate(); remove_layer_command->set_reverse_command(add_layer_command); + remove_layer_command->add_hook(callable_mp(this, &LayersEditor::refresh)); + commands.push_back(remove_layer_command); commands::emitters::CommandToActionTransformer action_transformer; action_transformer.transform(commands, current_map); - - emit_signal(layer_removed_signal); } } -void LayersEditor::_notification(int notif) { - if (notif != NOTIFICATION_POSTINITIALIZE) { - return; - } - - connect(layer_removed_signal, callable_mp(this, &LayersEditor::refresh)); -} - -void LayersEditor::_bind_methods() { - ADD_SIGNAL(MethodInfo(layer_removed_signal)); -} - #endif \ No newline at end of file diff --git a/src/editor/inspector/layers_editor.h b/src/editor/inspector/layers_editor.h index 3c42137..e70fbb2 100644 --- a/src/editor/inspector/layers_editor.h +++ b/src/editor/inspector/layers_editor.h @@ -35,10 +35,6 @@ namespace editor { void _set_current_layer(uint32_t p_layer_id); void _layer_color_changed(const Color& p_color, uint32_t p_layer_id); void _remove_layer(uint32_t p_layer_id, const String& p_layer_name); - - protected: - void _notification(int notif); - static void _bind_methods(); }; }// namespace inspector }// namespace editor