Skip to content

Commit a2ecece

Browse files
committed
Remove auto-generated InputText functions and add manual overloads taking a by-ref string.
1 parent 1901729 commit a2ecece

4 files changed

Lines changed: 84 additions & 83 deletions

File tree

src/CodeGenerator/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ class Program
101101
"double",
102102
};
103103

104+
private static readonly HashSet<string> s_skippedFunctions = new HashSet<string>()
105+
{
106+
"igInputText"
107+
};
108+
104109
static void Main(string[] args)
105110
{
106111
string outputPath;
@@ -485,6 +490,8 @@ static void Main(string[] args)
485490
writer.PushBlock("public static unsafe partial class ImGui");
486491
foreach (FunctionDefinition fd in functions)
487492
{
493+
if (s_skippedFunctions.Contains(fd.Name)) { continue; }
494+
488495
foreach (OverloadDefinition overload in fd.Overloads)
489496
{
490497
string exportedName = overload.ExportedName;

src/ImGui.NET/Generated/ImGui.gen.cs

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4555,89 +4555,6 @@ public static void Bullet()
45554555
{
45564556
ImGuiNative.igBullet();
45574557
}
4558-
public static bool InputText(string label, string buf, uint buf_size)
4559-
{
4560-
int label_byteCount = Encoding.UTF8.GetByteCount(label);
4561-
byte* native_label = stackalloc byte[label_byteCount + 1];
4562-
fixed (char* label_ptr = label)
4563-
{
4564-
int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount);
4565-
native_label[native_label_offset] = 0;
4566-
}
4567-
int buf_byteCount = Encoding.UTF8.GetByteCount(buf);
4568-
byte* native_buf = stackalloc byte[buf_byteCount + 1];
4569-
fixed (char* buf_ptr = buf)
4570-
{
4571-
int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount);
4572-
native_buf[native_buf_offset] = 0;
4573-
}
4574-
ImGuiInputTextFlags flags = 0;
4575-
ImGuiInputTextCallback callback = null;
4576-
void* user_data = null;
4577-
byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, user_data);
4578-
return ret != 0;
4579-
}
4580-
public static bool InputText(string label, string buf, uint buf_size, ImGuiInputTextFlags flags)
4581-
{
4582-
int label_byteCount = Encoding.UTF8.GetByteCount(label);
4583-
byte* native_label = stackalloc byte[label_byteCount + 1];
4584-
fixed (char* label_ptr = label)
4585-
{
4586-
int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount);
4587-
native_label[native_label_offset] = 0;
4588-
}
4589-
int buf_byteCount = Encoding.UTF8.GetByteCount(buf);
4590-
byte* native_buf = stackalloc byte[buf_byteCount + 1];
4591-
fixed (char* buf_ptr = buf)
4592-
{
4593-
int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount);
4594-
native_buf[native_buf_offset] = 0;
4595-
}
4596-
ImGuiInputTextCallback callback = null;
4597-
void* user_data = null;
4598-
byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, user_data);
4599-
return ret != 0;
4600-
}
4601-
public static bool InputText(string label, string buf, uint buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback)
4602-
{
4603-
int label_byteCount = Encoding.UTF8.GetByteCount(label);
4604-
byte* native_label = stackalloc byte[label_byteCount + 1];
4605-
fixed (char* label_ptr = label)
4606-
{
4607-
int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount);
4608-
native_label[native_label_offset] = 0;
4609-
}
4610-
int buf_byteCount = Encoding.UTF8.GetByteCount(buf);
4611-
byte* native_buf = stackalloc byte[buf_byteCount + 1];
4612-
fixed (char* buf_ptr = buf)
4613-
{
4614-
int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount);
4615-
native_buf[native_buf_offset] = 0;
4616-
}
4617-
void* user_data = null;
4618-
byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, user_data);
4619-
return ret != 0;
4620-
}
4621-
public static bool InputText(string label, string buf, uint buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, IntPtr user_data)
4622-
{
4623-
int label_byteCount = Encoding.UTF8.GetByteCount(label);
4624-
byte* native_label = stackalloc byte[label_byteCount + 1];
4625-
fixed (char* label_ptr = label)
4626-
{
4627-
int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount);
4628-
native_label[native_label_offset] = 0;
4629-
}
4630-
int buf_byteCount = Encoding.UTF8.GetByteCount(buf);
4631-
byte* native_buf = stackalloc byte[buf_byteCount + 1];
4632-
fixed (char* buf_ptr = buf)
4633-
{
4634-
int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount);
4635-
native_buf[native_buf_offset] = 0;
4636-
}
4637-
void* native_user_data = (void*)user_data.ToPointer();
4638-
byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, native_user_data);
4639-
return ret != 0;
4640-
}
46414558
public static bool InputInt3(string label, ref int v)
46424559
{
46434560
int label_byteCount = Encoding.UTF8.GetByteCount(label);

src/ImGui.NET/ImGui.Manual.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,62 @@ public static bool InputText(
5454
}
5555
}
5656

57+
public static bool InputText(
58+
string label,
59+
ref string input,
60+
uint maxLength) => InputText(label, ref input, maxLength, 0, null, IntPtr.Zero);
61+
62+
public static bool InputText(
63+
string label,
64+
ref string input,
65+
uint maxLength,
66+
ImGuiInputTextFlags flags) => InputText(label, ref input, maxLength, flags, null, IntPtr.Zero);
67+
68+
public static bool InputText(
69+
string label,
70+
ref string input,
71+
uint maxLength,
72+
ImGuiInputTextFlags flags,
73+
ImGuiInputTextCallback callback) => InputText(label, ref input, maxLength, flags, callback, IntPtr.Zero);
74+
75+
public static bool InputText(
76+
string label,
77+
ref string input,
78+
uint maxLength,
79+
ImGuiInputTextFlags flags,
80+
ImGuiInputTextCallback callback,
81+
IntPtr user_data)
82+
{
83+
int labelByteCount = Encoding.UTF8.GetByteCount(label);
84+
byte* labelBytes = stackalloc byte[labelByteCount];
85+
fixed (char* labelPtr = label)
86+
{
87+
Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount);
88+
}
89+
90+
int bytesNeeded = Encoding.UTF8.GetByteCount(input);
91+
int stackBufSize = Math.Max((int)maxLength, bytesNeeded);
92+
byte* bufBytes = stackalloc byte[stackBufSize];
93+
fixed (char* u16Ptr = input)
94+
{
95+
Encoding.UTF8.GetBytes(u16Ptr, input.Length, bufBytes, stackBufSize);
96+
}
97+
98+
byte result = ImGuiNative.igInputText(
99+
labelBytes,
100+
bufBytes,
101+
(uint)stackBufSize,
102+
flags,
103+
callback,
104+
user_data.ToPointer());
105+
if (!Util.AreStringsEqual(input, bufBytes))
106+
{
107+
input = Util.StringFromPtr(bufBytes);
108+
}
109+
110+
return result != 0;
111+
}
112+
57113
public static bool InputText(
58114
string label,
59115
IntPtr buf,

src/ImGui.NET/Util.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,26 @@ public static unsafe string StringFromPtr(byte* ptr)
1414

1515
return Encoding.UTF8.GetString(ptr, characters);
1616
}
17+
18+
internal static unsafe bool AreStringsEqual(string a, byte* b)
19+
{
20+
if (a.Length == 0) { return b[0] == 0; }
21+
22+
int aCount = Encoding.UTF8.GetByteCount(a);
23+
byte* aBytes = stackalloc byte[aCount];
24+
fixed (char* labelPtr = a)
25+
{
26+
Encoding.UTF8.GetBytes(labelPtr, a.Length, aBytes, aCount);
27+
}
28+
29+
for (int i = 0; i < aCount; i++)
30+
{
31+
if (aBytes[i] != b[i]) { return false; }
32+
}
33+
34+
if (b[aCount] != 0) { return false; }
35+
36+
return true;
37+
}
1738
}
1839
}

0 commit comments

Comments
 (0)