Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide/show AcceptDialog's button spacer on button visibility changed #79274

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions scene/gui/dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
kleonc marked this conversation as resolved.
Show resolved Hide resolved

child_controls_changed();
if (is_visible()) {
Expand Down Expand Up @@ -330,18 +341,23 @@ 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));
}
if (button->is_connected("pressed", callable_mp(this, &AcceptDialog::_cancel_pressed))) {
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);

Expand Down
1 change: 1 addition & 0 deletions scene/gui/dialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading