diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 26aa65af74..95712d0b20 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -4733,21 +4733,25 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private fun inputGameKey(): String? = bottomGameKey() - /** Opens the button binder or the on-screen layout editor on what this game uses, giving the game its own copy first when changes are for it. */ + /** + * Opens the button binder or the on-screen layout editor on what this game uses. Button changes for a game are stored as + * overrides on top of these bindings; an on-screen part is copied for the game first. + */ private fun openInputEditor(role: InputRole) { val desktop = loadingMode() == SessionService.MODE_DESKTOP val game = inputGameKey()?.takeUnless { SessionState.inputForAllGames } val name = inputGameName() - val forked = game != null && runCatching { com.droiddeck.launcher.input.InputLibraries.read(this).games[game]?.parts?.containsKey(role) != true }.getOrDefault(false) + val physical = role == InputRole.PHYSICAL + val forked = game != null && !physical && runCatching { com.droiddeck.launcher.input.InputLibraries.read(this).games[game]?.parts?.containsKey(role) != true }.getOrDefault(false) val opened = runCatching { - val library = if (game != null) com.droiddeck.launcher.input.InputLibraries.update(this) { it.withGamePart(desktop, game, name, role) } + val library = if (forked) com.droiddeck.launcher.input.InputLibraries.update(this) { it.withGamePart(desktop, game!!, name, role) } else com.droiddeck.launcher.input.InputLibraries.read(this) val part = library.activePart(desktop, role, game) val layout = part.layout?.let { id -> library.layouts.first { it.id == id } } if (role != InputRole.PHYSICAL && layout?.editable != true) null else com.droiddeck.launcher.ui.InputEditorTarget(role, library.activeId(desktop, game), part.bindings, part.layout, scope = if (game != null) name else getString(R.string.flow_shared_defaults_of, com.droiddeck.launcher.ui.profileLabel(this, library.profile(library.activeId(desktop, game)))), - game = game?.takeIf { forked }, desktop = desktop) + game = game?.takeIf { forked || physical }, desktop = desktop) }.onFailure { Toast.makeText(this, it.message ?: getString(R.string.input_save_failed), Toast.LENGTH_LONG).show() }.getOrNull() leaveImmersiveForDialog(); releasePads(); drawerOpen = false inputEditor = opened diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt index d12edb3b0d..1c36b4be61 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt @@ -11,7 +11,7 @@ import java.util.UUID object InputLibraries { // 15 and 16 belong to the reverted controller redesign; see redesignVersions. - private const val VERSION = 17 + private const val VERSION = 18 private val redesignVersions = 15..16 private const val KIT_DPAD = "kit:dpad" private const val KEYBOARD = "builtin:keyboard" @@ -388,7 +388,8 @@ object InputLibraries { for ((key, game) in library.games) { val parts = JSONObject() game.parts.forEach { (role, part) -> parts.put(role.name, JSONObject().put("bindings", part.bindings).put("layout", part.layout)) } - games.put(key, JSONObject().put("name", game.name).put("profile", game.profile ?: JSONObject.NULL).put("parts", parts)) + val controls = JSONObject(); game.controls.forEach { (c, a) -> controls.put(c.id, InputProfiles.encode(a)) } + games.put(key, JSONObject().put("name", game.name).put("profile", game.profile ?: JSONObject.NULL).put("parts", parts).put("controls", controls)) } return JSONObject().put("selections", selections).put("version", VERSION).put("selected", library.selected).put("steam", library.steam).put("desktop", library.desktop) .put("profiles", profiles).put("layouts", layouts).put("bindings", bindings).put("games", games).toString() @@ -448,10 +449,11 @@ object InputLibraries { val parts = game.getJSONObject("parts") GameInput(game.getString("name"), game.optionalString("profile"), parts.keys().asSequence().associate { role -> val part = parts.getJSONObject(role); InputRole.valueOf(role) to InputPart(part.getString("bindings"), part.optionalString("layout")) - }) + }, game.optJSONObject("controls")?.actions(28).orEmpty().mapKeys { requireNotNull(InputControl.fromId(it.key)) }) } }.orEmpty() val library = InputLibrary(profiles, layouts, bindings, doc.getString("selected"), doc.getString("steam"), doc.getString("desktop"), selections, games) + .let { if (version < 18) it.withGameControlOverrides() else it } (if (restore) restoreGamepads(requireNotNull(context), library, generated) else library).validate() }.getOrNull() 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 cb55c8fe73..2817905b34 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt @@ -69,8 +69,14 @@ data class SavedInputProfile(val id: String, val name: String, val base: String, fun binding(role: InputRole) = parts.getValue(role).bindings } -/** One game's own controls: a profile it always uses, and parts copied from that profile and then changed for it alone. */ -data class GameInput(val name: String, val profile: String? = null, val parts: Map = emptyMap()) +/** + * One game's own controls: a profile it always uses, the button actions it sets on top of whatever bindings it would otherwise + * use, and on-screen parts copied for it alone. + */ +data class GameInput(val name: String, val profile: String? = null, val parts: Map = emptyMap(), + val controls: Map = emptyMap()) { + val empty get() = profile == null && parts.isEmpty() && controls.isEmpty() +} data class InputLibrary( val profiles: List, val layouts: List, val bindings: List, @@ -101,6 +107,12 @@ 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 bindings [role] uses, with [game]'s own button actions on top for the physical controller. */ + fun activeBindings(isDesktop: Boolean, role: InputRole, game: String? = null): SavedInputBindings { + val preset = preset(activePart(isDesktop, role, game).bindings) + val own = games[game]?.controls.orEmpty() + return if (role != InputRole.PHYSICAL || own.isEmpty()) preset else preset.copy(controls = preset.controls + own) + } 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 = @@ -108,7 +120,7 @@ data class InputLibrary( fun panelLayout(isDesktop: Boolean, panel: String?, game: String? = null) = panelPart(isDesktop, panel, game).layout?.let { id -> layouts.firstOrNull { it.id == id } } fun compileActive(isDesktop: Boolean, panel: String? = null, game: String? = null): InputProfile = compileParts(activeId(isDesktop, game), InputRole.entries.associateWith { - preset(if (it == InputRole.SECONDARY) panelPart(isDesktop, panel, game).bindings else activePart(isDesktop, it, game).bindings) + if (it == InputRole.SECONDARY) preset(panelPart(isDesktop, panel, game).bindings) else activeBindings(isDesktop, it, game) }) fun compileStock(isDesktop: Boolean, panel: String? = null, game: String? = null): InputProfile { val defaults = InputProfile.defaults(profile(activeId(isDesktop, game)).base) @@ -147,9 +159,10 @@ data class InputLibrary( fun usersOfLayout(id: String) = profiles.filter { p -> p.parts.values.any { it.layout == id } } private fun gamesUsingLayout(id: String) = games.values.any { g -> g.parts.values.any { it.layout == id } } - /** Gives [game] its own copy of the part it uses for [role], so edits made for it leave the profile alone. */ + /** Gives [game] its own copy of the on-screen part it uses for [role], so edits made for it leave the profile alone. */ fun withGamePart(isDesktop: Boolean, game: String, name: String, role: InputRole, bindingsId: String = newInputId(), layoutId: String = newInputId()): InputLibrary { + require(role != InputRole.PHYSICAL) val current = games[game] ?: GameInput(inputName(name)) if (role in current.parts) return this val source = activePart(isDesktop, role, game) @@ -162,20 +175,39 @@ data class InputLibrary( .copy(games = games + (game to current.copy(parts = current.parts + (role to part)))) } - fun withGameProfile(game: String, name: String, profile: String?): InputLibrary { - require(profile == null || profiles.any { it.id == profile }) + /** + * Sets what [control] does in [game] alone. An action equal to the one the game would otherwise use drops the override, + * so the control follows the shared bindings again. + */ + fun withGameControl(isDesktop: Boolean, game: String, name: String, control: InputControl, action: InputAction): InputLibrary { + require(action.validFor(control)) val current = games[game] ?: GameInput(inputName(name)) - val next = current.copy(profile = profile) - return copy(games = if (next.profile == null && next.parts.isEmpty()) games - game else games + (game to next)) + val base = preset(activePart(isDesktop, InputRole.PHYSICAL, game).bindings).controls[control] ?: InputAction.Native() + return withGame(game, current.copy(controls = if (action == base) current.controls - control else current.controls + (control to action))) } - /** Drops [game]'s own controls (one part, or all of them) and the copies nothing else uses. */ + fun withGameProfile(game: String, name: String, profile: String?): InputLibrary { + require(profile == null || profiles.any { it.id == profile }) + return withGame(game, (games[game] ?: GameInput(inputName(name))).copy(profile = profile)) + } + + private fun withGame(key: String, game: GameInput) = copy(games = if (game.empty) games - key else games + (key to game)) + + /** Drops [game]'s own controls (its button overrides, one on-screen part, or all of them) and the copies nothing else uses. */ fun resetGame(game: String, role: InputRole? = null): InputLibrary { val current = games[game] ?: return this val dropped = if (role == null) current.parts.values.toList() else listOfNotNull(current.parts[role]) - val remaining = if (role == null) null else current.copy(parts = current.parts - role).takeIf { it.profile != null || it.parts.isNotEmpty() } - var result = copy(games = if (remaining == null) games - game else games + (game to remaining)) - for (part in dropped) { + val remaining = when (role) { + null -> GameInput(current.name) + InputRole.PHYSICAL -> current.copy(controls = emptyMap()) + else -> current.copy(parts = current.parts - role) + } + return withGame(game, remaining).dropUnused(dropped) + } + + private fun dropUnused(parts: List): InputLibrary { + var result = this + for (part in parts) { if (result.usersOfBindings(part.bindings).isEmpty() && !result.selectedBindings(part.bindings)) result = result.copy(bindings = result.bindings.filterNot { it.id == part.bindings }) val layout = part.layout ?: continue @@ -186,10 +218,29 @@ data class InputLibrary( return result } - /** How many controls [game]'s own parts set differently from the profile it would otherwise use. */ + /** + * Turns each game's copied button bindings (libraries before version 18) into the actions it sets differently from the + * bindings it would otherwise use in game mode, and drops the copies. + */ + internal fun withGameControlOverrides(): InputLibrary { + var result = this + for ((key, game) in games) { + val part = game.parts[InputRole.PHYSICAL] ?: continue + val stripped = result.copy(games = result.games + (key to game.copy(parts = game.parts - InputRole.PHYSICAL))) + val mine = preset(part.bindings).controls + val base = stripped.preset(stripped.activePart(false, InputRole.PHYSICAL, key).bindings).controls + val controls = (mine.keys + base.keys).filter { (mine[it] ?: InputAction.Native()) != (base[it] ?: InputAction.Native()) } + .associateWith { mine[it] ?: InputAction.Native() } + result = stripped.withGame(key, stripped.games.getValue(key).copy(controls = controls)).dropUnused(listOf(part)) + } + return result + } + + /** How many controls [game] sets for itself: one per button override, and per element its on-screen copies change. */ 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 buttons = if (only == null || only == InputRole.PHYSICAL) current.controls.size else 0 + return buttons + 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 mine = preset(part.bindings) val theirs = preset(base.bindings) @@ -284,7 +335,7 @@ data class InputLibrary( return copy(profiles = profiles.filterNot { it.id == id }, selected = selected.takeUnless { it == id } ?: InputProfiles.AUTO, steam = steam.takeUnless { it == id } ?: InputProfile.GAME, desktop = desktop.takeUnless { it == id } ?: InputProfile.DESKTOP, selections = selections.mapValues { (_, selection) -> if (selection.profile == id) selection.copy(profile = InputProfiles.AUTO) else selection }, - games = games.mapValues { (_, game) -> if (game.profile == id) game.copy(profile = null) else game }.filterValues { it.profile != null || it.parts.isNotEmpty() }) + games = games.mapValues { (_, game) -> if (game.profile == id) game.copy(profile = null) else game }.filterValues { !it.empty }) } fun removeLayout(id: String): InputLibrary { @@ -369,7 +420,7 @@ data class InputLibrary( require(key.isNotBlank() && key.length <= 128) inputName(game.name) require(game.profile == null || profiles.any { it.id == game.profile }) - require(game.profile != null || game.parts.isNotEmpty()) + require(!game.empty && InputRole.PHYSICAL !in game.parts && game.controls.all { (c, a) -> a.validFor(c) }) for ((role, part) in game.parts) { val binding = preset(part.bindings) require(binding.physical == (role == InputRole.PHYSICAL)) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/DrawerControls.kt b/app/src/main/java/com/droiddeck/launcher/ui/DrawerControls.kt index e8146813a3..19ce5c7ddc 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/DrawerControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/DrawerControls.kt @@ -69,7 +69,7 @@ internal fun DrawerControlsGroup(host: MenuHost, a: DrawerActions, track: (Strin if (gameScope && game != null) update { it.withGameProfile(game.first, game.second, id.takeUnless { it == InputProfiles.AUTO }) } else update { it.selectProfile(a.desktop, id) } } - val physical = if (gameScope && gameKey != null && own?.parts?.containsKey(InputRole.PHYSICAL) == true) + val physical = if (gameScope && gameKey != null && own?.controls?.isNotEmpty() == true) stringResource(R.string.flow_game_changes, library.gameChanges(a.desktop, gameKey, InputRole.PHYSICAL)) else profileLabel(context, active) ActionRow(stringResource(R.string.flow_buttons), physical, stringResource(R.string.ctrl_edit), a.onEditButtons, 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 d67987eb1b..e963dca775 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputButtonsEditor.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputButtonsEditor.kt @@ -28,8 +28,9 @@ import com.droiddeck.launcher.R import com.droiddeck.launcher.input.* /** - * What an in-game editor works on: the bindings (and layout) one role uses now, already copied for the game when changes - * are for it alone. [scope] names whose controls these are; [game] is set when the copy was made just for this edit. + * What an in-game editor works on: the bindings (and layout) one role uses now. [scope] names whose controls these are. + * [game] is set when changes are for that game alone: button changes become its overrides on top of [bindings], and an + * on-screen part was copied for it just for this edit. */ internal data class InputEditorTarget( val role: InputRole, val profile: String, val bindings: String, val layout: String?, @@ -41,8 +42,9 @@ internal fun InputEditorWindow(target: InputEditorTarget, onDismiss: () -> Unit, if (target.role == InputRole.PHYSICAL) Dialog(onDismissRequest = onDismiss, properties = DialogProperties(usePlatformDefaultWidth = false)) { 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 { it != target.bindings } - InputButtonsPane(target.bindings, target.scope, base, onDismiss, onChanged) + val base = if (target.game != null) target.bindings + else library.profiles.firstOrNull { it.id == target.profile }?.binding(InputRole.PHYSICAL)?.takeIf { it != target.bindings } + InputButtonsPane(target.bindings, target.scope, base, onDismiss, onChanged, game = target.game, desktop = target.desktop) } } else InputSurfaceDialog(target.profile, onDismiss, onChanged, role = target.role, layoutId = target.layout, bindingsId = target.bindings, scope = target.scope) } @@ -64,15 +66,17 @@ internal fun controlForKey(keyCode: Int): InputControl? = controllerKeys[keyCode /** * Buttons and sticks: tap a control on a drawing of the controller, or press it, then set what it does. Saves into the - * bindings [bindingsId]; [baseId] is the profile's own bindings when these are a game's copy, so a control can go back. + * bindings [bindingsId], or as [game]'s overrides on top of them (named [scope]); [baseId] is the bindings a control can + * go back to. */ @Composable internal fun InputButtonsPane(bindingsId: String, scope: String, baseId: String?, onBack: () -> Unit, onChanged: () -> Unit, - onMore: (() -> Unit)? = null) { + onMore: (() -> Unit)? = null, game: String? = null, desktop: Boolean = false) { val context = LocalContext.current val host = rememberMenuHost() val library by rememberInputLibrary() - val preset = library.bindings.firstOrNull { it.id == bindingsId } ?: return + val stored = library.bindings.firstOrNull { it.id == bindingsId } ?: return + val preset = game?.let { library.games[it] }?.let { stored.copy(controls = stored.controls + it.controls) } ?: stored val base = baseId?.let { id -> library.bindings.firstOrNull { it.id == id } } var selected by remember { mutableStateOf(InputControl.A) } var capturing by remember { mutableStateOf(false) } @@ -81,7 +85,10 @@ internal fun InputButtonsPane(bindingsId: String, scope: String, baseId: String? val current = preset.controls[selected] ?: InputAction.Native() val draft = remember(selected) { InputActionDraft(current) } fun save(action: InputAction) { - runCatching { InputLibraries.update(context) { l -> l.withPreset(l.preset(bindingsId).let { it.copy(controls = it.controls + (selected to action)) }) } } + runCatching { InputLibraries.update(context) { l -> + if (game != null) l.withGameControl(desktop, game, scope, selected, action) + else l.withPreset(l.preset(bindingsId).let { it.copy(controls = it.controls + (selected to action)) }) + } } .onSuccess { onChanged() }.onFailure { draft.action = current; failure = it.message ?: context.getString(R.string.input_save_failed) } } // Every change applies as it is made, as in the layout editor, so leaving never loses an edit. diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryGamesTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryGamesTest.kt index 5ba48ed2c3..8bac218ac2 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryGamesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryGamesTest.kt @@ -21,15 +21,18 @@ class InputLibraryGamesTest { private fun library() = InputLibraries.read(context) - @Test fun aGameCopyChangesOnlyThatGame() { - val base = library() - val forked = base.withGamePart(false, "620", "Portal 2", InputRole.PHYSICAL) - val part = forked.activePart(false, InputRole.PHYSICAL, "620") - assertNotEquals(base.activePart(false, InputRole.PHYSICAL).bindings, part.bindings) - assertEquals(0, forked.gameChanges(false, "620")) + private val jump = InputAction.Keys(listOf(18)) - val changed = forked.withPreset(forked.preset(part.bindings).let { it.copy(controls = it.controls + (InputControl.A to InputAction.Keys(listOf(18)))) }) - assertEquals(InputAction.Keys(listOf(18)), changed.compileActive(false, game = "620").action(InputRole.PHYSICAL, InputControl.A)) + private fun InputLibrary.withShared(control: InputControl, action: InputAction, profile: String = InputProfile.GAME) = + withPreset(preset(profile(profile).binding(InputRole.PHYSICAL)).let { it.copy(controls = it.controls + (control to action)) }) + + @Test fun aGameOverrideChangesOnlyThatGame() { + val base = library() + val changed = base.withGameControl(false, "620", "Portal 2", InputControl.A, jump) + assertEquals(mapOf(InputControl.A to jump), changed.games.getValue("620").controls) + assertEquals(base.bindings, changed.bindings) + assertEquals(base.activePart(false, InputRole.PHYSICAL), changed.activePart(false, InputRole.PHYSICAL, "620")) + assertEquals(jump, changed.compileActive(false, game = "620").action(InputRole.PHYSICAL, InputControl.A)) assertEquals(InputAction.Native(), changed.compileActive(false).action(InputRole.PHYSICAL, InputControl.A)) assertEquals(InputAction.Native(), changed.compileActive(false, game = "413150").action(InputRole.PHYSICAL, InputControl.A)) assertEquals(1, changed.gameChanges(false, "620")) @@ -38,9 +41,56 @@ class InputLibraryGamesTest { changed.validate() assertEquals(changed, InputLibraries.decode(InputLibraries.encode(changed))) - val reset = changed.resetGame("620") - assertTrue(reset.games.isEmpty()) - assertEquals(base.bindings.map { it.id }.toSet(), reset.bindings.map { it.id }.toSet()) + assertTrue(changed.resetGame("620").games.isEmpty()) + assertTrue(changed.resetGame("620", InputRole.PHYSICAL).games.isEmpty()) + } + + @Test fun aSharedChangeReachesButtonsTheGameLeftAlone() { + val changed = library().withGameControl(false, "620", "Portal 2", InputControl.A, jump) + val shared = changed.withShared(InputControl.B, InputAction.Keys(listOf(57))) + assertEquals(InputAction.Keys(listOf(57)), shared.compileActive(false, game = "620").action(InputRole.PHYSICAL, InputControl.B)) + assertEquals(1, shared.gameChanges(false, "620")) + } + + @Test fun anOverrideSurvivesASharedChangeToTheSameButton() { + val changed = library().withGameControl(false, "620", "Portal 2", InputControl.A, jump) + val shared = changed.withShared(InputControl.A, InputAction.Keys(listOf(57))) + assertEquals(jump, shared.compileActive(false, game = "620").action(InputRole.PHYSICAL, InputControl.A)) + assertEquals(InputAction.Keys(listOf(57)), shared.compileActive(false).action(InputRole.PHYSICAL, InputControl.A)) + assertEquals(1, shared.gameChanges(false, "620")) + } + + @Test fun settingAButtonBackToTheSharedActionDropsItsOverride() { + val changed = library().withGameControl(false, "620", "Portal 2", InputControl.A, jump) + .withGameControl(false, "620", "Portal 2", InputControl.B, InputAction.Disabled) + val back = changed.withGameControl(false, "620", "Portal 2", InputControl.A, InputAction.Native()) + assertEquals(mapOf(InputControl.B to InputAction.Disabled), back.games.getValue("620").controls) + assertEquals(1, back.gameChanges(false, "620")) + assertTrue(back.withGameControl(false, "620", "Portal 2", InputControl.B, InputAction.Native()).games.isEmpty()) + } + + @Test fun oldGameCopiesBecomeOverridesOnUpgrade() { + val base = library().withShared(InputControl.X, InputAction.Keys(listOf(57))) + val shared = base.preset(base.profile(InputProfile.GAME).binding(InputRole.PHYSICAL)) + val copy = shared.copy(id = "portal-copy", name = "Portal 2 / physical", + controls = shared.controls + (InputControl.A to jump) + (InputControl.B to InputAction.Disabled)) + val untouched = shared.copy(id = "hades-copy", name = "Hades / physical") + val old = base.withPreset(copy).withPreset(untouched).copy(games = mapOf( + "620" to GameInput("Portal 2", parts = mapOf(InputRole.PHYSICAL to InputPart(copy.id))), + "1145360" to GameInput("Hades", parts = mapOf(InputRole.PHYSICAL to InputPart(untouched.id))))) + val raw = org.json.JSONObject(InputLibraries.encode(old)).put("version", 17).toString() + context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).edit().putString("library", raw).commit() + + val upgraded = library() + assertEquals(mapOf(InputControl.A to jump, InputControl.B to InputAction.Disabled), upgraded.games.getValue("620").controls) + assertTrue(upgraded.games.getValue("620").parts.isEmpty()) + assertFalse(upgraded.games.containsKey("1145360")) + assertTrue(upgraded.bindings.none { it.id == copy.id || it.id == untouched.id }) + assertEquals(InputAction.Keys(listOf(57)), upgraded.compileActive(false, game = "620").action(InputRole.PHYSICAL, InputControl.X)) + assertEquals(jump, upgraded.compileActive(false, game = "620").action(InputRole.PHYSICAL, InputControl.A)) + assertEquals(2, upgraded.gameChanges(false, "620")) + val stored = context.getSharedPreferences("input-profiles", Context.MODE_PRIVATE).getString("library", null)!! + assertEquals(18, org.json.JSONObject(stored).getInt("version")) } @Test fun aGameCopyOfTheMainScreenLayoutKeepsItsGamepadOrigin() { @@ -76,27 +126,23 @@ class InputLibraryGamesTest { assertTrue(pinned.withGameProfile("1293830", "Forza Horizon 4", null).games.isEmpty()) } - @Test fun aGamesOwnPartWinsOverItsPinnedProfile() { + @Test fun aGamesOverridesSitOnTopOfItsPinnedProfile() { val base = library().duplicateProfile(InputProfile.GAME, "Racing", "racing") - val both = base.withGameProfile("620", "Portal 2", "racing").withGamePart(false, "620", "Portal 2", InputRole.PHYSICAL) - val own = both.games.getValue("620").parts.getValue(InputRole.PHYSICAL) - assertEquals(own, both.activePart(false, InputRole.PHYSICAL, "620")) - assertEquals(both.profile("racing").parts.getValue(InputRole.PRIMARY), both.activePart(false, InputRole.PRIMARY, "620")) + val both = base.withGameProfile("620", "Portal 2", "racing").withGameControl(false, "620", "Portal 2", InputControl.A, jump) + assertEquals(both.profile("racing").parts.getValue(InputRole.PHYSICAL), both.activePart(false, InputRole.PHYSICAL, "620")) + val shared = both.withShared(InputControl.B, InputAction.Disabled, "racing") + assertEquals(InputAction.Disabled, shared.compileActive(false, game = "620").action(InputRole.PHYSICAL, InputControl.B)) + assertEquals(jump, shared.compileActive(false, game = "620").action(InputRole.PHYSICAL, InputControl.A)) val cleared = both.resetGame("620", InputRole.PHYSICAL) assertEquals("racing", cleared.games.getValue("620").profile) - assertTrue(cleared.games.getValue("620").parts.isEmpty()) + assertTrue(cleared.games.getValue("620").controls.isEmpty()) } - @Test fun changesAreCountedAgainstTheGamesPinnedProfile() { + @Test fun overridesAreComparedWithTheGamesPinnedProfile() { val base = library().duplicateProfile(InputProfile.GAME, "Racing", "racing") - val racing = base.profile("racing").binding(InputRole.PHYSICAL) - val pinned = base.withPreset(base.preset(racing).let { it.copy(controls = it.controls + (InputControl.A to InputAction.Keys(listOf(18)))) }) - .withGameProfile("620", "Portal 2", "racing") - val forked = pinned.withGamePart(false, "620", "Portal 2", InputRole.PHYSICAL) - assertEquals(0, forked.gameChanges(false, "620")) - val own = forked.activePart(false, InputRole.PHYSICAL, "620").bindings - val native = forked.withPreset(forked.preset(own).let { it.copy(controls = it.controls + (InputControl.A to InputAction.Native())) }) - assertEquals(1, native.gameChanges(false, "620")) + val pinned = base.withShared(InputControl.A, jump, "racing").withGameProfile("620", "Portal 2", "racing") + assertEquals(0, pinned.withGameControl(false, "620", "Portal 2", InputControl.A, jump).gameChanges(false, "620")) + assertEquals(1, pinned.withGameControl(false, "620", "Portal 2", InputControl.A, InputAction.Native()).gameChanges(false, "620")) } @Test fun hidingAQuickButtonInAGamesLayoutCountsAsAChange() { @@ -117,8 +163,8 @@ class InputLibraryGamesTest { } @Test fun aGamesCopiesCantBeDeletedFromUnderIt() { - val forked = library().withGamePart(false, "620", "Portal 2", InputRole.PHYSICAL) - val id = forked.activePart(false, InputRole.PHYSICAL, "620").bindings + val forked = library().withGamePart(false, "620", "Portal 2", InputRole.PRIMARY) + val id = forked.activePart(false, InputRole.PRIMARY, "620").bindings assertTrue(forked.selectedBindings(id)) assertTrue(runCatching { forked.removeBindings(id) }.isFailure) } diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt index e36c3c8819..726970d755 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt @@ -90,7 +90,7 @@ class InputLibraryTest { assertEquals("previous backup", prefs.getString(backup, null)) assertEquals(original, InputLibraries.read(context)) - assertEquals(17, JSONObject(prefs.getString("library", null)!!).getInt("version")) + assertEquals(18, JSONObject(prefs.getString("library", null)!!).getInt("version")) assertEquals(raw, prefs.getString(backup, null)) } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt index 61e2c0ebbe..eef945b46e 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt @@ -529,6 +529,22 @@ class InputSettingsTest { compose.onAllNodesWithText(InputControl.RIGHT_CLICK.label, substring = false).assertCountEquals(2) } + @Test fun theGameBinderGoesBackToTheSharedActionByDroppingTheOverride() { + val library = InputLibraries.update(context) { it.withGameControl(false, "620", "Portal 2", InputControl.A, InputAction.Disabled) } + val target = InputEditorTarget(InputRole.PHYSICAL, InputProfile.GAME, library.activePart(false, InputRole.PHYSICAL, "620").bindings, null, + scope = "Portal 2", game = "620") + compose.setContent { DroidDeckTheme { InputEditorWindow(target, {}, {}) } } + compose.onNodeWithText(context.getString(R.string.flow_editing, "Portal 2")).assertExists() + compose.onNodeWithTag("input-buttons-changed").assertExists() + compose.onNodeWithTag("input-buttons-reset").performScrollTo().performClick() + compose.runOnIdle { + val updated = InputLibraries.read(context) + assertTrue(updated.games.isEmpty()) + assertEquals(library.bindings, updated.bindings) + } + compose.onNodeWithTag("input-buttons-changed").assertDoesNotExist() + } + @Test fun theProfilePageSetsWhatItIsTheDefaultFor() { InputLibraries.update(context) { it.duplicateProfile(InputProfile.GAME, "Racing", "racing") } compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } @@ -548,10 +564,7 @@ class InputSettingsTest { } @Test fun gamesWithTheirOwnControlsAreListedAndCanBeReset() { - InputLibraries.update(context) { l -> l.withGamePart(false, "620", "Portal 2", InputRole.PHYSICAL).let { forked -> - val id = forked.activePart(false, InputRole.PHYSICAL, "620").bindings - forked.withPreset(forked.preset(id).let { it.copy(controls = it.controls + (InputControl.A to InputAction.Disabled)) }) - } } + InputLibraries.update(context) { it.withGameControl(false, "620", "Portal 2", InputControl.A, InputAction.Disabled) } compose.setContent { DroidDeckTheme { InputSettingsPage({}) } } compose.onNodeWithTag("input-game-620").performScrollTo().assertExists() compose.onAllNodesWithText("Portal 2", substring = true).onFirst().assertExists()