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
internals
Eric Mellino 9 years ago
parent 10c4c89841
commit bf7aab44e2
  1. 2
      src/ImGui.NET/NativeIO.cs
  2. 33
      src/ImGui.NET/TextInputBuffer.cs

@ -69,7 +69,7 @@ namespace ImGuiNET
public float KeyRepeatRate; public float KeyRepeatRate;
/// <summary> /// <summary>
/// Store your own data for retrieval by callbacks. /// Store your own data for retrieval by callbacks.
/// Default value; IntPtr.Zero. /// Default value: IntPtr.Zero.
/// </summary> /// </summary>
public IntPtr UserData; public IntPtr UserData;

@ -21,29 +21,46 @@ namespace ImGuiNET
public void Resize(int newSize) public void Resize(int newSize)
{ {
ClearBuffer(); FreeNativeBuffer();
CreateBuffer(newSize); CreateBuffer(newSize);
} }
private void CreateBuffer(int size) private unsafe void CreateBuffer(int size)
{ {
Buffer = Marshal.AllocHGlobal(size); Buffer = Marshal.AllocHGlobal(size);
Length = Length; Length = (uint)size;
ClearData();
} }
private void ClearBuffer() public unsafe void ClearData()
{ {
Marshal.FreeHGlobal(Buffer); byte* ptr = (byte*)Buffer.ToPointer();
Buffer = IntPtr.Zero; for (int i = 0; i < Length; i++)
Length = 0; {
ptr[i] = 0;
}
} }
public void Dispose() public void Dispose()
{ {
if (Buffer != IntPtr.Zero) 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();
} }
} }

Loading…
Cancel
Save