Skip to content

Commit

Permalink
enh: remove all LayersEditor classes in favor of method binds
Browse files Browse the repository at this point in the history
  • Loading branch information
piiertho committed Apr 8, 2024
1 parent 19cd75c commit 3936f76
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 255 deletions.
3 changes: 0 additions & 3 deletions register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ void initialize_isometric_maps_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<editor::inspector::PositionableSelectionPane>();
ClassDB::register_class<editor::inspector::PositionableSetEditor>();
ClassDB::register_class<editor::inspector::LayersEditor>();
ClassDB::register_class<editor::inspector::LayerVisibleCheckBox>();
ClassDB::register_class<editor::inspector::LayerRemoveButton>();
ClassDB::register_class<editor::inspector::CurrentLayerCheckBox>();
}
#endif
}
Expand Down
208 changes: 56 additions & 152 deletions src/editor/inspector/layers_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)};
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -125,30 +136,30 @@ void LayersEditor::_add_layer() {
refresh();
}

uint32_t LayersEditor::get_selected_layer_id() const {
if (auto check_box {Object::cast_to<CurrentLayerCheckBox>(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<Ref<commands::Command<node::IsometricMap>>> commands;

Ref<commands::SetLayerVisibilityCommand> 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);

Expand All @@ -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<Ref<commands::Command<node::IsometricMap>>> commands;

Expand All @@ -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;
}

Expand All @@ -229,8 +218,8 @@ void RemoveLayerPopup::_remove_layer() {

Ref<commands::AddLayerCommand> 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<commands::RevertCommand<node::IsometricMap>> remove_layer_command;
remove_layer_command.instantiate();
Expand All @@ -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
Loading

0 comments on commit 3936f76

Please # to comment.