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 127c525d1..55f31ad6f 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibraries.kt @@ -239,14 +239,36 @@ object InputLibraries { return library.withLayout(saved.copy(elements = InputStockLayouts.gamepad(leftPad, rightStick))).validate() } - /** Gamepads saved by the old editor follow the virtual controller's kit again; their positions don't map onto the new controls. */ + /** + * Gamepads saved by the old editor follow the virtual controller's kit again; their positions don't map onto the new controls. + * One that kept positions for the kit's controls is also kept as it was, under its own name, with its actions. Where the copy + * doesn't fit the library's limits, the gamepad itself keeps its positions instead. + */ private fun kitGamepads(library: InputLibrary): InputLibrary { - return library.copy(layouts = library.layouts.map { layout -> - val origin = layout.origin ?: layout.id - if (origin != OVERLAY && !origin.startsWith("migrated.layout.")) return@map layout + fun old(layout: SavedInputLayout) = (layout.origin ?: layout.id).let { it == OVERLAY || it.startsWith("migrated.layout.") } + fun follow(layout: SavedInputLayout): SavedInputLayout { val extras = layout.elements.filterNot { KitLayout.isKit(it.id) } - layout.copy(elements = extras, origin = if (layout.id == OVERLAY) null else OVERLAY, aspect = if (extras.isEmpty()) 0f else layout.aspect, scale = 1f) - }).validate() + return layout.copy(elements = extras, origin = if (layout.id == OVERLAY) null else OVERLAY, aspect = if (extras.isEmpty()) 0f else layout.aspect, scale = 1f) + } + val positioned = library.layouts.filter { old(it) && it.elements.any { e -> KitLayout.isKit(e.id) } } + val inPlace = positioned.fold(library.copy(layouts = library.layouts.map { if (old(it) && it !in positioned) follow(it) else it })) { acc, layout -> + acc.withLayout(layout.copy(kitless = true)) + } + var result = library.copy(layouts = library.layouts.map { if (old(it)) follow(it) else it }) + for (layout in positioned) { + val id = "kept." + UUID.nameUUIDFromBytes(layout.id.toByteArray(Charsets.UTF_8)) + val presets = library.bindings.filter { it.layoutId == layout.id } + if (result.layouts.any { it.id == id } || result.layouts.size >= 256 || result.bindings.size + presets.size > 512) { + result = result.copy(layouts = result.layouts.map { if (it.id == layout.id) layout else it }).withLayout(layout.copy(kitless = true)) + continue + } + val name = "${layout.name} (old layout)".take(80) + result = result.copy(bindings = result.bindings + presets.map { b -> + b.copy(id = "kept." + UUID.nameUUIDFromBytes((b.id + "\u0000" + id).toByteArray(Charsets.UTF_8)), name = name, layoutId = id) + }).withLayout(layout.copy(id = id, name = name, origin = null, kitless = true)) + } + val expanded = runCatching { result.validate() }.getOrNull() + return if (expanded != null && encode(expanded).length <= 2_000_000) expanded else inPlace.validate() } private fun migrationBindingId(library: InputLibrary, binding: String, layout: String): String { diff --git a/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt index 0682c43d2..0768f870f 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt @@ -56,15 +56,23 @@ object OnScreenLayout { val w = width.toFloat() val h = height.toFloat() val scale = min(w / referenceWidth, h / referenceHeight) + // A layout shown on a screen of the other orientation keeps its shape: spread by thirds, its clusters would come apart. + val turned = (w > h) != (referenceWidth > referenceHeight) + val blockX = turned && w > referenceWidth * scale * BLOCK_STRETCH + val blockY = turned && h > referenceHeight * scale * BLOCK_STRETCH return elements.map { e -> val ew = e.width * referenceWidth * scale val eh = e.height * referenceHeight * scale - val left = anchor(e.x, e.width, referenceWidth, w, scale).coerceIn(safe.left.toFloat(), max(safe.left.toFloat(), w - safe.right - ew)) - val top = anchor(e.y, e.height, referenceHeight, h, scale).coerceIn(safe.top.toFloat(), max(safe.top.toFloat(), h - safe.bottom - eh)) + val x = if (blockX) (w + safe.left - safe.right) / 2f + (e.x - .5f) * referenceWidth * scale else anchor(e.x, e.width, referenceWidth, w, scale) + val y = if (blockY) h - safe.bottom - (1f - e.y) * referenceHeight * scale else anchor(e.y, e.height, referenceHeight, h, scale) + val left = x.coerceIn(safe.left.toFloat(), max(safe.left.toFloat(), w - safe.right - ew)) + val top = y.coerceIn(safe.top.toFloat(), max(safe.top.toFloat(), h - safe.bottom - eh)) e.copy(x = left / w, y = top / h, width = ew / w, height = eh / h).bounded() } } + private const val BLOCK_STRETCH = 1.5f + private fun anchor(start: Float, size: Float, reference: Float, actual: Float, scale: Float): Float { val centre = start + size / 2f val scaled = size * reference * scale diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt index 0c297e0d5..198f929e0 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt @@ -493,8 +493,8 @@ class InputSurfaceView( override fun onDraw(canvas: Canvas) { if (hideWhenIdle && !touching) return - val cx = if (touching) originX else centreX() - val cy = if (touching) originY else height / 2f + val cx = if (touching) inParent(originX, left, (parent as? View)?.width ?: width) else centreX() + val cy = if (touching) inParent(originY, top, (parent as? View)?.height ?: height) else height / 2f val r = radius() val travel = r * KNOB_TRAVEL / reach().coerceAtLeast(1f) art.stick(canvas, cx, cy, r, kit, knobX * travel, knobY * travel, touching || clicking) @@ -532,6 +532,13 @@ class InputSurfaceView( return true } + /** Where the base is drawn for a stick centred at [at], so it stays whole inside the overlay; input still centres on the touch. */ + private fun inParent(at: Float, offset: Int, span: Int): Float { + val r = radius() + val low = r - offset; val high = span - offset - r + return if (low > high) at else at.coerceIn(low, high) + } + private fun follow(x: Float, y: Float, r: Float, force: Boolean = false) { travel = maxOf(travel, hypot(x - downX, y - downY)) var dx = x - originX 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 95f28ef13..c4548168f 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLibraryTest.kt @@ -398,6 +398,64 @@ class InputLibraryTest { assertEquals(InputLibraries.read(context), InputLibraries.read(context)) } + @Test fun anOldEditorGamepadFollowsTheKitAndIsAlsoKeptAsItWas() { + val fresh = InputLibraries.read(context) + val a = InputSurfaceElement(InputControl.A, .7f, .6f, .1f, .2f) + val key = InputSurfaceElement(InputControl.A, .4f, .1f, .08f, .15f, "jump", name = "Jump") + val gamepad = fresh.layouts.single { it.id == InputLibraries.OVERLAY }.copy(elements = listOf(a, key), aspect = 2f) + val actions = SavedInputBindings("old.actions", "Old", false, InputLibraries.OVERLAY, elements = mapOf("jump" to InputAction.Keys(listOf(57)))) + val old = JSONObject(InputLibraries.encode(fresh.withLayout(gamepad).withPreset(actions))).put("version", 14) + prefs.edit().putString("library", old.toString()).commit() + val upgraded = InputLibraries.read(context) + assertEquals(listOf(key), upgraded.layouts.single { it.id == InputLibraries.OVERLAY }.elements) + val kept = upgraded.layouts.single { it.name == "Gamepad (old layout)" } + assertEquals(listOf(a, key), kept.elements) + assertTrue(kept.kitless) + assertEquals(2f, kept.aspect) + assertTrue(upgraded.bindings.any { it.layoutId == kept.id && it.elements["jump"] == InputAction.Keys(listOf(57)) }) + assertEquals(upgraded.bindings.count { it.layoutId == InputLibraries.OVERLAY }, upgraded.bindings.count { it.layoutId == kept.id }) + assertTrue(kept.supports(InputRole.PRIMARY) && kept.editable) + assertEquals(upgraded, InputLibraries.read(context)) + } + + @Test fun anActionForAKitButtonTheOldGamepadDidNotPlaceStillLoads() { + val fresh = InputLibraries.read(context) + val a = InputSurfaceElement(InputControl.A, .7f, .6f, .1f, .2f) + val gamepad = fresh.layouts.single { it.id == InputLibraries.OVERLAY }.copy(elements = listOf(a), aspect = 2f) + val actions = SavedInputBindings("old.actions", "Old", false, InputLibraries.OVERLAY, + elements = mapOf("legacy:a" to InputAction.Keys(listOf(30)), "legacy:b" to InputAction.Keys(listOf(48)))) + prefs.edit().putString("library", JSONObject(InputLibraries.encode(fresh.withLayout(gamepad).withPreset(actions))).put("version", 14).toString()).commit() + val upgraded = InputLibraries.read(context) + val kept = upgraded.layouts.single { it.name == "Gamepad (old layout)" } + val copy = upgraded.bindings.single { it.layoutId == kept.id && it.name == kept.name && it.elements.containsKey("legacy:a") } + assertEquals(setOf("legacy:a"), copy.elements.keys) + assertEquals(actions.elements, upgraded.preset("old.actions").elements) + } + + @Test fun anOldGamepadKeepsItsPositionsInPlaceWhenTheLibraryIsFull() { + val fresh = InputLibraries.read(context) + val a = InputSurfaceElement(InputControl.A, .7f, .6f, .1f, .2f) + val gamepad = fresh.layouts.single { it.id == InputLibraries.OVERLAY }.copy(elements = listOf(a), aspect = 2f) + val filler = (fresh.layouts.size until 256).map { SavedInputLayout("filler.$it", "Filler $it", listOf(InputSurfaceElement(InputControl.B, .1f, .1f, .1f, .1f))) } + val full = filler.fold(fresh.withLayout(gamepad)) { library, layout -> library.withLayout(layout) } + assertEquals(256, full.layouts.size) + prefs.edit().putString("library", JSONObject(InputLibraries.encode(full)).put("version", 14).toString()).commit() + val upgraded = InputLibraries.read(context) + val overlay = upgraded.layouts.single { it.id == InputLibraries.OVERLAY } + assertEquals(listOf(a), overlay.elements) + assertTrue(overlay.kitless) + assertEquals(256, upgraded.layouts.size) + } + + @Test fun aGamepadWithOnlyAddedKeysIsNotKeptTwice() { + val fresh = InputLibraries.read(context) + val key = InputSurfaceElement(InputControl.A, .4f, .1f, .08f, .15f, "jump", name = "Jump") + val old = JSONObject(InputLibraries.encode(fresh.withLayout(fresh.layouts.single { it.id == InputLibraries.OVERLAY }.copy(elements = listOf(key), aspect = 2f)))) + .put("version", 14) + prefs.edit().putString("library", old.toString()).commit() + assertTrue(InputLibraries.read(context).layouts.none { it.name.endsWith("(old layout)") }) + } + @Test fun versionTenStockPanelsReturnToGrayOnce() { val fresh = InputLibraries.read(context) val blue = fresh.layouts.filter { it.id == InputLibraries.DECK || it.id == InputLibraries.TRACKPAD } diff --git a/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt index 50b60fa05..20a875433 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt @@ -128,6 +128,24 @@ class OnScreenLayoutTest { private fun round(v: Float) = Math.round(v * 10000f) / 10000f + @Test fun aNarrowLandscapeLayoutOnAWideLandscapeScreenKeepsItsEdgeAnchoring() { + val right = InputSurfaceElement(InputControl.A, .9f, .4f, .06f, .08f, "a") + val layout = SavedInputLayout("edited", "Edited", listOf(right), aspect = 4f / 3f) + val (x, _, w, _) = px(OnScreenLayout.elements(context, layout, 2400, 1029, Rect()).single(), 2400, 1029).toList() + assertEquals(2400f - .04f * 1029f * 4f / 3f, x + w, 1f) + } + + @Test fun aLandscapeLayoutShownInPortraitKeepsItsClustersTogetherAtTheBottom() { + val y = InputSurfaceElement(InputControl.Y, .8f, .25f, .08f, .16f, "y") + val a = InputSurfaceElement(InputControl.A, .8f, .6f, .08f, .16f, "a") + val layout = SavedInputLayout("edited", "Edited", listOf(y, a), aspect = 2f) + val (_, yTop, _, yHeight) = px(OnScreenLayout.elements(context, layout, 1000, 2000, Rect(0, 0, 0, 40)).single { it.id == "y" }, 1000, 2000).toList() + val (_, aTop, _, _) = px(OnScreenLayout.elements(context, layout, 1000, 2000, Rect(0, 0, 0, 40)).single { it.id == "a" }, 1000, 2000).toList() + assertEquals(.35f * 500f, aTop - yTop, 1f) + assertEquals(2000f - 40f - .75f * 500f, yTop, 1f) + assertEquals(.16f * 500f, yHeight, 1f) + } + @Test fun controlsStayOutOfTheDisplayCutout() { val safe = Rect(120, 0, 0, 40) val generated = OnScreenLayout.elements(context, gamepad(), 2400, 1080, safe)