This commit is contained in:
2026-05-18 01:36:55 -05:00
parent fc0b04a4bc
commit 060f117a01
7 changed files with 233 additions and 624 deletions
+97 -92
View File
@@ -1,7 +1,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Hexa.NET.ImGui;
using ImGuiNET;
using SDL3;
namespace SDL3_TestingSuite.SDL3;
@@ -32,7 +32,7 @@ public unsafe static class ImGuiSDL3Platform
// Mouse Handling
public uint MouseWindowID;
public int MouseButtonsDown;
public readonly nint[] MouseCursors = new nint[(int)ImGuiMouseCursor.Count];
public readonly nint[] MouseCursors = new nint[(int)ImGuiMouseCursor.COUNT];
public nint MouseLastCursor;
public int MouseLastLeaveFrame;
public bool MouseCanUseGlobalState;
@@ -85,7 +85,7 @@ public unsafe static class ImGuiSDL3Platform
private static readonly PlatformOpenInShellFn OpenInShellDelegate = OpenInShell;
// ReSharper restore NotAccessedField.Local
public static PlatformData Data => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<PlatformData>.Get(ImGui.GetIO().BackendPlatformUserData)! : null!;
public static PlatformData Data => ImGui.GetCurrentContext() != nint.Zero ? ImGuiUserData<PlatformData>.Get(ImGui.GetIO().BackendPlatformUserData)! : null!;
public static bool Init(nint window, nint renderer)
{
@@ -124,10 +124,10 @@ public unsafe static class ImGuiSDL3Platform
#endif
ImGuiPlatformIOPtr platformIo = ImGui.GetPlatformIO();
platformIo.PlatformGetClipboardTextFn = (void*)Marshal.GetFunctionPointerForDelegate(GetClipboardDelegate);
platformIo.PlatformSetClipboardTextFn = (void*)Marshal.GetFunctionPointerForDelegate(SetClipboardDelegate);
platformIo.PlatformSetImeDataFn = (void*)Marshal.GetFunctionPointerForDelegate(SetImeDataDelegate);
platformIo.PlatformOpenInShellFn = (void*)Marshal.GetFunctionPointerForDelegate(OpenInShellDelegate);
platformIo.Platform_GetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(GetClipboardDelegate);
platformIo.Platform_SetClipboardTextFn = Marshal.GetFunctionPointerForDelegate(SetClipboardDelegate);
platformIo.Platform_SetImeDataFn = Marshal.GetFunctionPointerForDelegate(SetImeDataDelegate);
platformIo.Platform_OpenInShellFn = Marshal.GetFunctionPointerForDelegate(OpenInShellDelegate);
UpdateMonitors();
@@ -137,13 +137,13 @@ public unsafe static class ImGuiSDL3Platform
data.MouseCursors[(int)ImGuiMouseCursor.Arrow] = SDL.CreateSystemCursor(SDL.SystemCursor.Default);
data.MouseCursors[(int)ImGuiMouseCursor.TextInput] = SDL.CreateSystemCursor(SDL.SystemCursor.Text);
data.MouseCursors[(int)ImGuiMouseCursor.ResizeAll] = SDL.CreateSystemCursor(SDL.SystemCursor.Move);
data.MouseCursors[(int)ImGuiMouseCursor.ResizeNs] = SDL.CreateSystemCursor(SDL.SystemCursor.NSResize);
data.MouseCursors[(int)ImGuiMouseCursor.ResizeEw] = SDL.CreateSystemCursor(SDL.SystemCursor.EWResize);
data.MouseCursors[(int)ImGuiMouseCursor.ResizeNesw] = SDL.CreateSystemCursor(SDL.SystemCursor.NESWResize);
data.MouseCursors[(int)ImGuiMouseCursor.ResizeNwse] = SDL.CreateSystemCursor(SDL.SystemCursor.NWSEResize);
data.MouseCursors[(int)ImGuiMouseCursor.ResizeNS] = SDL.CreateSystemCursor(SDL.SystemCursor.NSResize);
data.MouseCursors[(int)ImGuiMouseCursor.ResizeEW] = SDL.CreateSystemCursor(SDL.SystemCursor.EWResize);
data.MouseCursors[(int)ImGuiMouseCursor.ResizeNESW] = SDL.CreateSystemCursor(SDL.SystemCursor.NESWResize);
data.MouseCursors[(int)ImGuiMouseCursor.ResizeNWSE] = SDL.CreateSystemCursor(SDL.SystemCursor.NWSEResize);
data.MouseCursors[(int)ImGuiMouseCursor.Hand] = SDL.CreateSystemCursor(SDL.SystemCursor.Pointer);
data.MouseCursors[(int)ImGuiMouseCursor.Wait] = SDL.CreateSystemCursor(SDL.SystemCursor.Wait);
data.MouseCursors[(int)ImGuiMouseCursor.Progress] = SDL.CreateSystemCursor(SDL.SystemCursor.Progress);
// data.MouseCursors[(int)ImGuiMouseCursor.Wait] = SDL.CreateSystemCursor(SDL.SystemCursor.Wait);
// data.MouseCursors[(int)ImGuiMouseCursor.Progress] = SDL.CreateSystemCursor(SDL.SystemCursor.Progress);
data.MouseCursors[(int)ImGuiMouseCursor.NotAllowed] = SDL.CreateSystemCursor(SDL.SystemCursor.NotAllowed);
SetupPlatformHandles(ImGui.GetMainViewport(), window);
@@ -266,7 +266,7 @@ public unsafe static class ImGuiSDL3Platform
if (GetViewportForWindowId(e.Text.WindowID) == null)
return false;
ImGui.GetIO().AddInputCharactersUTF8((byte*)e.Text.Text);
ImGuiNative.ImGuiIO_AddInputCharactersUTF8(io, (byte*)e.Text.Text);
return true;
case SDL.EventType.KeyDown:
case SDL.EventType.KeyUp:
@@ -479,32 +479,51 @@ public unsafe static class ImGuiSDL3Platform
UpdateGamepadAnalog(data, io, ImGuiKey.GamepadRStickDown, SDL.GamepadAxis.RightY, thumbDeadZone, 32767);
}
public static void UpdateMonitors()
public static unsafe void UpdateMonitors()
{
PlatformData bd = Data;
ImGuiPlatformIOPtr platformio = ImGui.GetPlatformIO();
platformio.Monitors.Resize(0);
bd.WantUpdateMonitors = false;
if (platformio.NativePtr == null)
return;
var displays = SDL.GetDisplays(out int displayCount);
for (int n = 0; n < displayCount; n++)
if (displays == null || displayCount <= 0)
return;
if (platformio.Monitors.Data == IntPtr.Zero)
return;
bd.WantUpdateMonitors = false;
ImGuiPlatformMonitor* dst = (ImGuiPlatformMonitor*)platformio.Monitors.Data;
for (int i = 0; i < displayCount; i++)
{
// Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime.
var displayID = displays[n];
var displayID = displays[i];
ImGuiPlatformMonitor monitor = new ImGuiPlatformMonitor();
SDL.GetDisplayBounds(displayID, out SDL.Rect r);
monitor.MainPos = monitor.WorkPos = new Vector2(r.X, r.Y);
monitor.MainSize = monitor.WorkSize = new Vector2(r.W, r.H);
if (SDL.GetDisplayUsableBounds(displayID, out SDL.Rect r2) && r2.W > 0 && r2.H > 0)
{
monitor.WorkPos = new Vector2(r2.X, r2.Y);
monitor.WorkSize = new Vector2(r2.W, r2.H);
}
monitor.DpiScale = SDL.GetDisplayContentScale(displayID); // See https://wiki.libsdl.org/SDL3/README-highdpi for details.
monitor.PlatformHandle = (void*)n;
monitor.DpiScale = SDL.GetDisplayContentScale(displayID);
if (monitor.DpiScale <= 0.0f)
continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902.
platformio.Monitors.PushBack(monitor);
continue;
monitor.PlatformHandle = (void*)i;
dst[i] = monitor;
}
}
@@ -571,16 +590,16 @@ public unsafe static class ImGuiSDL3Platform
SDL.Keycode.RAlt => ImGuiKey.RightAlt,
SDL.Keycode.RGUI => ImGuiKey.RightSuper,
SDL.Keycode.Application => ImGuiKey.Menu,
SDL.Keycode.Alpha0 => ImGuiKey.Key0,
SDL.Keycode.Alpha1 => ImGuiKey.Key1,
SDL.Keycode.Alpha2 => ImGuiKey.Key2,
SDL.Keycode.Alpha3 => ImGuiKey.Key3,
SDL.Keycode.Alpha4 => ImGuiKey.Key4,
SDL.Keycode.Alpha5 => ImGuiKey.Key5,
SDL.Keycode.Alpha6 => ImGuiKey.Key6,
SDL.Keycode.Alpha7 => ImGuiKey.Key7,
SDL.Keycode.Alpha8 => ImGuiKey.Key8,
SDL.Keycode.Alpha9 => ImGuiKey.Key9,
SDL.Keycode.Alpha0 => ImGuiKey._0,
SDL.Keycode.Alpha1 => ImGuiKey._1,
SDL.Keycode.Alpha2 => ImGuiKey._2,
SDL.Keycode.Alpha3 => ImGuiKey._3,
SDL.Keycode.Alpha4 => ImGuiKey._4,
SDL.Keycode.Alpha5 => ImGuiKey._5,
SDL.Keycode.Alpha6 => ImGuiKey._6,
SDL.Keycode.Alpha7 => ImGuiKey._7,
SDL.Keycode.Alpha8 => ImGuiKey._8,
SDL.Keycode.Alpha9 => ImGuiKey._9,
SDL.Keycode.A => ImGuiKey.A,
SDL.Keycode.B => ImGuiKey.B,
SDL.Keycode.C => ImGuiKey.C,
@@ -723,7 +742,6 @@ public unsafe static class ImGuiSDL3Platform
private static void CreateWindow(ImGuiViewportPtr viewport)
{
Program.Logger.Log(null);
PlatformData bd = Data;
ViewPortData vd = new ViewPortData();
@@ -758,7 +776,7 @@ public unsafe static class ImGuiSDL3Platform
vd.Window = SDL.CreateWindow("Untitled", (int)viewport.Size.X, (int)viewport.Size.Y, flags);
ImGuiViewportPtr? parentViewport = viewport.ParentViewportId != 0 ? ImGui.FindViewportByID(viewport.ParentViewportId) : null;
if (parentViewport != null && parentViewport.Value.Handle != null)
if (parentViewport != null)
{
vd.ParentWindow = GetSDLWindowFromViewport(parentViewport.Value);
if (vd.ParentWindow != nint.Zero)
@@ -783,7 +801,6 @@ public unsafe static class ImGuiSDL3Platform
private static void DestroyWindow(ImGuiViewportPtr viewport)
{
Program.Logger.Log(null);
ViewPortData? vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData);
if (vd != null)
@@ -796,16 +813,14 @@ public unsafe static class ImGuiSDL3Platform
ImGuiUserData<ViewPortData>.Free(viewport.PlatformUserData);
viewport.PlatformUserData = null;
viewport.PlatformHandle = null;
viewport.PlatformHandleRaw = null;
Program.Logger.Log(null);
viewport.PlatformUserData = nint.Zero;
viewport.PlatformHandle = nint.Zero;
viewport.PlatformHandleRaw = nint.Zero;
}
private static void ShowWindow(ImGuiViewportPtr viewport)
{
Program.Logger.Log(null);
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
string? oldHint = SDL.GetHint(SDL.Hints.WindowActivateWhenShown);
@@ -815,12 +830,10 @@ public unsafe static class ImGuiSDL3Platform
SDL.ShowWindow(vd.Window);
SDL.SetHint(SDL.Hints.WindowActivateWhenShown, oldHint);
Program.Logger.Log(null);
}
private static void UpdateWindow(ImGuiViewportPtr viewport)
{
Program.Logger.Log(null);
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
if ((viewport.Flags & ImGuiViewportFlags.TopMost) != 0)
@@ -831,7 +844,6 @@ public unsafe static class ImGuiSDL3Platform
{
SDL.SetWindowAlwaysOnTop(vd.Window, false);
}
Program.Logger.Log(null);
}
private static Vector2 GetWindowPos(ImGuiViewportPtr viewport)
@@ -850,7 +862,6 @@ public unsafe static class ImGuiSDL3Platform
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
SDL.SetWindowPosition(vd.Window, (int)pos.X, (int)pos.Y);
Program.Logger.Log(null);
}
private static Vector2 GetWindowSize(ImGuiViewportPtr viewport)
@@ -868,7 +879,6 @@ public unsafe static class ImGuiSDL3Platform
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
SDL.SetWindowSize(vd.Window, (int)size.X, (int)size.Y);
Program.Logger.Log(null);
}
private static Vector2 GetWindowFramebufferScale(ImGuiViewportPtr viewport)
@@ -886,7 +896,6 @@ public unsafe static class ImGuiSDL3Platform
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
SDL.SetWindowTitle(vd.Window, title);
Program.Logger.Log(null);
}
private static void SetWindowAlpha(ImGuiViewportPtr viewport, float alpha)
@@ -894,7 +903,6 @@ public unsafe static class ImGuiSDL3Platform
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
SDL.SetWindowOpacity(vd.Window, alpha);
Program.Logger.Log(null);
}
private static void SetWindowFocus(ImGuiViewportPtr viewport)
@@ -902,7 +910,6 @@ public unsafe static class ImGuiSDL3Platform
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
SDL.RaiseWindow(vd.Window);
Program.Logger.Log(null);
}
private static bool GetWindowFocus(ImGuiViewportPtr viewport)
@@ -924,13 +931,11 @@ public unsafe static class ImGuiSDL3Platform
private static void RenderWindow(ImGuiViewportPtr viewport)
{
// Renderer-backed viewport rendering is handled by ImGuiSDL3Renderer.
Program.Logger.Log(null);
}
private static void SwapBuffers(ImGuiViewportPtr viewport)
{
// Renderer-backed viewport presentation is handled by ImGuiSDL3Renderer.
Program.Logger.Log(null);
}
@@ -1016,24 +1021,25 @@ public unsafe static class ImGuiSDL3Platform
private static void InitMultiViewportSupport(nint window)
{
Program.Logger.Log(null);
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
platformIO.PlatformCreateWindow = (void*)Marshal.GetFunctionPointerForDelegate(CreateWindowDelegate);
platformIO.PlatformDestroyWindow = (void*)Marshal.GetFunctionPointerForDelegate(DestroyWindowDelegate);
platformIO.PlatformShowWindow = (void*)Marshal.GetFunctionPointerForDelegate(ShowWindowDelegate);
platformIO.PlatformSetWindowPos = (void*)Marshal.GetFunctionPointerForDelegate(SetWindowPosDelegate);
platformIO.PlatformGetWindowPos = (void*)Marshal.GetFunctionPointerForDelegate(GetWindowPosDelegate);
platformIO.PlatformSetWindowSize = (void*)Marshal.GetFunctionPointerForDelegate(SetWindowSizeDelegate);
platformIO.PlatformGetWindowSize = (void*)Marshal.GetFunctionPointerForDelegate(GetWindowSizeDelegate);
platformIO.PlatformSetWindowFocus = (void*)Marshal.GetFunctionPointerForDelegate(SetWindowFocusDelegate);
platformIO.PlatformGetWindowFocus = (void*)Marshal.GetFunctionPointerForDelegate(GetWindowFocusDelegate);
platformIO.PlatformGetWindowMinimized = (void*)Marshal.GetFunctionPointerForDelegate(GetWindowMinimizedDelegate);
platformIO.PlatformSetWindowTitle = (void*)Marshal.GetFunctionPointerForDelegate(SetWindowTitleDelegate);
platformIO.PlatformSetWindowAlpha = (void*)Marshal.GetFunctionPointerForDelegate(SetWindowAlphaDelegate);
platformIO.PlatformUpdateWindow = (void*)Marshal.GetFunctionPointerForDelegate(UpdateWindowDelegate);
platformIO.PlatformGetWindowFramebufferScale = (void*)Marshal.GetFunctionPointerForDelegate(GetWindowFramebufferScaleDelegate);
platformIO.Platform_CreateWindow = Marshal.GetFunctionPointerForDelegate(CreateWindowDelegate);
platformIO.Platform_DestroyWindow = Marshal.GetFunctionPointerForDelegate(DestroyWindowDelegate);
platformIO.Platform_ShowWindow = Marshal.GetFunctionPointerForDelegate(ShowWindowDelegate);
platformIO.Platform_SetWindowPos = Marshal.GetFunctionPointerForDelegate(SetWindowPosDelegate);
platformIO.Platform_GetWindowPos = Marshal.GetFunctionPointerForDelegate(GetWindowPosDelegate);
platformIO.Platform_SetWindowSize = Marshal.GetFunctionPointerForDelegate(SetWindowSizeDelegate);
platformIO.Platform_GetWindowSize = Marshal.GetFunctionPointerForDelegate(GetWindowSizeDelegate);
platformIO.Platform_SetWindowFocus = Marshal.GetFunctionPointerForDelegate(SetWindowFocusDelegate);
platformIO.Platform_GetWindowFocus = Marshal.GetFunctionPointerForDelegate(GetWindowFocusDelegate);
platformIO.Platform_GetWindowMinimized = Marshal.GetFunctionPointerForDelegate(GetWindowMinimizedDelegate);
platformIO.Platform_SetWindowTitle = Marshal.GetFunctionPointerForDelegate(SetWindowTitleDelegate);
platformIO.Platform_RenderWindow = Marshal.GetFunctionPointerForDelegate(RenderWindowDelegate);
platformIO.Platform_SwapBuffers = Marshal.GetFunctionPointerForDelegate(SwapBuffersDelegate);
platformIO.Platform_SetWindowAlpha = Marshal.GetFunctionPointerForDelegate(SetWindowAlphaDelegate);
platformIO.Platform_UpdateWindow = Marshal.GetFunctionPointerForDelegate(UpdateWindowDelegate);
// platformIO.Platform_GetWindowFramebufferScale = Marshal.GetFunctionPointerForDelegate(GetWindowFramebufferScaleDelegate);
ImGuiViewportPtr mainViewport = ImGui.GetMainViewport();
@@ -1047,9 +1053,8 @@ public unsafe static class ImGuiSDL3Platform
};
mainViewport.PlatformUserData = ImGuiUserData<ViewPortData>.Store(vd);
mainViewport.PlatformHandle = (void*)(nint)SDL.GetWindowID(window);
Program.Logger.Log(null);
mainViewport.PlatformHandle = (nint)SDL.GetWindowID(window);
#if WINDOWS
mainViewport.PlatformHandleRaw = SDL.GetPointerProperty(
@@ -1062,18 +1067,18 @@ public unsafe static class ImGuiSDL3Platform
public static ImGuiViewportPtr? GetViewportForWindowId(uint id)
{
return ImGui.FindViewportByPlatformHandle((void*)(nint)id);
return ImGui.FindViewportByPlatformHandle((nint)id);
}
private static nint GetSDLWindowFromViewport(ImGuiViewportPtr viewport)
{
return SDL.GetWindowFromID((uint)(nint)viewport.PlatformHandle);
return SDL.GetWindowFromID((uint)viewport.PlatformHandle);
}
private static void SetupPlatformHandles(ImGuiViewportPtr viewport, nint window)
{
viewport.PlatformHandle = (void*)(nint)SDL.GetWindowID(window);
viewport.PlatformHandleRaw = (void*)nint.Zero;
viewport.PlatformHandle = (nint)SDL.GetWindowID(window);
viewport.PlatformHandleRaw = nint.Zero;
#if _WIN32 && !__WINTR__
SDL.GetPointerProperty(SDL.GetWindowProperties(window), SDL.Props.WindowWin32HWNDPointer, 0);
#elif __APPLE__ && SDL_VIDEO_DRIVER_COCOA
@@ -1096,12 +1101,12 @@ public unsafe static class ImGuiSDL3Platform
ImGuiIOPtr io = ImGui.GetIO();
ImGuiPlatformIOPtr platformIo = ImGui.GetPlatformIO();
platformIo.PlatformGetClipboardTextFn = null;
platformIo.PlatformSetClipboardTextFn = null;
platformIo.PlatformSetImeDataFn = null;
platformIo.PlatformOpenInShellFn = null;
platformIo.Platform_GetClipboardTextFn = nint.Zero;
platformIo.Platform_SetClipboardTextFn = nint.Zero;
platformIo.Platform_SetImeDataFn = nint.Zero;
platformIo.Platform_OpenInShellFn = nint.Zero;
ImGuiUserData<PlatformData>.Free(io.BackendPlatformUserData);
io.BackendPlatformUserData = null;
io.BackendPlatformUserData = nint.Zero;
}
}
@@ -1119,32 +1124,32 @@ public enum MouseCaptureMode
Disabled
}
public unsafe static class ImGuiUserData<T> where T : class
public static class ImGuiUserData<T> where T : class
{
public static void* Store(T value)
public static nint Store(T value)
{
if (value == null)
return null;
return nint.Zero;
GCHandle handle = GCHandle.Alloc(value, GCHandleType.Normal);
return (void*)GCHandle.ToIntPtr(handle);
return GCHandle.ToIntPtr(handle);
}
public static T Get(void* ptr)
public static T Get(nint ptr)
{
if (ptr == null)
return null;
if (ptr == nint.Zero)
return null!;
GCHandle handle = GCHandle.FromIntPtr((nint)ptr);
return (T)handle.Target;
GCHandle handle = GCHandle.FromIntPtr(ptr);
return (T)handle.Target!;
}
public static void Free(void* ptr)
public static void Free(nint ptr)
{
if (ptr == null)
if (ptr == nint.Zero)
return;
GCHandle handle = GCHandle.FromIntPtr((nint)ptr);
GCHandle handle = GCHandle.FromIntPtr(ptr);
if (handle.IsAllocated)
handle.Free();