Skip to content

Commit 66f1afa

Browse files
Florian Mauniermellinoe
authored andcommitted
Add InputTextWithHint overloads
1 parent 820e5c3 commit 66f1afa

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

src/ImGui.NET/ImGui.Manual.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,114 @@ public static bool InputTextMultiline(
247247
return result != 0;
248248
}
249249

250+
public static bool InputTextWithHint(
251+
string label,
252+
string hint,
253+
ref string input,
254+
uint maxLength) => InputTextWithHint(label, hint, ref input, maxLength, 0, null, IntPtr.Zero);
255+
256+
public static bool InputTextWithHint(
257+
string label,
258+
string hint,
259+
ref string input,
260+
uint maxLength,
261+
ImGuiInputTextFlags flags) => InputTextWithHint(label, hint, ref input, maxLength, flags, null, IntPtr.Zero);
262+
263+
public static bool InputTextWithHint(
264+
string label,
265+
string hint,
266+
ref string input,
267+
uint maxLength,
268+
ImGuiInputTextFlags flags,
269+
ImGuiInputTextCallback callback) => InputTextWithHint(label, hint, ref input, maxLength, flags, callback, IntPtr.Zero);
270+
271+
public static bool InputTextWithHint(
272+
string label,
273+
string hint,
274+
ref string input,
275+
uint maxLength,
276+
ImGuiInputTextFlags flags,
277+
ImGuiInputTextCallback callback,
278+
IntPtr user_data)
279+
{
280+
int utf8LabelByteCount = Encoding.UTF8.GetByteCount(label);
281+
byte* utf8LabelBytes;
282+
if (utf8LabelByteCount > Util.StackAllocationSizeLimit)
283+
{
284+
utf8LabelBytes = Util.Allocate(utf8LabelByteCount + 1);
285+
}
286+
else
287+
{
288+
byte* stackPtr = stackalloc byte[utf8LabelByteCount + 1];
289+
utf8LabelBytes = stackPtr;
290+
}
291+
Util.GetUtf8(label, utf8LabelBytes, utf8LabelByteCount);
292+
293+
int utf8HintByteCount = Encoding.UTF8.GetByteCount(hint);
294+
byte* utf8HintBytes;
295+
if (utf8HintByteCount > Util.StackAllocationSizeLimit)
296+
{
297+
utf8HintBytes = Util.Allocate(utf8HintByteCount + 1);
298+
}
299+
else
300+
{
301+
byte* stackPtr = stackalloc byte[utf8HintByteCount + 1];
302+
utf8HintBytes = stackPtr;
303+
}
304+
Util.GetUtf8(hint, utf8HintBytes, utf8HintByteCount);
305+
306+
int utf8InputByteCount = Encoding.UTF8.GetByteCount(input);
307+
int inputBufSize = Math.Max((int)maxLength + 1, utf8InputByteCount + 1);
308+
309+
byte* utf8InputBytes;
310+
byte* originalUtf8InputBytes;
311+
if (inputBufSize > Util.StackAllocationSizeLimit)
312+
{
313+
utf8InputBytes = Util.Allocate(inputBufSize);
314+
originalUtf8InputBytes = Util.Allocate(inputBufSize);
315+
}
316+
else
317+
{
318+
byte* inputStackBytes = stackalloc byte[inputBufSize];
319+
utf8InputBytes = inputStackBytes;
320+
byte* originalInputStackBytes = stackalloc byte[inputBufSize];
321+
originalUtf8InputBytes = originalInputStackBytes;
322+
}
323+
Util.GetUtf8(input, utf8InputBytes, inputBufSize);
324+
uint clearBytesCount = (uint)(inputBufSize - utf8InputByteCount);
325+
Unsafe.InitBlockUnaligned(utf8InputBytes + utf8InputByteCount, 0, clearBytesCount);
326+
Unsafe.CopyBlock(originalUtf8InputBytes, utf8InputBytes, (uint)inputBufSize);
327+
328+
byte result = ImGuiNative.igInputTextWithHint(
329+
utf8LabelBytes,
330+
utf8HintBytes,
331+
utf8InputBytes,
332+
(uint)inputBufSize,
333+
flags,
334+
callback,
335+
user_data.ToPointer());
336+
if (!Util.AreStringsEqual(originalUtf8InputBytes, inputBufSize, utf8InputBytes))
337+
{
338+
input = Util.StringFromPtr(utf8InputBytes);
339+
}
340+
341+
if (utf8LabelByteCount > Util.StackAllocationSizeLimit)
342+
{
343+
Util.Free(utf8LabelBytes);
344+
}
345+
if (utf8HintByteCount > Util.StackAllocationSizeLimit)
346+
{
347+
Util.Free(utf8HintBytes);
348+
}
349+
if (inputBufSize > Util.StackAllocationSizeLimit)
350+
{
351+
Util.Free(utf8InputBytes);
352+
Util.Free(originalUtf8InputBytes);
353+
}
354+
355+
return result != 0;
356+
}
357+
250358
public static bool InputText(
251359
string label,
252360
IntPtr buf,

0 commit comments

Comments
 (0)