ImGuiNET / ImGui.NET

An ImGui wrapper for .NET.
MIT License
1.85k stars 300 forks source link

Hiding close button on windows #40

Closed xposure closed 6 years ago

xposure commented 6 years ago

ImGui supports hiding the close button on BeginWindow by passing in a null pointer to the p_opened value. I made the following change to support this in the .NET bindings but it would break the existing API if anyone is using it and not sure if thats something you would want a PR on.

It looks like some of the native structs don't have real wrappings to them (especially the window struct). There is some functionality I need and was thinking about implementing them.

Also some of the lower level imgui functions are missing from the cimgui library like the XYZBehavior methods creating a lot of hacks to get around this with setting positions and using invisible buttons.

ImGui.cs

        public unsafe static bool BeginWindow(string windowTitle, ref bool? opened, WindowFlags flags)
        {
            byte value = (opened.HasValue && opened.Value) ? (byte)1 : (byte)0;
            byte* p_opened = opened.HasValue ? &value : null;

            var result = ImGuiNative.igBegin(windowTitle, p_opened, flags);

            if (opened.HasValue)
                 opened = value == 1;

            return result;

        }

ImGuiNative.cs

        public static extern bool igBegin(string name, byte* p_opened, WindowFlags flags);

Edit: Corrected the code to use p_opened instead of the result of the call.

xposure commented 6 years ago

Still learning here...

Guessing this is how its supposed to be implemented, I modified your sample window inside memory editor to have a public bool? isOpened = true; and inside Draw I added...

            if (isOpened.HasValue && !isOpened.Value)
                return;

Then inside SampleWindow I just have the About menu item setting _memoryEditor.isOpened = true

This correctly handles collapsing and hiding of the window when the close button is clicked and showing when the About menu is clicked. Is this the correct work flow for ImGui?

mellinoe commented 6 years ago

I think having the Begin() method take a nullable by-reference boolean would be a bit odd. If I were doing something here, I would rather have the p_opened parameter be null by default, rather than a pointer to true.

public static class ImGui
{
    public static bool BeginWindow(string name)
        => igBegin(name, null, WindowFlags.Default);
    public static bool BeginWindow(string name, WindowFlags flags)
        => igBegin(name, null, flags);
    public static unsafe bool BeginWindow(string name, WIndowFlags flags, ref bool opened)
    {
        byte openedByte = opened ? (byte)1 : (byte)0;
        bool result = igBegin(name, &openedByte, flags);
        opened = openedByte == 1;
        return result;
    }
}

I may throw these changes into the next release, and note that it is sort of a breaking change.

xposure commented 6 years ago

Makes sense. Haven't had a chance to sit down and go through it like I wanted with the holidays.

ghost commented 6 years ago

Hey @mellinoe !

Has this change made it into any builds yet? I need to hide the close button on some of my windows and can't (currently) see how to do so (in comparision to "native" ImGui).

Thanks for your continued work on this :)

mellinoe commented 6 years ago

@DannyGlover It hasn't gotten in yet, but I have some time tonight that I can spend on it. It shouldn't take long to fix.

mellinoe commented 6 years ago

This should be fixed with https://github.com/mellinoe/ImGui.NET/commit/1f35b5ae9b36ddc33a93ff2197b9fd7fe6095ffd, and I've also put up a new version on nuget.org:

https://www.nuget.org/packages/ImGui.NET/0.4.4

ghost commented 6 years ago

@mellinoe You are a legend my friend.

Works great. Thank you so much :)