Remove auto-generated InputText functions and add manual overloads taking a by-ref string.

internals
Eric Mellino 6 years ago
parent 19017294c2
commit a2ecece82d
  1. 7
      src/CodeGenerator/Program.cs
  2. 83
      src/ImGui.NET/Generated/ImGui.gen.cs
  3. 56
      src/ImGui.NET/ImGui.Manual.cs
  4. 21
      src/ImGui.NET/Util.cs

@ -101,6 +101,11 @@ namespace CodeGenerator
"double",
};
private static readonly HashSet<string> s_skippedFunctions = new HashSet<string>()
{
"igInputText"
};
static void Main(string[] args)
{
string outputPath;
@ -485,6 +490,8 @@ namespace CodeGenerator
writer.PushBlock("public static unsafe partial class ImGui");
foreach (FunctionDefinition fd in functions)
{
if (s_skippedFunctions.Contains(fd.Name)) { continue; }
foreach (OverloadDefinition overload in fd.Overloads)
{
string exportedName = overload.ExportedName;

@ -4555,89 +4555,6 @@ namespace ImGuiNET
{
ImGuiNative.igBullet();
}
public static bool InputText(string label, string buf, uint buf_size)
{
int label_byteCount = Encoding.UTF8.GetByteCount(label);
byte* native_label = stackalloc byte[label_byteCount + 1];
fixed (char* label_ptr = label)
{
int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount);
native_label[native_label_offset] = 0;
}
int buf_byteCount = Encoding.UTF8.GetByteCount(buf);
byte* native_buf = stackalloc byte[buf_byteCount + 1];
fixed (char* buf_ptr = buf)
{
int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount);
native_buf[native_buf_offset] = 0;
}
ImGuiInputTextFlags flags = 0;
ImGuiInputTextCallback callback = null;
void* user_data = null;
byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, user_data);
return ret != 0;
}
public static bool InputText(string label, string buf, uint buf_size, ImGuiInputTextFlags flags)
{
int label_byteCount = Encoding.UTF8.GetByteCount(label);
byte* native_label = stackalloc byte[label_byteCount + 1];
fixed (char* label_ptr = label)
{
int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount);
native_label[native_label_offset] = 0;
}
int buf_byteCount = Encoding.UTF8.GetByteCount(buf);
byte* native_buf = stackalloc byte[buf_byteCount + 1];
fixed (char* buf_ptr = buf)
{
int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount);
native_buf[native_buf_offset] = 0;
}
ImGuiInputTextCallback callback = null;
void* user_data = null;
byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, user_data);
return ret != 0;
}
public static bool InputText(string label, string buf, uint buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback)
{
int label_byteCount = Encoding.UTF8.GetByteCount(label);
byte* native_label = stackalloc byte[label_byteCount + 1];
fixed (char* label_ptr = label)
{
int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount);
native_label[native_label_offset] = 0;
}
int buf_byteCount = Encoding.UTF8.GetByteCount(buf);
byte* native_buf = stackalloc byte[buf_byteCount + 1];
fixed (char* buf_ptr = buf)
{
int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount);
native_buf[native_buf_offset] = 0;
}
void* user_data = null;
byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, user_data);
return ret != 0;
}
public static bool InputText(string label, string buf, uint buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, IntPtr user_data)
{
int label_byteCount = Encoding.UTF8.GetByteCount(label);
byte* native_label = stackalloc byte[label_byteCount + 1];
fixed (char* label_ptr = label)
{
int native_label_offset = Encoding.UTF8.GetBytes(label_ptr, label.Length, native_label, label_byteCount);
native_label[native_label_offset] = 0;
}
int buf_byteCount = Encoding.UTF8.GetByteCount(buf);
byte* native_buf = stackalloc byte[buf_byteCount + 1];
fixed (char* buf_ptr = buf)
{
int native_buf_offset = Encoding.UTF8.GetBytes(buf_ptr, buf.Length, native_buf, buf_byteCount);
native_buf[native_buf_offset] = 0;
}
void* native_user_data = (void*)user_data.ToPointer();
byte ret = ImGuiNative.igInputText(native_label, native_buf, buf_size, flags, callback, native_user_data);
return ret != 0;
}
public static bool InputInt3(string label, ref int v)
{
int label_byteCount = Encoding.UTF8.GetByteCount(label);

@ -54,6 +54,62 @@ namespace ImGuiNET
}
}
public static bool InputText(
string label,
ref string input,
uint maxLength) => InputText(label, ref input, maxLength, 0, null, IntPtr.Zero);
public static bool InputText(
string label,
ref string input,
uint maxLength,
ImGuiInputTextFlags flags) => InputText(label, ref input, maxLength, flags, null, IntPtr.Zero);
public static bool InputText(
string label,
ref string input,
uint maxLength,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback) => InputText(label, ref input, maxLength, flags, callback, IntPtr.Zero);
public static bool InputText(
string label,
ref string input,
uint maxLength,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback,
IntPtr user_data)
{
int labelByteCount = Encoding.UTF8.GetByteCount(label);
byte* labelBytes = stackalloc byte[labelByteCount];
fixed (char* labelPtr = label)
{
Encoding.UTF8.GetBytes(labelPtr, label.Length, labelBytes, labelByteCount);
}
int bytesNeeded = Encoding.UTF8.GetByteCount(input);
int stackBufSize = Math.Max((int)maxLength, bytesNeeded);
byte* bufBytes = stackalloc byte[stackBufSize];
fixed (char* u16Ptr = input)
{
Encoding.UTF8.GetBytes(u16Ptr, input.Length, bufBytes, stackBufSize);
}
byte result = ImGuiNative.igInputText(
labelBytes,
bufBytes,
(uint)stackBufSize,
flags,
callback,
user_data.ToPointer());
if (!Util.AreStringsEqual(input, bufBytes))
{
input = Util.StringFromPtr(bufBytes);
}
return result != 0;
}
public static bool InputText(
string label,
IntPtr buf,

@ -14,5 +14,26 @@ namespace ImGuiNET
return Encoding.UTF8.GetString(ptr, characters);
}
internal static unsafe bool AreStringsEqual(string a, byte* b)
{
if (a.Length == 0) { return b[0] == 0; }
int aCount = Encoding.UTF8.GetByteCount(a);
byte* aBytes = stackalloc byte[aCount];
fixed (char* labelPtr = a)
{
Encoding.UTF8.GetBytes(labelPtr, a.Length, aBytes, aCount);
}
for (int i = 0; i < aCount; i++)
{
if (aBytes[i] != b[i]) { return false; }
}
if (b[aCount] != 0) { return false; }
return true;
}
}
}

Loading…
Cancel
Save