Skip to content

Commit 2756f30

Browse files
committed
Use game widget's mouse move event, clean up InputRouter
1 parent 0348094 commit 2756f30

3 files changed

Lines changed: 12 additions & 27 deletions

File tree

engine/Sandbox.Engine/Systems/Input/InputRouter.Input.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,10 @@ internal static void OnMouseMotion( float dx, float dy )
8787
/// </summary>
8888
internal static void OnMousePositionChange( float x, float y, float dx, float dy )
8989
{
90-
ApplyAbsoluteMousePosition( new Vector2( x, y ), new Vector2( dx, dy ) );
91-
}
92-
93-
/// <summary>
94-
/// Shared absolute-position path for native and editor-driven mouse updates.
95-
/// </summary>
96-
internal static void ApplyAbsoluteMousePosition( Vector2 pos, Vector2 delta )
97-
{
98-
MouseCursorPosition = pos;
90+
MouseCursorPosition = new Vector2( x, y );
9991

10092
if ( InputSystem.GetRelativeMouseMode() )
101-
{
102-
delta = Vector2.Zero;
103-
}
93+
dx = dy = 0;
10494

10595
// If this is set, we're in capture mode - so just update the position
10696
// cache we restore when capture ends. This intentionally records the
@@ -111,10 +101,10 @@ internal static void ApplyAbsoluteMousePosition( Vector2 pos, Vector2 delta )
111101
return;
112102
}
113103

114-
MouseCursorDelta += delta;
104+
MouseCursorDelta += new Vector2( dx, dy );
115105

116106
var mouse = Contexts.FirstOrDefault( x => x.MouseState != InputContext.InputState.Ignore );
117-
mouse?.In_MousePosition( MouseCursorPosition, delta );
107+
mouse?.In_MousePosition( MouseCursorPosition, new Vector2( dx, dy ) );
118108
}
119109

120110
internal static void OnGameControllerButton( int deviceId, GameControllerCode button, bool down )

engine/Sandbox.Tools/GameMode.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public static void SetPlayWidget( SceneRenderingWidget widget )
2020

2121
widget.Focused += WidgetFocused;
2222
widget.Blurred += WidgetBlurred;
23+
widget.MouseTracking = true;
24+
widget.MouseMove += OnPlayWidgetMouseMove;
2325

2426
NativeEngine.InputSystem.RegisterWindowWithSDL( widget._widget.winId() );
2527
g_pEngineServiceMgr.SetEngineState( widget._widget.winId(), widget.SwapChain );
@@ -40,6 +42,8 @@ public static void ClearPlayMode()
4042

4143
_inPlay.Focused -= WidgetFocused;
4244
_inPlay.Blurred -= WidgetBlurred;
45+
_inPlay.MouseMove -= OnPlayWidgetMouseMove;
46+
_inPlay.MouseTracking = false;
4347

4448
NativeEngine.InputSystem.UnregisterWindowFromSDL( _inPlay._widget.winId() );
4549

@@ -68,22 +72,15 @@ private static void WidgetBlurred( FocusChangeReason reason )
6872
NativeEngine.InputSystem.OnEditorGameFocusChange( _inPlay._widget.winId(), false );
6973
}
7074

71-
internal static void Tick()
75+
private static void OnPlayWidgetMouseMove( Vector2 local )
7276
{
73-
// Keep game mouse position updated while the play widget is unfocused.
74-
var playWidget = _inPlay;
75-
if ( playWidget is null || playWidget.IsFocused )
76-
return;
77-
78-
var local = playWidget.FromScreen( Application.CursorPosition );
79-
80-
// Don't snap to borders if cursor isn't over the widget.
81-
if ( local.x < 0 || local.y < 0 || local.x >= playWidget.Size.x || local.y >= playWidget.Size.y )
77+
// SDL handles position when the widget is focused; only fill in the gap when unfocused.
78+
if ( _inPlay is null || _inPlay.IsFocused )
8279
return;
8380

8481
var pos = new Vector2( (int)local.x, (int)local.y );
8582
var delta = pos - InputRouter.MouseCursorPosition;
8683

87-
InputRouter.ApplyAbsoluteMousePosition( pos, delta );
84+
InputRouter.OnMousePositionChange( pos.x, pos.y, delta.x, delta.y );
8885
}
8986
}

engine/Sandbox.Tools/ToolsDll.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@ public void Tick()
260260
Time.Update( scene.TimeNow, scene.TimeDelta );
261261
}
262262

263-
GameMode.Tick();
264-
265263
// Escape was pressed in game and wasn't swallowed
266264
// so lets change focus from the game window to the main editor
267265
// window, which is going to free the mouse cursor from being captured

0 commit comments

Comments
 (0)