From 30a9c90785d1b0cdf5345de43cc1554207b32b09 Mon Sep 17 00:00:00 2001 From: kleonc <9283098+kleonc@users.noreply.github.com> Date: Mon, 10 Jul 2023 12:17:51 +0200 Subject: [PATCH] Hide/show AcceptDialog's button spacer on button visibility changed --- scene/gui/dialogs.cpp | 26 +++++++++++++++++++++----- scene/gui/dialogs.h | 1 + 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/scene/gui/dialogs.cpp b/scene/gui/dialogs.cpp index 0860bf39682..902b76c5a7c 100644 --- a/scene/gui/dialogs.cpp +++ b/scene/gui/dialogs.cpp @@ -286,18 +286,29 @@ void AcceptDialog::_custom_action(const String &p_action) { custom_action(p_action); } +void AcceptDialog::_custom_button_visibility_changed(Button *button) { + Control *right_spacer = Object::cast_to(button->get_meta("__right_spacer")); + if (right_spacer) { + right_spacer->set_visible(button->is_visible()); + } +} + Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) { Button *button = memnew(Button); button->set_text(p_text); + Control *right_spacer; if (p_right) { buttons_hbox->add_child(button); - buttons_hbox->add_spacer(); + right_spacer = buttons_hbox->add_spacer(); } else { buttons_hbox->add_child(button); buttons_hbox->move_child(button, 0); - buttons_hbox->add_spacer(true); + right_spacer = buttons_hbox->add_spacer(true); } + button->set_meta("__right_spacer", right_spacer); + + button->connect("visibility_changed", callable_mp(this, &AcceptDialog::_custom_button_visibility_changed).bind(button)); child_controls_changed(); if (is_visible()) { @@ -330,6 +341,12 @@ void AcceptDialog::remove_button(Control *p_button) { ERR_FAIL_COND_MSG(button->get_parent() != buttons_hbox, vformat("Cannot remove button %s as it does not belong to this dialog.", button->get_name())); ERR_FAIL_COND_MSG(button == ok_button, "Cannot remove dialog's OK button."); + Control *right_spacer = Object::cast_to(button->get_meta("__right_spacer")); + if (right_spacer) { + ERR_FAIL_COND_MSG(right_spacer->get_parent() != buttons_hbox, vformat("Cannot remove button %s as its associated spacer does not belong to this dialog.", button->get_name())); + } + + button->disconnect("visibility_changed", callable_mp(this, &AcceptDialog::_custom_button_visibility_changed)); if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_custom_action))) { button->disconnect("pressed", callable_mp(this, &AcceptDialog::_custom_action)); } @@ -337,11 +354,10 @@ void AcceptDialog::remove_button(Control *p_button) { button->disconnect("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed)); } - Node *right_spacer = buttons_hbox->get_child(button->get_index() + 1); - // Should always be valid but let's avoid crashing. if (right_spacer) { buttons_hbox->remove_child(right_spacer); - memdelete(right_spacer); + button->remove_meta("__right_spacer"); + right_spacer->queue_free(); } buttons_hbox->remove_child(button); diff --git a/scene/gui/dialogs.h b/scene/gui/dialogs.h index 1821a886c06..109c7172d06 100644 --- a/scene/gui/dialogs.h +++ b/scene/gui/dialogs.h @@ -60,6 +60,7 @@ class AcceptDialog : public Window { } theme_cache; void _custom_action(const String &p_action); + void _custom_button_visibility_changed(Button *button); void _update_child_rects(); static bool swap_cancel_ok;