Skip to content

Commit

Permalink
Merge pull request #50208 from kleonc/accept_dialog-remove_button
Browse files Browse the repository at this point in the history
Add AcceptDialog::remove_button method
  • Loading branch information
akien-mga authored Jul 7, 2021
2 parents 29e5979 + 714e6a5 commit e4c940e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/classes/AcceptDialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<description>
Adds a button with label [code]text[/code] and a custom [code]action[/code] to the dialog and returns the created button. [code]action[/code] will be passed to the [signal custom_action] signal when pressed.
If [code]true[/code], [code]right[/code] will place the button to the right of any sibling buttons.
You can use [method remove_button] method to remove a button created with this method from the dialog.
</description>
</method>
<method name="add_cancel_button">
Expand All @@ -30,6 +31,7 @@
</argument>
<description>
Adds a button with label [code]name[/code] and a cancel action to the dialog and returns the created button.
You can use [method remove_button] method to remove a button created with this method from the dialog.
</description>
</method>
<method name="get_label">
Expand All @@ -55,6 +57,15 @@
Registers a [LineEdit] in the dialog. When the enter key is pressed, the dialog will be accepted.
</description>
</method>
<method name="remove_button">
<return type="void">
</return>
<argument index="0" name="button" type="Control">
</argument>
<description>
Removes the [code]button[/code] from the dialog. Does NOT free the [code]button[/code]. The [code]button[/code] must be a [Button] added with [method add_button] or [method add_cancel_button] method. After removal, pressing the [code]button[/code] will no longer emit this dialog's [signal custom_action] or [signal cancelled] signals.
</description>
</method>
</methods>
<members>
<member name="dialog_autowrap" type="bool" setter="set_autowrap" getter="has_autowrap" default="false">
Expand Down
23 changes: 23 additions & 0 deletions scene/gui/dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,36 @@ Button *AcceptDialog::add_cancel_button(const String &p_cancel) {
return b;
}

void AcceptDialog::remove_button(Control *p_button) {
Button *button = Object::cast_to<Button>(p_button);
ERR_FAIL_NULL(button);
ERR_FAIL_COND_MSG(button->get_parent() != hbc, vformat("Cannot remove button %s as it does not belong to this dialog.", button->get_name()));
ERR_FAIL_COND_MSG(button == ok, "Cannot remove dialog's OK button.");

Node *right_spacer = hbc->get_child(button->get_index() + 1);
// Should always be valid but let's avoid crashing
if (right_spacer) {
hbc->remove_child(right_spacer);
memdelete(right_spacer);
}
hbc->remove_child(button);

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));
}
}

void AcceptDialog::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_ok_button"), &AcceptDialog::get_ok_button);
ClassDB::bind_method(D_METHOD("get_label"), &AcceptDialog::get_label);
ClassDB::bind_method(D_METHOD("set_hide_on_ok", "enabled"), &AcceptDialog::set_hide_on_ok);
ClassDB::bind_method(D_METHOD("get_hide_on_ok"), &AcceptDialog::get_hide_on_ok);
ClassDB::bind_method(D_METHOD("add_button", "text", "right", "action"), &AcceptDialog::add_button, DEFVAL(false), DEFVAL(""));
ClassDB::bind_method(D_METHOD("add_cancel_button", "name"), &AcceptDialog::add_cancel_button);
ClassDB::bind_method(D_METHOD("remove_button", "button"), &AcceptDialog::remove_button);
ClassDB::bind_method(D_METHOD("register_text_enter", "line_edit"), &AcceptDialog::register_text_enter);
ClassDB::bind_method(D_METHOD("set_text", "text"), &AcceptDialog::set_text);
ClassDB::bind_method(D_METHOD("get_text"), &AcceptDialog::get_text);
Expand Down
1 change: 1 addition & 0 deletions scene/gui/dialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class AcceptDialog : public Window {
Button *get_ok_button() { return ok; }
Button *add_button(const String &p_text, bool p_right = false, const String &p_action = "");
Button *add_cancel_button(const String &p_cancel = "");
void remove_button(Control *p_button);

void set_hide_on_ok(bool p_hide);
bool get_hide_on_ok() const;
Expand Down

0 comments on commit e4c940e

Please sign in to comment.