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

Buttons not re-centering after deleting Custom buttons in popup Dialog. #50192

Closed
ondesic opened this issue Jul 6, 2021 · 1 comment · Fixed by #50208
Closed

Buttons not re-centering after deleting Custom buttons in popup Dialog. #50192

ondesic opened this issue Jul 6, 2021 · 1 comment · Fixed by #50208
Milestone

Comments

@ondesic
Copy link

ondesic commented Jul 6, 2021

Godot version

3.3.2 official

System information

Windows 10

Issue description

If you add custom buttons to any popup Dialogs, upon deleting them, the remaining buttons do not recenter.

Steps to reproduce

Create a project with a button and any Dialog node that you can use "add_button()" on.
In the button put code that opens the dialog and adds a custom button through "add_button()".
Next close the Dialog node and make the second time the DIalog opens delete the custom button.
The resulting Dialog will have all remaining buttons off-centered.

Minimal reproduction project

In this reproduction project press the button once and you get this:
Untitled-1
Close the Dialog and press the button again. This time it will add a new button. All is well:
Untitled-2
close the dialog and push the Button again. This time it will delete the custom button. Notice how even though the custom button is gone, things are off-centered:
Untitled-3
Close dialog and press the button again. Now the code adds a custom button back. Again, everything is off centered:
Untitled-4

Here is the project (code is in C#):
Dialog.zip

@kleonc
Copy link
Member

kleonc commented Jul 6, 2021

Happens because AcceptDialog::add_button adds spacers (here Controls extending horizontally) around buttons so the layout in HBoxContainer with the buttons is like: spacer-button-...-(spacer-button)-...-spacer. Removing a button makes spacers adjacent, hence the issue.
firefox_gJxOndLWZR

Adding AcceptDialog::remove_button which would handle removing spacers accordingly should solve this issue. I'll open a PR.


@ondesic As a workaround for now you'd need to remove them manually. For example adding such extension method should do the thing (C#):

public static class AcceptDialogExtensions
{
    public static void RemoveButton(this AcceptDialog acceptDialog, Button button)
    {
        if (button == null)
        {
            return;
        }

        HBoxContainer hbc = acceptDialog.GetOk().GetParent<HBoxContainer>();
        if (button.GetParent() != hbc)
        {
            return;
        }

        Node rightSpacer = hbc.GetChild(button.GetIndex() + 1);
        if (rightSpacer != null)
        {
            hbc.RemoveChild(rightSpacer);
            rightSpacer.QueueFree();
        }
        hbc.RemoveChild(button);

        if (button.IsConnected("pressed", acceptDialog, "_custom_action"))
        {
            button.Disconnect("pressed", acceptDialog, "_custom_action");
        }
    }
}

and here's your button pressed handler modified to use it:

public void _btnPressed()
{
    if (clickCount == 2)
    {
        cd.RemoveButton(btnCustom);
        btnCustom.QueueFree();
    }
    else if (clickCount == 1 || clickCount == 3)
    {
        btnCustom = cd.AddButton("Extra", false, "custom");
    }

    cd.PopupCentered();
    clickCount++;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants