mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
fix(input): saved D-pad keeps direction overrides, held directions wait for neutral and Reset regenerates an emptied gamepad
This commit is contained in:
1 parent
952f363b45
commit
bf75dfb9c0
7 files changed
+39
-5
No files matched your search
@@ -1715,6 +1715,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde
|
||||
SessionPrefs.setOscMode(this@SessionActivity, v)
|
||||
controlsHiddenByTap = false
|
||||
readPrefs()
|
||||
applyUnfoldedControls()
|
||||
updateOnScreenControls()
|
||||
},
|
||||
layoutGame = controlsGameName(),
|
||||
|
||||
@@ -317,4 +317,4 @@ internal fun InputAction.validFor(element: InputSurfaceElement): Boolean = when
|
||||
|
||||
/** A saved element, or, in a gamepad that keeps no kit positions, the kit control that [id] names. */
|
||||
internal fun SavedInputLayout.element(id: String): InputSurfaceElement? =
|
||||
elements.firstOrNull { it.id == id } ?: KitLayout.element(id)?.takeIf { OnScreenLayout.generated(this) }
|
||||
elements.firstOrNull { it.id == id } ?: KitLayout.element(id)?.takeIf { OnScreenLayout.generated(this) || KitLayout.bindsTo(id, elements) }
|
||||
@@ -60,8 +60,8 @@ class InputSurfaceSource(private val pad: PadBridge, private val role: InputRole
|
||||
if (latched) 1f else 0f
|
||||
} else value
|
||||
if (arms.isNotEmpty() && route?.action is InputAction.Native) {
|
||||
route.update(x, y, touching)
|
||||
val values = listOf(y < -.5f, x > .5f, y > .5f, x < -.5f).map { if (it) 1f else 0f }
|
||||
val ready = route.update(x, y, touching) != null
|
||||
val values = listOf(y < -.5f, x > .5f, y > .5f, x < -.5f).map { if (it && ready) 1f else 0f }
|
||||
val toPad = arms.mapIndexed { i, arm -> arm.update(values[i])?.takeIf { it.isPad }?.let { it to values[i] } }.filterNotNull()
|
||||
if (toPad.isNotEmpty() || nativePad) writePad(this, null) { state -> state.clear(); for ((t, v) in toPad) state.add(t, v) }
|
||||
nativePad = toPad.isNotEmpty()
|
||||
|
||||
@@ -27,6 +27,13 @@ object KitLayout {
|
||||
else -> null
|
||||
}
|
||||
|
||||
/** The direction buttons the kit's D-pad stands in for; their saved overrides stay with the D-pad. */
|
||||
val dpadDirectionIds = listOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT).map { "legacy:${it.id}" }
|
||||
|
||||
/** Whether a binding for [id] belongs to a layout made of [elements]. */
|
||||
fun bindsTo(id: String, elements: List<InputSurfaceElement>) =
|
||||
elements.any { it.id == id } || id in dpadDirectionIds && elements.any { it.id == DPAD_ID }
|
||||
|
||||
/** Every kit control that has a click, across all virtual controllers. */
|
||||
val clickIds = listOf(InputControl.LS, InputControl.RS, InputControl.LEFT_PAD, InputControl.RIGHT_PAD).map { "legacy:${it.id}" }
|
||||
private const val REF_W = 1600f
|
||||
|
||||
@@ -163,10 +163,10 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () ->
|
||||
InputLibraries.update(context) { latest ->
|
||||
val l = latest.layouts.first { it.id == savedLayout.id }
|
||||
val p = latest.preset(savedBindings.id)
|
||||
val generated = draft.kitAtDefaults && OnScreenLayout.generated(l.copy(elements = emptyList()))
|
||||
val generated = draft.kitAtDefaults && OnScreenLayout.generated(l.copy(elements = emptyList(), kitless = false))
|
||||
val extras = draft.elements.filterNot { KitLayout.isKit(it.id) }
|
||||
latest.withLayout(if (generated) l.copy(elements = extras, aspect = if (extras.isEmpty()) 0f else draft.aspect, scale = draft.layoutScale, kitless = false)
|
||||
else l.copy(elements = draft.elements, aspect = draft.aspect, scale = draft.layoutScale, kitless = draft.elements.none { KitLayout.isKit(it.id) })).withPreset(p.copy(elements = draft.actions.filterKeys { id -> draft.elements.any { it.id == id } },
|
||||
else l.copy(elements = draft.elements, aspect = draft.aspect, scale = draft.layoutScale, kitless = draft.elements.none { KitLayout.isKit(it.id) })).withPreset(p.copy(elements = draft.actions.filterKeys { id -> KitLayout.bindsTo(id, draft.elements) },
|
||||
clicks = draft.clicks.filterKeys { id -> draft.elements.any { it.id == id && it.hasClick } }))
|
||||
}
|
||||
}.onSuccess { onChanged(); onDismiss() }.onFailure { failure = it.message ?: context.getString(R.string.input_save_failed) }
|
||||
|
||||
@@ -213,4 +213,18 @@ class InputSurfaceSourceTest {
|
||||
pad.cancel()
|
||||
assertTrue(events.none { it.startsWith("KEYBOARD") })
|
||||
}
|
||||
|
||||
@Test fun aDirectionHeldAcrossAProfileChangeWaitsForNeutral() {
|
||||
val dpad = InputSurfaceElement(InputControl.LS, .1f, .1f, .3f, .3f, KitLayout.DPAD_ID, InputElementKind.DPAD)
|
||||
bridge.setInputProfile(InputProfile("first", elements = mapOf(InputRole.SECONDARY to mapOf(KitLayout.DPAD_ID to InputAction.Directions(17, 32, 31, 30)))))
|
||||
val pad = source(dpad)
|
||||
pad.update(0f, -1f)
|
||||
bridge.setInputProfile(InputProfile("second", elements = mapOf(InputRole.SECONDARY to mapOf("legacy:up" to InputAction.Keys(listOf(103))))))
|
||||
events.clear()
|
||||
pad.update(0f, -1f)
|
||||
assertTrue(events.none { it == "key:103:true" })
|
||||
pad.update(0f, 0f)
|
||||
pad.update(0f, -1f)
|
||||
assertEquals("key:103:true", events.last())
|
||||
}
|
||||
}
|
||||
@@ -166,4 +166,16 @@ class OnScreenControlPrefsTest {
|
||||
val dimmed = withKey.withLayout(overlay.copy(style = overlay.style.copy(opacity = 50)))
|
||||
assertEquals(InputAction.Keys(listOf(30)), dimmed.preset(bindings.id).elements["legacy:a"])
|
||||
}
|
||||
|
||||
@Test fun aSavedKitDpadKeepsItsDirectionOverrides() {
|
||||
val library = InputLibraries.read(context)
|
||||
val overlay = library.layouts.single { it.id == InputLibraries.OVERLAY }
|
||||
val saved = overlay.copy(elements = KitLayout.elements(context, PadKit.XBOX, 1920, 1080), aspect = 16f / 9)
|
||||
val bindings = library.preset(library.activePart(false, InputRole.PRIMARY).bindings)
|
||||
val withUp = library.withLayout(saved).withPreset(bindings.copy(elements = bindings.elements + ("legacy:up" to InputAction.Keys(listOf(103)))))
|
||||
val edited = withUp.withLayout(saved.copy(style = saved.style.copy(opacity = 60))).validate()
|
||||
assertEquals(InputAction.Keys(listOf(103)), edited.preset(bindings.id).elements["legacy:up"])
|
||||
val noDpad = edited.withLayout(saved.copy(elements = saved.elements.filterNot { it.id == KitLayout.DPAD_ID }))
|
||||
assertNull(noDpad.preset(bindings.id).elements["legacy:up"])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user