gui events

fix scroll input
handle gui side, move and closed events
This commit is contained in:
max 2024-07-06 01:29:12 +02:00
parent 7cbb745721
commit 1eb899b240
5 changed files with 73 additions and 32 deletions

View File

@ -153,8 +153,8 @@ private static void Tick()
ProcessSDLEvents(); ProcessSDLEvents();
Controller.Update((float)Timestep.TotalSeconds);
// Tick game here... // Tick game here...
Controller.Update((float)Timestep.TotalSeconds);
AudioDevice.WakeThread(); AudioDevice.WakeThread();
accumulatedUpdateTime -= Timestep; accumulatedUpdateTime -= Timestep;

View File

@ -113,7 +113,7 @@ public Controller(GraphicsDevice graphicsDevice, Window mainWindow, Color clearC
mainViewport.PlatformHandle = mainWindow.Handle; mainViewport.PlatformHandle = mainWindow.Handle;
GCHandle handle = GCHandle.Alloc(mainWindow); GCHandle handle = GCHandle.Alloc(mainWindow);
mainViewport.PlatformUserData = (IntPtr)handle; mainViewport.PlatformUserData = (IntPtr)handle;
windows.Add(mainWindow, handle); AddWindow(mainWindow, mainViewport, handle);
unsafe unsafe
{ {
@ -504,7 +504,11 @@ out int bytesPerPixel
#region Window #region Window
private void CreateWindow(ImGuiViewportPtr vp) private void CreateWindow(ImGuiViewportPtr vp)
{ {
WindowCreateInfo info = new WindowCreateInfo("Window Title", (uint)vp.Pos.X, (uint)vp.Pos.Y, ScreenMode.Windowed); // TODO: Handle all flags.
ScreenMode screenMode = vp.Flags.HasFlag(ImGuiViewportFlags.NoDecoration) ? ScreenMode.WindowedBorderless : ScreenMode.Windowed;
bool systemResizable = !vp.Flags.HasFlag(ImGuiViewportFlags.NoDecoration);
WindowCreateInfo info = new WindowCreateInfo("Window Title", (uint)vp.Pos.X, (uint)vp.Pos.Y, screenMode, systemResizable, false);
Window window = new Window(graphicsDevice, info); Window window = new Window(graphicsDevice, info);
graphicsDevice.ClaimWindow(window, SwapchainComposition.SDR, PresentMode.Immediate); // What PresentMode do we need? graphicsDevice.ClaimWindow(window, SwapchainComposition.SDR, PresentMode.Immediate); // What PresentMode do we need?
@ -512,7 +516,7 @@ private void CreateWindow(ImGuiViewportPtr vp)
GCHandle handle = GCHandle.Alloc(window); GCHandle handle = GCHandle.Alloc(window);
vp.PlatformUserData = (IntPtr)handle; vp.PlatformUserData = (IntPtr)handle;
windows.Add(window, handle); AddWindow(window, vp, handle);
} }
private void DestroyWindow(ImGuiViewportPtr vp) private void DestroyWindow(ImGuiViewportPtr vp)
@ -528,8 +532,38 @@ private void DestroyWindow(ImGuiViewportPtr vp)
windows.Remove(window); windows.Remove(window);
} }
graphicsDevice.Wait(); //graphicsDevice.Wait();
window.Dispose(); window.Dispose();
vp.PlatformUserData = IntPtr.Zero;
}
private void AddWindow(Window window, ImGuiViewportPtr vp, GCHandle handle)
{
window.OnResizedEvent += ((win, w, h) => {
vp.PlatformRequestResize = true;
});
window.OnMovedEvent += ((win, x, y) =>
{
vp.PlatformRequestMove = true;
});
window.OnCloseEvent += (win) =>
{
ImGuiPlatformIOPtr platformIO = ImGui.GetPlatformIO();
for (int i = 0; i < platformIO.Viewports.Capacity; i++)
{
ImGuiViewportPtr vp = platformIO.Viewports[i];
if(win == (Window)GCHandle.FromIntPtr(vp.PlatformUserData).Target)
{
DestroyWindow(vp);
}
}
};
windows.Add(window, handle);
} }
private void ShowWindow(ImGuiViewportPtr vp) private void ShowWindow(ImGuiViewportPtr vp)

View File

@ -1,5 +1,5 @@
using System.Numerics; using SDL2;
using SDL2; using System.Numerics;
namespace Nerfed.Runtime; namespace Nerfed.Runtime;
@ -65,6 +65,8 @@ public static int GetWheelHorizontal()
internal static void Update() internal static void Update()
{ {
wheelX = 0;
wheelY = 0;
Array.Copy(buttonStates, lastButtonStates, buttonStates.Length); Array.Copy(buttonStates, lastButtonStates, buttonStates.Length);
} }
@ -105,8 +107,8 @@ private static void ProcessButtonUpEvent(ref SDL.SDL_MouseButtonEvent ev)
private static void ProcessWheelEvent(ref SDL.SDL_MouseWheelEvent ev) private static void ProcessWheelEvent(ref SDL.SDL_MouseWheelEvent ev)
{ {
wheelX += ev.x; wheelX = ev.x;
wheelY += ev.y; wheelY = ev.y;
} }
private static void ProcessMotionEvent(ref SDL.SDL_MouseMotionEvent ev) private static void ProcessMotionEvent(ref SDL.SDL_MouseMotionEvent ev)

View File

@ -3,6 +3,7 @@ namespace Nerfed.Runtime;
public enum ScreenMode public enum ScreenMode
{ {
Fullscreen, Fullscreen,
BorderlessFullscreen, FullscreenBorderless,
Windowed Windowed,
WindowedBorderless
} }

View File

@ -11,6 +11,10 @@ namespace Nerfed.Runtime;
/// </summary> /// </summary>
public class Window public class Window
{ {
public event Action<Window, uint, uint> OnResizedEvent;
public event Action<Window, int, int> OnMovedEvent;
public event Action<Window> OnCloseEvent;
internal IntPtr Handle { get; } internal IntPtr Handle { get; }
public ScreenMode ScreenMode { get; private set; } public ScreenMode ScreenMode { get; private set; }
public uint Width { get; private set; } public uint Width { get; private set; }
@ -33,8 +37,6 @@ public class Window
public string Title { get; private set;} public string Title { get; private set;}
private bool IsDisposed; private bool IsDisposed;
private System.Action<uint, uint> SizeChangeCallback = null;
private static readonly Dictionary<uint, Window> windowsById = new Dictionary<uint, Window>(); private static readonly Dictionary<uint, Window> windowsById = new Dictionary<uint, Window>();
public Window(GraphicsDevice graphicsDevice, WindowCreateInfo windowCreateInfo) public Window(GraphicsDevice graphicsDevice, WindowCreateInfo windowCreateInfo)
@ -54,10 +56,14 @@ public Window(GraphicsDevice graphicsDevice, WindowCreateInfo windowCreateInfo)
{ {
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN; flags |= SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN;
} }
else if (windowCreateInfo.ScreenMode == ScreenMode.BorderlessFullscreen) else if (windowCreateInfo.ScreenMode == ScreenMode.FullscreenBorderless)
{ {
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP; flags |= SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP;
} }
else if(windowCreateInfo.ScreenMode == ScreenMode.WindowedBorderless)
{
flags |= SDL.SDL_WindowFlags.SDL_WINDOW_BORDERLESS;
}
if (windowCreateInfo.SystemResizable) if (windowCreateInfo.SystemResizable)
{ {
@ -104,6 +110,9 @@ internal static void ProcessEvent(ref SDL.SDL_Event ev)
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED: case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED:
window.ProcessSizeChangedEvent(ref ev.window); window.ProcessSizeChangedEvent(ref ev.window);
break; break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_MOVED:
window.ProcessMovedChangedEvent(ref ev.window);
break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE: case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE:
window.ProcessCloseEvent(ref ev.window); window.ProcessCloseEvent(ref ev.window);
break; break;
@ -117,16 +126,19 @@ private void ProcessSizeChangedEvent(ref SDL.SDL_WindowEvent ev)
Width = newWidth; Width = newWidth;
Height = newHeight; Height = newHeight;
if (SizeChangeCallback != null) OnResizedEvent?.Invoke(this, Width, Height);
{
SizeChangeCallback(newWidth, newHeight);
} }
private void ProcessMovedChangedEvent(ref SDL.SDL_WindowEvent ev)
{
OnMovedEvent?.Invoke(this, ev.data1, ev.data2);
} }
private void ProcessCloseEvent(ref SDL.SDL_WindowEvent ev) private void ProcessCloseEvent(ref SDL.SDL_WindowEvent ev)
{ {
Engine.GraphicsDevice.UnclaimWindow(this); Engine.GraphicsDevice.UnclaimWindow(this);
Dispose(); Dispose();
OnCloseEvent?.Invoke(this);
} }
/// <summary> /// <summary>
@ -140,7 +152,7 @@ public void SetScreenMode(ScreenMode screenMode)
{ {
windowFlag = SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN; windowFlag = SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN;
} }
else if (screenMode == ScreenMode.BorderlessFullscreen) else if (screenMode == ScreenMode.FullscreenBorderless)
{ {
windowFlag = SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP; windowFlag = SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP;
} }
@ -195,14 +207,6 @@ internal void Show()
SDL.SDL_ShowWindow(Handle); SDL.SDL_ShowWindow(Handle);
} }
/// <summary>
/// You can specify a method to run when the window size changes.
/// </summary>
public void RegisterSizeChangeCallback(System.Action<uint, uint> sizeChangeCallback)
{
SizeChangeCallback = sizeChangeCallback;
}
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (!IsDisposed) if (!IsDisposed)