|
|
@ -1,15 +1,39 @@ |
|
|
|
using System; |
|
|
|
using System; |
|
|
|
|
|
|
|
using System.Runtime.CompilerServices; |
|
|
|
using System.Runtime.InteropServices; |
|
|
|
using System.Runtime.InteropServices; |
|
|
|
|
|
|
|
|
|
|
|
namespace ImGuiNET |
|
|
|
namespace ImGuiNET |
|
|
|
{ |
|
|
|
{ |
|
|
|
public class TextInputBuffer : IDisposable |
|
|
|
public class TextInputBuffer : IDisposable |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
private uint _length; |
|
|
|
|
|
|
|
|
|
|
|
public IntPtr Buffer { get; private set; } |
|
|
|
public IntPtr Buffer { get; private set; } |
|
|
|
public uint Length { get; private set; } |
|
|
|
|
|
|
|
|
|
|
|
public uint Length |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
get |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return _length; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
set |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (value > int.MaxValue) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
throw new ArgumentOutOfRangeException("Length cannot be greater that Int32.MaxValue."); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Resize((int)value); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public TextInputBuffer(int length) |
|
|
|
public TextInputBuffer(int length) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
if (length < 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(length)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
CreateBuffer(length); |
|
|
|
CreateBuffer(length); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -19,10 +43,13 @@ namespace ImGuiNET |
|
|
|
Length = (uint)initialText.Length; |
|
|
|
Length = (uint)initialText.Length; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void Resize(int newSize) |
|
|
|
private unsafe void Resize(int newSize) |
|
|
|
{ |
|
|
|
{ |
|
|
|
FreeNativeBuffer(); |
|
|
|
IntPtr newBuffer = Marshal.AllocHGlobal(newSize); |
|
|
|
CreateBuffer(newSize); |
|
|
|
Unsafe.CopyBlock(newBuffer.ToPointer(), Buffer.ToPointer(), Length); |
|
|
|
|
|
|
|
Marshal.FreeHGlobal(Buffer); |
|
|
|
|
|
|
|
Buffer = newBuffer; |
|
|
|
|
|
|
|
_length = (uint)newSize; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private unsafe void CreateBuffer(int size) |
|
|
|
private unsafe void CreateBuffer(int size) |
|
|
@ -53,7 +80,7 @@ namespace ImGuiNET |
|
|
|
{ |
|
|
|
{ |
|
|
|
Marshal.FreeHGlobal(Buffer); |
|
|
|
Marshal.FreeHGlobal(Buffer); |
|
|
|
Buffer = IntPtr.Zero; |
|
|
|
Buffer = IntPtr.Zero; |
|
|
|
Length = 0; |
|
|
|
_length = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public string StringValue |
|
|
|
public string StringValue |
|
|
@ -64,9 +91,22 @@ namespace ImGuiNET |
|
|
|
} |
|
|
|
} |
|
|
|
set |
|
|
|
set |
|
|
|
{ |
|
|
|
{ |
|
|
|
FreeNativeBuffer(); |
|
|
|
if (value.Length > Length) // Doesn't fit into current buffer |
|
|
|
Buffer = Marshal.StringToHGlobalAnsi(value); |
|
|
|
{ |
|
|
|
Length = (uint)value.Length; |
|
|
|
FreeNativeBuffer(); |
|
|
|
|
|
|
|
Buffer = Marshal.StringToHGlobalAnsi(value); |
|
|
|
|
|
|
|
_length = (uint)value.Length; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else // Fits in current buffer, just copy data in. |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
IntPtr tempNativeString = Marshal.StringToHGlobalAnsi(value); |
|
|
|
|
|
|
|
uint bytesToCopy = (uint)Math.Min(Length, value.Length); |
|
|
|
|
|
|
|
unsafe |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Unsafe.CopyBlock(Buffer.ToPointer(), tempNativeString.ToPointer(), bytesToCopy); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Marshal.FreeHGlobal(tempNativeString); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|