From a2ecece82d4715023468ef967c141abd66207812 Mon Sep 17 00:00:00 2001 From: Eric Mellino Date: Sun, 28 Oct 2018 18:56:58 -0700 Subject: [PATCH] Remove auto-generated InputText functions and add manual overloads taking a by-ref string. --- src/CodeGenerator/Program.cs | 7 +++ src/ImGui.NET/Generated/ImGui.gen.cs | 83 ---------------------------- src/ImGui.NET/ImGui.Manual.cs | 56 +++++++++++++++++++ src/ImGui.NET/Util.cs | 21 +++++++ 4 files changed, 84 insertions(+), 83 deletions(-) diff --git a/src/CodeGenerator/Program.cs b/src/CodeGenerator/Program.cs index 6b38064..4614f7f 100644 --- a/src/CodeGenerator/Program.cs +++ b/src/CodeGenerator/Program.cs @@ -101,6 +101,11 @@ namespace CodeGenerator "double", }; + private static readonly HashSet s_skippedFunctions = new HashSet() + { + "igInputText" + }; + static void Main(string[] args) { string outputPath; @@ -485,6 +490,8 @@ namespace CodeGenerator writer.PushBlock("public static unsafe partial class ImGui"); foreach (FunctionDefinition fd in functions) { + if (s_skippedFunctions.Contains(fd.Name)) { continue; } + foreach (OverloadDefinition overload in fd.Overloads) { string exportedName = overload.ExportedName; diff --git a/src/ImGui.NET/Generated/ImGui.gen.cs b/src/ImGui.NET/Generated/ImGui.gen.cs index 0c93af9..475fa45 100644 --- a/src/ImGui.NET/Generated/ImGui.gen.cs +++ b/src/ImGui.NET/Generated/ImGui.gen.cs @@ -4555,89 +4555,6 @@ namespace ImGuiNET { ImGuiNative.igBullet(); } - public static bool InputText(string label, string buf, uint buf_size) - { - int label_byteCount = Encoding.UTF8.GetByteCount(label); - byte* native_label = stackalloc byte[label_byteCount + 1]; - fixed (char* label_ptr = label) - { - int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount); - native_label[native_label_offset] = 0; - } - int buf_byteCount = Encoding.UTF8.GetByteCount(buf); - byte* native_buf = stackalloc byte[buf_byteCount + 1]; - fixed (char* buf_ptr = buf) - { - int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount); - native_buf[native_buf_offset] = 0; - } - ImGuiInputTextFlags flags = 0; - ImGuiInputTextCallback callback = null; - void* user_data = null; - byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, user_data); - return ret != 0; - } - public static bool InputText(string label, string buf, uint buf_size, ImGuiInputTextFlags flags) - { - int label_byteCount = Encoding.UTF8.GetByteCount(label); - byte* native_label = stackalloc byte[label_byteCount + 1]; - fixed (char* label_ptr = label) - { - int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount); - native_label[native_label_offset] = 0; - } - int buf_byteCount = Encoding.UTF8.GetByteCount(buf); - byte* native_buf = stackalloc byte[buf_byteCount + 1]; - fixed (char* buf_ptr = buf) - { - int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount); - native_buf[native_buf_offset] = 0; - } - ImGuiInputTextCallback callback = null; - void* user_data = null; - byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, user_data); - return ret != 0; - } - public static bool InputText(string label, string buf, uint buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback) - { - int label_byteCount = Encoding.UTF8.GetByteCount(label); - byte* native_label = stackalloc byte[label_byteCount + 1]; - fixed (char* label_ptr = label) - { - int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount); - native_label[native_label_offset] = 0; - } - int buf_byteCount = Encoding.UTF8.GetByteCount(buf); - byte* native_buf = stackalloc byte[buf_byteCount + 1]; - fixed (char* buf_ptr = buf) - { - int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount); - native_buf[native_buf_offset] = 0; - } - void* user_data = null; - byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, user_data); - return ret != 0; - } - public static bool InputText(string label, string buf, uint buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, IntPtr user_data) - { - int label_byteCount = Encoding.UTF8.GetByteCount(label); - byte* native_label = stackalloc byte[label_byteCount + 1]; - fixed (char* label_ptr = label) - { - int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount); - native_label[native_label_offset] = 0; - } - int buf_byteCount = Encoding.UTF8.GetByteCount(buf); - byte* native_buf = stackalloc byte[buf_byteCount + 1]; - fixed (char* buf_ptr = buf) - { - int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount); - native_buf[native_buf_offset] = 0; - } - void* native_user_data = (void*)user_data.ToPointer(); - byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, native_user_data); - return ret != 0; - } public static bool InputInt3(string label, ref int v) { int label_byteCount = Encoding.UTF8.GetByteCount(label); diff --git a/src/ImGui.NET/ImGui.Manual.cs b/src/ImGui.NET/ImGui.Manual.cs index 97063c4..b113505 100644 --- a/src/ImGui.NET/ImGui.Manual.cs +++ b/src/ImGui.NET/ImGui.Manual.cs @@ -54,6 +54,62 @@ namespace ImGuiNET } } + public static bool InputText( + string label, + ref string input, + uint maxLength) => InputText(label, ref input, maxLength, 0, null, IntPtr.Zero); + + public static bool InputText( + string label, + ref string input, + uint maxLength, + ImGuiInputTextFlags flags) => InputText(label, ref input, maxLength, flags, null, IntPtr.Zero); + + public static bool InputText( + string label, + ref string input, + uint maxLength, + ImGuiInputTextFlags flags, + ImGuiInputTextCallback callback) => InputText(label, ref input, maxLength, flags, callback, IntPtr.Zero); + + public static bool InputText( + string label, + ref string input, + uint maxLength, + ImGuiInputTextFlags flags, + ImGuiInputTextCallback callback, + IntPtr user_data) + { + int labelByteCount = Encoding.UTF8.GetByteCount(label); + byte* labelBytes = stackalloc byte[labelByteCount]; + fixed (char* labelPtr = label) + { + Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount); + } + + int bytesNeeded = Encoding.UTF8.GetByteCount(input); + int stackBufSize = Math.Max((int)maxLength, bytesNeeded); + byte* bufBytes = stackalloc byte[stackBufSize]; + fixed (char* u16Ptr = input) + { + Encoding.UTF8.GetBytes(u16Ptr, input.Length, bufBytes, stackBufSize); + } + + byte result = ImGuiNative.igInputText( + labelBytes, + bufBytes, + (uint)stackBufSize, + flags, + callback, + user_data.ToPointer()); + if (!Util.AreStringsEqual(input, bufBytes)) + { + input = Util.StringFromPtr(bufBytes); + } + + return result != 0; + } + public static bool InputText( string label, IntPtr buf, diff --git a/src/ImGui.NET/Util.cs b/src/ImGui.NET/Util.cs index 470d480..81a590a 100644 --- a/src/ImGui.NET/Util.cs +++ b/src/ImGui.NET/Util.cs @@ -14,5 +14,26 @@ namespace ImGuiNET return Encoding.UTF8.GetString(ptr, characters); } + + internal static unsafe bool AreStringsEqual(string a, byte* b) + { + if (a.Length == 0) { return b[0] == 0; } + + int aCount = Encoding.UTF8.GetByteCount(a); + byte* aBytes = stackalloc byte[aCount]; + fixed (char* labelPtr = a) + { + Encoding.UTF8.GetBytes(labelPtr, a.Length, aBytes, aCount); + } + + for (int i = 0; i < aCount; i++) + { + if (aBytes[i] != b[i]) { return false; } + } + + if (b[aCount] != 0) { return false; } + + return true; + } } }