fix: keep held Steam Touch controls pressed across action-set and layer changes (#1143)

This commit is contained in:
Resul Övür authored and GitHub committed 2026-10-19 23:50:29 -04:00
1 parent 06e819f864
commit a42ad82bcb
3 files changed
+38 -3

No files matched your search

@@ -333,6 +333,11 @@ object SteamTouchConfig {
}.sortedBy { it.type }
}
fun remapHeld(held: Map<Int, Int?>, next: List<Element>): Map<Int, Element> {
val byType = next.associateBy { it.type }
return held.mapNotNull { (id, type) -> byType[type]?.let { id to it } }.toMap()
}
fun layoutColor(config: Config, actionSet: Int): FloatArray? = config.layouts?.forActionSet(layoutIdOf(actionSet))?.color
@@ -154,7 +154,7 @@ class SteamTouchControls(
actionSet = state.actionSet
layers = state.layers
appId = app
rebuild()
rebuild(keepHeld = true)
}
}
@@ -175,7 +175,7 @@ class SteamTouchControls(
refreshState()
}
private fun rebuild() {
private fun rebuild(keepHeld: Boolean = false) {
elements = SteamTouchConfig.elementsFor(config, actionSet, layers)
val nextOptions = pendingOptions ?: config.layouts?.options ?: SteamTouchConfig.Options()
if (nextOptions != options) { options = nextOptions; onOptions(options) }
@@ -183,7 +183,7 @@ class SteamTouchControls(
rebuildVisuals()
if (!editing) {
Log.i(TAG, "steam touch: app $appId, action set $actionSet, layers $layers: ${elements.size} controls")
releaseAll()
if (keepHeld) keepHeldFingers() else releaseAll()
}
invalidate()
}
@@ -590,6 +590,13 @@ class SteamTouchControls(
invalidate()
}
private fun keepHeldFingers() {
val moved = SteamTouchConfig.remapHeld(fingers.mapValues { it.value.element?.type }, live())
fingers.keys.retainAll(moved.keys)
fingers.replaceAll { id, f -> Finger(moved.getValue(id), f.x, f.y, f.toolbar) }
publish()
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
@@ -65,4 +65,27 @@ class SteamTouchConfigTest {
val config = SteamTouchConfig.Config(null, null, null, emptyMap(), kv)
assertEquals(listOf(1 to "Desktop", 2 to "Gamepad"), SteamTouchConfig.actionSets(config))
}
@Test
fun remapHeldKeepsSurvivingControlsAndDropsRemovedOrNullOnes() {
val layouts = SteamTouchConfig.decodeLayouts(SteamTouchConfig.hexToBytes(saved))
val baseConfig = SteamTouchConfig.Config(null, null, layouts, mapOf(0 to setOf(SteamTouchConfig.A, SteamTouchConfig.JOYSTICK_LEFT)))
val layerConfig = SteamTouchConfig.Config(null, null, layouts, mapOf(0 to setOf(SteamTouchConfig.A), 1 to setOf(SteamTouchConfig.B)))
val after = SteamTouchConfig.elementsFor(layerConfig, 0, listOf(2))
val remapped = SteamTouchConfig.remapHeld(
mapOf(
7 to SteamTouchConfig.A,
8 to SteamTouchConfig.JOYSTICK_LEFT,
9 to null,
10 to SteamTouchConfig.B,
),
after,
)
assertEquals(setOf(7, 10), remapped.keys)
assertEquals(after.first { it.type == SteamTouchConfig.A }, remapped[7])
assertEquals(after.first { it.type == SteamTouchConfig.B }, remapped[10])
assertFalse(baseConfig.availableFor(0, emptyList()).isEmpty())
}
}