@@ -13,87 +13,38 @@ internal class SdlMouse : SdlPointerDevice, IMouse, ISdlDevice<SdlMouse>
1313
1414 private readonly MouseState _state ;
1515
16- private SdlMouse ( uint sdlDeviceId , nint uniqueId , SdlInputBackend backend , IPointerTarget unboundedPointerTarget , ICursorConfiguration cursor )
17- : base ( backend , uniqueId , sdlDeviceId )
16+ private SdlMouse ( uint sdlDeviceId , nint uniqueId , SdlInputBackend backend , IPointerTarget unboundedPointerTarget ,
17+ ICursorConfiguration cursor )
18+ : base ( backend , uniqueId , sdlDeviceId , unboundedPointerTarget )
1819 {
19- _state = new MouseState ( new ButtonReadOnlyList < PointerButton > ( _buttons ) ,
20- new InputReadOnlyList < TargetPoint > ( _points ) , Vector2 . Zero ) ;
20+ _state = new MouseState ( Buttons , Points , Vector2 . Zero ) ;
2121 Cursor = cursor ;
2222 float x = 0 , y = 0 ;
23- var buttonMask = NativeBackend . GetMouseState ( x . AsRef ( ) , y . AsRef ( ) ) ;
24- var pos = new Vector2 ( x , y ) ;
25-
26- for ( var i = 0 ; i < EnumInfo < PointerButton > . UniqueValues . Count ; i ++ )
27- {
28- var button = EnumInfo < PointerButton > . UniqueValues [ i ] ;
29- var pressed = IsPointerButtonPressedSdl ( button , buttonMask ) ;
30- _buttons . Add ( new Button < PointerButton > ( button , pressed , pressed ? 1.0f : 0.0f ) ) ;
31- }
23+ _ = NativeBackend . GetMouseState ( x . AsRef ( ) , y . AsRef ( ) ) ;
3224
3325 var window = NativeBackend . GetMouseFocus ( ) ;
34- var pressure = _state . Buttons [ PointerButton . Primary ] . Pressure ;
35- AddTargetPoint ( window , pos , pressure ) ;
36-
37- // add unbounded target
38- // var point = _unboundedPointerTarget.GetPoint(this, 0);
39- _targetListNoWindow = [ unboundedPointerTarget ] ;
40- _targetListWithWindow = [ null ! , unboundedPointerTarget ] ;
41- }
42-
43- private void AddTargetPoint ( WindowHandle window , Vector2 pos , float pressure )
44- {
45- if ( ! Backend . TryGetPointerTargetForWindow ( window , out var windowTarget ) )
26+ uint windowId ;
27+ if ( window == nullptr )
4628 {
47- AddUnboundedPoint ( pos , pressure ) ;
29+ windowId = 0 ;
4830 }
4931 else
5032 {
51- AddWindowPoint ( pos , pressure , windowTarget ) ;
33+ windowId = NativeBackend . GetWindowID ( window ) ;
34+ if ( windowId == 0 )
35+ {
36+ SdlLog . Error ( "Mouse has no window" ) ;
37+ }
5238 }
53- }
5439
55- private void AddTargetPoint ( uint windowId , Vector2 pos , float pressure )
56- {
57- if ( Backend . TryGetPointerTargetForWindow ( windowId , out var windowTarget ) )
58- {
59- AddWindowPoint ( pos , pressure , windowTarget ) ;
60- }
61- else
62- {
63- AddUnboundedPoint ( pos , pressure ) ;
64- }
40+ _mouseWindowId = windowId ;
41+ var pressure = _state . Buttons [ PointerButton . Primary ] . Pressure ;
42+ SetTargetPoint ( windowId , new Vector3 ( x , y , 0 ) , pressure ) ;
43+ // var point = _unboundedPointerTarget.GetPoint(this, 0);
6544 }
6645
67- private void AddUnboundedPoint ( Vector2 pos , float pressure ) =>
68- // add raw position (likely just 0, but that's ok for now)
69- _points . Add (
70- new TargetPoint ( 0 , // todo: use a unique id
71- Flags : TargetPointFlags . NotPointingAtTarget ,
72- Position : new Vector3 ( pos , 0 ) ,
73- NormalizedPosition : default ,
74- Pointer : default ,
75- Pressure : pressure ,
76- Target : null
77- )
78- ) ;
79-
80- private void AddWindowPoint ( Vector2 pos , float pressure , IPointerTarget windowTarget )
81- {
82- var bounds = windowTarget . Bounds ;
83- var min = new Vector2 ( bounds . Min . X , bounds . Min . Y ) ;
84- var max = new Vector2 ( bounds . Max . X , bounds . Max . Y ) ;
85-
86- _points . Add (
87- new TargetPoint (
88- Id : 0 , // todo - use a unique id
89- Flags : TargetPointFlags . PointingAtTarget ,
90- Position : new Vector3 ( pos , 0 ) ,
91- NormalizedPosition : new Vector3 ( ( pos - min ) / ( max - min ) , 0 ) ,
92- Pointer : default ,
93- Pressure : pressure ,
94- Target : windowTarget
95- ) ) ;
96- }
46+ protected override uint GetButtonMaskSdl ( ) => NativeBackend . GetMouseState ( nullptr , nullptr ) ;
47+
9748
9849 public static unsafe SdlMouse CreateDevice ( uint sdlDeviceId , SdlInputBackend backend )
9950 {
@@ -105,7 +56,8 @@ public static unsafe SdlMouse CreateDevice(uint sdlDeviceId, SdlInputBackend bac
10556 }
10657
10758 backend . Sdl . Free ( deviceName ) ;
108- return new SdlMouse ( sdlDeviceId , uniqueId , backend , backend . UnboundedPointerTarget , backend . CursorConfiguration ) ;
59+ return new SdlMouse ( sdlDeviceId , uniqueId , backend , backend . UnboundedPointerTarget ,
60+ backend . CursorConfiguration ) ;
10961 }
11062
11163 public override string Name => NativeBackend . GetMouseNameForID ( SdlDeviceId ) . ReadToString ( ) ;
@@ -116,64 +68,65 @@ protected override void Release()
11668
11769 MouseState IMouse . State => _state ;
11870
119- public override unsafe IReadOnlyList < IPointerTarget > Targets
71+ private bool IsMouseRelative
12072 {
12173 get
12274 {
123- if ( _mouseWindowId == 0 )
75+ //var focus = NativeBackend.GetMouseFocus();
76+ if ( ! Backend . TryGetWindowHandles ( out var windows ) )
12477 {
125- return _targetListNoWindow ;
78+ return false ;
12679 }
12780
128- if ( ! Backend . TryGetPointerTargetForWindow ( _mouseWindowId , out var target ) )
81+ var isRelative = false ;
82+
83+ for ( var i = 0 ; i < windows . Count ; i ++ )
12984 {
130- return _targetListNoWindow ;
85+ isRelative |= NativeBackend . GetWindowRelativeMouseMode ( windows [ i ] ) ;
13186 }
13287
133- _targetListWithWindow [ 0 ] = target ;
134- return _targetListWithWindow ;
88+ windows . Dispose ( ) ;
89+ return isRelative ;
13590 }
13691 }
13792
138- protected override bool IsBounded { get ; }
139-
93+ protected override bool OnePointOnly => true ;
14094
14195 public bool TrySetPosition ( Vector2 position )
14296 {
14397 if ( NativeBackend . WarpMouseGlobal ( position . X , position . Y ) )
14498 {
99+ SetTargetPoint ( _mouseWindowId , new Vector3 ( position . X , position . Y , 0 ) , 0 ) ;
145100 return true ;
146101 }
147102
148- NativeBackend . ClearError ( ) ;
103+ SdlLog . Error ( "Failed to set mouse position" ) ;
149104 return false ;
150105 }
151106
152-
153107 public void AddMotion ( in MouseMotionEvent evtMotion )
154108 {
155- _mouseWindowId = evtMotion . WindowID ;
156- var movementRelative = new Vector2 ( evtMotion . Xrel , evtMotion . Yrel ) ;
109+ var mouseWindowId = evtMotion . WindowID ;
110+ var movementRelative = new Vector3 ( evtMotion . Xrel , evtMotion . Yrel , 0 ) ;
157111 _accumulatedMotion += movementRelative ;
112+ // todo - test against evtMotion state values
158113
159- // add clear old point, add new point
160- _points . Clear ( ) ;
161- AddTargetPoint ( _mouseWindowId , _accumulatedMotion , 0 ) ;
114+ SetTargetPoint ( mouseWindowId , _accumulatedMotion , 0 ) ;
162115 }
163116
164117 public void AddButtonEvent ( in MouseButtonEvent evtButton )
165118 {
166119 var button = PointerButton . Primary + evtButton . Button ;
167- var idx = EnumInfo < PointerButton > . ValueIndexOfUnnamed ( button ) ;
168120 const float mult = 1 / 255f ;
169- _buttons [ idx ] = new Button < PointerButton > ( button , evtButton . Down > 0 , evtButton . Down * mult ) ;
121+ AddButtonEvent ( button , evtButton . Down > 0 , evtButton . Down * mult ) ;
170122 }
171123
172124 public void AddWheelEvent ( in MouseWheelEvent evtWheel )
173125 {
174126 _mouseScroll [ 0 ] += evtWheel . X ;
175127 _mouseScroll [ 1 ] += evtWheel . Y ;
176128
129+ // todo - evt.Which?
177130 var hMagnitude = MathF . Abs ( _mouseScroll [ 0 ] ) ;
178131 var vMagnitude = MathF . Abs ( _mouseScroll [ 1 ] ) ;
179132
@@ -189,26 +142,35 @@ public void AddWheelEvent(in MouseWheelEvent evtWheel)
189142 _mouseScroll . Y = 0 ;
190143 }
191144
192- // todo - actually do stuff
193- throw new NotImplementedException ( ) ;
145+ _state . WheelPosition = _mouseScroll ;
194146 }
195147
196- private static bool IsPointerButtonPressedSdl ( PointerButton button , uint state )
148+
149+ private void SetTargetPoint ( uint windowId , in Vector3 pos , float pressure )
197150 {
198- var index = EnumInfo < PointerButton > . ValueIndexOf ( button ) ;
199- if ( index is < 0 or >= 32 )
151+ _ = Backend . TryGetPointerTargetForWindow ( windowId , out var windowTarget ) ;
152+
153+ if ( TryGetPointIndexForTarget ( windowTarget , out var index ) )
154+ {
155+ UpdatePoint ( ToTargetPoint ( pos , pressure , windowTarget ) , index ) ;
156+ }
157+ else
200158 {
201- return false ;
159+ AddPoint ( ToTargetPoint ( pos , pressure , windowTarget ) ) ;
202160 }
203161
204- return ( state & ( 1 << index ) ) != 0 ;
162+ #if DEBUG
163+ if ( _mouseWindowId != windowId )
164+ {
165+ InputLog . Warn ( $ "Mouse window changed from { _mouseWindowId } to { windowId } ") ;
166+ }
167+ #endif
168+
169+ _mouseWindowId = windowId ;
205170 }
206171
172+
207173 private uint _mouseWindowId ;
208174 private Vector2 _mouseScroll ;
209- private Vector2 _accumulatedMotion ;
210- private readonly List < Button < PointerButton > > _buttons = [ ] ;
211- private readonly List < TargetPoint > _points = new ( ) ;
212- private readonly IPointerTarget [ ] _targetListNoWindow ;
213- private readonly IPointerTarget [ ] _targetListWithWindow ;
175+ private Vector3 _accumulatedMotion ;
214176}
0 commit comments