From c8cd77b35a3d9716ae08edecb512fbfd4226f328 Mon Sep 17 00:00:00 2001 From: Pierre-Thomas Meisels Date: Sat, 25 May 2024 23:07:32 +0200 Subject: [PATCH] feat: Add layer rename in LayerEditor (#47) --- .../commands/change_layer_name_command.cpp | 31 +++++++++++ .../commands/change_layer_name_command.h | 32 ++++++++++++ .../emitters/command_to_action_transformer.h | 9 +++- src/editor/inspector/layers_editor.cpp | 52 ++++++++++++++++--- src/editor/inspector/layers_editor.h | 1 + src/editor/isometric_editor_plugin.cpp | 5 ++ src/node/isometric_map.cpp | 10 +++- src/node/isometric_map.h | 3 ++ 8 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 src/editor/commands/change_layer_name_command.cpp create mode 100644 src/editor/commands/change_layer_name_command.h diff --git a/src/editor/commands/change_layer_name_command.cpp b/src/editor/commands/change_layer_name_command.cpp new file mode 100644 index 0000000..9b983be --- /dev/null +++ b/src/editor/commands/change_layer_name_command.cpp @@ -0,0 +1,31 @@ +#ifdef TOOLS_ENABLED + +#include "change_layer_name_command.h" + +using namespace editor::commands; + +void ChangeLayerNameCommand::redo() { + context_node->set_layer_name(layer_id, new_layer_name); + + Command::redo(); +} + +void ChangeLayerNameCommand::undo() { + context_node->set_layer_name(layer_id, former_layer_name); + + Command::undo(); +} + +void ChangeLayerNameCommand::set_layer_id(uint32_t p_layer_id) { + layer_id = p_layer_id; +} + +void ChangeLayerNameCommand::set_new_layer_name(const String& p_new_layer_name) { + new_layer_name = p_new_layer_name; +} + +void ChangeLayerNameCommand::set_former_layer_name(const String& p_former_layer_name) { + former_layer_name = p_former_layer_name; +} + +#endif diff --git a/src/editor/commands/change_layer_name_command.h b/src/editor/commands/change_layer_name_command.h new file mode 100644 index 0000000..8dc0764 --- /dev/null +++ b/src/editor/commands/change_layer_name_command.h @@ -0,0 +1,32 @@ +#ifndef ISOMETRIC_MAPS_CHANGE_LAYER_NAME_COMMAND_H +#define ISOMETRIC_MAPS_CHANGE_LAYER_NAME_COMMAND_H + +#include "command.h" +#include "node/isometric_map.h" +#ifdef TOOLS_ENABLED + +namespace editor { + namespace commands { + class ChangeLayerNameCommand : public Command { + public: + void redo() override; + void undo() override; + + void set_layer_id(uint32_t p_layer_id); + void set_new_layer_name(const String& p_new_layer_name); + void set_former_layer_name(const String& p_former_layer_name); + + ChangeLayerNameCommand() = default; + ~ChangeLayerNameCommand() override = default; + + private: + String new_layer_name; + String former_layer_name; + uint32_t layer_id; + }; + } +} + +#endif + +#endif// ISOMETRIC_MAPS_CHANGE_LAYER_NAME_COMMAND_H diff --git a/src/editor/commands/emitters/command_to_action_transformer.h b/src/editor/commands/emitters/command_to_action_transformer.h index d146832..34e060d 100644 --- a/src/editor/commands/emitters/command_to_action_transformer.h +++ b/src/editor/commands/emitters/command_to_action_transformer.h @@ -15,6 +15,8 @@ namespace editor { public: template void transform(const Vector>>& commands, TContextNode* p_context); + template + void transform(const Vector>>& commands, TContextNode* p_context, const String& p_action_name); CommandToActionTransformer() = default; ~CommandToActionTransformer() = default; @@ -22,13 +24,18 @@ namespace editor { template void CommandToActionTransformer::transform(const Vector>>& commands, TContextNode* p_context) { + transform(commands, p_context, action_title); + } + + template + void CommandToActionTransformer::transform(const Vector>>& commands, TContextNode* p_context, const String& p_action_name) { bool has_valid_command {false}; for (int i = 0; i < commands.size(); ++i) { Ref> command {commands[i]}; if (command.is_null()) { continue; } if (!has_valid_command) { EditorUndoRedoManager::get_singleton()->create_action( - action_title, + p_action_name, merge_mode, p_context, backward_undo_ops diff --git a/src/editor/inspector/layers_editor.cpp b/src/editor/inspector/layers_editor.cpp index 9fbe192..8dc593e 100644 --- a/src/editor/inspector/layers_editor.cpp +++ b/src/editor/inspector/layers_editor.cpp @@ -1,16 +1,19 @@ #ifdef TOOLS_ENABLED #include "layers_editor.h" -#include "editor/isometric_editor_plugin.h" + #include "editor/commands/add_layer_command.h" +#include "editor/commands/change_layer_name_command.h" #include "editor/commands/revert_command.h" #include "editor/commands/set_layer_visibility_command.h" +#include "editor/isometric_editor_plugin.h" using namespace editor::inspector; -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 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 change_layer_name_action_name_format[] = "Change layer name (id: %s)"; LayersEditor::LayersEditor() { HBoxContainer* top_bar {memnew(HBoxContainer)}; @@ -87,9 +90,21 @@ void LayersEditor::refresh() { ); current_layer_check_box->set_button_group(current_layer_button_group); layer_controls_container->add_child(current_layer_check_box); - Label* current_layer_name_label {memnew(Label)}; - current_layer_name_label->set_text(layer_name); - layer_controls_container->add_child(current_layer_name_label); + + if (layer_name == node::IsometricMap::DEFAULT_LAYER_NAME) { + Label* current_layer_name_label {memnew(Label)}; + current_layer_name_label->set_text(layer_name); + layer_controls_container->add_child(current_layer_name_label); + } else { + LineEdit* current_layer_name_line_edit {memnew(LineEdit)}; + current_layer_name_line_edit->set_text(layer_name); + current_layer_name_line_edit->connect( + SNAME("text_changed"), + callable_mp(this, &LayersEditor::_on_layer_name_changed).bind(layer_id) + ); + layer_controls_container->add_child(current_layer_name_line_edit); + } + ColorPickerButton* color_picker_button {memnew(ColorPickerButton)}; color_picker_button->connect( SNAME("color_changed"), @@ -133,6 +148,8 @@ void LayersEditor::_add_layer() { commands::emitters::CommandToActionTransformer action_transformer; action_transformer.transform(commands, IsometricEditorPlugin::get_instance()->get_selected_map()); + + layer_line_edit->clear(); } void LayersEditor::_on_remove_layer_button(const uint32_t p_layer_id, const String& p_layer_name) { @@ -152,6 +169,27 @@ void LayersEditor::_on_remove_layer_button(const uint32_t p_layer_id, const Stri remove_layer_popup->popup_centered(); } +void LayersEditor::_on_layer_name_changed(const String& p_layer_name, uint32_t p_layer_id) { // NOLINT(*-convert-member-functions-to-static) + if (node::IsometricMap* current_map {IsometricEditorPlugin::get_instance()->get_selected_map()}) { + Vector>> commands; + + Ref change_layer_name_command; + change_layer_name_command.instantiate(); + change_layer_name_command->set_layer_id(p_layer_id); + change_layer_name_command->set_new_layer_name(p_layer_name); + change_layer_name_command->set_former_layer_name(current_map->get_layer_name(p_layer_id)); + + commands.push_back(change_layer_name_command); + + commands::emitters::CommandToActionTransformer action_transformer; + action_transformer.transform( + commands, + current_map, + vformat(change_layer_name_action_name_format, p_layer_id) + ); + } +} + void LayersEditor::_set_layer_visible(const uint32_t p_layer_id, CheckBox* p_check_box) { // NOLINT(*-convert-member-functions-to-static) Vector>> commands; diff --git a/src/editor/inspector/layers_editor.h b/src/editor/inspector/layers_editor.h index e70fbb2..28227df 100644 --- a/src/editor/inspector/layers_editor.h +++ b/src/editor/inspector/layers_editor.h @@ -31,6 +31,7 @@ namespace editor { void _add_layer(); void _on_remove_layer_button(uint32_t p_layer_id, const String& p_layer_name); + void _on_layer_name_changed(const String& p_layer_name, uint32_t p_layer_id); void _set_layer_visible(uint32_t p_layer_id, CheckBox* p_check_box); void _set_current_layer(uint32_t p_layer_id); void _layer_color_changed(const Color& p_color, uint32_t p_layer_id); diff --git a/src/editor/isometric_editor_plugin.cpp b/src/editor/isometric_editor_plugin.cpp index 950c7e6..eda1d14 100644 --- a/src/editor/isometric_editor_plugin.cpp +++ b/src/editor/isometric_editor_plugin.cpp @@ -120,6 +120,11 @@ void IsometricEditorPlugin::_notification(int p_notification) { // Add layers editor to dock layers_editor = memnew(editor::inspector::LayersEditor); + EditorUndoRedoManager::get_singleton()->connect( + SNAME("version_changed"), + callable_mp(layers_editor, &inspector::LayersEditor::refresh) + ); + RenderingServer::get_singleton()->connect("frame_post_draw", Callable(this, "_on_frame_post_draw")); } diff --git a/src/node/isometric_map.cpp b/src/node/isometric_map.cpp index 5a9d4b6..0f887b8 100644 --- a/src/node/isometric_map.cpp +++ b/src/node/isometric_map.cpp @@ -2,8 +2,6 @@ using namespace node; -static const char* DEFAULT_LAYER_NAME = "Default"; - IsometricMap::IsometricMap() : child_positionable_initialized(false) #ifdef TOOLS_ENABLED , last_layer_id(0) @@ -135,6 +133,14 @@ void IsometricMap::remove_layer(const String& p_layer_name) { remove_layer(layer_id); } +void IsometricMap::set_layer_name(uint32_t p_layer_id, const String& p_layer_name) { + layers[p_layer_id] = p_layer_name; +} + +String IsometricMap::get_layer_name(uint32_t p_layer_id) { + return layers[p_layer_id]; +} + uint32_t IsometricMap::get_layer_id_at(const Vector3& p_position) { return layers_grid_3d.get_data(p_position); } diff --git a/src/node/isometric_map.h b/src/node/isometric_map.h index cdf244f..ec07c86 100644 --- a/src/node/isometric_map.h +++ b/src/node/isometric_map.h @@ -13,6 +13,7 @@ namespace node { public: static constexpr uint32_t DEFAULT_LAYER_ID = 0; static constexpr uint32_t NO_LAYER_ID = 0xffffffff; + static constexpr const char* DEFAULT_LAYER_NAME = "Default"; #ifdef TOOLS_ENABLED static constexpr const char* LAST_EDITED_LAYER_META_NAME = "_LAST_EDITED_LAYER"; @@ -59,6 +60,8 @@ namespace node { uint32_t add_layer(const String& p_layer, uint32_t p_layer_id = NO_LAYER_ID); void remove_layer(uint32_t p_layer_id); void remove_layer(const String& p_layer_name); + void set_layer_name(uint32_t p_layer_id, const String& p_layer_name); + String get_layer_name(uint32_t p_layer_id); uint32_t get_layer_id_at(const Vector3& p_position); uint32_t get_last_layer_id() const; void set_last_layer_id(uint32_t p_last_layer_id);