1
1
package org.flame_engine.gamepads_android
2
2
3
- import android.util.Log
4
- import android.view.InputDevice
5
3
import android.view.KeyEvent
6
4
import android.view.MotionEvent
7
5
8
6
import io.flutter.plugin.common.MethodChannel
9
7
import kotlin.math.abs
10
8
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
+ )
12
13
13
14
class EventListener {
14
15
companion object {
15
- private const val TAG = " EventListener"
16
- private const val axisEpisilon = 0.001
16
+ private const val EPSILON = 0.001
17
17
}
18
18
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 (
20
21
SupportedAxis (MotionEvent .AXIS_X ),
21
22
SupportedAxis (MotionEvent .AXIS_Y , invert = true ),
22
23
SupportedAxis (MotionEvent .AXIS_Z ),
@@ -25,46 +26,52 @@ class EventListener {
25
26
SupportedAxis (MotionEvent .AXIS_HAT_Y , invert = true ),
26
27
SupportedAxis (MotionEvent .AXIS_LTRIGGER ),
27
28
SupportedAxis (MotionEvent .AXIS_RTRIGGER ),
29
+ SupportedAxis (MotionEvent .AXIS_BRAKE ),
30
+ SupportedAxis (MotionEvent .AXIS_GAS ),
28
31
)
29
32
30
33
fun onKeyEvent (keyEvent : KeyEvent , channel : MethodChannel ): Boolean {
31
34
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 ,
34
37
" 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()
37
40
)
38
41
channel.invokeMethod(" onGamepadEvent" , arguments)
39
42
return true
40
43
}
41
44
42
45
fun onMotionEvent (motionEvent : MotionEvent , channel : MethodChannel ): Boolean {
43
46
supportedAxes.forEach {
44
- reportAxis(motionEvent, channel, it.axisId, it.invert )
47
+ reportAxis(motionEvent, channel, it)
45
48
}
46
49
return true
47
50
}
48
51
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
52
59
53
60
// No-op if threshold is not met
54
- val lastValue = lastAxisValue[axis]
61
+ val lastValue = lastAxisValue[axis.axisId ]
55
62
if (lastValue is Float ) {
56
- if (abs(value - lastValue) < axisEpisilon ) {
57
- return true ;
63
+ if (abs(value - lastValue) < EPSILON ) {
64
+ return true
58
65
}
59
66
}
60
67
// Update last value
61
- lastAxisValue[axis] = value
68
+ lastAxisValue[axis.axisId ] = value
62
69
63
70
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 ,
66
73
" type" to " analog" ,
67
- " key" to MotionEvent .axisToString(axis),
74
+ " key" to MotionEvent .axisToString(axis.axisId ),
68
75
" value" to value,
69
76
)
70
77
channel.invokeMethod(" onGamepadEvent" , arguments)
0 commit comments