diff --git a/Nerfed.Runtime/Engine.cs b/Nerfed.Runtime/Engine.cs index f02da70..7a6a21b 100644 --- a/Nerfed.Runtime/Engine.cs +++ b/Nerfed.Runtime/Engine.cs @@ -39,7 +39,7 @@ public static class Engine private const string WindowTitle = "Nerfed"; //.. - private static Gui.Controller Controller; + private static Gui.GuiController Controller; internal static void Run(string[] args) { @@ -67,7 +67,7 @@ internal static void Run(string[] args) AudioDevice = new AudioDevice(); - Controller = new Gui.Controller(GraphicsDevice, MainWindow, Color.DarkOliveGreen); + Controller = new Gui.GuiController(GraphicsDevice, MainWindow, Color.DarkOliveGreen); Controller.OnGui += () => { ImGuiNET.ImGui.ShowDemoWindow(); }; diff --git a/Nerfed.Runtime/Gui/Clipboard.cs b/Nerfed.Runtime/Gui/GuiClipboard.cs similarity index 93% rename from Nerfed.Runtime/Gui/Clipboard.cs rename to Nerfed.Runtime/Gui/GuiClipboard.cs index ddb883f..0c1b7e7 100644 --- a/Nerfed.Runtime/Gui/Clipboard.cs +++ b/Nerfed.Runtime/Gui/GuiClipboard.cs @@ -3,10 +3,13 @@ namespace Nerfed.Runtime.Gui; -public static unsafe class Clipboard +public static unsafe class GuiClipboard { private static IntPtr clipboard; - private static readonly Dictionary pinned = new(); + private static readonly Dictionary pinned = new Dictionary(); + + public static readonly IntPtr GetFnPtr = GetPointerTo(Get); + public static readonly IntPtr SetFnPtr = GetPointerTo(Set); private static unsafe void Set(void* userdata, byte* text) { @@ -36,13 +39,12 @@ private static unsafe void Set(void* userdata, byte* text) private static IntPtr GetPointerTo(T fn) where T : Delegate { if (pinned.TryGetValue(fn, out nint ptr)) + { return ptr; + } ptr = Marshal.GetFunctionPointerForDelegate(fn); pinned.Add(fn, ptr); return ptr; } - - public static readonly IntPtr GetFnPtr = GetPointerTo(Get); - public static readonly IntPtr SetFnPtr = GetPointerTo(Set); } \ No newline at end of file diff --git a/Nerfed.Runtime/Gui/Controller.cs b/Nerfed.Runtime/Gui/GuiController.cs similarity index 90% rename from Nerfed.Runtime/Gui/Controller.cs rename to Nerfed.Runtime/Gui/GuiController.cs index e11c366..5b278de 100644 --- a/Nerfed.Runtime/Gui/Controller.cs +++ b/Nerfed.Runtime/Gui/GuiController.cs @@ -9,7 +9,7 @@ namespace Nerfed.Runtime.Gui; -internal class Controller : IDisposable +internal class GuiController : IDisposable { public event Action OnGui; @@ -36,7 +36,7 @@ internal class Controller : IDisposable private readonly Shader imGuiVertexShader; private readonly Shader imGuiFragmentShader; private readonly Sampler imGuiSampler; - private readonly TextureStorage textureStorage = new TextureStorage(); + private readonly GuiTextureStorage textureStorage = new GuiTextureStorage(); private readonly Dictionary windows = new Dictionary(16); private Texture fontTexture = null; @@ -46,7 +46,7 @@ internal class Controller : IDisposable private Graphics.Buffer imGuiIndexBuffer = null; private bool frameBegun = false; - public Controller(GraphicsDevice graphicsDevice, Window mainWindow, Color clearColor, ImGuiConfigFlags configFlags = ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.DockingEnable | ImGuiConfigFlags.ViewportsEnable) + public GuiController(GraphicsDevice graphicsDevice, Window mainWindow, Color clearColor, ImGuiConfigFlags configFlags = ImGuiConfigFlags.NavEnableKeyboard | ImGuiConfigFlags.DockingEnable | ImGuiConfigFlags.ViewportsEnable) { this.mainWindow = mainWindow; this.graphicsDevice = graphicsDevice; @@ -104,10 +104,10 @@ public Controller(GraphicsDevice graphicsDevice, Window mainWindow, Color clearC if (!OperatingSystem.IsWindows()) { - io.SetClipboardTextFn = Clipboard.SetFnPtr; - io.GetClipboardTextFn = Clipboard.GetFnPtr; + io.SetClipboardTextFn = GuiClipboard.SetFnPtr; + io.GetClipboardTextFn = GuiClipboard.GetFnPtr; } - + ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); ImGuiViewportPtr mainViewport = platformIO.Viewports[0]; mainViewport.PlatformHandle = mainWindow.Handle; @@ -143,7 +143,7 @@ public Controller(GraphicsDevice graphicsDevice, Window mainWindow, Color clearC ImGuiNative.ImGuiPlatformIO_Set_Platform_GetWindowSize(platformIO.NativePtr, Marshal.GetFunctionPointerForDelegate(getWindowSize)); } - //io.BackendFlags |= ImGuiBackendFlags.HasMouseCursors; + io.BackendFlags |= ImGuiBackendFlags.HasMouseCursors; io.BackendFlags |= ImGuiBackendFlags.HasSetMousePos; io.BackendFlags |= ImGuiBackendFlags.PlatformHasViewports; io.BackendFlags |= ImGuiBackendFlags.RendererHasViewports; @@ -159,6 +159,7 @@ public void Update(float deltaTime) UpdatePerFrameImGuiData(deltaTime); UpdateInput(); + UpdateCursor(); UpdateMonitors(); frameBegun = true; @@ -247,6 +248,41 @@ private void UpdateInput() } } + private void UpdateCursor() + { + ImGuiIOPtr io = ImGui.GetIO(); + + if ((io.ConfigFlags & ImGuiConfigFlags.NoMouseCursorChange) != 0) + { + return; + } + + ImGuiMouseCursor imGuiCursor = ImGui.GetMouseCursor(); + + if (imGuiCursor == ImGuiMouseCursor.None || io.MouseDrawCursor) + { + SDL2.SDL.SDL_ShowCursor(0); + } + else + { + nint sdlCursor = imGuiCursor switch + { + ImGuiMouseCursor.Arrow => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_ARROW), + ImGuiMouseCursor.TextInput => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_IBEAM), + ImGuiMouseCursor.ResizeAll => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEALL), + ImGuiMouseCursor.ResizeNS => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENS), + ImGuiMouseCursor.ResizeEW => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZEWE), + ImGuiMouseCursor.ResizeNESW => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENESW), + ImGuiMouseCursor.ResizeNWSE => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_SIZENWSE), + ImGuiMouseCursor.Hand => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_HAND), + ImGuiMouseCursor.NotAllowed => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_NO), + _ => SDL2.SDL.SDL_CreateSystemCursor(SDL2.SDL.SDL_SystemCursor.SDL_SYSTEM_CURSOR_ARROW), + }; + SDL2.SDL.SDL_SetCursor(sdlCursor); + SDL2.SDL.SDL_ShowCursor(1); + } + } + private unsafe void UpdateMonitors() { ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO(); @@ -304,7 +340,7 @@ public void Render() { RenderCommandLists(commandBuffer, swapchainTexture, vp.DrawData); graphicsDevice.Submit(commandBuffer); - graphicsDevice.Wait(); + //graphicsDevice.Wait(); } } } diff --git a/Nerfed.Runtime/Gui/GuiStructs.cs b/Nerfed.Runtime/Gui/GuiStructs.cs new file mode 100644 index 0000000..d34d349 --- /dev/null +++ b/Nerfed.Runtime/Gui/GuiStructs.cs @@ -0,0 +1,33 @@ +using Nerfed.Runtime.Graphics; +using System.Numerics; +using System.Runtime.InteropServices; + +namespace Nerfed.Runtime.Gui; + +[StructLayout(LayoutKind.Sequential)] +public struct Position2DTextureColorVertex(Vector2 position, Vector2 texcoord, Color color) : IVertexType +{ + public Vector2 Position = position; + public Vector2 TexCoord = texcoord; + public Color Color = color; + + public static VertexElementFormat[] Formats { get; } = new VertexElementFormat[3] + { + VertexElementFormat.Vector2, + VertexElementFormat.Vector2, + VertexElementFormat.Color + }; + + public static uint[] Offsets { get; } = new uint[3] + { + 0, + 8, + 16 + }; +} + +[StructLayout(LayoutKind.Sequential)] +public struct TransformVertexUniform(Matrix4x4 projectionMatrix) +{ + public Matrix4x4 ProjectionMatrix = projectionMatrix; +} \ No newline at end of file diff --git a/Nerfed.Runtime/Gui/TextureStorage.cs b/Nerfed.Runtime/Gui/GuiTextureStorage.cs similarity index 81% rename from Nerfed.Runtime/Gui/TextureStorage.cs rename to Nerfed.Runtime/Gui/GuiTextureStorage.cs index e56da06..817204b 100644 --- a/Nerfed.Runtime/Gui/TextureStorage.cs +++ b/Nerfed.Runtime/Gui/GuiTextureStorage.cs @@ -2,7 +2,7 @@ namespace Nerfed.Runtime.Gui; -public class TextureStorage +public class GuiTextureStorage { private readonly Dictionary> pointerToTexture = new Dictionary>(); @@ -17,12 +17,12 @@ public IntPtr Add(Texture texture) public Texture GetTexture(IntPtr pointer) { - if (!pointerToTexture.ContainsKey(pointer)) + if (!pointerToTexture.TryGetValue(pointer, out WeakReference value)) { return null; } - WeakReference result = pointerToTexture[pointer]; + WeakReference result = value; if (!result.TryGetTarget(out Texture texture)) { diff --git a/Nerfed.Runtime/Gui/Structs.cs b/Nerfed.Runtime/Gui/Structs.cs deleted file mode 100644 index a633ba6..0000000 --- a/Nerfed.Runtime/Gui/Structs.cs +++ /dev/null @@ -1,49 +0,0 @@ -using Nerfed.Runtime.Graphics; -using System.Numerics; -using System.Runtime.InteropServices; - -namespace Nerfed.Runtime.Gui; - -[StructLayout(LayoutKind.Sequential)] -public struct Position2DTextureColorVertex : IVertexType -{ - public Vector2 Position; - public Vector2 TexCoord; - public Color Color; - - public Position2DTextureColorVertex( - Vector2 position, - Vector2 texcoord, - Color color - ) - { - Position = position; - TexCoord = texcoord; - Color = color; - } - - public static VertexElementFormat[] Formats { get; } = new VertexElementFormat[3] - { - VertexElementFormat.Vector2, - VertexElementFormat.Vector2, - VertexElementFormat.Color - }; - - public static uint[] Offsets => new uint[3] - { - 0, - 8, - 16 - }; -} - -[StructLayout(LayoutKind.Sequential)] -public struct TransformVertexUniform -{ - public Matrix4x4 ProjectionMatrix; - - public TransformVertexUniform(Matrix4x4 projectionMatrix) - { - ProjectionMatrix = projectionMatrix; - } -} \ No newline at end of file