Use LibraryImport for custom ImGuiNative functinos

This commit is contained in:
Soreepeong 2025-08-02 20:35:05 +09:00
parent 1a15600a8f
commit 334a02e69a
4 changed files with 72 additions and 41 deletions

View file

@ -1,6 +1,6 @@
namespace Dalamud.Bindings.ImGui; namespace Dalamud.Bindings.ImGui;
public enum ImDrawCallbackEnum public enum ImDrawCallbackEnum : long
{ {
Empty, Empty,
@ -11,5 +11,5 @@ public enum ImDrawCallbackEnum
/// state, and you want it to be restored. It is not done by default because they are many perfectly useful way of /// state, and you want it to be restored. It is not done by default because they are many perfectly useful way of
/// altering render state for imgui contents (e.g. changing shader/blending settings before an Image call). /// altering render state for imgui contents (e.g. changing shader/blending settings before an Image call).
/// </summary> /// </summary>
ResetRenderState = -1, ResetRenderState = ImGui.ImDrawCallbackResetRenderState,
} }

View file

@ -1,4 +1,5 @@
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
@ -160,17 +161,15 @@ public unsafe partial class ImGui
{ {
var dataBuffer = PointerTuple.Create(&callback); 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 #pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
var r = var r = ImGuiNative.InputTextEx(
((delegate* unmanaged[Cdecl]<byte*, byte*, byte*, int, Vector2, ImGuiInputTextFlags, delegate* unmanaged labelPtr,
<ImGuiInputTextCallbackData*, int>, void*, byte>)ImGui.funcTable[1277])( hintPtr,
labelPtr, bufPtr,
hintPtr, buf.Length,
bufPtr, sizeArg,
buf.Length, flags,
sizeArg, callback == null ? null : &InputTextCallbackStatic,
flags, callback == null ? null : &dataBuffer) != 0;
callback == null ? null : &InputTextCallbackStatic,
callback == null ? null : &dataBuffer) != 0;
label.Dispose(); label.Dispose();
hint.Dispose(); hint.Dispose();
return r; return r;
@ -189,17 +188,15 @@ public unsafe partial class ImGui
{ {
var dataBuffer = PointerTuple.Create(&callback, contextPtr); var dataBuffer = PointerTuple.Create(&callback, contextPtr);
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type #pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
var r = var r = ImGuiNative.InputTextEx(
((delegate* unmanaged[Cdecl]<byte*, byte*, byte*, int, Vector2, ImGuiInputTextFlags, delegate* unmanaged labelPtr,
<ImGuiInputTextCallbackData*, int>, void*, byte>)ImGui.funcTable[1277])( hintPtr,
labelPtr, bufPtr,
hintPtr, buf.Length,
bufPtr, sizeArg,
buf.Length, flags,
sizeArg, &InputTextCallbackRefContextStatic,
flags, &dataBuffer) != 0;
&InputTextCallbackRefContextStatic,
&dataBuffer) != 0;
label.Dispose(); label.Dispose();
hint.Dispose(); hint.Dispose();
return r; return r;
@ -218,17 +215,15 @@ public unsafe partial class ImGui
{ {
var dataBuffer = PointerTuple.Create(&callback, contextPtr); var dataBuffer = PointerTuple.Create(&callback, contextPtr);
#pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type #pragma warning restore CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
var r = var r = ImGuiNative.InputTextEx(
((delegate* unmanaged[Cdecl]<byte*, byte*, byte*, int, Vector2, ImGuiInputTextFlags, delegate* unmanaged labelPtr,
<ImGuiInputTextCallbackData*, int>, void*, byte>)ImGui.funcTable[1277])( hintPtr,
labelPtr, bufPtr,
hintPtr, buf.Length,
bufPtr, sizeArg,
buf.Length, flags,
sizeArg, &InputTextCallbackInContextStatic,
flags, &dataBuffer) != 0;
&InputTextCallbackInContextStatic,
&dataBuffer) != 0;
label.Dispose(); label.Dispose();
hint.Dispose(); hint.Dispose();
return r; return r;
@ -445,21 +440,21 @@ public unsafe partial class ImGui
return r; return r;
} }
[UnmanagedCallersOnly] [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int InputTextCallbackStatic(ImGuiInputTextCallbackData* data) private static int InputTextCallbackStatic(ImGuiInputTextCallbackData* data)
{ {
ref var dvps = ref PointerTuple.From<ImGuiInputTextCallbackDelegate>(data->UserData); ref var dvps = ref PointerTuple.From<ImGuiInputTextCallbackDelegate>(data->UserData);
return dvps.Item1.Invoke(ref *data); return dvps.Item1.Invoke(ref *data);
} }
[UnmanagedCallersOnly] [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int InputTextCallbackRefContextStatic(ImGuiInputTextCallbackData* data) private static int InputTextCallbackRefContextStatic(ImGuiInputTextCallbackData* data)
{ {
ref var dvps = ref PointerTuple.From<ImGuiInputTextCallbackRefContextDelegate<object>, object>(data->UserData); ref var dvps = ref PointerTuple.From<ImGuiInputTextCallbackRefContextDelegate<object>, object>(data->UserData);
return dvps.Item1.Invoke(ref *data, ref dvps.Item2); return dvps.Item1.Invoke(ref *data, ref dvps.Item2);
} }
[UnmanagedCallersOnly] [UnmanagedCallersOnly(CallConvs = [typeof(CallConvCdecl)])]
private static int InputTextCallbackInContextStatic(ImGuiInputTextCallbackData* data) private static int InputTextCallbackInContextStatic(ImGuiInputTextCallbackData* data)
{ {
ref var dvps = ref PointerTuple.From<ImGuiInputTextCallbackInContextDelegate<object>, object>(data->UserData); ref var dvps = ref PointerTuple.From<ImGuiInputTextCallbackInContextDelegate<object>, object>(data->UserData);

View file

@ -8,8 +8,7 @@ public static unsafe partial class ImGui
{ {
public static void AddCallback( public static void AddCallback(
ImDrawListPtr self, delegate*<ImDrawList*, ImDrawCmd*, void> callback, void* callbackData = null) => ImDrawListPtr self, delegate*<ImDrawList*, ImDrawCmd*, void> callback, void* callbackData = null) =>
((delegate* unmanaged[Cdecl]<ImDrawList*, delegate*<ImDrawList*, ImDrawCmd*, void>, void*, void>)funcTable ImGuiNative.AddCallback(self, callback, callbackData);
[540])(self, callback, callbackData);
public static void AddCallback( public static void AddCallback(
ImDrawListPtr self, delegate*<ImDrawListPtr, ImDrawCmdPtr, void> callback, void* callbackData = null) => ImDrawListPtr self, delegate*<ImDrawListPtr, ImDrawCmdPtr, void> callback, void* callbackData = null) =>

View file

@ -1,3 +1,40 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Dalamud.Bindings.ImGui; namespace Dalamud.Bindings.ImGui;
public static partial class ImGuiNative; public static unsafe partial class ImGuiNative
{
private const string LibraryName = "cimgui";
static ImGuiNative()
{
if (LibraryName != ImGui.GetLibraryName())
{
throw new(
$"{nameof(LibraryName)}(={LibraryName})" +
$" does not match " +
$"{nameof(ImGui)}.{nameof(ImGui.GetLibraryName)}(={ImGui.GetLibraryName()})");
}
}
[LibraryImport($"{LibraryName}.dll", EntryPoint = "ImDrawList_AddCallback")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial void AddCallback(
ImDrawList* self,
delegate*<ImDrawList*, ImDrawCmd*, void> callback,
void* callbackData = null);
[LibraryImport($"{LibraryName}.dll", EntryPoint = "igInputTextEx")]
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial int InputTextEx(
byte* label,
byte* hint,
byte* buf,
int bufSize,
Vector2 sizeArg,
ImGuiInputTextFlags flags = ImGuiInputTextFlags.None,
delegate* unmanaged[Cdecl]<ImGuiInputTextCallbackData*, int> callback = null,
void* userData = null);
}