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

Directly Setting Child By Index Doesn't Work Correctly? #173

Closed
AristurtleDev opened this issue Jul 3, 2024 · 1 comment
Closed

Directly Setting Child By Index Doesn't Work Correctly? #173

AristurtleDev opened this issue Jul 3, 2024 · 1 comment

Comments

@AristurtleDev
Copy link
Contributor

AristurtleDev commented Jul 3, 2024

Description

I'm working on some extension methods in MonoGame.Extended for the MonoGame implementation of Gum. The Button class by default uses a ColoredRectangleRuntime as for the background, so I wanted to make an extension method to easily change it without the user having to create a full custom control. However, when changing the child element from a ColorRectangleRuntime to a NineSliceRuntime by setting it in the index directly, it does not render properly.

image

Reproduction

To recreate this, you can use the following extension method

public static NineSliceRuntime UseNinePatch(this Button button, Texture2D texture)
{
    if (button.Visual.GetChildByName("ButtonBackground") is IRenderableIpso child)
    {
        NineSliceRuntime nineSlice = new NineSliceRuntime();
        nineSlice.Width = 0;
        nineSlice.Height = 0;
        nineSlice.WidthUnits = DimensionUnitType.RelativeToContainer;
        nineSlice.HeightUnits = DimensionUnitType.RelativeToContainer;
        nineSlice.Name = "ButtonBackground";
        nineSlice.SourceFile = texture;

        int index = button.Visual.Children.IndexOf(child);

        // Setting directly by index here causes the issue
        button.Visual.Children[index] = nineSlice;
        return nineSlice;
    }

    return null;
}

Workaround

Instead of setting the child directly by index as shown above, you can instead remove the child then insert the new element at the same index and it works as expected

public static NineSliceRuntime UseNinePatch(this Button button, Texture2D texture)
{
    if (button.Visual.GetChildByName("ButtonBackground") is IRenderableIpso child)
    {
        NineSliceRuntime nineSlice = new NineSliceRuntime();
        nineSlice.Width = 0;
        nineSlice.Height = 0;
        nineSlice.WidthUnits = DimensionUnitType.RelativeToContainer;
        nineSlice.HeightUnits = DimensionUnitType.RelativeToContainer;
        nineSlice.Name = "ButtonBackground";
        nineSlice.SourceFile = texture;

        int index = button.Visual.Children.IndexOf(child);

        // Remove the old child and then insert the new one at the same index works
        button.Visual.Children.RemoveAt(index);
        button.Visual.Children.Insert(index, nineSlice);
        return nineSlice;
    }

    return null;
}
@vchelaru
Copy link
Owner

Fixed by f8688ea

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

No branches or pull requests

2 participants