done context menu, and fix controllers in wsa
This commit is contained in:
@@ -5,38 +5,25 @@ import android.app.AlertDialog
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.graphics.BitmapFactory
|
||||
import android.hardware.SensorManager
|
||||
import android.hardware.input.InputManager
|
||||
import android.media.AudioManager
|
||||
import android.opengl.GLES20
|
||||
import android.opengl.GLUtils
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.os.PowerManager
|
||||
import android.os.PowerManager.WakeLock
|
||||
import android.util.DisplayMetrics
|
||||
import android.util.Log
|
||||
import android.view.InputDevice
|
||||
import android.view.KeyEvent
|
||||
import android.view.MotionEvent
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import com.bda.controller.StateEvent
|
||||
import com.ea.EAIO.EAIO
|
||||
import com.ea.EAMIO.StorageDirectory
|
||||
import com.ea.easp.EASPHandler
|
||||
import com.ea.games.nfs13_na.BuildConfig
|
||||
import com.ea.games.nfs13_na.R
|
||||
import com.ea.nimble.ApplicationLifecycle
|
||||
import com.megboyzz.actionForAllControllers
|
||||
import com.megboyzz.androidKeyEventToBda
|
||||
import com.megboyzz.androidMotionEventToBda
|
||||
import com.megboyzz.createConnectEvent
|
||||
import com.megboyzz.createDisconnectEvent
|
||||
import com.megboyzz.registerControllerHandlers
|
||||
import java.io.BufferedReader
|
||||
import java.io.File
|
||||
import java.io.FileReader
|
||||
@@ -299,23 +286,20 @@ class GameActivity : ComponentActivity(), DrawFrameListener {
|
||||
ApplicationLifecycle.onActivityResult(requestCode, resultCode, data, this)
|
||||
}
|
||||
|
||||
override fun onBackPressed() {
|
||||
//ApplicationLifecycle.onBackPressed()
|
||||
}
|
||||
|
||||
public override fun onCreate(bundle: Bundle?) {
|
||||
|
||||
super.onCreate(bundle)
|
||||
|
||||
registerControllerHandlers(
|
||||
MogaController.registerControllerHandlers(
|
||||
context = baseContext,
|
||||
onInputDeviceAdded = {
|
||||
Log.i("Controller", "controller is connected!")
|
||||
val event = createConnectEvent(it)
|
||||
val event = MogaController.createConnectEvent(it)
|
||||
MogaController.nativeOnStateEvent(event)
|
||||
},
|
||||
onInputDeviceRemoved = {
|
||||
Log.i("Controller", "controller is disconnected!")
|
||||
val event = createDisconnectEvent(it)
|
||||
val event = MogaController.createDisconnectEvent(it)
|
||||
MogaController.nativeOnStateEvent(event)
|
||||
},
|
||||
onInputDeviceChanged = {
|
||||
@@ -393,7 +377,8 @@ class GameActivity : ComponentActivity(), DrawFrameListener {
|
||||
override fun dispatchGenericMotionEvent(ev: MotionEvent?): Boolean {
|
||||
Log.i("GameActivity", ev.toString())
|
||||
|
||||
MogaController.onMotionEvent(androidMotionEventToBda(ev!!))
|
||||
if (MogaController.deviceIsController(ev?.deviceId!!))
|
||||
MogaController.onMotionEvent(MogaController.androidMotionEventToBda(ev))
|
||||
|
||||
return super.dispatchGenericMotionEvent(ev)
|
||||
}
|
||||
@@ -401,7 +386,8 @@ class GameActivity : ComponentActivity(), DrawFrameListener {
|
||||
@SuppressLint("RestrictedApi")
|
||||
override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
|
||||
|
||||
MogaController.onKeyEvent(androidKeyEventToBda(event!!))
|
||||
if (MogaController.deviceIsController(event?.deviceId!!))
|
||||
MogaController.onKeyEvent(MogaController.androidKeyEventToBda(event))
|
||||
|
||||
return super.dispatchKeyEvent(event)
|
||||
}
|
||||
@@ -409,8 +395,6 @@ class GameActivity : ComponentActivity(), DrawFrameListener {
|
||||
|
||||
|
||||
override fun onDrawFrame(gl10: GL10) {
|
||||
//InputDevice.getDeviceIds()
|
||||
//InputDevice.getDevice(0).sources
|
||||
Log.d(this.localClassName, "onDrawFrame state=$state")
|
||||
var isStarted = false
|
||||
when (state) {
|
||||
@@ -522,8 +506,8 @@ class GameActivity : ComponentActivity(), DrawFrameListener {
|
||||
gameGLSurfaceView.onResume()
|
||||
ApplicationLifecycle.onActivityResume(this)
|
||||
nativeOnResume()
|
||||
actionForAllControllers {
|
||||
val event = createConnectEvent(it)
|
||||
MogaController.doForAllControllers {
|
||||
val event = MogaController.createConnectEvent(it)
|
||||
MogaController.nativeOnStateEvent(event)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package com.ea.ironmonkey
|
||||
|
||||
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
|
||||
@@ -19,4 +24,94 @@ object MogaController : ControllerListener {
|
||||
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
|
||||
Log.i("Controller", inputDevice.toString())
|
||||
|
||||
val b = (inputDevice.sources isFlag InputDevice.SOURCE_GAMEPAD) &&
|
||||
(inputDevice.sources isFlag InputDevice.SOURCE_JOYSTICK)
|
||||
|
||||
Log.i("Controller", b.toString())
|
||||
|
||||
if(inputDevice.id == -1) return false
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
package com.megboyzz
|
||||
|
||||
import android.content.Context
|
||||
import android.hardware.input.InputManager
|
||||
import android.view.InputDevice
|
||||
import androidx.activity.ComponentActivity
|
||||
import com.bda.controller.KeyEvent
|
||||
import com.bda.controller.MotionEvent
|
||||
import android.view.KeyEvent as androidKeyEvent
|
||||
import android.view.MotionEvent as androidMotionEvent
|
||||
import com.bda.controller.StateEvent
|
||||
|
||||
|
||||
fun actionForAllControllers(
|
||||
action: (Int) -> Unit
|
||||
){
|
||||
val deviceIds = InputDevice.getDeviceIds()
|
||||
deviceIds.forEach {
|
||||
if(deviceIsController(it)) action(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun deviceIsController(deviceId: Int): Boolean {
|
||||
val inputDevice = InputDevice.getDevice(deviceId) ?: return false
|
||||
|
||||
return (inputDevice.sources and InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD ||
|
||||
(inputDevice.sources and InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK
|
||||
}
|
||||
|
||||
fun Context.registerControllerHandlers(
|
||||
onInputDeviceAdded: (Int) -> Unit,
|
||||
onInputDeviceRemoved: (Int) -> Unit,
|
||||
onInputDeviceChanged: (Int) -> Unit = {},
|
||||
){
|
||||
|
||||
val inputManager = 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: androidKeyEvent) = KeyEvent(
|
||||
keyEvent.eventTime,
|
||||
keyEvent.deviceId,
|
||||
keyEvent.keyCode,
|
||||
keyEvent.action
|
||||
)
|
||||
|
||||
fun androidMotionEventToBda(motionEvent: androidMotionEvent) = MotionEvent(
|
||||
motionEvent.eventTime,
|
||||
motionEvent.deviceId,
|
||||
motionEvent.x,
|
||||
motionEvent.y,
|
||||
motionEvent.rawX,
|
||||
motionEvent.rawY,
|
||||
motionEvent.xPrecision,
|
||||
motionEvent.yPrecision
|
||||
)
|
||||
Reference in New Issue
Block a user