Skip to content

Commit

Permalink
feat: Add select layer content button in LayerEditor (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
piiertho authored May 26, 2024
1 parent c8cd77b commit 4e936a9
Showing 4 changed files with 71 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/editor/inspector/layers_editor.cpp
Original file line number Diff line number Diff line change
@@ -7,13 +7,15 @@
#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;

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,13 +121,23 @@ 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(
SNAME("pressed"),
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<Ref<commands::Command<node::IsometricMap>>> commands;

for (const Vector3& position : PositionableSelectorManager::get_instance().get_selected_for_map(current_map)) {
Ref<commands::SelectPositionableCommand> select_command;
select_command.instantiate();
select_command->set_position(position);

Ref<commands::RevertCommand<node::IsometricMap>> 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<commands::SelectPositionableCommand> select_command;
select_command.instantiate();
select_command->set_position(position);

commands.push_back(select_command);
}

commands::emitters::CommandToActionTransformer action_transformer;
action_transformer.transform<node::IsometricMap, select_layer_content_action_name, UndoRedo::MERGE_DISABLE, true>(
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<Ref<commands::Command<node::IsometricMap>>> commands;

1 change: 1 addition & 0 deletions src/editor/inspector/layers_editor.h
Original file line number Diff line number Diff line change
@@ -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);
21 changes: 21 additions & 0 deletions src/node/isometric_map.cpp
Original file line number Diff line number Diff line change
@@ -180,6 +180,27 @@ void IsometricMap::set_layer_color(const uint32_t p_layer_id, const Color& p_col
}
}

Vector<Vector3> IsometricMap::get_layer_positions(uint32_t p_layer_id) const {
Vector<Vector3> positions;

const Vector<uint32_t>& 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() {
1 change: 1 addition & 0 deletions src/node/isometric_map.h
Original file line number Diff line number Diff line change
@@ -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<Vector3> get_layer_positions(uint32_t p_layer_id) const;
#endif

IsometricMap();

0 comments on commit 4e936a9

Please # to comment.