22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System . Numerics ;
5+ using System . Runtime . CompilerServices ;
6+ using Silk . NET . Maths ;
57using Silk . NET . SDL ;
68
79namespace Silk . NET . Input . SDL3 . Devices . Pointers ;
810
9- internal class SdlMouse : SdlPointerDevice , IMouse , ISdlDevice < SdlMouse >
11+ internal sealed class SdlMouse : SdlPointerDevice , IMouse , ISdlDevice < SdlMouse >
1012{
1113 public override PointerState State => _state ;
1214 public ICursorConfiguration Cursor { get ; }
@@ -20,7 +22,8 @@ private SdlMouse(ulong sdlDeviceId, nint uniqueId, SdlInputBackend backend, IPoi
2022 _state = new MouseState ( Buttons , Points , Vector2 . Zero ) ;
2123 Cursor = cursor ;
2224 float x = 0 , y = 0 ;
23- _ = NativeBackend . GetMouseState ( x . AsRef ( ) , y . AsRef ( ) ) ;
25+ var mouseInputFlags = GetButtonMaskSdl ( ref x , ref y ) ;
26+ ApplyMouseButtonState ( mouseInputFlags ) ;
2427
2528 var window = NativeBackend . GetMouseFocus ( ) ;
2629 uint windowId ;
@@ -37,14 +40,28 @@ private SdlMouse(ulong sdlDeviceId, nint uniqueId, SdlInputBackend backend, IPoi
3740 }
3841 }
3942
40- _mouseWindowId = windowId ;
4143 var pressure = _state . Buttons [ PointerButton . Primary ] . Pressure ;
4244 SetTargetPoint ( windowId , new Vector3 ( x , y , 0 ) , pressure ) ;
4345 // var point = _unboundedPointerTarget.GetPoint(this, 0);
4446 }
4547
46- protected override uint GetButtonMaskSdl ( ) => NativeBackend . GetMouseState ( nullptr , nullptr ) ;
48+ private void ApplyMouseButtonState ( SdlMouseInputFlags mouseState )
49+ {
50+ foreach ( var pointerButtonName in EnumInfo < PointerButton > . UniqueValues )
51+ {
52+ ref var button = ref GetButtonRef ( pointerButtonName ) ;
53+ var isDown = mouseState . Has ( pointerButtonName ) ;
54+ button = button with { IsDown = isDown , Pressure = isDown ? 1 : 0 } ;
55+ }
56+ }
57+
58+ public override void Initialize ( )
59+ {
4760
61+ }
62+
63+ private unsafe SdlMouseInputFlags GetButtonMaskSdl ( ref float x , ref float y ) =>
64+ ( SdlMouseInputFlags ) NativeBackend . GetMouseState ( ( float * ) Unsafe . AsPointer ( ref x ) , ( float * ) Unsafe . AsPointer ( ref y ) ) ;
4865
4966 public static SdlMouse CreateDevice ( ulong sdlDeviceId , SdlInputBackend backend )
5067 {
@@ -101,7 +118,7 @@ public bool TrySetPosition(Vector2 position)
101118 {
102119 if ( NativeBackend . WarpMouseGlobal ( position . X , position . Y ) )
103120 {
104- SetTargetPoint ( _mouseWindowId , new Vector3 ( position . X , position . Y , 0 ) , 0 ) ;
121+ SetTargetPoint ( null , new Vector3 ( position . X , position . Y , 0 ) , 0 ) ;
105122 return true ;
106123 }
107124
@@ -116,9 +133,12 @@ public void AddMotion(in MouseMotionEvent evtMotion)
116133 _accumulatedMotion += movementRelative ;
117134 // todo - test against evtMotion state values
118135
119- SetTargetPoint ( mouseWindowId , _accumulatedMotion , 0 ) ;
136+ //SetTargetPoint(mouseWindowId, _accumulatedMotion, 0, 0);
137+ SetTargetPoint ( mouseWindowId , new Vector3 ( evtMotion . X , evtMotion . Y , 0 ) , 0 ) ;
120138 }
121139
140+
141+
122142 public void AddButtonEvent ( in MouseButtonEvent evtButton )
123143 {
124144 var button = PointerButton . Primary + evtButton . Button ;
@@ -128,12 +148,14 @@ public void AddButtonEvent(in MouseButtonEvent evtButton)
128148
129149 public void AddWheelEvent ( in MouseWheelEvent evtWheel )
130150 {
131- _mouseScroll [ 0 ] += evtWheel . X ;
132- _mouseScroll [ 1 ] += evtWheel . Y ;
151+ ref var x = ref _mouseScroll . X ;
152+ ref var y = ref _mouseScroll . Y ;
153+ x += evtWheel . X ;
154+ y += evtWheel . Y ;
133155
134156 // todo - evt.Which?
135- var hMagnitude = MathF . Abs ( _mouseScroll [ 0 ] ) ;
136- var vMagnitude = MathF . Abs ( _mouseScroll [ 1 ] ) ;
157+ var hMagnitude = MathF . Abs ( x ) ;
158+ var vMagnitude = MathF . Abs ( y ) ;
137159
138160 if ( hMagnitude >= 1 )
139161 {
@@ -151,31 +173,6 @@ public void AddWheelEvent(in MouseWheelEvent evtWheel)
151173 }
152174
153175
154- private void SetTargetPoint ( uint windowId , in Vector3 pos , float pressure )
155- {
156- _ = Backend . TryGetPointerTargetForWindow ( windowId , out var windowTarget ) ;
157-
158- if ( TryGetPointIndexForTarget ( windowTarget , out var index ) )
159- {
160- UpdatePoint ( ToTargetPoint ( pos , pressure , windowTarget ) , index ) ;
161- }
162- else
163- {
164- AddPoint ( ToTargetPoint ( pos , pressure , windowTarget ) ) ;
165- }
166-
167- #if DEBUG
168- if ( _mouseWindowId != windowId )
169- {
170- InputLog . Warn ( $ "Mouse window changed from { _mouseWindowId } to { windowId } ") ;
171- }
172- #endif
173-
174- _mouseWindowId = windowId ;
175- }
176-
177-
178- private uint _mouseWindowId ;
179176 private Vector2 _mouseScroll ;
180177 private Vector3 _accumulatedMotion ;
181178}
0 commit comments