This works :)
This commit is contained in:
+97
-60
@@ -2,6 +2,7 @@ using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Hexa.NET.ImGui;
|
||||
using Hexa.NET.OpenGL;
|
||||
using SDL3;
|
||||
|
||||
namespace SDL3_TestingSuite.SDL3;
|
||||
@@ -16,7 +17,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
{
|
||||
public nint Window;
|
||||
public uint WindowID;
|
||||
public nint Renderer;
|
||||
// public nint Renderer;
|
||||
public ulong Time;
|
||||
public nint ClipboardTextData;
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 48)]
|
||||
@@ -60,15 +61,19 @@ public unsafe static class ImGuiSDL3Platform
|
||||
public static PlatformData GetPlatformData() => ImGui.GetCurrentContext().Handle != null ? ImGuiUserData<PlatformData>.Get(ImGui.GetIO().BackendPlatformUserData)! : null!;
|
||||
|
||||
public static SDL3Window? Parent;
|
||||
private static GL _gl;
|
||||
private static nint _glContext;
|
||||
|
||||
public static bool Init(nint window, nint renderer, SDL3Window? parentWindow = null)
|
||||
public static bool Init(GL gl, nint ctx, nint window, SDL3Window? parentWindow = null)
|
||||
{
|
||||
_gl = gl;
|
||||
_glContext = ctx;
|
||||
Parent = parentWindow;
|
||||
ImGuiIOPtr io = ImGui.GetIO();
|
||||
PlatformData data = new PlatformData
|
||||
{
|
||||
Window = window,
|
||||
Renderer = renderer,
|
||||
// Renderer = renderer,
|
||||
WindowID = SDL.GetWindowID(window)
|
||||
};
|
||||
|
||||
@@ -128,7 +133,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
SDL.SetHint("SDL_BORDERLESS_WINDOWED_STYLE", "0");
|
||||
|
||||
if ((io.BackendFlags & ImGuiBackendFlags.PlatformHasViewports) != 0)
|
||||
InitMultiViewportSupport(window);
|
||||
InitMultiViewportSupport(ctx, window);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -162,7 +167,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
io.AddMousePosEvent(-float.MaxValue, -float.MaxValue);
|
||||
}
|
||||
|
||||
if (data.MouseCanReportHoveredViewport && ImGui.GetDragDropPayload().Equals(nint.Zero))
|
||||
if (data.MouseCanReportHoveredViewport && ImGui.GetDragDropPayload().IsNull)
|
||||
{
|
||||
io.BackendFlags |= ImGuiBackendFlags.HasMouseHoveredViewport;
|
||||
}
|
||||
@@ -322,7 +327,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
// - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to mitigate the issue on X11 we we wait until mouse has moved to begin capture.
|
||||
if (bd.MouseCaptureMode == MouseCaptureMode.Enabled)
|
||||
{
|
||||
SDL.CaptureMouse(bd.MouseButtonsDown != 0 ? true : false);
|
||||
SDL.CaptureMouse(bd.MouseButtonsDown != 0);
|
||||
}
|
||||
else if (bd.MouseCaptureMode == MouseCaptureMode.EnabledAfterDrag)
|
||||
{
|
||||
@@ -332,7 +337,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
if (ImGui.IsMouseDragging((ImGuiMouseButton)buttonN, 1.0f))
|
||||
wantCapture = true;
|
||||
}
|
||||
SDL.CaptureMouse(wantCapture ? true : false);
|
||||
SDL.CaptureMouse(wantCapture);
|
||||
}
|
||||
|
||||
nint focusedWindow = SDL.GetKeyboardFocus();
|
||||
@@ -379,7 +384,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
{
|
||||
uint mouseViewportID = 0;
|
||||
ImGuiViewportPtr? mouseViewport = GetViewportForWindowId(bd.MouseWindowID);
|
||||
if (mouseViewport != null)
|
||||
if (mouseViewport != null && !mouseViewport.Value.IsNull)
|
||||
mouseViewportID = mouseViewport.Value.ID;
|
||||
io.AddMouseViewportEvent(mouseViewportID);
|
||||
}
|
||||
@@ -729,22 +734,17 @@ public unsafe static class ImGuiSDL3Platform
|
||||
io.AddKeyEvent(ImGuiKey.ModSuper, (mod & SDL.Keymod.GUI) != 0);
|
||||
}
|
||||
|
||||
|
||||
public class ImGui_ImplSDL3_ViewportData : ViewPortData { }
|
||||
|
||||
public class ViewPortData
|
||||
{
|
||||
public nint Window;
|
||||
public nint Renderer;
|
||||
public nint ParentWindow;
|
||||
public uint WindowID; // Stored in ImGuiViewport.PlatformHandle. Use SDL.GetWindowFromID() to get SDL.Window* from Uint32 WindowID.
|
||||
public bool WindowOwned;
|
||||
public bool RendererOwned;
|
||||
public nint GLContext;
|
||||
}
|
||||
|
||||
private static void InitMultiViewportSupport(nint window)
|
||||
private static void InitMultiViewportSupport(nint _gl, nint window)
|
||||
{
|
||||
|
||||
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
|
||||
|
||||
platformIO.PlatformCreateWindow = DelegateStorage.CreateWindowDelegate.GetPtrForDelegate();
|
||||
@@ -760,6 +760,8 @@ public unsafe static class ImGuiSDL3Platform
|
||||
platformIO.PlatformSetWindowTitle = DelegateStorage.SetWindowTitleDelegate.GetPtrForDelegate();
|
||||
platformIO.PlatformSetWindowAlpha = DelegateStorage.SetWindowAlphaDelegate.GetPtrForDelegate();
|
||||
platformIO.PlatformUpdateWindow = DelegateStorage.UpdateWindowDelegate.GetPtrForDelegate();
|
||||
platformIO.PlatformRenderWindow = DelegateStorage.RenderWindowDelegate.GetPtrForDelegate();
|
||||
platformIO.PlatformSwapBuffers = DelegateStorage.SwapBuffersDelegate.GetPtrForDelegate();
|
||||
platformIO.PlatformGetWindowFramebufferScale = DelegateStorage.GetWindowFramebufferScaleDelegate.GetPtrForDelegate();
|
||||
|
||||
ImGuiViewportPtr mainViewport = ImGui.GetMainViewport();
|
||||
@@ -767,10 +769,9 @@ public unsafe static class ImGuiSDL3Platform
|
||||
ViewPortData vd = new ViewPortData
|
||||
{
|
||||
Window = window,
|
||||
Renderer = GetPlatformData().Renderer,
|
||||
WindowID = SDL.GetWindowID(window),
|
||||
WindowOwned = false,
|
||||
RendererOwned = false
|
||||
GLContext = _gl
|
||||
};
|
||||
|
||||
mainViewport.PlatformUserData = ImGuiUserData<ViewPortData>.Store(vd);
|
||||
@@ -892,16 +893,40 @@ public unsafe static class ImGuiSDL3Platform
|
||||
// //
|
||||
// // // // // // // // // // // // // //
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void CreateWindowDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly CreateWindowDelegateFn CreateWindowDelegate = CreateWindow;
|
||||
internal delegate void ViewportCallback(ImGuiViewportPtr ctx);
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void ViewportCallbackSetVec2(ImGuiViewportPtr ctx, Vector2 vec);
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate Vector2 ViewportCallbackGetVec2(ImGuiViewportPtr ctx);
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate bool ViewportCallbackBool(ImGuiViewportPtr ctx);
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void ViewportCallbackSetString(ImGuiViewportPtr ctx, string title);
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void ViewportCallbackSetFloat(ImGuiViewportPtr ctx, float alpha);
|
||||
|
||||
internal static readonly ViewportCallback CreateWindowDelegate = CreateWindow;
|
||||
private static void CreateWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
PlatformData bd = GetPlatformData();
|
||||
|
||||
ViewPortData vd = new ViewPortData();
|
||||
viewport.PlatformUserData = ImGuiUserData<ViewPortData>.Store(vd);
|
||||
|
||||
bool useOpengl = _glContext != nint.Zero;
|
||||
nint backupContext = nint.Zero;
|
||||
if (useOpengl)
|
||||
{
|
||||
backupContext = SDL.GLGetCurrentContext();
|
||||
SDL.GLSetAttribute(SDL.GLAttr.ShareWithCurrentContext, 1);
|
||||
}
|
||||
|
||||
SDL.WindowFlags flags = SDL.WindowFlags.Hidden;
|
||||
|
||||
if (useOpengl)
|
||||
flags |= SDL.WindowFlags.OpenGL;
|
||||
else if (bd.UseVulkan)
|
||||
flags |= SDL.WindowFlags.Vulkan;
|
||||
|
||||
flags |= SDL.GetWindowFlags(bd.Window) & SDL.WindowFlags.HighPixelDensity;
|
||||
|
||||
@@ -915,6 +940,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
flags |= SDL.WindowFlags.AlwaysOnTop;
|
||||
|
||||
vd.Window = SDL.CreateWindow("Untitled", (int)viewport.Size.X, (int)viewport.Size.Y, flags);
|
||||
vd.WindowID = SDL.GetWindowID(vd.Window);
|
||||
|
||||
ImGuiViewportPtr? parentViewport = viewport.ParentViewportId != 0 ? ImGui.FindViewportByID(viewport.ParentViewportId) : null;
|
||||
if (parentViewport != null && parentViewport.Value.Handle != null)
|
||||
@@ -926,6 +952,15 @@ public unsafe static class ImGuiSDL3Platform
|
||||
|
||||
SDL.SetWindowPosition(vd.Window, (int)viewport.Pos.X, (int)viewport.Pos.Y);
|
||||
vd.WindowOwned = true;
|
||||
if (useOpengl)
|
||||
{
|
||||
vd.GLContext = SDL.GLCreateContext(vd.Window);
|
||||
SDL.GLSetSwapInterval(0);
|
||||
}
|
||||
if (useOpengl && backupContext != nint.Zero)
|
||||
{
|
||||
SDL.GLMakeCurrent(bd.Window, backupContext);
|
||||
}
|
||||
|
||||
SetupPlatformHandles(viewport, vd.Window);
|
||||
|
||||
@@ -939,20 +974,25 @@ public unsafe static class ImGuiSDL3Platform
|
||||
).ToPointer();
|
||||
#endif
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void DestroyWindowDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly DestroyWindowDelegateFn DestroyWindowDelegate = DestroyWindow;
|
||||
|
||||
internal static readonly ViewportCallback DestroyWindowDelegate = DestroyWindow;
|
||||
private static void DestroyWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData? vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData);
|
||||
|
||||
if (vd != null)
|
||||
{
|
||||
if (vd.GLContext != nint.Zero && vd.WindowOwned)
|
||||
{
|
||||
SDL.GLDestroyContext(vd.GLContext);
|
||||
}
|
||||
if (vd.Window != nint.Zero && vd.WindowOwned)
|
||||
{
|
||||
SDL.DestroyWindow(vd.Window);
|
||||
}
|
||||
|
||||
vd.GLContext = nint.Zero;
|
||||
vd.Window = nint.Zero;
|
||||
}
|
||||
|
||||
ImGuiUserData<ViewPortData>.Free(viewport.PlatformUserData);
|
||||
@@ -962,9 +1002,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
viewport.PlatformHandleRaw = null;
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void ShowWindowDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly ShowWindowDelegateFn ShowWindowDelegate = ShowWindow;
|
||||
internal static readonly ViewportCallback ShowWindowDelegate = ShowWindow;
|
||||
private static void ShowWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -977,10 +1015,8 @@ public unsafe static class ImGuiSDL3Platform
|
||||
|
||||
SDL.SetHint(SDL.Hints.WindowActivateWhenShown, oldHint);
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void UpdateWindowDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly UpdateWindowDelegateFn UpdateWindowDelegate = UpdateWindow;
|
||||
|
||||
internal static readonly ViewportCallback UpdateWindowDelegate = UpdateWindow;
|
||||
private static void UpdateWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -995,9 +1031,28 @@ public unsafe static class ImGuiSDL3Platform
|
||||
}
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void SetWindowPosDelegateFn(ImGuiViewportPtr ctx, Vector2 pos);
|
||||
internal static readonly SetWindowPosDelegateFn SetWindowPosDelegate = SetWindowPos;
|
||||
internal static readonly ViewportCallback RenderWindowDelegate = RenderWindow;
|
||||
private static void RenderWindow(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
|
||||
if (vd.GLContext != nint.Zero)
|
||||
SDL.GLMakeCurrent(vd.Window, vd.GLContext);
|
||||
}
|
||||
|
||||
internal static readonly ViewportCallback SwapBuffersDelegate = SwapBuffers;
|
||||
private static void SwapBuffers(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
|
||||
if (vd.GLContext != nint.Zero)
|
||||
{
|
||||
SDL.GLMakeCurrent(vd.Window, vd.GLContext);
|
||||
SDL.GLSwapWindow(vd.Window);
|
||||
}
|
||||
}
|
||||
|
||||
internal static readonly ViewportCallbackSetVec2 SetWindowPosDelegate = SetWindowPos;
|
||||
private static void SetWindowPos(ImGuiViewportPtr viewport, Vector2 pos)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -1005,9 +1060,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
SDL.SetWindowPosition(vd.Window, (int)pos.X, (int)pos.Y);
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate Vector2 GetWindowPosDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly GetWindowPosDelegateFn GetWindowPosDelegate = GetWindowPos;
|
||||
internal static readonly ViewportCallbackGetVec2 GetWindowPosDelegate = GetWindowPos;
|
||||
private static Vector2 GetWindowPos(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -1015,9 +1068,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
return new Vector2(x, y);
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void SetWindowSizeDelegateFn(ImGuiViewportPtr ctx, Vector2 size);
|
||||
internal static readonly SetWindowSizeDelegateFn SetWindowSizeDelegate = SetWindowSize;
|
||||
internal static readonly ViewportCallbackSetVec2 SetWindowSizeDelegate = SetWindowSize;
|
||||
private static void SetWindowSize(ImGuiViewportPtr viewport, Vector2 size)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -1025,9 +1076,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
SDL.SetWindowSize(vd.Window, (int)size.X, (int)size.Y);
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate Vector2 GetWindowSizeDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly GetWindowSizeDelegateFn GetWindowSizeDelegate = GetWindowSize;
|
||||
internal static readonly ViewportCallbackGetVec2 GetWindowSizeDelegate = GetWindowSize;
|
||||
private static Vector2 GetWindowSize(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -1037,9 +1086,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
return new Vector2(w, h);
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate Vector2 GetWindowFramebufferScaleDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly GetWindowFramebufferScaleDelegateFn GetWindowFramebufferScaleDelegate = GetWindowFramebufferScale;
|
||||
internal static readonly ViewportCallbackGetVec2 GetWindowFramebufferScaleDelegate = GetWindowFramebufferScale;
|
||||
private static Vector2 GetWindowFramebufferScale(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -1049,9 +1096,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
return framebufferScale;
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void SetWindowFocusDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly SetWindowFocusDelegateFn SetWindowFocusDelegate = SetWindowFocus;
|
||||
internal static readonly ViewportCallback SetWindowFocusDelegate = SetWindowFocus;
|
||||
private static void SetWindowFocus(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -1059,9 +1104,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
SDL.RaiseWindow(vd.Window);
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate bool GetWindowFocusDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly GetWindowFocusDelegateFn GetWindowFocusDelegate = GetWindowFocus;
|
||||
internal static readonly ViewportCallbackBool GetWindowFocusDelegate = GetWindowFocus;
|
||||
private static bool GetWindowFocus(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -1069,9 +1112,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
return (SDL.GetWindowFlags(vd.Window) & SDL.WindowFlags.InputFocus) != 0;
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate bool GetWindowMinimizedDelegateFn(ImGuiViewportPtr ctx);
|
||||
internal static readonly GetWindowMinimizedDelegateFn GetWindowMinimizedDelegate = GetWindowMinimized;
|
||||
internal static readonly ViewportCallbackBool GetWindowMinimizedDelegate = GetWindowMinimized;
|
||||
private static bool GetWindowMinimized(ImGuiViewportPtr viewport)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -1079,9 +1120,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
return (SDL.GetWindowFlags(vd.Window) & SDL.WindowFlags.Minimized) != 0;
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void SetWindowTitleDelegateFn(ImGuiViewportPtr ctx, string title);
|
||||
internal static readonly SetWindowTitleDelegateFn SetWindowTitleDelegate = SetWindowTitle;
|
||||
internal static readonly ViewportCallbackSetString SetWindowTitleDelegate = SetWindowTitle;
|
||||
private static void SetWindowTitle(ImGuiViewportPtr viewport, string title)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
@@ -1089,9 +1128,7 @@ public unsafe static class ImGuiSDL3Platform
|
||||
SDL.SetWindowTitle(vd.Window, title);
|
||||
}
|
||||
|
||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||
internal delegate void SetWindowAlphaDelegateFn(ImGuiViewportPtr ctx, float alpha);
|
||||
internal static readonly SetWindowAlphaDelegateFn SetWindowAlphaDelegate = SetWindowAlpha;
|
||||
internal static readonly ViewportCallbackSetFloat SetWindowAlphaDelegate = SetWindowAlpha;
|
||||
private static void SetWindowAlpha(ImGuiViewportPtr viewport, float alpha)
|
||||
{
|
||||
ViewPortData vd = ImGuiUserData<ViewPortData>.Get(viewport.PlatformUserData)!;
|
||||
|
||||
Reference in New Issue
Block a user