Skip to content

Commit adfb8d1

Browse files
markvideonMark Videon
and
Mark Videon
authored
feat: Add AXIS_BRAKE and AXIS_GAS as supported axes on Android. (#50)
Co-authored-by: Mark Videon <[email protected]>
1 parent b93a154 commit adfb8d1

File tree

1 file changed

+28
-21
lines changed
  • packages/gamepads_android/android/src/main/kotlin/org/flame_engine/gamepads_android

1 file changed

+28
-21
lines changed

packages/gamepads_android/android/src/main/kotlin/org/flame_engine/gamepads_android/EventListener.kt

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
package org.flame_engine.gamepads_android
22

3-
import android.util.Log
4-
import android.view.InputDevice
53
import android.view.KeyEvent
64
import android.view.MotionEvent
75

86
import io.flutter.plugin.common.MethodChannel
97
import kotlin.math.abs
108

11-
data class SupportedAxis(val axisId: Int, val invert: Boolean = false)
9+
data class SupportedAxis(
10+
val axisId: Int,
11+
val invert: Boolean = false,
12+
)
1213

1314
class EventListener {
1415
companion object {
15-
private const val TAG = "EventListener"
16-
private const val axisEpisilon = 0.001
16+
private const val EPSILON = 0.001
1717
}
1818
private val lastAxisValue = mutableMapOf<Int, Float>()
19-
private val supportedAxes = listOf<SupportedAxis>(
19+
// Reference: https://developer.android.com/reference/android/view/MotionEvent
20+
private val supportedAxes = listOf(
2021
SupportedAxis(MotionEvent.AXIS_X),
2122
SupportedAxis(MotionEvent.AXIS_Y, invert = true),
2223
SupportedAxis(MotionEvent.AXIS_Z),
@@ -25,46 +26,52 @@ class EventListener {
2526
SupportedAxis(MotionEvent.AXIS_HAT_Y, invert = true),
2627
SupportedAxis(MotionEvent.AXIS_LTRIGGER),
2728
SupportedAxis(MotionEvent.AXIS_RTRIGGER),
29+
SupportedAxis(MotionEvent.AXIS_BRAKE),
30+
SupportedAxis(MotionEvent.AXIS_GAS),
2831
)
2932

3033
fun onKeyEvent(keyEvent: KeyEvent, channel: MethodChannel): Boolean {
3134
val arguments = mapOf(
32-
"gamepadId" to keyEvent.getDeviceId().toString(),
33-
"time" to keyEvent.getEventTime(),
35+
"gamepadId" to keyEvent.deviceId.toString(),
36+
"time" to keyEvent.eventTime,
3437
"type" to "button",
35-
"key" to KeyEvent.keyCodeToString(keyEvent.getKeyCode()),
36-
"value" to keyEvent.getAction().toDouble()
38+
"key" to KeyEvent.keyCodeToString(keyEvent.keyCode),
39+
"value" to keyEvent.action.toDouble()
3740
)
3841
channel.invokeMethod("onGamepadEvent", arguments)
3942
return true
4043
}
4144

4245
fun onMotionEvent(motionEvent: MotionEvent, channel: MethodChannel): Boolean {
4346
supportedAxes.forEach {
44-
reportAxis(motionEvent, channel, it.axisId, it.invert)
47+
reportAxis(motionEvent, channel, it)
4548
}
4649
return true
4750
}
4851

49-
private fun reportAxis(motionEvent: MotionEvent, channel: MethodChannel, axis: Int, invert: Boolean = false): Boolean {
50-
val multiplier = if (invert) -1 else 1
51-
val value = motionEvent.getAxisValue(axis) * multiplier
52+
private fun reportAxis(
53+
motionEvent: MotionEvent,
54+
channel: MethodChannel,
55+
axis: SupportedAxis,
56+
): Boolean {
57+
val multiplier = if (axis.invert) -1 else 1
58+
val value = motionEvent.getAxisValue(axis.axisId) * multiplier
5259

5360
// No-op if threshold is not met
54-
val lastValue = lastAxisValue[axis]
61+
val lastValue = lastAxisValue[axis.axisId]
5562
if (lastValue is Float) {
56-
if (abs(value - lastValue) < axisEpisilon) {
57-
return true;
63+
if (abs(value - lastValue) < EPSILON) {
64+
return true
5865
}
5966
}
6067
// Update last value
61-
lastAxisValue[axis] = value
68+
lastAxisValue[axis.axisId] = value
6269

6370
val arguments = mapOf(
64-
"gamepadId" to motionEvent.getDeviceId().toString(),
65-
"time" to motionEvent.getEventTime(),
71+
"gamepadId" to motionEvent.deviceId.toString(),
72+
"time" to motionEvent.eventTime,
6673
"type" to "analog",
67-
"key" to MotionEvent.axisToString(axis),
74+
"key" to MotionEvent.axisToString(axis.axisId),
6875
"value" to value,
6976
)
7077
channel.invokeMethod("onGamepadEvent", arguments)

0 commit comments

Comments
 (0)