From bf7aab44e27eb780795a24938fd6f63b72837b09 Mon Sep 17 00:00:00 2001 From: Eric Mellino Date: Wed, 27 Jul 2016 20:45:00 -0700 Subject: [PATCH] Fix a couple minor issues * Typo in NativeIO.cs * TextInputBuffer was not properly initializing its Length property in the non-string constructor. * Cleaned up TextInputBuffer's interface a bit, added some helpful methods --- src/ImGui.NET/NativeIO.cs | 2 +- src/ImGui.NET/TextInputBuffer.cs | 33 ++++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/ImGui.NET/NativeIO.cs b/src/ImGui.NET/NativeIO.cs index f13627e..514338b 100644 --- a/src/ImGui.NET/NativeIO.cs +++ b/src/ImGui.NET/NativeIO.cs @@ -69,7 +69,7 @@ namespace ImGuiNET public float KeyRepeatRate; /// /// Store your own data for retrieval by callbacks. - /// Default value; IntPtr.Zero. + /// Default value: IntPtr.Zero. /// public IntPtr UserData; diff --git a/src/ImGui.NET/TextInputBuffer.cs b/src/ImGui.NET/TextInputBuffer.cs index 2c37169..3a509c3 100644 --- a/src/ImGui.NET/TextInputBuffer.cs +++ b/src/ImGui.NET/TextInputBuffer.cs @@ -21,29 +21,46 @@ namespace ImGuiNET public void Resize(int newSize) { - ClearBuffer(); + FreeNativeBuffer(); CreateBuffer(newSize); } - private void CreateBuffer(int size) + private unsafe void CreateBuffer(int size) { Buffer = Marshal.AllocHGlobal(size); - Length = Length; + Length = (uint)size; + ClearData(); } - private void ClearBuffer() + public unsafe void ClearData() { - Marshal.FreeHGlobal(Buffer); - Buffer = IntPtr.Zero; - Length = 0; + byte* ptr = (byte*)Buffer.ToPointer(); + for (int i = 0; i < Length; i++) + { + ptr[i] = 0; + } } public void Dispose() { if (Buffer != IntPtr.Zero) { - ClearBuffer(); + FreeNativeBuffer(); } } + + private void FreeNativeBuffer() + { + Marshal.FreeHGlobal(Buffer); + Buffer = IntPtr.Zero; + Length = 0; + } + + public string GetString() + { + return Marshal.PtrToStringAnsi(Buffer); + } + + public override string ToString() => GetString(); } }