diff options
| author | 2023-04-28 11:12:19 -0600 | |
|---|---|---|
| committer | 2023-06-03 00:05:56 -0700 | |
| commit | c46a1da5dc6d9ec2c4994c5defcb81cce994e953 (patch) | |
| tree | bdbe1c4beabca9c7b13cc20a6b8f675b209f0578 /src/android | |
| parent | android: Move motion listener to emulation activity (diff) | |
| download | yuzu-c46a1da5dc6d9ec2c4994c5defcb81cce994e953.tar.gz yuzu-c46a1da5dc6d9ec2c4994c5defcb81cce994e953.tar.xz yuzu-c46a1da5dc6d9ec2c4994c5defcb81cce994e953.zip | |
android: Add deadzone to stick input
Diffstat (limited to 'src/android')
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt | 64 |
1 files changed, 45 insertions, 19 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt index a48b7e00a..4303939ef 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt | |||
| @@ -1,14 +1,14 @@ | |||
| 1 | package org.yuzu.yuzu_emu.utils | 1 | package org.yuzu.yuzu_emu.utils |
| 2 | 2 | ||
| 3 | import android.view.InputDevice | ||
| 4 | import android.view.KeyEvent | 3 | import android.view.KeyEvent |
| 5 | import android.view.MotionEvent | 4 | import android.view.MotionEvent |
| 6 | import org.yuzu.yuzu_emu.NativeLibrary | 5 | import org.yuzu.yuzu_emu.NativeLibrary |
| 6 | import kotlin.math.sqrt | ||
| 7 | 7 | ||
| 8 | class InputHandler { | 8 | class InputHandler { |
| 9 | fun initialize() { | 9 | fun initialize() { |
| 10 | // Connect first controller | 10 | // Connect first controller |
| 11 | NativeLibrary.onGamePadConnectEvent(getPlayerNumber(NativeLibrary.Player1Device)); | 11 | NativeLibrary.onGamePadConnectEvent(getPlayerNumber(NativeLibrary.Player1Device)) |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | fun dispatchKeyEvent(event: KeyEvent): Boolean { | 14 | fun dispatchKeyEvent(event: KeyEvent): Boolean { |
| @@ -42,7 +42,7 @@ class InputHandler { | |||
| 42 | val device = event.device | 42 | val device = event.device |
| 43 | // Check every axis input available on the controller | 43 | // Check every axis input available on the controller |
| 44 | for (range in device.motionRanges) { | 44 | for (range in device.motionRanges) { |
| 45 | val axis = range.axis; | 45 | val axis = range.axis |
| 46 | when (device.vendorId) { | 46 | when (device.vendorId) { |
| 47 | 0x045E -> setGenericAxisInput(event, axis) | 47 | 0x045E -> setGenericAxisInput(event, axis) |
| 48 | 0x054C -> setGenericAxisInput(event, axis) | 48 | 0x054C -> setGenericAxisInput(event, axis) |
| @@ -69,6 +69,32 @@ class InputHandler { | |||
| 69 | } | 69 | } |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | private fun setStickState(playerNumber: Int, index: Int, xAxis: Float, yAxis: Float) { | ||
| 73 | // Calculate vector size | ||
| 74 | val r2 = xAxis * xAxis + yAxis * yAxis | ||
| 75 | var r = sqrt(r2.toDouble()).toFloat() | ||
| 76 | |||
| 77 | // Adjust range of joystick | ||
| 78 | val deadzone = 0.15f | ||
| 79 | val deadzoneFactor = 1.0f / r * (r - deadzone) / (1.0f - deadzone) | ||
| 80 | var x = xAxis * deadzoneFactor | ||
| 81 | var y = yAxis * deadzoneFactor | ||
| 82 | r *= deadzoneFactor | ||
| 83 | |||
| 84 | // Normalize joystick | ||
| 85 | if (r > 1.0f) { | ||
| 86 | x /= r | ||
| 87 | y /= r | ||
| 88 | } | ||
| 89 | |||
| 90 | NativeLibrary.onGamePadJoystickEvent( | ||
| 91 | playerNumber, | ||
| 92 | index, | ||
| 93 | x, | ||
| 94 | -y | ||
| 95 | ) | ||
| 96 | } | ||
| 97 | |||
| 72 | private fun getAxisToButton(axis: Float): Int { | 98 | private fun getAxisToButton(axis: Float): Int { |
| 73 | return if (axis > 0.5f) NativeLibrary.ButtonState.PRESSED else NativeLibrary.ButtonState.RELEASED | 99 | return if (axis > 0.5f) NativeLibrary.ButtonState.PRESSED else NativeLibrary.ButtonState.RELEASED |
| 74 | } | 100 | } |
| @@ -197,25 +223,25 @@ class InputHandler { | |||
| 197 | 223 | ||
| 198 | when (axis) { | 224 | when (axis) { |
| 199 | MotionEvent.AXIS_X, MotionEvent.AXIS_Y -> | 225 | MotionEvent.AXIS_X, MotionEvent.AXIS_Y -> |
| 200 | NativeLibrary.onGamePadJoystickEvent( | 226 | setStickState( |
| 201 | playerNumber, | 227 | playerNumber, |
| 202 | NativeLibrary.StickType.STICK_L, | 228 | NativeLibrary.StickType.STICK_L, |
| 203 | event.getAxisValue(MotionEvent.AXIS_X), | 229 | event.getAxisValue(MotionEvent.AXIS_X), |
| 204 | -event.getAxisValue(MotionEvent.AXIS_Y) | 230 | event.getAxisValue(MotionEvent.AXIS_Y) |
| 205 | ) | 231 | ) |
| 206 | MotionEvent.AXIS_RX, MotionEvent.AXIS_RY -> | 232 | MotionEvent.AXIS_RX, MotionEvent.AXIS_RY -> |
| 207 | NativeLibrary.onGamePadJoystickEvent( | 233 | setStickState( |
| 208 | playerNumber, | 234 | playerNumber, |
| 209 | NativeLibrary.StickType.STICK_R, | 235 | NativeLibrary.StickType.STICK_R, |
| 210 | event.getAxisValue(MotionEvent.AXIS_RX), | 236 | event.getAxisValue(MotionEvent.AXIS_RX), |
| 211 | -event.getAxisValue(MotionEvent.AXIS_RY) | 237 | event.getAxisValue(MotionEvent.AXIS_RY) |
| 212 | ) | 238 | ) |
| 213 | MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ -> | 239 | MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ -> |
| 214 | NativeLibrary.onGamePadJoystickEvent( | 240 | setStickState( |
| 215 | playerNumber, | 241 | playerNumber, |
| 216 | NativeLibrary.StickType.STICK_R, | 242 | NativeLibrary.StickType.STICK_R, |
| 217 | event.getAxisValue(MotionEvent.AXIS_Z), | 243 | event.getAxisValue(MotionEvent.AXIS_Z), |
| 218 | -event.getAxisValue(MotionEvent.AXIS_RZ) | 244 | event.getAxisValue(MotionEvent.AXIS_RZ) |
| 219 | ) | 245 | ) |
| 220 | MotionEvent.AXIS_LTRIGGER -> | 246 | MotionEvent.AXIS_LTRIGGER -> |
| 221 | NativeLibrary.onGamePadButtonEvent( | 247 | NativeLibrary.onGamePadButtonEvent( |
| @@ -257,25 +283,25 @@ class InputHandler { | |||
| 257 | 283 | ||
| 258 | when (axis) { | 284 | when (axis) { |
| 259 | MotionEvent.AXIS_X, MotionEvent.AXIS_Y -> | 285 | MotionEvent.AXIS_X, MotionEvent.AXIS_Y -> |
| 260 | NativeLibrary.onGamePadJoystickEvent( | 286 | setStickState( |
| 261 | playerNumber, | 287 | playerNumber, |
| 262 | NativeLibrary.StickType.STICK_L, | 288 | NativeLibrary.StickType.STICK_L, |
| 263 | event.getAxisValue(MotionEvent.AXIS_X), | 289 | event.getAxisValue(MotionEvent.AXIS_X), |
| 264 | -event.getAxisValue(MotionEvent.AXIS_Y) | 290 | event.getAxisValue(MotionEvent.AXIS_Y) |
| 265 | ) | 291 | ) |
| 266 | MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ -> | 292 | MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ -> |
| 267 | NativeLibrary.onGamePadJoystickEvent( | 293 | setStickState( |
| 268 | playerNumber, | 294 | playerNumber, |
| 269 | NativeLibrary.StickType.STICK_R, | 295 | NativeLibrary.StickType.STICK_R, |
| 270 | event.getAxisValue(MotionEvent.AXIS_Z), | 296 | event.getAxisValue(MotionEvent.AXIS_Z), |
| 271 | -event.getAxisValue(MotionEvent.AXIS_RZ) | 297 | event.getAxisValue(MotionEvent.AXIS_RZ) |
| 272 | ) | 298 | ) |
| 273 | MotionEvent.AXIS_RX, MotionEvent.AXIS_RY -> | 299 | MotionEvent.AXIS_RX, MotionEvent.AXIS_RY -> |
| 274 | NativeLibrary.onGamePadJoystickEvent( | 300 | setStickState( |
| 275 | playerNumber, | 301 | playerNumber, |
| 276 | NativeLibrary.StickType.STICK_R, | 302 | NativeLibrary.StickType.STICK_R, |
| 277 | event.getAxisValue(MotionEvent.AXIS_RX), | 303 | event.getAxisValue(MotionEvent.AXIS_RX), |
| 278 | -event.getAxisValue(MotionEvent.AXIS_RY) | 304 | event.getAxisValue(MotionEvent.AXIS_RY) |
| 279 | ) | 305 | ) |
| 280 | } | 306 | } |
| 281 | } | 307 | } |
| @@ -285,18 +311,18 @@ class InputHandler { | |||
| 285 | 311 | ||
| 286 | when (axis) { | 312 | when (axis) { |
| 287 | MotionEvent.AXIS_X, MotionEvent.AXIS_Y -> | 313 | MotionEvent.AXIS_X, MotionEvent.AXIS_Y -> |
| 288 | NativeLibrary.onGamePadJoystickEvent( | 314 | setStickState( |
| 289 | playerNumber, | 315 | playerNumber, |
| 290 | NativeLibrary.StickType.STICK_L, | 316 | NativeLibrary.StickType.STICK_L, |
| 291 | event.getAxisValue(MotionEvent.AXIS_X), | 317 | event.getAxisValue(MotionEvent.AXIS_X), |
| 292 | -event.getAxisValue(MotionEvent.AXIS_Y) | 318 | event.getAxisValue(MotionEvent.AXIS_Y) |
| 293 | ) | 319 | ) |
| 294 | MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ -> | 320 | MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ -> |
| 295 | NativeLibrary.onGamePadJoystickEvent( | 321 | setStickState( |
| 296 | playerNumber, | 322 | playerNumber, |
| 297 | NativeLibrary.StickType.STICK_R, | 323 | NativeLibrary.StickType.STICK_R, |
| 298 | event.getAxisValue(MotionEvent.AXIS_Z), | 324 | event.getAxisValue(MotionEvent.AXIS_Z), |
| 299 | -event.getAxisValue(MotionEvent.AXIS_RZ) | 325 | event.getAxisValue(MotionEvent.AXIS_RZ) |
| 300 | ) | 326 | ) |
| 301 | MotionEvent.AXIS_BRAKE -> | 327 | MotionEvent.AXIS_BRAKE -> |
| 302 | NativeLibrary.onGamePadButtonEvent( | 328 | NativeLibrary.onGamePadButtonEvent( |