You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.5 KiB
66 lines
1.5 KiB
using System; |
|
using System.Runtime.InteropServices; |
|
|
|
namespace ImGuiNET |
|
{ |
|
public class TextInputBuffer : IDisposable |
|
{ |
|
public IntPtr Buffer { get; private set; } |
|
public uint Length { get; private set; } |
|
|
|
public TextInputBuffer(int length) |
|
{ |
|
CreateBuffer(length); |
|
} |
|
|
|
public TextInputBuffer(string initialText) |
|
{ |
|
Buffer = Marshal.StringToHGlobalAnsi(initialText); |
|
Length = (uint)initialText.Length; |
|
} |
|
|
|
public void Resize(int newSize) |
|
{ |
|
FreeNativeBuffer(); |
|
CreateBuffer(newSize); |
|
} |
|
|
|
private unsafe void CreateBuffer(int size) |
|
{ |
|
Buffer = Marshal.AllocHGlobal(size); |
|
Length = (uint)size; |
|
ClearData(); |
|
} |
|
|
|
public unsafe void ClearData() |
|
{ |
|
byte* ptr = (byte*)Buffer.ToPointer(); |
|
for (int i = 0; i < Length; i++) |
|
{ |
|
ptr[i] = 0; |
|
} |
|
} |
|
|
|
public void Dispose() |
|
{ |
|
if (Buffer != IntPtr.Zero) |
|
{ |
|
FreeNativeBuffer(); |
|
} |
|
} |
|
|
|
private void FreeNativeBuffer() |
|
{ |
|
Marshal.FreeHGlobal(Buffer); |
|
Buffer = IntPtr.Zero; |
|
Length = 0; |
|
} |
|
|
|
public string GetString() |
|
{ |
|
return Marshal.PtrToStringAnsi(Buffer); |
|
} |
|
|
|
public override string ToString() => GetString(); |
|
} |
|
}
|
|
|