Skip to content

Commit 576e1f1

Browse files
committed
Ensure hiding AcceptDialog OK button keeps other buttons centered
1 parent 42c7f14 commit 576e1f1

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

scene/gui/dialogs.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -329,29 +329,29 @@ void AcceptDialog::_custom_action(const String &p_action) {
329329
custom_action(p_action);
330330
}
331331

332-
void AcceptDialog::_custom_button_visibility_changed(Button *button) {
333-
Control *right_spacer = Object::cast_to<Control>(button->get_meta("__right_spacer"));
334-
if (right_spacer) {
335-
right_spacer->set_visible(button->is_visible());
332+
void AcceptDialog::_button_visibility_changed(Button *button) {
333+
Control *bound_spacer = Object::cast_to<Control>(button->get_meta("__bound_spacer"));
334+
if (bound_spacer) {
335+
bound_spacer->set_visible(button->is_visible());
336336
}
337337
}
338338

339339
Button *AcceptDialog::add_button(const String &p_text, bool p_right, const String &p_action) {
340340
Button *button = memnew(Button);
341341
button->set_text(p_text);
342342

343-
Control *right_spacer;
343+
Control *bound_spacer;
344344
if (p_right) {
345345
buttons_hbox->add_child(button);
346-
right_spacer = buttons_hbox->add_spacer();
346+
bound_spacer = buttons_hbox->add_spacer();
347347
} else {
348348
buttons_hbox->add_child(button);
349349
buttons_hbox->move_child(button, 0);
350-
right_spacer = buttons_hbox->add_spacer(true);
350+
bound_spacer = buttons_hbox->add_spacer(true);
351351
}
352-
button->set_meta("__right_spacer", right_spacer);
352+
button->set_meta("__bound_spacer", bound_spacer);
353353

354-
button->connect(SceneStringName(visibility_changed), callable_mp(this, &AcceptDialog::_custom_button_visibility_changed).bind(button));
354+
button->connect(SceneStringName(visibility_changed), callable_mp(this, &AcceptDialog::_button_visibility_changed).bind(button));
355355

356356
child_controls_changed();
357357
if (is_visible()) {
@@ -383,23 +383,23 @@ void AcceptDialog::remove_button(Button *p_button) {
383383
ERR_FAIL_COND_MSG(p_button->get_parent() != buttons_hbox, vformat("Cannot remove button %s as it does not belong to this dialog.", p_button->get_name()));
384384
ERR_FAIL_COND_MSG(p_button == ok_button, "Cannot remove dialog's OK button.");
385385

386-
Control *right_spacer = Object::cast_to<Control>(p_button->get_meta("__right_spacer"));
387-
if (right_spacer) {
388-
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.", p_button->get_name()));
386+
Control *bound_spacer = Object::cast_to<Control>(p_button->get_meta("__bound_spacer"));
387+
if (bound_spacer) {
388+
ERR_FAIL_COND_MSG(bound_spacer->get_parent() != buttons_hbox, vformat("Cannot remove button %s as its associated spacer does not belong to this dialog.", p_button->get_name()));
389389
}
390390

391-
p_button->disconnect(SceneStringName(visibility_changed), callable_mp(this, &AcceptDialog::_custom_button_visibility_changed));
391+
p_button->disconnect(SceneStringName(visibility_changed), callable_mp(this, &AcceptDialog::_button_visibility_changed));
392392
if (p_button->is_connected(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_custom_action))) {
393393
p_button->disconnect(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_custom_action));
394394
}
395395
if (p_button->is_connected(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_cancel_pressed))) {
396396
p_button->disconnect(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_cancel_pressed));
397397
}
398398

399-
if (right_spacer) {
400-
buttons_hbox->remove_child(right_spacer);
401-
p_button->remove_meta("__right_spacer");
402-
right_spacer->queue_free();
399+
if (bound_spacer) {
400+
buttons_hbox->remove_child(bound_spacer);
401+
p_button->remove_meta("__bound_spacer");
402+
bound_spacer->queue_free();
403403
}
404404
buttons_hbox->remove_child(p_button);
405405

@@ -486,7 +486,10 @@ AcceptDialog::AcceptDialog() {
486486
ok_button = memnew(Button);
487487
set_default_ok_text(ETR("OK"));
488488
buttons_hbox->add_child(ok_button);
489-
buttons_hbox->add_spacer();
489+
// Ensure hiding OK button will hide one of the initial spacers.
490+
Control *bound_spacer = buttons_hbox->add_spacer();
491+
ok_button->set_meta("__bound_spacer", bound_spacer);
492+
ok_button->connect(SceneStringName(visibility_changed), callable_mp(this, &AcceptDialog::_button_visibility_changed).bind(ok_button));
490493

491494
ok_button->connect(SceneStringName(pressed), callable_mp(this, &AcceptDialog::_ok_pressed));
492495

scene/gui/dialogs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class AcceptDialog : public Window {
6565
} theme_cache;
6666

6767
void _custom_action(const String &p_action);
68-
void _custom_button_visibility_changed(Button *button);
68+
void _button_visibility_changed(Button *button);
6969
void _update_child_rects();
7070
void _update_ok_text();
7171

0 commit comments

Comments
 (0)