Skip to content

Commit

Permalink
Fix gamepads detection in certain scenarios.
Browse files Browse the repository at this point in the history
  • Loading branch information
Swordfish90 committed Oct 19, 2020
1 parent 64c61cb commit bc1eaba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.InputDevice
import android.view.InputEvent
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.Gravity
Expand Down Expand Up @@ -321,8 +320,12 @@ abstract class BaseGameActivity : ImmersiveActivity() {
)
}

val firstPlayerPressedKeys = keyEventsSubjects
.filter { getDevicePort(it) == 0 }
val firstPlayerPressedKeys = Observables.combineLatest(
gamePadManager.getGamePadsPortMapperObservable(),
keyEventsSubjects
)
.filter { (ports, key) -> ports(key.device) == 0 }
.map { (_, key) -> key }
.scan(mutableSetOf<Int>()) { keys, event ->
if (event.action == KeyEvent.ACTION_DOWN) {
keys.add(event.keyCode)
Expand All @@ -342,16 +345,19 @@ abstract class BaseGameActivity : ImmersiveActivity() {
}

private fun setupGamePadMotions() {
val events = Observables.combineLatest(getGamePadPortMappingsObservable(), motionEventsSubjects)
val events = Observables.combineLatest(
gamePadManager.getGamePadsPortMapperObservable(),
motionEventsSubjects
)
.share()

events
.autoDispose(scope())
.subscribeBy { (ports, event) -> sendStickMotions(event, ports[event.deviceId] ?: 0) }
.subscribeBy { (ports, event) -> sendStickMotions(event, ports(event.device)) }

events
.flatMap { (ports, event) ->
val port = ports[event.deviceId] ?: 0
val port = ports(event.device)
val axes = GamePadManager.TRIGGER_MOTIONS_TO_KEYS.entries
Observable.fromIterable(axes).map { (axis, button) ->
val action = if (event.getAxisValue(axis) > 0.5) {
Expand All @@ -373,12 +379,12 @@ abstract class BaseGameActivity : ImmersiveActivity() {

private fun setupGamePadKeys() {
val bindKeys = Observables.combineLatest(
getGamePadPortMappingsObservable(),
getGamePadBindingsObservable(),
gamePadManager.getGamePadsPortMapperObservable(),
gamePadManager.getGamePadsBindingsObservable(),
keyEventsSubjects
)
.map { (ports, bindings, event) ->
val port = ports[event.deviceId] ?: 0
val port = ports(event.device)
val bindKeyCode = bindings[event.device]?.get(event.keyCode) ?: event.keyCode
Triple(event.action, port, bindKeyCode)
}
Expand All @@ -398,23 +404,6 @@ abstract class BaseGameActivity : ImmersiveActivity() {
.subscribeBy { displayOptionsDialog() }
}

private fun getGamePadBindingsObservable(): Observable<Map<InputDevice, Map<Int, Int>>> {
return gamePadManager.getGamePadsObservable()
.flatMapSingle { inputDevices ->
Observable.fromIterable(inputDevices).flatMapSingle { inputDevice ->
gamePadManager.getBindings(inputDevice).map { inputDevice to it }
}.toList()
}
.map { it.toMap() }
}

private fun getGamePadPortMappingsObservable(): Observable<Map<Int, Int>> {
return gamePadManager.getGamePadsObservable().map {
it.mapIndexed { index, inputDevice -> inputDevice.id to index }
.toMap()
}
}

private fun sendStickMotions(event: MotionEvent, port: Int) {
if (port < 0) return
when (event.source) {
Expand Down Expand Up @@ -508,9 +497,6 @@ abstract class BaseGameActivity : ImmersiveActivity() {
}
}

private fun getDevicePort(inputEvent: InputEvent) =
(inputEvent.device?.controllerNumber ?: 0) - 1

override fun onBackPressed() {
if (loading) return
autoSaveAndFinish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,26 @@ class GamePadManager(context: Context) {
return PreferenceManager.getDefaultSharedPreferences(context)
}

fun getBindings(inputDevice: InputDevice): Single<Map<Int, Int>> {
fun getGamePadsBindingsObservable(): Observable<Map<InputDevice, Map<Int, Int>>> {
return getGamePadsObservable()
.flatMapSingle { inputDevices ->
Observable.fromIterable(inputDevices).flatMapSingle { inputDevice ->
getBindings(inputDevice).map { inputDevice to it }
}.toList()
}
.map { it.toMap() }
}

fun getGamePadsPortMapperObservable(): Observable<(InputDevice)->Int> {
return getGamePadsObservable().map { gamePads ->
val portMappings = gamePads
.mapIndexed { index, inputDevice -> inputDevice.controllerNumber to index }
.toMap()
return@map { inputDevice: InputDevice -> portMappings[inputDevice.controllerNumber] ?: 0 }
}
}

private fun getBindings(inputDevice: InputDevice): Single<Map<Int, Int>> {
return Observable.fromIterable(INPUT_KEYS)
.flatMapSingle { keyCode ->
retrieveMappingFromPreferences(
Expand Down Expand Up @@ -92,6 +111,7 @@ class GamePadManager(context: Context) {
InputDevice.getDeviceIds()
.map { InputDevice.getDevice(it) }
.filter { isGamePad(it) }
.filter { it.controllerNumber > 0 }
.sortedBy { it.controllerNumber }
}.getOrNull() ?: listOf()
}
Expand Down

0 comments on commit bc1eaba

Please sign in to comment.