summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkleonc <9283098+kleonc@users.noreply.github.com>2023-07-10 12:17:51 +0200
committerkleonc <9283098+kleonc@users.noreply.github.com>2023-07-11 12:18:46 +0200
commit30a9c90785d1b0cdf5345de43cc1554207b32b09 (patch)
treed8255d11369265d9bb538b3bff9abf705fa00b6b
parentef155c1aeb216fa5a732913b6f2dc321e4b512dc (diff)
downloadredot-engine-30a9c90785d1b0cdf5345de43cc1554207b32b09.tar.gz
Hide/show AcceptDialog's button spacer on button visibility changed
-rw-r--r--scene/gui/dialogs.cpp26
-rw-r--r--scene/gui/dialogs.h1
2 files changed, 22 insertions, 5 deletions
diff --git a/scene/gui/dialogs.cpp b/scene/gui/dialogs.cpp
index 0860bf3968..902b76c5a7 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<Control>(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<Control>(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 1821a886c0..109c7172d0 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;