diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 64e5e3b4b2..e324b5c669 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -1422,6 +1422,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde com.droiddeck.launcher.ui.GestureFeedback(recognizedGesture) val inputChanged = { controllerSettings = ControllerPrefs.read(this@SessionActivity) + applyOnScreenOpacity() oscMode = SessionPrefs.oscMode(this@SessionActivity, SessionState.mode == SessionService.MODE_STEAM) padBridge?.setStickDeadZone(ControllerPrefs.deadZone(this@SessionActivity)) joyConVr?.refresh() diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt index c86acd1f44..04f7a37546 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt @@ -93,6 +93,7 @@ data class InputLibrary( } fun selectProfile(isDesktop: Boolean, id: String) = copy(selections = selections + (mode(isDesktop) to InputSelection(id, profileSelected = true))) fun selectPart(isDesktop: Boolean, role: InputRole, bindingsId: String): InputLibrary { + require(!isGameOwnedBindings(bindingsId)) { "Game-owned mappings cannot be used as shared mappings" } val previous = selection(isDesktop) return copy(selections = selections + (mode(isDesktop) to previous.copy(parts = previous.parts + (role to bindingsId)))) } @@ -101,6 +102,11 @@ data class InputLibrary( val binding = selection(isDesktop).parts[role]?.takeIf { games[game]?.profile == null }?.let(::preset) return if (binding != null) InputPart(binding.id, binding.layoutId) else profile(activeId(isDesktop, game)).parts.getValue(role) } + /** The part the game would use without its own copy, including the live shared mix when unpinned. */ + fun inheritedGamePart(isDesktop: Boolean, role: InputRole, game: String): InputPart { + val current = games[game] ?: return activePart(isDesktop, role) + return copy(games = games + (game to current.copy(parts = current.parts - role))).activePart(isDesktop, role, game) + } fun activeLayout(isDesktop: Boolean, role: InputRole, game: String? = null) = activePart(isDesktop, role, game).layout?.let { id -> layouts.first { it.id == id } } fun panelPart(isDesktop: Boolean, panel: String?, game: String? = null): InputPart = @@ -142,6 +148,7 @@ data class InputLibrary( parts.mapValues { it.value.clicks }.filterValues { it.isNotEmpty() }) } + fun isGameOwnedBindings(id: String) = games.values.any { game -> game.parts.values.any { it.bindings == id } } fun usersOfBindings(id: String) = profiles.filter { p -> p.parts.values.any { it.bindings == id } } fun selectedBindings(id: String) = selections.values.any { id in it.parts.values } || games.values.any { g -> g.parts.values.any { it.bindings == id } } fun usersOfLayout(id: String) = profiles.filter { p -> p.parts.values.any { it.layout == id } } @@ -198,7 +205,7 @@ data class InputLibrary( fun gameChanges(isDesktop: Boolean, game: String, only: InputRole? = null): Int { val current = games[game] ?: return 0 return current.parts.entries.filter { only == null || it.key == only }.sumOf { (role, part) -> - val base = copy(games = games + (game to current.copy(parts = current.parts - role))).activePart(isDesktop, role, game) + val base = inheritedGamePart(isDesktop, role, game) val mine = preset(part.bindings) val theirs = preset(base.bindings) val controls = (mine.controls.keys + theirs.controls.keys).count { (mine.controls[it] ?: InputAction.Native()) != (theirs.controls[it] ?: InputAction.Native()) } @@ -266,6 +273,7 @@ data class InputLibrary( } fun chooseBindings(profileId: String, role: InputRole, id: String): InputLibrary { + require(!isGameOwnedBindings(id)) { "Game-owned mappings cannot be used as shared mappings" } val profile = profile(profileId) require(preset(id).compatible(layout(profileId, role))) return withProfile(profile.copy(parts = profile.parts + (role to profile.parts.getValue(role).copy(bindings = id)))) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputButtonsEditor.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputButtonsEditor.kt index 8fb9a64bd6..8ede4f6b0d 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputButtonsEditor.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputButtonsEditor.kt @@ -30,6 +30,21 @@ import com.droiddeck.launcher.input.* /** A game edit reads its effective part until a real change needs a private copy. */ internal data class GameInputEdit(val desktop: Boolean, val game: String, val name: String, val role: InputRole) { fun part(library: InputLibrary) = library.activePart(desktop, role, game) + fun inheritedPart(library: InputLibrary) = library.inheritedGamePart(desktop, role, game) + + fun setPhysicalAction(library: InputLibrary, control: InputControl, action: InputAction): InputLibrary { + require(role == InputRole.PHYSICAL) + val current = library.preset(part(library).bindings) + if ((current.controls[control] ?: InputAction.Native()) == action) return library + val base = library.preset(inheritedPart(library).bindings) + val next = current.controls + (control to action) + if (current.keys == base.keys && (next.keys + base.controls.keys).all { + (next[it] ?: InputAction.Native()) == (base.controls[it] ?: InputAction.Native()) + }) return library.resetGame(game, role) + return apply(library) { editable, part -> + editable.withPreset(editable.preset(part.bindings).copy(controls = next)) + } + } fun apply(library: InputLibrary, change: (InputLibrary, InputPart) -> InputLibrary): InputLibrary { val editable = library.withGamePart(desktop, game, name, role) @@ -48,7 +63,7 @@ internal fun InputEditorWindow(target: InputEditorTarget, onDismiss: () -> Unit, Surface(Modifier.fillMaxSize().systemBarsPadding().padding(8.dp), color = MaterialTheme.colorScheme.surface, shape = RoundedCornerShape(18.dp)) { val library by rememberInputLibrary() val base = library.profiles.firstOrNull { it.id == target.profile }?.binding(InputRole.PHYSICAL) - ?.takeIf { target.gameEdit != null || it != target.bindings } + ?.takeIf { it != target.bindings } InputButtonsPane(target.bindings, target.scope, base, onDismiss, onChanged, gameEdit = target.gameEdit) } } else InputSurfaceDialog(target.profile, onDismiss, onChanged, role = target.role, layoutId = target.layout, @@ -82,7 +97,8 @@ internal fun InputButtonsPane(bindingsId: String, scope: String, baseId: String? val library by rememberInputLibrary() val activeBindings = gameEdit?.part(library)?.bindings ?: bindingsId val preset = library.bindings.firstOrNull { it.id == activeBindings } ?: return - val base = baseId?.let { id -> library.bindings.firstOrNull { it.id == id } } + val base = (gameEdit?.inheritedPart(library)?.bindings ?: baseId) + ?.let { id -> library.bindings.firstOrNull { it.id == id } } var selected by remember { mutableStateOf(InputControl.A) } var capturing by remember { mutableStateOf(false) } var unknownKey by remember { mutableStateOf(false) } @@ -97,7 +113,7 @@ internal fun InputButtonsPane(bindingsId: String, scope: String, baseId: String? runCatching { InputLibraries.update(context) { latest -> if (gameEdit == null) change(latest, bindingsId) - else gameEdit.apply(latest) { editable, part -> change(editable, part.bindings) } + else gameEdit.setPhysicalAction(latest, selected, action) } } .onSuccess { onChanged() }.onFailure { draft.action = current; failure = it.message ?: context.getString(R.string.input_save_failed) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt index 87a87f3829..532d32ba2d 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputQuickSettings.kt @@ -47,11 +47,13 @@ internal fun quickMixedChoiceId(library: InputLibrary): String = candidate != InputProfiles.AUTO && library.profiles.none { it.id == candidate } } -internal fun quickRoleChoices(library: InputLibrary, role: InputRole): List> = - library.bindings.filter { binding -> - if (role == InputRole.PHYSICAL) binding.physical - else !binding.physical && library.layouts.any { it.id == binding.layoutId && it.supports(role) } +internal fun quickRoleChoices(library: InputLibrary, role: InputRole): List> { + val layoutIds = if (role == InputRole.PHYSICAL) emptySet() else library.sharedLayouts(role).map { it.id }.toSet() + return library.bindings.filter { binding -> + !library.isGameOwnedBindings(binding.id) && (if (role == InputRole.PHYSICAL) binding.physical + else !binding.physical && binding.layoutId?.let { it in layoutIds } == true) }.map { it.id to it.name } +} /** Mode-local combinations belong to shared defaults, not to a game's pinned profile. */ @Composable diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt index a695362a31..904ec48c58 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt @@ -203,17 +203,24 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB } fun update(element: InputSurfaceElement) { - stored(element.id) + val existing = elements.firstOrNull { it.id == element.id } + if (existing == element) return + if (existing != null) checkpoint() else if (!stored(element.id)) return elements = elements.map { if (it.id == element.id) element else it } } /** A D-pad arm is part of one control and can't be copied on its own. */ - fun canDuplicate(e: InputSurfaceElement) = !e.dpadPiece && elements.size < 64 + fun canDuplicate(e: InputSurfaceElement): Boolean { + if (e.dpadPiece) return false + val frame = if (elements.any { it.id == e.id }) elements + else OnScreenLayout.editorFrame(layout.copy(elements = elements), shown) + return frame.size < 64 && frame.any { it.id == e.id } + } /** Adds a copy of [id] next to it, doing what the original does, and selects it. */ fun duplicate(id: String) { val original = (elements.firstOrNull { it.id == id } ?: shown.firstOrNull { it.id == id })?.takeIf(::canDuplicate) ?: return - if (elements.any { it.id == id }) checkpoint() else stored(id) + if (elements.any { it.id == id }) checkpoint() else if (!stored(id)) return val copy = original.copy(id = newInputId(), control = original.kind.control, x = original.x + .03f, y = original.y + .03f).bounded() fun resolved(action: InputAction) = if (action is InputAction.Native && action.target == null && original.kind != InputElementKind.DPAD) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt index a36fa56ed4..84f8e3a01e 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionOverlay.kt @@ -816,7 +816,7 @@ private fun DrawerControllerPage(host: MenuHost, a: DrawerActions, padConnected: SettingsGroup(stringResource(R.string.flow_touch_controls), key = "controller-osc", tag = noPadTag, detail = summary(sources.labelOf(source), if (source == TOUCH_DROIDDECK) shows.labelOf(oscValue) else null, c?.let { "${it.size}%" }, c?.let { "${it.onScreenOpacity}%" })) { ChoiceRow(host, "osc", stringResource(R.string.flow_touch_type), stringResource(R.string.flow_scope_every_game), sources, source, chipModifier = track("osc")) { picked -> - a.onOsc(touchMode(picked)) + if (picked != source) a.onOsc(touchMode(picked)) } if (source == TOUCH_DROIDDECK) ChoiceRow(host, "osc-show", stringResource(R.string.flow_show), stringResource(R.string.mode_osc_note), shows, oscValue, chipModifier = track("osc-show"), onPick = a.onOsc) diff --git a/app/src/test/java/com/droiddeck/launcher/ui/GameInputEditTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/GameInputEditTest.kt index ef454435e8..9991ff5d4b 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/GameInputEditTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/GameInputEditTest.kt @@ -43,4 +43,27 @@ class GameInputEditTest { assertEquals(library.activePart(false, InputRole.PRIMARY), updated.activePart(false, InputRole.PRIMARY, "game:edit")) assertEquals(library.preset(original.bindings), updated.preset(original.bindings)) } + @Test + fun resettingToSharedMixDropsOnlyTheRedundantGameCopy() { + val library = InputLibraries.read(RuntimeEnvironment.getApplication()) + val editor = GameInputEdit(false, "game:mixed", "Mixed", InputRole.PHYSICAL) + val stock = library.preset(library.activePart(false, InputRole.PHYSICAL).bindings) + val sharedAction = InputAction.Keys(listOf(18)) + val mixedPreset = stock.copy(id = "mixed-physical", name = "Mixed physical", + controls = stock.controls + (InputControl.A to sharedAction)) + val mixed = library.withPreset(mixedPreset).selectPart(false, InputRole.PHYSICAL, mixedPreset.id) + assertEquals(mixedPreset.id, editor.inheritedPart(mixed).bindings) + + val other = InputAction.Native(InputControl.X) + val custom = editor.setPhysicalAction(mixed, InputControl.A, other).validate() + assertEquals(mixedPreset.id, editor.inheritedPart(custom).bindings) + val reset = editor.setPhysicalAction(custom, InputControl.A, sharedAction).validate() + assertEquals(mixed, reset) + assertEquals(mixed, editor.setPhysicalAction(mixed, InputControl.A, sharedAction)) + + val more = editor.setPhysicalAction(custom, InputControl.B, other) + val partial = editor.setPhysicalAction(more, InputControl.A, sharedAction).validate() + assertEquals(other, partial.preset(editor.part(partial).bindings).controls[InputControl.B]) + } + } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/SharedInputRoleMixTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/SharedInputRoleMixTest.kt index 1632f6b9a6..b49b4235d6 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/SharedInputRoleMixTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/SharedInputRoleMixTest.kt @@ -55,4 +55,11 @@ class SharedInputRoleMixTest { assertNotEquals("mixed", quickMixedChoiceId(library)) assertFalse(library.profiles.any { it.id == quickMixedChoiceId(library) }) } + @Test fun gameOwnedPartsCannotBecomeSharedChoices() { + val library = InputLibraries.read(context) + .withGamePart(false, "game:private", "Private", InputRole.PHYSICAL, bindingsId = "private-physical") + assertFalse(quickRoleChoices(library, InputRole.PHYSICAL).any { it.first == "private-physical" }) + assertTrue(runCatching { library.selectPart(false, InputRole.PHYSICAL, "private-physical") }.isFailure) + assertTrue(runCatching { library.chooseBindings("game", InputRole.PHYSICAL, "private-physical") }.isFailure) + } }