From 3936f76370587f27cafb837739bc67c058cefe65 Mon Sep 17 00:00:00 2001 From: Pierre-Thomas Meisels Date: Mon, 8 Apr 2024 09:59:19 +0200 Subject: [PATCH] enh: remove all LayersEditor classes in favor of method binds --- register_types.cpp | 3 - src/editor/inspector/layers_editor.cpp | 208 +++++++------------------ src/editor/inspector/layers_editor.h | 106 ++----------- src/editor/isometric_editor_plugin.cpp | 6 +- 4 files changed, 68 insertions(+), 255 deletions(-) diff --git a/register_types.cpp b/register_types.cpp index cff66aa..38fcd77 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -58,9 +58,6 @@ void initialize_isometric_maps_module(ModuleInitializationLevel p_level) { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); - ClassDB::register_class(); } #endif } diff --git a/src/editor/inspector/layers_editor.cpp b/src/editor/inspector/layers_editor.cpp index 5162356..189f079 100644 --- a/src/editor/inspector/layers_editor.cpp +++ b/src/editor/inspector/layers_editor.cpp @@ -14,9 +14,7 @@ constexpr const char set_layer_visible_action_name[] = "set_layer_visible"; constexpr const char layer_removed_signal[] = "layer_removed"; -LayersEditor::LayersEditor() : layer_line_edit(memnew(LineEdit)), - layer_controls_container(memnew(GridContainer)), - remove_layer_popup(memnew(RemoveLayerPopup)) { +LayersEditor::LayersEditor() { HBoxContainer* top_bar {memnew(HBoxContainer)}; layer_line_edit->set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); Button* add_button {memnew(Button)}; @@ -41,8 +39,9 @@ LayersEditor::LayersEditor() : layer_line_edit(memnew(LineEdit)), set_h_size_flags(SizeFlags::SIZE_EXPAND_FILL); set_v_size_flags(SizeFlags::SIZE_EXPAND_FILL); + remove_layer_popup->set_exclusive(true); + remove_layer_popup->add_child(remove_layer_popup_label); add_child(remove_layer_popup); - remove_layer_popup->connect(layer_removed_signal, callable_mp(this, &LayersEditor::refresh)); } void LayersEditor::refresh() { @@ -83,28 +82,40 @@ void LayersEditor::refresh() { for (int i = 0; i < layers.size(); ++i) { const String& layer_name {layer_names[i]}; const uint32_t layer_id {ids[i]}; - CurrentLayerCheckBox* current_layer_check_box {memnew(CurrentLayerCheckBox)}; - current_layer_check_box->set_layer_id(layer_id); + CheckBox* current_layer_check_box {memnew(CheckBox)}; + current_layer_check_box->connect( + SNAME("pressed"), + callable_mp(this, &LayersEditor::_set_current_layer).bind(layer_id) + ); 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); - LayerColorPickerButton* color_picker_button {memnew(LayerColorPickerButton)}; - color_picker_button->set_layer_id(layer_id); + ColorPickerButton* color_picker_button {memnew(ColorPickerButton)}; + color_picker_button->connect( + SNAME("color_changed"), + callable_mp(this, &LayersEditor::_layer_color_changed).bind(layer_id) + ); const Variant& layer_color { current_map->get_meta(vformat(node::IsometricMap::LAYER_COLOR_META_NAME_FORMAT, layer_id), Color()) }; 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); - LayerVisibleCheckBox* visible_check_box {memnew(LayerVisibleCheckBox)}; - visible_check_box->set_layer_id(layer_id); + 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); - LayerRemoveButton* layer_remove_button {memnew(LayerRemoveButton)}; - layer_remove_button->set_layer_informations(layer_id, layer_name); - layer_remove_button->set_layer_remove_popup(remove_layer_popup); - layer_remove_button->connect(SNAME("pressed"), callable_mp(this, &LayersEditor::refresh)); + Button* layer_remove_button {memnew(Button)}; + layer_remove_button->set_text("-"); + layer_remove_button->connect( + SNAME("pressed"), + callable_mp(this, &LayersEditor::_on_remove_layer_button).bind(layer_id, layer_name) + ); layer_controls_container->add_child(layer_remove_button); current_layer_check_box->set_pressed(layer_id == last_layer_edited); @@ -125,30 +136,30 @@ void LayersEditor::_add_layer() { refresh(); } -uint32_t LayersEditor::get_selected_layer_id() const { - if (auto check_box {Object::cast_to(current_layer_button_group->get_pressed_button())}) { - return check_box->get_layer_id(); +void LayersEditor::_on_remove_layer_button(const uint32_t p_layer_id, const String& p_layer_name) { + remove_layer_popup_label->set_text( + vformat( + "You're going to remove layer %s.\n" + "This will remove all tiles included in layer from map.\n" + "Please confirm removal.", + p_layer_name + ) + ); + Callable callable {callable_mp(this, &LayersEditor::_remove_layer)}; + if (remove_layer_popup->is_connected(SNAME("confirmed"), callable)) { + remove_layer_popup->disconnect(SNAME("confirmed"), callable); } - - return node::IsometricMap::DEFAULT_LAYER_ID; + remove_layer_popup->connect(SNAME("confirmed"), callable.bind(p_layer_id, p_layer_name)); + remove_layer_popup->popup_centered(); } -void LayersEditor::_bind_methods() { -} - -///////////////////////////////// LayerVisibleCheckBox //////////////////////////////////////////// - -void LayerVisibleCheckBox::set_layer_id(uint32_t p_layer_id) { - layer_id = p_layer_id; -} - -void LayerVisibleCheckBox::_set_layer_visible() { +void LayersEditor::_set_layer_visible(const uint32_t p_layer_id, CheckBox* p_check_box) { // NOLINT(*-convert-member-functions-to-static) Vector>> commands; Ref visibility_command; visibility_command.instantiate(); - visibility_command->set_layer_id(layer_id); - visibility_command->set_visible(is_pressed()); + visibility_command->set_layer_id(p_layer_id); + visibility_command->set_visible(p_check_box->is_pressed()); commands.push_back(visibility_command); @@ -159,41 +170,19 @@ void LayerVisibleCheckBox::_set_layer_visible() { ); } -void LayerVisibleCheckBox::_notification(int notif) { - if (notif != NOTIFICATION_POSTINITIALIZE) { - return; +void LayersEditor::_set_current_layer(uint32_t p_layer_id) { // NOLINT(*-convert-member-functions-to-static) + if (node::IsometricMap* current_map{IsometricEditorPlugin::get_instance()->get_selected_map()}) { + current_map->set_meta(node::IsometricMap::LAST_EDITED_LAYER_META_NAME,p_layer_id); } - - set_pressed(true); - - connect(SNAME("pressed"), Callable(this, "_set_layer_visible")); -} - -LayerVisibleCheckBox::LayerVisibleCheckBox() : layer_id(node::IsometricMap::NO_LAYER_ID) { - } -void LayerVisibleCheckBox::_bind_methods() { - ClassDB::bind_method(D_METHOD("_set_layer_visible"), &LayerVisibleCheckBox::_set_layer_visible); -} - -///////////////////////////////// RemoveLayerPopup //////////////////////////////////////////// - -void RemoveLayerPopup::set_layer_informations(uint32_t p_layer_id, const String& p_layer_name) { - layer_id = p_layer_id; - layer_name = p_layer_name; - - label->set_text( - vformat( - "You're going to remove layer %s.\n" - "This will remove all tiles included in layer from map.\n" - "Please confirm removal.", - layer_name - ) - ); +void LayersEditor::_layer_color_changed(const Color& p_color, uint32_t p_layer_id) { // NOLINT(*-convert-member-functions-to-static) + if (node::IsometricMap* map{IsometricEditorPlugin::get_instance()->get_selected_map()}) { + map->set_layer_color(p_layer_id, p_color); + } } -void RemoveLayerPopup::_remove_layer() { +void LayersEditor::_remove_layer(uint32_t p_layer_id, const String& p_layer_name) { if (node::IsometricMap* current_map = IsometricEditorPlugin::get_instance()->get_selected_map()) { Vector>> commands; @@ -204,7 +193,7 @@ void RemoveLayerPopup::_remove_layer() { Vector3i position_in_map {x, y, z}; uint32_t layer_id_at_position {current_map->get_layer_id_at(position_in_map)}; - if (layer_id_at_position != layer_id) { + if (layer_id_at_position != p_layer_id) { continue; } @@ -229,8 +218,8 @@ void RemoveLayerPopup::_remove_layer() { Ref add_layer_command; add_layer_command.instantiate(); - add_layer_command->set_layer_id(layer_id); - add_layer_command->set_layer_name(layer_name); + add_layer_command->set_layer_id(p_layer_id); + add_layer_command->set_layer_name(p_layer_name); Ref> remove_layer_command; remove_layer_command.instantiate(); @@ -245,101 +234,16 @@ void RemoveLayerPopup::_remove_layer() { } } -void RemoveLayerPopup::_notification(int notif) { +void LayersEditor::_notification(int notif) { if (notif != NOTIFICATION_POSTINITIALIZE) { return; } - connect("confirmed", callable_mp(this, &RemoveLayerPopup::_remove_layer)); -} - -RemoveLayerPopup::RemoveLayerPopup() : label(memnew(Label)), layer_id(node::IsometricMap::NO_LAYER_ID) { - set_exclusive(true); - add_child(label); + connect(layer_removed_signal, callable_mp(this, &LayersEditor::refresh)); } -void RemoveLayerPopup::_bind_methods() { +void LayersEditor::_bind_methods() { ADD_SIGNAL(MethodInfo(layer_removed_signal)); } -///////////////////////////////// LayerRemoveButton //////////////////////////////////////////// - -void LayerRemoveButton::set_layer_informations(uint32_t p_layer_id, const String& p_layer_name) { - layer_id = p_layer_id; - layer_name = p_layer_name; - connect(SNAME("pressed"), callable_mp(this, &LayerRemoveButton::_on_remove_button)); - - set_text("-"); -} - -void LayerRemoveButton::set_layer_remove_popup(RemoveLayerPopup* p_remove_layer_popup) { - remove_popup = p_remove_layer_popup; -} - -void LayerRemoveButton::_on_remove_button() { - remove_popup->set_layer_informations(layer_id, layer_name); - remove_popup->popup_centered(); -} - -LayerRemoveButton::LayerRemoveButton() : remove_popup(nullptr), layer_name(), layer_id(node::IsometricMap::NO_LAYER_ID) { -} - -void LayerRemoveButton::_bind_methods() {} - -///////////////////////////////// CurrentLayerCheckBox //////////////////////////////////////////// - -uint32_t CurrentLayerCheckBox::get_layer_id() const { - return layer_id; -} - -void CurrentLayerCheckBox::set_layer_id(uint32_t p_layer_id) { - layer_id = p_layer_id; -} - -void CurrentLayerCheckBox::on_pressed() { // NOLINT(*-make-member-function-const) - if (node::IsometricMap* current_map{IsometricEditorPlugin::get_instance()->get_selected_map()}) { - current_map->set_meta(node::IsometricMap::LAST_EDITED_LAYER_META_NAME,layer_id); - } -} - -void CurrentLayerCheckBox::_notification(int notif) { - if (notif != NOTIFICATION_POSTINITIALIZE) { - return; - } - - connect(SNAME("pressed"), callable_mp(this, &CurrentLayerCheckBox::on_pressed)); -} - -CurrentLayerCheckBox::CurrentLayerCheckBox() : layer_id(node::IsometricMap::NO_LAYER_ID) { - -} - -void CurrentLayerCheckBox::_bind_methods() { - -} - -///////////////////////////////// LayerColorPickerButton //////////////////////////////////////////// - -void LayerColorPickerButton::set_layer_id(const uint32_t p_layer_id) { - layer_id = p_layer_id; -} - -void LayerColorPickerButton::on_color_changed(const Color& p_color) { // NOLINT(*-make-member-function-const) - if (node::IsometricMap* map{IsometricEditorPlugin::get_instance()->get_selected_map()}) { - map->set_layer_color(layer_id, p_color); - } -} - -void LayerColorPickerButton::_notification(int notif) { - if (notif != NOTIFICATION_POSTINITIALIZE) { - return; - } - - connect(SNAME("color_changed"), callable_mp(this, &LayerColorPickerButton::on_color_changed)); -} - -LayerColorPickerButton::LayerColorPickerButton() : layer_id(node::IsometricMap::NO_LAYER_ID) {} - -void LayerColorPickerButton::_bind_methods() {} - #endif \ No newline at end of file diff --git a/src/editor/inspector/layers_editor.h b/src/editor/inspector/layers_editor.h index 60a67b7..3c42137 100644 --- a/src/editor/inspector/layers_editor.h +++ b/src/editor/inspector/layers_editor.h @@ -14,115 +14,27 @@ namespace editor { namespace inspector { - class RemoveLayerPopup : public ConfirmationDialog { - GDCLASS(RemoveLayerPopup, ConfirmationDialog) - - public: - void set_layer_informations(uint32_t p_layer_id, const String& p_layer_name); - - RemoveLayerPopup(); - - private: - Label* label; - String layer_name; - uint32_t layer_id; - - void _remove_layer(); - - protected: - void _notification(int notif); - static void _bind_methods(); - }; - class LayersEditor : public VBoxContainer { GDCLASS(LayersEditor, VBoxContainer) public: void refresh(); - uint32_t get_selected_layer_id() const; LayersEditor(); private: - LineEdit* layer_line_edit; - GridContainer* layer_controls_container; - RemoveLayerPopup* remove_layer_popup; + LineEdit* layer_line_edit {memnew(LineEdit)}; + GridContainer* layer_controls_container {memnew(GridContainer)}; + ConfirmationDialog* remove_layer_popup {memnew(ConfirmationDialog)}; + Label* remove_layer_popup_label {memnew(Label)}; Ref current_layer_button_group; void _add_layer(); - - protected: - static void _bind_methods(); - }; - - class LayerVisibleCheckBox : public CheckBox { - GDCLASS(LayerVisibleCheckBox, CheckBox) - - public: - void set_layer_id(uint32_t p_layer_id); - - LayerVisibleCheckBox(); - - private: - uint32_t layer_id; - - void _set_layer_visible(); - - protected: - void _notification(int notif); - static void _bind_methods(); - }; - - class LayerRemoveButton : public Button { - GDCLASS(LayerRemoveButton, Button) - - public: - void set_layer_informations(uint32_t p_layer_id, const String& p_layer_name); - void set_layer_remove_popup(RemoveLayerPopup* p_remove_layer_popup); - - LayerRemoveButton(); - - private: - RemoveLayerPopup* remove_popup; - String layer_name; - uint32_t layer_id; - - void _on_remove_button(); - - protected: - static void _bind_methods(); - }; - - class CurrentLayerCheckBox : public CheckBox { - GDCLASS(CurrentLayerCheckBox, CheckBox) - - public: - uint32_t get_layer_id() const; - void set_layer_id(uint32_t p_layer_id); - - void on_pressed(); - - CurrentLayerCheckBox(); - - private: - uint32_t layer_id; - - protected: - void _notification(int notif); - static void _bind_methods(); - }; - - class LayerColorPickerButton : public ColorPickerButton { - GDCLASS(LayerColorPickerButton, ColorPickerButton) - - public: - void set_layer_id(uint32_t p_layer_id); - void on_color_changed(const Color& p_color); - - LayerColorPickerButton(); - - private: - uint32_t layer_id; + void _on_remove_layer_button(uint32_t p_layer_id, const String& p_layer_name); + 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); + void _remove_layer(uint32_t p_layer_id, const String& p_layer_name); protected: void _notification(int notif); diff --git a/src/editor/isometric_editor_plugin.cpp b/src/editor/isometric_editor_plugin.cpp index ea225f3..b2c087d 100644 --- a/src/editor/isometric_editor_plugin.cpp +++ b/src/editor/isometric_editor_plugin.cpp @@ -285,11 +285,11 @@ bool IsometricEditorPlugin::is_aabb_in_view_limiters(const AABB& p_aabb) const { } uint32_t IsometricEditorPlugin::get_selected_layer() const { - if (!layers_editor->get_parent()) { - return node::IsometricMap::DEFAULT_LAYER_ID; + if (selected_map) { + return selected_map->get_meta(node::IsometricMap::LAST_EDITED_LAYER_META_NAME, node::IsometricMap::DEFAULT_LAYER_ID); } - return layers_editor->get_selected_layer_id(); + return node::IsometricMap::DEFAULT_LAYER_ID; } IsometricEditorPlugin::MapHandlingData::MapHandlingData() :