Более менее стабильная версия

This commit is contained in:
2025-08-28 12:49:40 +03:00
parent 3cc802a9ea
commit 8c0276c562
16 changed files with 1228 additions and 1404 deletions
@@ -1,30 +1,117 @@
package com.ea.ironmonkey;
package com.ea.ironmonkey
import com.bda.controller.ControllerListener;
import com.bda.controller.KeyEvent;
import com.bda.controller.MotionEvent;
import com.bda.controller.StateEvent;
import android.content.Context
import android.hardware.input.InputManager
import android.util.Log
import android.view.InputDevice
import androidx.activity.ComponentActivity
import com.bda.controller.ControllerListener
import com.bda.controller.KeyEvent
import com.bda.controller.MotionEvent
import com.bda.controller.StateEvent
object MogaController : ControllerListener {
external fun nativeOnKeyEvent(keyEvent: KeyEvent?)
public class MogaController implements ControllerListener {
native void nativeOnKeyEvent(KeyEvent keyEvent);
external fun nativeOnMotionEvent(motionEvent: MotionEvent?)
native void nativeOnMotionEvent(MotionEvent motionEvent);
external fun nativeOnStateEvent(stateEvent: StateEvent?)
native void nativeOnStateEvent(StateEvent stateEvent);
@Override // com.bda.controller.ControllerListener
public void onKeyEvent(KeyEvent keyEvent) {
nativeOnKeyEvent(keyEvent);
override fun onKeyEvent(keyEvent: KeyEvent?) {
nativeOnKeyEvent(keyEvent)
}
@Override // com.bda.controller.ControllerListener
public void onMotionEvent(MotionEvent motionEvent) {
nativeOnMotionEvent(motionEvent);
override fun onMotionEvent(motionEvent: MotionEvent?) {
nativeOnMotionEvent(motionEvent)
}
@Override // com.bda.controller.ControllerListener
public void onStateEvent(StateEvent stateEvent) {
nativeOnStateEvent(stateEvent);
override fun onStateEvent(stateEvent: StateEvent?) {
nativeOnStateEvent(stateEvent)
}
fun doForAllControllers(
action: (Int) -> Unit
){
val deviceIds = InputDevice.getDeviceIds()
deviceIds.forEach {
if(deviceIsController(it)) action(it)
}
}
infix fun Int.isFlag(flag: Int) = this and flag == flag
fun deviceIsController(deviceId: Int): Boolean {
val inputDevice = InputDevice.getDevice(deviceId) ?: return false
if(inputDevice.id == -1) return false
return (inputDevice.sources isFlag InputDevice.SOURCE_GAMEPAD) &&
(inputDevice.sources isFlag InputDevice.SOURCE_JOYSTICK)
}
fun registerControllerHandlers(
context: Context,
onInputDeviceAdded: (Int) -> Unit,
onInputDeviceRemoved: (Int) -> Unit,
onInputDeviceChanged: (Int) -> Unit = {},
){
val inputManager = context.getSystemService(ComponentActivity.INPUT_SERVICE) as InputManager
inputManager.registerInputDeviceListener(object : InputManager.InputDeviceListener {
var lastControllerId: Int = 0;
override fun onInputDeviceAdded(deviceId: Int) {
if(deviceIsController(deviceId)) {
lastControllerId = deviceId
onInputDeviceAdded(deviceId)
}
}
override fun onInputDeviceRemoved(deviceId: Int) {
if(deviceIsController(deviceId) || lastControllerId == deviceId) onInputDeviceRemoved(deviceId)
}
override fun onInputDeviceChanged(deviceId: Int) {
if(deviceIsController(deviceId)) onInputDeviceChanged(deviceId)
}
}, null)
}
fun createDisconnectEvent(deviceId: Int) = StateEvent(
System.currentTimeMillis(),
deviceId,
StateEvent.STATE_UNKNOWN,
StateEvent.ACTION_DISCONNECTED
)
fun createConnectEvent(deviceId: Int) = StateEvent(
System.currentTimeMillis(),
deviceId,
StateEvent.STATE_CONNECTION,
StateEvent.ACTION_CONNECTED
)
fun androidKeyEventToBda(keyEvent: android.view.KeyEvent) = KeyEvent(
keyEvent.eventTime,
keyEvent.deviceId,
keyEvent.keyCode,
keyEvent.action
)
fun androidMotionEventToBda(motionEvent: android.view.MotionEvent) = MotionEvent(
motionEvent.eventTime,
motionEvent.deviceId,
motionEvent.x,
motionEvent.y,
motionEvent.rawX,
motionEvent.rawY,
motionEvent.xPrecision,
motionEvent.yPrecision
)
}