From 1f35b5ae9b36ddc33a93ff2197b9fd7fe6095ffd Mon Sep 17 00:00:00 2001 From: Eric Mellino Date: Fri, 27 Apr 2018 18:48:33 -0700 Subject: [PATCH] Better handling of the "p_opened" parameter in ImGui.Begin overloads. * Previously, there would always be a "close window" button in the corner, because internally ImGui.cs was passing a non-null pointer to the underlying function. Now, it only does so if you call an overload with the "ref bool" parameter. --- src/ImGui.NET/ImGui.cs | 33 ++++++++++++++++++++++----------- src/ImGui.NET/ImGuiNative.cs | 4 ++-- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/ImGui.NET/ImGui.cs b/src/ImGui.NET/ImGui.cs index b6b2268..90b10bd 100644 --- a/src/ImGui.NET/ImGui.cs +++ b/src/ImGui.NET/ImGui.cs @@ -520,30 +520,41 @@ namespace ImGuiNET public static bool BeginWindow(string windowTitle) => BeginWindow(windowTitle, WindowFlags.Default); - public static bool BeginWindow(string windowTitle, WindowFlags flags) + public static unsafe bool BeginWindow(string windowTitle, WindowFlags flags) { - bool opened = true; - return ImGuiNative.igBegin(windowTitle, ref opened, flags); + return ImGuiNative.igBegin(windowTitle, null, flags); } - public static bool BeginWindow(string windowTitle, ref bool opened, WindowFlags flags) + public static unsafe bool BeginWindow(string windowTitle, ref bool opened, WindowFlags flags) { - return ImGuiNative.igBegin(windowTitle, ref opened, flags); + byte openedLocal = opened ? (byte)1 : (byte)0; + bool ret = ImGuiNative.igBegin(windowTitle, &openedLocal, flags); + opened = openedLocal != 0; + return ret; } - public static bool BeginWindow(string windowTitle, ref bool opened, float backgroundAlpha, WindowFlags flags) + public static unsafe bool BeginWindow(string windowTitle, ref bool opened, float backgroundAlpha, WindowFlags flags) { - return ImGuiNative.igBegin2(windowTitle, ref opened, new Vector2(), backgroundAlpha, flags); + byte openedLocal = opened ? (byte)1 : (byte)0; + bool ret = ImGuiNative.igBegin2(windowTitle, &openedLocal, new Vector2(), backgroundAlpha, flags); + opened = openedLocal != 0; + return ret; } - public static bool BeginWindow(string windowTitle, ref bool opened, Vector2 startingSize, WindowFlags flags) + public static unsafe bool BeginWindow(string windowTitle, ref bool opened, Vector2 startingSize, WindowFlags flags) { - return ImGuiNative.igBegin2(windowTitle, ref opened, startingSize, 1f, flags); + byte openedLocal = opened ? (byte)1 : (byte)0; + bool ret = ImGuiNative.igBegin2(windowTitle, &openedLocal, startingSize, 1f, flags); + opened = openedLocal != 0; + return ret; } - public static bool BeginWindow(string windowTitle, ref bool opened, Vector2 startingSize, float backgroundAlpha, WindowFlags flags) + public static unsafe bool BeginWindow(string windowTitle, ref bool opened, Vector2 startingSize, float backgroundAlpha, WindowFlags flags) { - return ImGuiNative.igBegin2(windowTitle, ref opened, startingSize, backgroundAlpha, flags); + byte openedLocal = opened ? (byte)1 : (byte)0; + bool ret = ImGuiNative.igBegin2(windowTitle, &openedLocal, startingSize, backgroundAlpha, flags); + opened = openedLocal != 0; + return ret; } public static bool BeginMenu(string label) diff --git a/src/ImGui.NET/ImGuiNative.cs b/src/ImGui.NET/ImGuiNative.cs index 647ce36..b393ee5 100644 --- a/src/ImGui.NET/ImGuiNative.cs +++ b/src/ImGui.NET/ImGuiNative.cs @@ -47,10 +47,10 @@ namespace ImGuiNET // Window [DllImport(cimguiLib, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] - public static extern bool igBegin(string name, ref bool p_opened, WindowFlags flags); + public static extern bool igBegin(string name, byte* p_opened, WindowFlags flags); [DllImport(cimguiLib, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] - public static extern bool igBegin2(string name, ref bool p_opened, Vector2 size_on_first_use, float bg_alpha, WindowFlags flags); + public static extern bool igBegin2(string name, byte* p_opened, Vector2 size_on_first_use, float bg_alpha, WindowFlags flags); [DllImport(cimguiLib, CallingConvention = CallingConvention.Cdecl)] public static extern void igEnd(); [DllImport(cimguiLib, CallingConvention = CallingConvention.Cdecl)]