Rename and mouse cursor support

This commit is contained in:
max 2024-07-10 21:50:04 +02:00
parent 1eb899b240
commit 2c839d8fad
6 changed files with 89 additions and 67 deletions

View File

@ -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();
};

View File

@ -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<Delegate, IntPtr> pinned = new();
private static readonly Dictionary<Delegate, IntPtr> pinned = new Dictionary<Delegate, IntPtr>();
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>(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);
}

View File

@ -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<Window, GCHandle> windows = new Dictionary<Window, GCHandle>(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,8 +104,8 @@ 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();
@ -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();
}
}
}

View File

@ -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;
}

View File

@ -2,7 +2,7 @@
namespace Nerfed.Runtime.Gui;
public class TextureStorage
public class GuiTextureStorage
{
private readonly Dictionary<IntPtr, WeakReference<Texture>> pointerToTexture = new Dictionary<IntPtr, WeakReference<Texture>>();
@ -17,12 +17,12 @@ public IntPtr Add(Texture texture)
public Texture GetTexture(IntPtr pointer)
{
if (!pointerToTexture.ContainsKey(pointer))
if (!pointerToTexture.TryGetValue(pointer, out WeakReference<Texture> value))
{
return null;
}
WeakReference<Texture> result = pointerToTexture[pointer];
WeakReference<Texture> result = value;
if (!result.TryGetTarget(out Texture texture))
{

View File

@ -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;
}
}