fix(input): saved D-pad keeps direction overrides, held directions wait for neutral and Reset regenerates an emptied gamepad

This commit is contained in:
Kurt Himebauch committed 2026-11-18 21:00:31 -05:00
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) }