This commit is contained in:
2026-05-20 11:29:55 -05:00
parent 7d5f242a43
commit 5c17e233bc
4 changed files with 149 additions and 55 deletions
+22 -31
View File
@@ -28,8 +28,6 @@ public unsafe static class ImGuiSDL3Platform
// IME Handling
public nint ImeWindow;
public ImGuiPlatformImeData ImeData;
public bool ImeDirty;
// Mouse Handling
public uint MouseWindowID;
@@ -177,8 +175,6 @@ public unsafe static class ImGuiSDL3Platform
UpdateMouseData();
UpdateMouseCursor();
UpdateIme();
UpdateGamepads();
}
@@ -752,43 +748,41 @@ public unsafe static class ImGuiSDL3Platform
return ImGuiKey.None;
}
public static void UpdateIme()
private static void UpdateIme(ImGuiViewportPtr viewport, ImGuiPlatformImeDataPtr imeData)
{
PlatformData data = GetPlatformData();
ImGuiPlatformImeData imeData = data.ImeData;
nint window = SDL.GetKeyboardFocus();
if (imeData.IsNull) return;
nint window = GetSDLWindowFromViewport(viewport);
bool wantTextInput = imeData.WantVisible || imeData.WantTextInput;
// Stop previous input
if ((!(imeData.WantVisible == 1 || imeData.WantTextInput == 1) || data.ImeWindow != window) && data.ImeWindow != nint.Zero)
if ((!wantTextInput || data.ImeWindow != window) && data.ImeWindow != nint.Zero)
{
SDL.StopTextInput(data.ImeWindow);
data.ImeWindow = nint.Zero;
}
if (!data.ImeDirty && data.ImeWindow == window || window == nint.Zero)
if (!wantTextInput || window == nint.Zero)
return;
// Start/update current input
data.ImeDirty = false;
if (imeData.WantVisible == 1)
if (imeData.WantVisible)
{
// Offset the IME input area by the viewport position so it tracks the correct screen location
// when multiple viewports are in use.
Vector2 viewportPos = Vector2.Zero;
ImGuiViewportPtr? viewport = GetViewportForWindowId(SDL.GetWindowID(window));
if (viewport != null && !viewport.Value.IsNull)
viewportPos = viewport.Value.Pos;
SDL.Rect r = new SDL.Rect
{
X = (int)(imeData.InputPos.X - viewportPos.X),
Y = (int)(imeData.InputPos.Y - viewportPos.Y),
X = (int)(imeData.InputPos.X - viewport.Pos.X),
Y = (int)(imeData.InputPos.Y - viewport.Pos.Y),
W = 1,
H = (int)imeData.InputLineHeight
};
SDL.SetTextInputArea(window, r, 0);
data.ImeWindow = window;
}
if (!SDL.TextInputActive(window) && (imeData.WantVisible == 1 || imeData.WantTextInput == 1))
data.ImeWindow = window;
if (!SDL.TextInputActive(window))
SDL.StartTextInput(window);
}
@@ -915,9 +909,9 @@ public unsafe static class ImGuiSDL3Platform
public static class DelegateStorage
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate nint PlatformGetClipboardTextFn(nint ctx);
internal delegate nint PlatformGetClipboardTextFn(ImGuiContextPtr ctx);
internal static readonly PlatformGetClipboardTextFn GetClipboardDelegate = GetClipboardText;
private static nint GetClipboardText(nint ctx)
private static nint GetClipboardText(ImGuiContextPtr ctx)
{
PlatformData data = GetPlatformData();
if (data.ClipboardTextData != nint.Zero)
@@ -934,9 +928,9 @@ public unsafe static class ImGuiSDL3Platform
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void PlatformSetClipboardTextFn(nint ctx, nint text);
internal delegate void PlatformSetClipboardTextFn(ImGuiContextPtr ctx, nint text);
internal static readonly PlatformSetClipboardTextFn SetClipboardDelegate = SetClipboardText;
private static void SetClipboardText(nint ctx, nint text)
private static void SetClipboardText(ImGuiContextPtr ctx, nint text)
{
string? managedText = Marshal.PtrToStringAnsi(text);
if (managedText != null)
@@ -944,14 +938,11 @@ public unsafe static class ImGuiSDL3Platform
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
internal delegate void PlatformSetImeDataFn(nint ctx, nint userData, nint imeData);
internal delegate void PlatformSetImeDataFn(ImGuiContextPtr ctx, ImGuiViewportPtr userData, ImGuiPlatformImeDataPtr imeData);
internal static readonly PlatformSetImeDataFn SetImeDataDelegate = SetImeData;
private static void SetImeData(nint ctx, nint userData, nint imeData)
private static void SetImeData(ImGuiContextPtr ctx, ImGuiViewportPtr viewport, ImGuiPlatformImeDataPtr imeData)
{
PlatformData data = GetPlatformData();
data.ImeData = Marshal.PtrToStructure<ImGuiPlatformImeData>(imeData);
data.ImeDirty = true;
UpdateIme();
UpdateIme(viewport, imeData);
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@@ -1202,4 +1193,4 @@ public enum MouseCaptureMode
Enabled,
EnabledAfterDrag,
Disabled
}
}
+21 -16
View File
@@ -22,6 +22,7 @@ public sealed unsafe class SDL3Window : IDisposable
}
public event Action? RenderCallback;
public event Action? Disposing;
private ImGuiContextPtr _imGuiContext;
private ImPlotContextPtr _imPlotContext;
@@ -31,7 +32,7 @@ public sealed unsafe class SDL3Window : IDisposable
private readonly FileSystemWatcher? _watcher;
private readonly GL _gl;
public readonly GL GL;
public SDL3Window(string name, int posX, int posY, int width, int height, SDL.WindowFlags flags = SDL.WindowFlags.Resizable | SDL.WindowFlags.HighPixelDensity)
{
@@ -116,11 +117,11 @@ public sealed unsafe class SDL3Window : IDisposable
Console.WriteLine(e);
}
_gl = new GL(new BindingsContext(Window, glContext));
GL = new GL(new BindingsContext(Window, glContext));
// Init platform and renderer
ImGuiSDL3Platform.Init(_gl, Window, glContext);
ImGuiSDL3Renderer.Init(_gl, "#version 330");
ImGuiSDL3Platform.Init(GL, Window, glContext);
ImGuiSDL3Renderer.Init(GL, "#version 330");
}
public bool ShouldClose;
@@ -130,11 +131,6 @@ public sealed unsafe class SDL3Window : IDisposable
while (!_disposed)
{
SDL.PumpEvents();
if (ImGui.GetIO().WantTextInput && !SDL.TextInputActive(Window))
SDL.StartTextInput(Window);
else if (!ImGui.GetIO().WantTextInput && SDL.TextInputActive(Window))
SDL.StopTextInput(Window);
while (SDL.PollEvent(out SDL.Event ev))
{
@@ -149,10 +145,16 @@ public sealed unsafe class SDL3Window : IDisposable
break;
}
}
if (SDL.GetWindowFlags(Window).HasFlag(SDL.WindowFlags.Minimized))
{
SDL.Delay(10);
continue;
}
_gl.MakeCurrent();
_gl.ClearColor(ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
_gl.Clear(GLClearBufferMask.ColorBufferBit);
GL.MakeCurrent();
GL.ClearColor(ClearColor.X, ClearColor.Y, ClearColor.Z, ClearColor.W);
GL.Clear(GLClearBufferMask.ColorBufferBit);
ImGuiSDL3Platform.NewFrame();
ImGuiSDL3Renderer.NewFrame();
@@ -164,7 +166,7 @@ public sealed unsafe class SDL3Window : IDisposable
ImGui.Render();
_gl.MakeCurrent();
GL.MakeCurrent();
ImGuiSDL3Renderer.RenderDrawData(ImGui.GetDrawData());
@@ -174,8 +176,8 @@ public sealed unsafe class SDL3Window : IDisposable
ImGui.RenderPlatformWindowsDefault();
}
_gl.MakeCurrent();
_gl.SwapBuffers();
GL.MakeCurrent();
GL.SwapBuffers();
if (ShouldClose)
{
@@ -215,10 +217,13 @@ public sealed unsafe class SDL3Window : IDisposable
ImPlot.SetCurrentContext(_imPlotContext);
}
Disposing?.Invoke();
ImGuiSDL3Renderer.Dispose();
ImGuiSDL3Platform.Dispose();
RenderCallback = null;
Disposing = null;
}
if (_imGuiContext.Handle != null)
@@ -294,4 +299,4 @@ public unsafe class BindingsContext : IGLContext
procAddress = (nint)SDL.GLGetProcAddress(procName).GetPtrForDelegate();
return procAddress != 0;
}
}
}