-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Lukas Klingsbo <[email protected]> Co-authored-by: Luan Nico <[email protected]>
- Loading branch information
1 parent
e8cb977
commit 6996109
Showing
18 changed files
with
458 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 0.1.1 | ||
|
||
- Initial Android version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Blue Fire | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include: package:flame_lint/analysis_options.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
/captures | ||
.cxx |
19 changes: 19 additions & 0 deletions
19
...epads_android/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.flutter.plugins; | ||
|
||
import androidx.annotation.Keep; | ||
import androidx.annotation.NonNull; | ||
import io.flutter.Log; | ||
|
||
import io.flutter.embedding.engine.FlutterEngine; | ||
|
||
/** | ||
* Generated file. Do not edit. | ||
* This file is generated by the Flutter tool based on the | ||
* plugins that support the Android platform. | ||
*/ | ||
@Keep | ||
public final class GeneratedPluginRegistrant { | ||
private static final String TAG = "GeneratedPluginRegistrant"; | ||
public static void registerWith(@NonNull FlutterEngine flutterEngine) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
group 'org.flame_engine.gamepads_android' | ||
version '1.0-SNAPSHOT' | ||
|
||
buildscript { | ||
ext.kotlin_version = '1.7.10' | ||
repositories { | ||
google() | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:7.3.0' | ||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | ||
} | ||
} | ||
|
||
allprojects { | ||
repositories { | ||
google() | ||
mavenCentral() | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
|
||
android { | ||
if (project.android.hasProperty("namespace")) { | ||
namespace 'org.flame_engine.gamepads_android' | ||
} | ||
|
||
compileSdk 34 | ||
|
||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
|
||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
|
||
sourceSets { | ||
main.java.srcDirs += 'src/main/kotlin' | ||
test.java.srcDirs += 'src/test/kotlin' | ||
} | ||
|
||
defaultConfig { | ||
minSdkVersion 19 | ||
} | ||
|
||
dependencies { | ||
testImplementation 'org.jetbrains.kotlin:kotlin-test' | ||
testImplementation 'org.mockito:mockito-core:5.0.0' | ||
} | ||
|
||
testOptions { | ||
unitTests.all { | ||
useJUnitPlatform() | ||
|
||
testLogging { | ||
events "passed", "skipped", "failed", "standardOut", "standardError" | ||
outputs.upToDateWhen {false} | ||
showStandardStreams = true | ||
} | ||
} | ||
} | ||
|
||
buildFeatures { | ||
prefab true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rootProject.name = 'gamepads_android' |
3 changes: 3 additions & 0 deletions
3
packages/gamepads_android/android/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="org.flame_engine.gamepads_android"> | ||
</manifest> |
65 changes: 65 additions & 0 deletions
65
...epads_android/android/src/main/kotlin/org/flame_engine/gamepads_android/DeviceListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.flame_engine.gamepads_android | ||
|
||
import android.hardware.input.InputManager | ||
import android.util.Log | ||
import android.view.InputDevice | ||
|
||
class DeviceListener(val isGamepadsInputDevice: (device: InputDevice) -> Boolean): InputManager.InputDeviceListener { | ||
private val devicesLookup: MutableMap<Int, InputDevice> = mutableMapOf() | ||
private val TAG = "ConnectionListener" | ||
|
||
init { | ||
getGameControllerIds() | ||
} | ||
|
||
fun getDevices(): Map<Int, InputDevice> { | ||
return devicesLookup.toMap() | ||
} | ||
|
||
private fun getGameControllerIds() { | ||
val gameControllerDeviceIds = mutableListOf<Int>() | ||
val deviceIds = InputDevice.getDeviceIds() | ||
deviceIds.forEach { deviceId -> | ||
InputDevice.getDevice(deviceId).apply { | ||
if (this != null) { | ||
if (isGamepadsInputDevice(this)) { | ||
Log.i(TAG, "${this.name} passed input device test") | ||
devicesLookup[deviceId] = this | ||
} else { | ||
Log.e(TAG, "${this.name} failed input device test") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun containsKey(deviceId: Int): Boolean { | ||
return devicesLookup.containsKey(deviceId) | ||
} | ||
|
||
override fun onInputDeviceAdded(deviceId: Int) { | ||
val device: InputDevice? = InputDevice.getDevice(deviceId) | ||
if (device != null) { | ||
if (isGamepadsInputDevice(device)) { | ||
Log.i(TAG, "${device.name} passed input device test") | ||
devicesLookup[deviceId] = device | ||
} else { | ||
Log.e(TAG, "${device.name} failed input device test") | ||
} | ||
} | ||
} | ||
|
||
override fun onInputDeviceRemoved(deviceId: Int) { | ||
val device: InputDevice? = InputDevice.getDevice(deviceId) | ||
devicesLookup.remove(deviceId) | ||
} | ||
|
||
override fun onInputDeviceChanged(deviceId: Int) { | ||
val device: InputDevice? = InputDevice.getDevice(deviceId) | ||
if (device != null && isGamepadsInputDevice(device)) { | ||
devicesLookup[deviceId] = device | ||
} else { | ||
devicesLookup.remove(deviceId) | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...mepads_android/android/src/main/kotlin/org/flame_engine/gamepads_android/EventListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.flame_engine.gamepads_android | ||
|
||
import android.util.Log | ||
import android.view.InputDevice | ||
import android.view.KeyEvent | ||
import android.view.MotionEvent | ||
|
||
import io.flutter.plugin.common.MethodChannel | ||
import kotlin.math.abs | ||
|
||
data class SupportedAxis(val axisId: Int, val invert: Boolean = false) | ||
|
||
class EventListener { | ||
companion object { | ||
private const val TAG = "EventListener" | ||
private const val axisEpisilon = 0.001 | ||
} | ||
private val lastAxisValue = mutableMapOf<Int, Float>() | ||
private val supportedAxes = listOf<SupportedAxis>( | ||
SupportedAxis(MotionEvent.AXIS_X), | ||
SupportedAxis(MotionEvent.AXIS_Y, invert = true), | ||
SupportedAxis(MotionEvent.AXIS_Z), | ||
SupportedAxis(MotionEvent.AXIS_RZ, invert = true), | ||
SupportedAxis(MotionEvent.AXIS_HAT_X), | ||
SupportedAxis(MotionEvent.AXIS_HAT_Y, invert = true), | ||
SupportedAxis(MotionEvent.AXIS_LTRIGGER), | ||
SupportedAxis(MotionEvent.AXIS_RTRIGGER), | ||
) | ||
|
||
fun onKeyEvent(keyEvent: KeyEvent, channel: MethodChannel): Boolean { | ||
val arguments = mapOf( | ||
"gamepadId" to keyEvent.getDeviceId().toString(), | ||
"time" to keyEvent.getEventTime(), | ||
"type" to "button", | ||
"key" to KeyEvent.keyCodeToString(keyEvent.getKeyCode()), | ||
"value" to keyEvent.getAction().toDouble() | ||
) | ||
channel.invokeMethod("onGamepadEvent", arguments) | ||
return true | ||
} | ||
|
||
fun onMotionEvent(motionEvent: MotionEvent, channel: MethodChannel): Boolean { | ||
supportedAxes.forEach { | ||
reportAxis(motionEvent, channel, it.axisId, it.invert) | ||
} | ||
return true | ||
} | ||
|
||
private fun reportAxis(motionEvent: MotionEvent, channel: MethodChannel, axis: Int, invert: Boolean = false): Boolean { | ||
val multiplier = if (invert) -1 else 1 | ||
val value = motionEvent.getAxisValue(axis) * multiplier | ||
|
||
// No-op if threshold is not met | ||
val lastValue = lastAxisValue[axis] | ||
if (lastValue is Float) { | ||
if (abs(value - lastValue) < axisEpisilon) { | ||
return true; | ||
} | ||
} | ||
// Update last value | ||
lastAxisValue[axis] = value | ||
|
||
val arguments = mapOf( | ||
"gamepadId" to motionEvent.getDeviceId().toString(), | ||
"time" to motionEvent.getEventTime(), | ||
"type" to "analog", | ||
"key" to MotionEvent.axisToString(axis), | ||
"value" to value, | ||
) | ||
channel.invokeMethod("onGamepadEvent", arguments) | ||
return true | ||
} | ||
} |
Oops, something went wrong.