Add InputTextWithHint overloads

internals
Florian Maunier 5 years ago committed by Eric Mellino
parent 820e5c3794
commit 66f1afa9a2
  1. 108
      src/ImGui.NET/ImGui.Manual.cs

@ -247,6 +247,114 @@ namespace ImGuiNET
return result != 0;
}
public static bool InputTextWithHint(
string label,
string hint,
ref string input,
uint maxLength) => InputTextWithHint(label, hint, ref input, maxLength, 0, null, IntPtr.Zero);
public static bool InputTextWithHint(
string label,
string hint,
ref string input,
uint maxLength,
ImGuiInputTextFlags flags) => InputTextWithHint(label, hint, ref input, maxLength, flags, null, IntPtr.Zero);
public static bool InputTextWithHint(
string label,
string hint,
ref string input,
uint maxLength,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback) => InputTextWithHint(label, hint, ref input, maxLength, flags, callback, IntPtr.Zero);
public static bool InputTextWithHint(
string label,
string hint,
ref string input,
uint maxLength,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback,
IntPtr user_data)
{
int utf8LabelByteCount = Encoding.UTF8.GetByteCount(label);
byte* utf8LabelBytes;
if (utf8LabelByteCount > Util.StackAllocationSizeLimit)
{
utf8LabelBytes = Util.Allocate(utf8LabelByteCount + 1);
}
else
{
byte* stackPtr = stackalloc byte[utf8LabelByteCount + 1];
utf8LabelBytes = stackPtr;
}
Util.GetUtf8(label, utf8LabelBytes, utf8LabelByteCount);
int utf8HintByteCount = Encoding.UTF8.GetByteCount(hint);
byte* utf8HintBytes;
if (utf8HintByteCount > Util.StackAllocationSizeLimit)
{
utf8HintBytes = Util.Allocate(utf8HintByteCount + 1);
}
else
{
byte* stackPtr = stackalloc byte[utf8HintByteCount + 1];
utf8HintBytes = stackPtr;
}
Util.GetUtf8(hint, utf8HintBytes, utf8HintByteCount);
int utf8InputByteCount = Encoding.UTF8.GetByteCount(input);
int inputBufSize = Math.Max((int)maxLength + 1, utf8InputByteCount + 1);
byte* utf8InputBytes;
byte* originalUtf8InputBytes;
if (inputBufSize > Util.StackAllocationSizeLimit)
{
utf8InputBytes = Util.Allocate(inputBufSize);
originalUtf8InputBytes = Util.Allocate(inputBufSize);
}
else
{
byte* inputStackBytes = stackalloc byte[inputBufSize];
utf8InputBytes = inputStackBytes;
byte* originalInputStackBytes = stackalloc byte[inputBufSize];
originalUtf8InputBytes = originalInputStackBytes;
}
Util.GetUtf8(input, utf8InputBytes, inputBufSize);
uint clearBytesCount = (uint)(inputBufSize - utf8InputByteCount);
Unsafe.InitBlockUnaligned(utf8InputBytes + utf8InputByteCount, 0, clearBytesCount);
Unsafe.CopyBlock(originalUtf8InputBytes, utf8InputBytes, (uint)inputBufSize);
byte result = ImGuiNative.igInputTextWithHint(
utf8LabelBytes,
utf8HintBytes,
utf8InputBytes,
(uint)inputBufSize,
flags,
callback,
user_data.ToPointer());
if (!Util.AreStringsEqual(originalUtf8InputBytes, inputBufSize, utf8InputBytes))
{
input = Util.StringFromPtr(utf8InputBytes);
}
if (utf8LabelByteCount > Util.StackAllocationSizeLimit)
{
Util.Free(utf8LabelBytes);
}
if (utf8HintByteCount > Util.StackAllocationSizeLimit)
{
Util.Free(utf8HintBytes);
}
if (inputBufSize > Util.StackAllocationSizeLimit)
{
Util.Free(utf8InputBytes);
Util.Free(originalUtf8InputBytes);
}
return result != 0;
}
public static bool InputText(
string label,
IntPtr buf,

Loading…
Cancel
Save