Allow changing the cursor, moved Window to GlitchyEngine.UI

This commit is contained in:
Simon Lübeß
2024-10-30 21:20:57 +01:00
parent 1818593394
commit 7506b8c118
11 changed files with 332 additions and 13 deletions
+1
View File
@@ -1,5 +1,6 @@
using System;
using GlitchyEngine.Events;
using GlitchyEngine.UI;
using GlitchyEngine.ImGui;
using GlitchyEngine.Renderer;
using GlitchyEngine.Debug;
@@ -8,6 +8,7 @@ using System;
using System.Interop;
using GlitchyEngine.Events;
using GlitchyEngine.Math;
using GlitchyEngine.UI;
using static System.Windows;
@@ -15,7 +15,7 @@ using static System.Windows;
using internal GlitchyEngine;
namespace GlitchyEngine
namespace GlitchyEngine.UI
{
/// The windows specific Window implementation
public extension Window
@@ -38,6 +38,8 @@ namespace GlitchyEngine
private bool _isActive;
private HCURSOR _cursor;
public override int32 MinWidth
{
get => _minMaxInfo.MinimumTrackingSize.x;
@@ -228,6 +230,9 @@ namespace GlitchyEngine
}
}
[LinkName(.C)]
static extern Windows.IntBool SetLayeredWindowAttributes(HWND hwnd, uint32 crKey, uint8 bAlpha, DWORD dwFlags);
private void Init(WindowDescription desc)
{
Debug.Profiler.ProfileFunction!();
@@ -242,7 +247,18 @@ namespace GlitchyEngine
{
Debug.Profiler.ProfileScope!("CreateWindow");
_windowHandle = CreateWindowExW(.None, WindowClass.ClassName, desc.Title.ToScopedNativeWChar!(), .WS_OVERLAPPEDWINDOW | .WS_VISIBLE,
DirectX.Windows.WindowStyles style = .WS_VISIBLE;
switch (desc.WindowStyle)
{
case .Normal:
style |= .WS_OVERLAPPEDWINDOW | .WS_VISIBLE;
case .Borderless:
style |= .WS_POPUP | .WS_THICKFRAME | .WS_CAPTION | .WS_SYSMENU | .WS_MAXIMIZEBOX | .WS_MINIMIZEBOX;
}
_windowHandle = CreateWindowExW(.None, WindowClass.ClassName, desc.Title.ToScopedNativeWChar!(),
style,
CW_USEDEFAULT, CW_USEDEFAULT, desc.Width, desc.Height, 0, 0, (.)_instanceHandle, null);
}
@@ -522,6 +538,14 @@ namespace GlitchyEngine
delete window._title;
window._title = null;
}
case WM_SETCURSOR:
if (GetLowOrder(lParam) == /*HTCLIENT*/ 1)
{
Winuser.SetCursor(window._cursor);
return 1;
}
break;
case WM_SYSCOMMAND:
{
/* Remove beeping sound when ALT + some key is pressed. */
@@ -609,6 +633,120 @@ namespace GlitchyEngine
_title = new String(titleSpan);
}
public override Result<void> SetCursor(CursorImage cursorImage)
{
LPCTSTR cursor = null;
// Firefox cursors: https://hg.mozilla.org/mozilla-central/file/tip/widget/windows/res
switch (cursorImage)
{
case .Pointer:
cursor = Winuser.IDC_ARROW;
case .Crosshair:
cursor = Winuser.IDC_CROSS;
case .Hand:
cursor = Winuser.IDC_HAND;
case .IBeam:
cursor = Winuser.IDC_IBEAM;
case .Wait:
cursor = Winuser.IDC_WAIT;
case .Help:
cursor = Winuser.IDC_HELP;
case .ResizeEastWest:
cursor = Winuser.IDC_SIZEWE;
case .ResizeNorthSouth:
cursor = Winuser.IDC_SIZENS;
case .ResizeNorthEastSouthWest:
cursor = Winuser.IDC_SIZENESW;
case .ResizeNorthWestSouthEast:
cursor = Winuser.IDC_SIZENWSE;
case .ResizeColumn:
// TODO use cursor file
cursor = Winuser.IDC_SIZEWE;
case .ResizeRow:
// TODO use cursor file
cursor = Winuser.IDC_SIZENS;
case .PanMiddle:
cursor = Winuser.MAKEINTRESOURCEW(32654);
case .PanEast:
cursor = Winuser.MAKEINTRESOURCEW(32658);
case .PanWest:
cursor = Winuser.MAKEINTRESOURCEW(32657);
case .PanNorth:
cursor = Winuser.MAKEINTRESOURCEW(32655);
case .PanSouth:
cursor = Winuser.MAKEINTRESOURCEW(32656);
case .PanNorthEast:
cursor = Winuser.MAKEINTRESOURCEW(32660);
case .PanNorthWest:
cursor = Winuser.MAKEINTRESOURCEW(32659);
case .PanSouthEast:
cursor = Winuser.MAKEINTRESOURCEW(32662);
case .PanSouthWest:
cursor = Winuser.MAKEINTRESOURCEW(32661);
case .Move:
cursor = Winuser.IDC_SIZEALL;
case .VerticalText:
// TODO
case .Cell:
// TODO
case .ContextMenu:
// TODO
case .Alias:
// TODO
case .Progress:
cursor = Winuser.IDC_APPSTARTING;
case .NotAllowed:
cursor = Winuser.IDC_NO;
case .Copy:
// TODO
case .None:
cursor = null;
case .ZoomIn:
// TODO
case .ZoomOut:
case .Grab:
// TODO
case .Grabbing:
// TODO
case .Custom:
// TODO: Use cursor file specified by user
}
var hCursor = Winuser.LoadCursorW((.)0, cursor);
_cursor = hCursor;
Winuser.SetCursor(hCursor);
return .Ok;
}
}
}
@@ -1,6 +1,7 @@
using System;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
using GlitchyEngine.UI;
namespace GlitchyEngine.Renderer
{
+2
View File
@@ -1,3 +1,5 @@
using GlitchyEngine.UI;
namespace GlitchyEngine.Renderer
{
public class SwapChain
+48
View File
@@ -0,0 +1,48 @@
namespace GlitchyEngine.UI;
enum CursorImage
{
None,
Pointer,
Crosshair,
Hand,
IBeam,
Wait,
Help,
ResizeEastWest,
ResizeNorthSouth,
ResizeNorthEastSouthWest,
ResizeNorthWestSouthEast,
ResizeColumn,
ResizeRow,
PanMiddle,
PanNorth,
PanNorthEast,
PanNorthWest,
PanSouth,
PanSouthEast,
PanSouthWest,
PanEast,
PanWest,
Move,
VerticalText,
Cell,
ContextMenu,
Alias,
Progress,
NotAllowed,
Copy,
ZoomIn,
ZoomOut,
Grab,
Grabbing,
Custom
}
@@ -3,23 +3,31 @@ using GlitchyEngine.Events;
using GlitchyEngine.Math;
using GlitchyEngine.Renderer;
namespace GlitchyEngine
namespace GlitchyEngine.UI
{
enum WindowStyle
{
Normal,
Borderless
}
public struct WindowDescription
{
public uint32 Width;
public uint32 Height;
public StringView Title;
public StringView Icon;
public WindowStyle WindowStyle;
public this() => this = default;
public this(uint32 width, uint32 height, StringView title, StringView icon = default)
public this(uint32 width, uint32 height, StringView title, StringView icon = default, WindowStyle windowStyle = .Normal)
{
Width = width;
Height = height;
Title = title;
Icon = icon;
WindowStyle = windowStyle;
}
public static readonly WindowDescription Default => .(1280, 720, "Glitchy Engine")
@@ -119,6 +127,8 @@ namespace GlitchyEngine
*/
public extern Result<void> SetIcon(StringView filePath);
public extern Result<void> SetCursor(CursorImage cursorImage);
private void DefaultEventHandler(Event e)
{
Log.EngineLogger.Error($"No event handler registered for window.");