5
5
using System . Collections . Generic ;
6
6
using System . Diagnostics ;
7
7
using System . Drawing ;
8
+ using System . Threading ;
8
9
using osu . Framework . Bindables ;
9
10
using osu . Framework . Configuration ;
10
11
using osu . Framework . Extensions . EnumExtensions ;
@@ -151,11 +152,12 @@ private void enqueueJoystickButtonInput(JoystickButton button, bool isPressed)
151
152
152
153
private PointF previousPolledPoint = PointF . Empty ;
153
154
154
- private SDL_MouseButtonFlags pressedButtons ;
155
+ private volatile uint pressedButtons ;
155
156
156
157
private void pollMouse ( )
157
158
{
158
159
float x , y ;
160
+ var pressed = ( SDL_MouseButtonFlags ) pressedButtons ;
159
161
SDL_MouseButtonFlags globalButtons = SDL_GetGlobalMouseState ( & x , & y ) ;
160
162
161
163
if ( previousPolledPoint . X != x || previousPolledPoint . Y != y )
@@ -170,18 +172,18 @@ private void pollMouse()
170
172
}
171
173
172
174
// a button should be released if it was pressed and its current global state differs (its bit in globalButtons is set to 0)
173
- SDL_MouseButtonFlags buttonsToRelease = pressedButtons & ( globalButtons ^ pressedButtons ) ;
175
+ SDL_MouseButtonFlags buttonsToRelease = pressed & ( globalButtons ^ pressed ) ;
174
176
175
177
// the outer if just optimises for the common case that there are no buttons to release.
176
178
if ( buttonsToRelease != 0 )
177
179
{
180
+ Interlocked . And ( ref pressedButtons , ( uint ) ~ buttonsToRelease ) ;
181
+
178
182
if ( buttonsToRelease . HasFlagFast ( SDL_MouseButtonFlags . SDL_BUTTON_LMASK ) ) MouseUp ? . Invoke ( MouseButton . Left ) ;
179
183
if ( buttonsToRelease . HasFlagFast ( SDL_MouseButtonFlags . SDL_BUTTON_MMASK ) ) MouseUp ? . Invoke ( MouseButton . Middle ) ;
180
184
if ( buttonsToRelease . HasFlagFast ( SDL_MouseButtonFlags . SDL_BUTTON_RMASK ) ) MouseUp ? . Invoke ( MouseButton . Right ) ;
181
185
if ( buttonsToRelease . HasFlagFast ( SDL_MouseButtonFlags . SDL_BUTTON_X1MASK ) ) MouseUp ? . Invoke ( MouseButton . Button1 ) ;
182
186
if ( buttonsToRelease . HasFlagFast ( SDL_MouseButtonFlags . SDL_BUTTON_X2MASK ) ) MouseUp ? . Invoke ( MouseButton . Button2 ) ;
183
-
184
- pressedButtons &= ~ buttonsToRelease ;
185
187
}
186
188
}
187
189
@@ -440,10 +442,17 @@ private void handleMouseWheelEvent(SDL_MouseWheelEvent evtWheel)
440
442
{
441
443
bool isPrecise ( float f ) => f % 1 != 0 ;
442
444
445
+ bool precise ;
446
+
443
447
if ( isPrecise ( evtWheel . x ) || isPrecise ( evtWheel . y ) )
448
+ {
449
+ precise = true ;
444
450
lastPreciseScroll = evtWheel . timestamp ;
445
-
446
- bool precise = evtWheel . timestamp < lastPreciseScroll + precise_scroll_debounce ;
451
+ }
452
+ else
453
+ {
454
+ precise = evtWheel . timestamp < lastPreciseScroll + precise_scroll_debounce ;
455
+ }
447
456
448
457
// SDL reports horizontal scroll opposite of what framework expects (in non-"natural" mode, scrolling to the right gives positive deltas while we want negative).
449
458
TriggerMouseWheel ( new Vector2 ( - evtWheel . x , evtWheel . y ) , precise ) ;
@@ -458,12 +467,12 @@ private void handleMouseButtonEvent(SDL_MouseButtonEvent evtButton)
458
467
switch ( evtButton . type )
459
468
{
460
469
case SDL_EventType . SDL_EVENT_MOUSE_BUTTON_DOWN :
461
- pressedButtons |= mask ;
462
470
MouseDown ? . Invoke ( button ) ;
471
+ Interlocked . Or ( ref pressedButtons , ( uint ) mask ) ;
463
472
break ;
464
473
465
474
case SDL_EventType . SDL_EVENT_MOUSE_BUTTON_UP :
466
- pressedButtons &= ~ mask ;
475
+ Interlocked . And ( ref pressedButtons , ( uint ) ~ mask ) ;
467
476
MouseUp ? . Invoke ( button ) ;
468
477
break ;
469
478
}
0 commit comments