From 4e936a94d8a3cbfb84823def104e824ca7a11293 Mon Sep 17 00:00:00 2001 From: Pierre-Thomas Meisels Date: Sun, 26 May 2024 10:58:54 +0200 Subject: [PATCH] feat: Add select layer content button in LayerEditor (#48) --- src/editor/inspector/layers_editor.cpp | 49 +++++++++++++++++++++++++- src/editor/inspector/layers_editor.h | 1 + src/node/isometric_map.cpp | 21 +++++++++++ src/node/isometric_map.h | 1 + 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/editor/inspector/layers_editor.cpp b/src/editor/inspector/layers_editor.cpp index 8dc593e..a691567 100644 --- a/src/editor/inspector/layers_editor.cpp +++ b/src/editor/inspector/layers_editor.cpp @@ -7,6 +7,7 @@ #include "editor/commands/revert_command.h" #include "editor/commands/set_layer_visibility_command.h" #include "editor/isometric_editor_plugin.h" +#include "editor/positionable_selector_manager.h" using namespace editor::inspector; @@ -14,6 +15,7 @@ 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)"; +constexpr const char select_layer_content_action_name[] = "Select layer content"; LayersEditor::LayersEditor() { HBoxContainer* top_bar {memnew(HBoxContainer)}; @@ -25,7 +27,7 @@ LayersEditor::LayersEditor() { add_button->connect(SNAME("pressed"), callable_mp(this, &LayersEditor::_add_layer)); add_child(top_bar); ScrollContainer* scroll_container {memnew(ScrollContainer)}; - layer_controls_container->set_columns(5); + layer_controls_container->set_columns(6); layer_controls_container->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); scroll_container->add_child(layer_controls_container); add_child(scroll_container); @@ -62,12 +64,15 @@ void LayersEditor::refresh() { layer_name_label->set_text("Layer name"); Label* color_label {memnew(Label)}; color_label->set_text("Color"); + Label* select_layer_content_label {memnew(Label)}; + select_layer_content_label->set_text("Select content"); Label* remove_label {memnew(Label)}; remove_label->set_text("Remove"); layer_controls_container->add_child(is_current_layer_label); layer_controls_container->add_child(layer_name_label); layer_controls_container->add_child(color_label); layer_controls_container->add_child(is_visible_label); + layer_controls_container->add_child(select_layer_content_label); layer_controls_container->add_child(remove_label); uint32_t last_layer_edited { @@ -116,6 +121,7 @@ void LayersEditor::refresh() { color_picker_button->set_pick_color(layer_color); current_map->set_layer_color(layer_id, layer_color); layer_controls_container->add_child(color_picker_button); + CheckBox* visible_check_box {memnew(CheckBox)}; visible_check_box->set_pressed(true); visible_check_box->connect( @@ -123,6 +129,15 @@ void LayersEditor::refresh() { callable_mp(this, &LayersEditor::_set_layer_visible).bind(layer_id, visible_check_box) ); layer_controls_container->add_child(visible_check_box); + + Button* layer_select_content_button {memnew(Button)}; + layer_select_content_button->set_text("select"); + layer_select_content_button->connect( + SNAME("pressed"), + callable_mp(this, &LayersEditor::_on_layer_select_content).bind(layer_id) + ); + layer_controls_container->add_child(layer_select_content_button); + Button* layer_remove_button {memnew(Button)}; layer_remove_button->set_text("-"); layer_remove_button->connect( @@ -190,6 +205,38 @@ void LayersEditor::_on_layer_name_changed(const String& p_layer_name, uint32_t p } } +void LayersEditor::_on_layer_select_content(uint32_t p_layer_id) { // NOLINT(*-convert-member-functions-to-static) + if (node::IsometricMap* current_map {IsometricEditorPlugin::get_instance()->get_selected_map()}) { + Vector>> commands; + + for (const Vector3& position : PositionableSelectorManager::get_instance().get_selected_for_map(current_map)) { + Ref select_command; + select_command.instantiate(); + select_command->set_position(position); + + Ref> deselect_command; + deselect_command.instantiate(); + deselect_command->set_reverse_command(select_command); + + commands.push_back(deselect_command); + } + + for (const Vector3& position : current_map->get_layer_positions(p_layer_id)) { + Ref select_command; + select_command.instantiate(); + select_command->set_position(position); + + commands.push_back(select_command); + } + + commands::emitters::CommandToActionTransformer action_transformer; + action_transformer.transform( + commands, + current_map + ); + } +} + 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 28227df..0792aa0 100644 --- a/src/editor/inspector/layers_editor.h +++ b/src/editor/inspector/layers_editor.h @@ -32,6 +32,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 _on_layer_select_content(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/node/isometric_map.cpp b/src/node/isometric_map.cpp index 0f887b8..e4e8b7a 100644 --- a/src/node/isometric_map.cpp +++ b/src/node/isometric_map.cpp @@ -180,6 +180,27 @@ void IsometricMap::set_layer_color(const uint32_t p_layer_id, const Color& p_col } } +Vector IsometricMap::get_layer_positions(uint32_t p_layer_id) const { + Vector positions; + + const Vector& layers_vector {layers_grid_3d.get_internal_array()}; + for(int i = 0; i < layers_vector.size(); ++i) { + uint32_t id {layers_vector[i]}; + if (p_layer_id != id) { + continue; + } + + const Vector3& position = layers_grid_3d.get_position_3d_from_index(i); + if (!instances_grid_3d.get_data(position)) { + continue; + } + + positions.push_back(position); + } + + return positions; +} + #endif void IsometricMap::_enter_tree() { diff --git a/src/node/isometric_map.h b/src/node/isometric_map.h index ec07c86..ba85a0d 100644 --- a/src/node/isometric_map.h +++ b/src/node/isometric_map.h @@ -67,6 +67,7 @@ namespace node { void set_last_layer_id(uint32_t p_last_layer_id); void set_layer_visible(uint32_t p_layer_id, bool is_visible); void set_layer_color(uint32_t p_layer_id, const Color& p_color); + Vector get_layer_positions(uint32_t p_layer_id) const; #endif IsometricMap();