Add overloads for InputText which callbacks take Ptr instead

This commit is contained in:
Soreepeong 2025-08-08 12:49:38 +09:00
parent 28658b4889
commit cc21480d21

View file

@ -77,6 +77,8 @@ public unsafe partial class ImGui
public delegate int ImGuiInputTextCallbackDelegate(scoped ref ImGuiInputTextCallbackData data);
public delegate int ImGuiInputTextCallbackPtrDelegate(ImGuiInputTextCallbackDataPtr data);
public delegate int ImGuiInputTextCallbackRefContextDelegate<TContext>(
scoped ref ImGuiInputTextCallbackData data, scoped ref TContext context);
@ -92,6 +94,14 @@ public unsafe partial class ImGui
return InputTextEx(label, default, buf, default, flags, callback);
}
public static bool InputText(
ImU8String label, Span<byte> buf, ImGuiInputTextFlags flags, ImGuiInputTextCallbackPtrDelegate? callback)
{
if ((flags & (ImGuiInputTextFlags)ImGuiInputTextFlagsPrivate.Multiline) != ImGuiInputTextFlags.None)
throw new ArgumentOutOfRangeException(nameof(flags), flags, "Multiline must not be set");
return InputTextEx(label, default, buf, default, flags, callback);
}
public static bool InputText<TContext>(
ImU8String label, Span<byte> buf, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackRefContextDelegate<TContext> callback, scoped ref TContext context)
@ -112,8 +122,20 @@ public unsafe partial class ImGui
public static bool InputText(
ImU8String label, scoped ref string buf, int maxLength = ImU8String.AllocFreeBufferSize,
ImGuiInputTextFlags flags = ImGuiInputTextFlags.None,
ImGuiInputTextCallbackDelegate? callback = null)
ImGuiInputTextFlags flags = ImGuiInputTextFlags.None, ImGuiInputTextCallbackDelegate? callback = null)
{
var t = new ImU8String(buf);
t.Reserve(maxLength + 1);
var r = InputText(label, t.Buffer[..(maxLength + 1)], flags, callback);
var i = t.Buffer.IndexOf((byte)0);
buf = Encoding.UTF8.GetString(i == -1 ? t.Buffer : t.Buffer[..i]);
t.Dispose();
return r;
}
public static bool InputText(
ImU8String label, scoped ref string buf, int maxLength, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackPtrDelegate? callback)
{
var t = new ImU8String(buf);
t.Reserve(maxLength + 1);
@ -176,6 +198,32 @@ public unsafe partial class ImGui
}
}
public static bool InputTextEx(
ImU8String label, ImU8String hint, Span<byte> buf, Vector2 sizeArg,
ImGuiInputTextFlags flags, ImGuiInputTextCallbackPtrDelegate? callback)
{
fixed (byte* labelPtr = &label.GetPinnableNullTerminatedReference())
fixed (byte* hintPtr = &hint.GetPinnableNullTerminatedReference())
fixed (byte* bufPtr = buf)
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
{
var dataBuffer = PointerTuple.Create(&callback);
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
var r = ImGuiNative.InputTextEx(
labelPtr,
hintPtr,
bufPtr,
buf.Length,
sizeArg,
flags,
callback == null ? null : &InputTextCallbackPtrStatic,
callback == null ? null : &dataBuffer) != 0;
label.Dispose();
hint.Dispose();
return r;
}
}
public static bool InputTextEx<TContext>(
ImU8String label, ImU8String hint, Span<byte> buf, Vector2 sizeArg, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackRefContextDelegate<TContext> callback, scoped ref TContext context)
@ -245,6 +293,19 @@ public unsafe partial class ImGui
return r;
}
public static bool InputTextEx(
ImU8String label, ImU8String hint, scoped ref string buf, int maxLength, Vector2 sizeArg,
ImGuiInputTextFlags flags, ImGuiInputTextCallbackPtrDelegate? callback)
{
var t = new ImU8String(buf);
t.Reserve(maxLength + 1);
var r = InputTextEx(label, hint, t.Buffer[..(maxLength + 1)], sizeArg, flags, callback);
var i = t.Buffer.IndexOf((byte)0);
buf = Encoding.UTF8.GetString(i == -1 ? t.Buffer : t.Buffer[..i]);
t.Dispose();
return r;
}
public static bool InputTextEx<TContext>(
ImU8String label, ImU8String hint, scoped ref string buf, int maxLength, Vector2 sizeArg,
ImGuiInputTextFlags flags,
@ -284,6 +345,17 @@ public unsafe partial class ImGui
flags | (ImGuiInputTextFlags)ImGuiInputTextFlagsPrivate.Multiline,
callback);
public static bool InputTextMultiline(
ImU8String label, Span<byte> buf, Vector2 size, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackPtrDelegate? callback) =>
InputTextEx(
label,
default,
buf,
size,
flags | (ImGuiInputTextFlags)ImGuiInputTextFlagsPrivate.Multiline,
callback);
public static bool InputTextMultiline<TContext>(
ImU8String label, Span<byte> buf, Vector2 size, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackRefContextDelegate<TContext> callback, scoped ref TContext context) =>
@ -322,6 +394,19 @@ public unsafe partial class ImGui
return r;
}
public static bool InputTextMultiline(
ImU8String label, scoped ref string buf, int maxLength, Vector2 size, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackPtrDelegate? callback)
{
var t = new ImU8String(buf);
t.Reserve(maxLength + 1);
var r = InputTextMultiline(label, t.Buffer[..(maxLength + 1)], size, flags, callback);
var i = t.Buffer.IndexOf((byte)0);
buf = Encoding.UTF8.GetString(i == -1 ? t.Buffer : t.Buffer[..i]);
t.Dispose();
return r;
}
public static bool InputTextMultiline<TContext>(
ImU8String label, scoped ref string buf, int maxLength, Vector2 size, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackRefContextDelegate<TContext> callback, scoped ref TContext context)
@ -357,6 +442,15 @@ public unsafe partial class ImGui
return InputTextEx(label, hint, buf, default, flags, callback);
}
public static bool InputTextWithHint(
ImU8String label, ImU8String hint, Span<byte> buf, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackPtrDelegate? callback)
{
if ((flags & (ImGuiInputTextFlags)ImGuiInputTextFlagsPrivate.Multiline) != ImGuiInputTextFlags.None)
throw new ArgumentOutOfRangeException(nameof(flags), flags, "Multiline must not be set");
return InputTextEx(label, hint, buf, default, flags, callback);
}
public static bool InputTextWithHint<TContext>(
ImU8String label, ImU8String hint, Span<byte> buf, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackRefContextDelegate<TContext> callback, scoped ref TContext context)
@ -389,6 +483,19 @@ public unsafe partial class ImGui
return r;
}
public static bool InputTextWithHint(
ImU8String label, ImU8String hint, scoped ref string buf, int maxLength, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackPtrDelegate? callback)
{
var t = new ImU8String(buf);
t.Reserve(maxLength + 1);
var r = InputTextWithHint(label, hint, t.Buffer[..(maxLength + 1)], flags, callback);
var i = t.Buffer.IndexOf((byte)0);
buf = Encoding.UTF8.GetString(i == -1 ? t.Buffer : t.Buffer[..i]);
t.Dispose();
return r;
}
public static bool InputTextWithHint<TContext>(
ImU8String label, ImU8String hint, scoped ref string buf, int maxLength, ImGuiInputTextFlags flags,
ImGuiInputTextCallbackRefContextDelegate<TContext> callback, scoped ref TContext context)
@ -447,6 +554,13 @@ public unsafe partial class ImGui
return dvps.Item1.Invoke(ref *data);
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int InputTextCallbackPtrStatic(ImGuiInputTextCallbackData* data)
{
ref var dvps = ref PointerTuple.From<ImGuiInputTextCallbackPtrDelegate>(data->UserData);
return dvps.Item1.Invoke(data);
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int InputTextCallbackRefContextStatic(ImGuiInputTextCallbackData* data)
{