Skip to content

Commit 7217997

Browse files
[Input] Added Cursor class;
1 parent 5523d01 commit 7217997

File tree

6 files changed

+155
-41
lines changed

6 files changed

+155
-41
lines changed

Engine/Core/Input/Cursor.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using Staple.Internal;
2+
3+
namespace Staple;
4+
5+
public static class Cursor
6+
{
7+
internal static IRenderWindow window;
8+
9+
private static CursorLockMode lockState = CursorLockMode.None;
10+
11+
/// <summary>
12+
/// The current lock state of the cursor
13+
/// </summary>
14+
public static CursorLockMode LockState
15+
{
16+
get => lockState;
17+
18+
set
19+
{
20+
lockState = value;
21+
22+
switch(value)
23+
{
24+
case CursorLockMode.None:
25+
26+
UnlockCursor();
27+
28+
break;
29+
30+
case CursorLockMode.Locked:
31+
32+
LockCursor();
33+
34+
break;
35+
}
36+
}
37+
}
38+
39+
internal static bool visible = true;
40+
41+
/// <summary>
42+
/// Whether the cursor is visible
43+
/// </summary>
44+
public static bool Visible
45+
{
46+
get => visible;
47+
48+
set
49+
{
50+
visible = value;
51+
52+
if(visible)
53+
{
54+
ShowCursor();
55+
}
56+
else
57+
{
58+
HideCursor();
59+
}
60+
}
61+
}
62+
63+
/// <summary>
64+
/// Locks the cursor to the window
65+
/// </summary>
66+
internal static void LockCursor()
67+
{
68+
window.LockCursor();
69+
}
70+
71+
/// <summary>
72+
/// Unlocks the cursor
73+
/// </summary>
74+
internal static void UnlockCursor()
75+
{
76+
window.UnlockCursor();
77+
}
78+
79+
/// <summary>
80+
/// Hides the cursor
81+
/// </summary>
82+
internal static void HideCursor()
83+
{
84+
window.HideCursor();
85+
}
86+
87+
/// <summary>
88+
/// Shows the cursor
89+
/// </summary>
90+
internal static void ShowCursor()
91+
{
92+
window.ShowCursor();
93+
}
94+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Staple;
2+
3+
/// <summary>
4+
/// How the mouse cursor should be handled
5+
/// </summary>
6+
public enum CursorLockMode
7+
{
8+
/// <summary>
9+
/// Not locked to the window at all
10+
/// </summary>
11+
None,
12+
13+
/// <summary>
14+
/// Locked to the center of the window
15+
/// </summary>
16+
Locked,
17+
}

Engine/Core/Input/Input.cs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -537,14 +537,21 @@ internal static void CursorPosCallback(float xpos, float ypos)
537537
{
538538
var newPos = new Vector2(xpos, ypos);
539539

540-
if(MousePosition == Vector2.Zero)
540+
if(Cursor.LockState == CursorLockMode.Locked)
541541
{
542-
previousMousePosition = newPos;
542+
MouseRelativePosition = newPos;
543543
}
544+
else
545+
{
546+
if (MousePosition == Vector2.Zero)
547+
{
548+
previousMousePosition = newPos;
549+
}
544550

545-
MousePosition = newPos;
551+
MousePosition = newPos;
546552

547-
MouseRelativePosition = newPos - previousMousePosition;
553+
MouseRelativePosition = newPos - previousMousePosition;
554+
}
548555
}
549556

550557
internal static void HandleTextEvent(AppEvent appEvent)
@@ -780,38 +787,6 @@ public static Vector2 GetGamepadRightAxis(int index)
780787
GetGamepadAxis(index, GamepadAxis.RightY));
781788
}
782789

783-
/// <summary>
784-
/// Locks the cursor to the window
785-
/// </summary>
786-
public static void LockCursor()
787-
{
788-
window.LockCursor();
789-
}
790-
791-
/// <summary>
792-
/// Unlocks the cursor
793-
/// </summary>
794-
public static void UnlockCursor()
795-
{
796-
window.UnlockCursor();
797-
}
798-
799-
/// <summary>
800-
/// Hides the cursor
801-
/// </summary>
802-
public static void HideCursor()
803-
{
804-
window.HideCursor();
805-
}
806-
807-
/// <summary>
808-
/// Shows the cursor
809-
/// </summary>
810-
public static void ShowCursor()
811-
{
812-
window.ShowCursor();
813-
}
814-
815790
/// <summary>
816791
/// Registers an input action for being pressed
817792
/// </summary>

Engine/Core/Rendering/Windowing/Impls/SDL2RenderWindow.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,14 @@ public void PollEvents()
402402

403403
case SDL.SDL_EventType.SDL_MOUSEMOTION:
404404

405-
Input.CursorPosCallback(_event.motion.x, _event.motion.y);
405+
if(SDL.SDL_GetRelativeMouseMode() == SDL.SDL_bool.SDL_TRUE)
406+
{
407+
Input.CursorPosCallback(_event.motion.xrel, _event.motion.yrel);
408+
}
409+
else
410+
{
411+
Input.CursorPosCallback(_event.motion.x, _event.motion.y);
412+
}
406413

407414
break;
408415

@@ -665,12 +672,16 @@ public nint MonitorPointer(AppPlatform platform)
665672

666673
public void LockCursor()
667674
{
668-
SDL.SDL_SetWindowMouseGrab(window, SDL.SDL_bool.SDL_TRUE);
675+
SDL.SDL_SetRelativeMouseMode(SDL.SDL_bool.SDL_TRUE);
676+
677+
Cursor.visible = false;
669678
}
670679

671680
public void UnlockCursor()
672681
{
673-
SDL.SDL_SetWindowMouseGrab(window, SDL.SDL_bool.SDL_FALSE);
682+
SDL.SDL_SetRelativeMouseMode(SDL.SDL_bool.SDL_FALSE);
683+
684+
Cursor.visible = true;
674685
}
675686

676687
public void HideCursor()

Engine/Core/Rendering/Windowing/RenderWindow.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ public string Title
5454

5555
private bool renderThreadReady = false;
5656
private Thread renderThread;
57-
private bgfx.Init init = new bgfx.Init();
57+
private bgfx.Init init = new();
5858
private AppPlatform currentPlatform;
59+
private CursorLockMode lastCursorLockMode;
5960

6061
public bool Paused => hasFocus == false && AppSettings.Current.runInBackground == false;
6162

@@ -162,6 +163,20 @@ private void SingleThreadLoop()
162163
{
163164
hasFocus = window.IsFocused;
164165

166+
if(Platform.IsDesktopPlatform)
167+
{
168+
if(hasFocus && lastCursorLockMode != Cursor.LockState)
169+
{
170+
Cursor.LockState = lastCursorLockMode;
171+
}
172+
else
173+
{
174+
lastCursorLockMode = Cursor.LockState;
175+
176+
Cursor.LockState = CursorLockMode.None;
177+
}
178+
}
179+
165180
try
166181
{
167182
OnScreenSizeChange?.Invoke(hasFocus);
@@ -865,7 +880,6 @@ public bool SetResolution(int width, int height, WindowMode windowMode)
865880
/// <param name="height">The window's height</param>
866881
/// <param name="resizable">Whether it should be resizable</param>
867882
/// <param name="windowMode">The window mode</param>
868-
/// <param name="appSettings">Application Settings</param>
869883
/// <param name="maximized">Whether the window should be maximized</param>
870884
/// <param name="position">A specific position for the window, or null for default</param>
871885
/// <param name="monitorIndex">The monitor index to use</param>
@@ -938,6 +952,7 @@ public static RenderWindow Create(int width, int height, bool resizable, WindowM
938952
}
939953

940954
Input.window = renderWindow.window;
955+
Cursor.window = renderWindow.window;
941956

942957
//Issue with Metal
943958
if(Platform.IsMacOS)

Engine/Core/StapleCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@
139139
<Compile Include="External\StbTruetypeSharp\src\StbTrueType.OldRasterizer.cs" />
140140
<Compile Include="Hooks\IStapleHook.cs" />
141141
<Compile Include="Hooks\StapleHookEvent.cs" />
142+
<Compile Include="Input\Cursor.cs" />
143+
<Compile Include="Input\CursorLockMode.cs" />
142144
<Compile Include="Input\GamepadAxis.cs" />
143145
<Compile Include="Input\GamepadButton.cs" />
144146
<Compile Include="Input\GamepadConnectionState.cs" />

0 commit comments

Comments
 (0)