1- using SharpDX . XInput ;
1+ using Windows . Gaming . Input ;
22using System . Windows . Threading ;
33using System . Diagnostics ;
44
55namespace GameController_Helper ;
66
77public class ControllerToMouseMapper
88{
9- private Controller ? _controller ;
9+ private Gamepad ? _controller ;
1010 private DispatcherTimer _timer ;
11- private const int Deadzone = 2500 ;
11+ private const double Deadzone = 2500.0 / 32768.0 ; // 对应原本的 2500 / 32768
1212 private const float SensitivityX = 0.0005f ;
1313 private const float SensitivityY = 0.0005f ;
1414 private const float ScrollSensitivity = 0.1f ;
1515
16+ private const float MaxMoveSpeedX = 32768 * SensitivityX ;
17+ private const float MaxMoveSpeedY = 32768 * SensitivityY ;
18+ private const float MaxScrollSpeed = 255 * ScrollSensitivity ;
19+
1620 private bool _isStarted ;
1721
1822 private bool _leftStickDown ;
@@ -22,7 +26,7 @@ public class ControllerToMouseMapper
2226
2327 public event Action < bool > ? ConnectionStatusChanged ;
2428
25- public bool IsControllerConnected => _controller ? . IsConnected ?? false ;
29+ public bool IsControllerConnected => _controller != null ;
2630
2731 public ControllerToMouseMapper ( )
2832 {
@@ -70,19 +74,17 @@ private void FindController()
7074 if ( ( DateTime . Now - _lastSearchTime ) . TotalSeconds < 1 ) return ;
7175 _lastSearchTime = DateTime . Now ;
7276
73- var userIndices = new [ ] { UserIndex . One , UserIndex . Two , UserIndex . Three , UserIndex . Four } ;
74- foreach ( var index in userIndices )
77+ var gamepads = Gamepad . Gamepads ;
78+ if ( gamepads . Count > 0 )
7579 {
76- var c = new Controller ( index ) ;
77- if ( c . IsConnected )
78- {
79- _controller = c ;
80- ConnectionStatusChanged ? . Invoke ( true ) ;
81- return ;
82- }
80+ _controller = gamepads [ 0 ] ;
81+ ConnectionStatusChanged ? . Invoke ( true ) ;
82+ }
83+ else
84+ {
85+ _controller = null ;
86+ ConnectionStatusChanged ? . Invoke ( false ) ;
8387 }
84- _controller = null ;
85- ConnectionStatusChanged ? . Invoke ( false ) ;
8688 }
8789
8890 private float _accumulatedX ;
@@ -91,29 +93,28 @@ private void FindController()
9193
9294 private void Timer_Tick ( object ? sender , EventArgs e )
9395 {
94- if ( _controller == null || ! _controller . IsConnected )
96+ if ( _controller == null || ! Gamepad . Gamepads . Contains ( _controller ) )
9597 {
9698 FindController ( ) ;
9799 return ;
98100 }
99101
100102 if ( ! _isStarted ) return ;
101103
102- var state = _controller . GetState ( ) ;
103- var gamepad = state . Gamepad ;
104+ var reading = _controller . GetCurrentReading ( ) ;
104105
105106 // 鼠标移动 (右摇杆)
106107 float targetX = 0 ;
107108 float targetY = 0 ;
108109
109- if ( Math . Abs ( ( int ) gamepad . RightThumbX ) > Deadzone )
110+ if ( Math . Abs ( reading . RightThumbstickX ) > Deadzone )
110111 {
111- targetX = gamepad . RightThumbX * SensitivityX ;
112+ targetX = ( float ) ( reading . RightThumbstickX * MaxMoveSpeedX ) ;
112113 }
113114
114- if ( Math . Abs ( ( int ) gamepad . RightThumbY ) > Deadzone )
115+ if ( Math . Abs ( reading . RightThumbstickY ) > Deadzone )
115116 {
116- targetY = - gamepad . RightThumbY * SensitivityY ; // Y轴反转
117+ targetY = ( float ) ( - reading . RightThumbstickY * MaxMoveSpeedY ) ; // Y轴反转
117118 }
118119
119120 _accumulatedX += targetX ;
@@ -130,8 +131,8 @@ private void Timer_Tick(object? sender, EventArgs e)
130131 }
131132
132133 // 滚轮 (LT/RT) 与 中键 (LT+RT)
133- bool ltPressed = gamepad . LeftTrigger > 128 ;
134- bool rtPressed = gamepad . RightTrigger > 128 ;
134+ bool ltPressed = reading . LeftTrigger > 0.5 ; // 对应 > 128
135+ bool rtPressed = reading . RightTrigger > 0.5 ;
135136
136137 if ( ltPressed && rtPressed )
137138 {
@@ -151,14 +152,14 @@ private void Timer_Tick(object? sender, EventArgs e)
151152
152153 float scrollAmount = 0 ;
153154 // LT 向上滚动 (正值), RT 向下滚动 (负值)
154- if ( gamepad . LeftTrigger > 30 ) // 设定触发死区
155+ if ( reading . LeftTrigger > 0.117 ) // 对应 > 30 / 255
155156 {
156- scrollAmount += gamepad . LeftTrigger * ScrollSensitivity ;
157+ scrollAmount += ( float ) ( reading . LeftTrigger * MaxScrollSpeed ) ;
157158 }
158159
159- if ( gamepad . RightTrigger > 30 )
160+ if ( reading . RightTrigger > 0.117 )
160161 {
161- scrollAmount -= gamepad . RightTrigger * ScrollSensitivity ;
162+ scrollAmount -= ( float ) ( reading . RightTrigger * MaxScrollSpeed ) ;
162163 }
163164
164165 if ( Math . Abs ( scrollAmount ) > 0 )
@@ -178,8 +179,8 @@ private void Timer_Tick(object? sender, EventArgs e)
178179 }
179180
180181 // 鼠标按键 (L3/R3) 与 虚拟键盘 (L3+R3)
181- bool isLeftThumbPressed = ( gamepad . Buttons & GamepadButtonFlags . LeftThumb ) != 0 ;
182- bool isRightThumbPressed = ( gamepad . Buttons & GamepadButtonFlags . RightThumb ) != 0 ;
182+ bool isLeftThumbPressed = reading . Buttons . HasFlag ( GamepadButtons . LeftThumbstick ) ;
183+ bool isRightThumbPressed = reading . Buttons . HasFlag ( GamepadButtons . RightThumbstick ) ;
183184
184185 if ( isLeftThumbPressed && isRightThumbPressed )
185186 {
0 commit comments