diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt index 720b5dff93..ed5920e4da 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt @@ -3,6 +3,7 @@ package com.droiddeck.launcher.input import android.content.Context import org.json.JSONArray import org.json.JSONObject +import kotlin.math.abs enum class InputElementKind(val control: InputControl) { BUTTON(InputControl.A), STICK(InputControl.LS), TRACKPAD(InputControl.LEFT_PAD), @@ -43,15 +44,24 @@ data class InputSurfaceElement( } private const val DUPLICATE_OFFSET = .04f +private const val DUPLICATE_ATTEMPTS = 25 +private val DUPLICATE_DIRECTIONS = listOf(1 to 1, -1 to -1, 1 to -1, -1 to 1) internal fun InputSurfaceElement.duplicate(id: String, siblings: List): InputSurfaceElement { val base = name.replace(Regex("\\s+\\d+$"), "") val taken = siblings.map { it.name }.toSet() - val number = generateSequence(2) { it + 1 }.first { "$base $it" !in taken } - fun shift(start: Float, size: Float) = if (start + size + DUPLICATE_OFFSET <= 1f) start + DUPLICATE_OFFSET else (start - DUPLICATE_OFFSET).coerceAtLeast(0f) - return copy(id = id, name = "$base $number", x = shift(x, width), y = shift(y, height)).bounded() + fun named(number: Int) = " $number".let { suffix -> base.take(80 - suffix.length).trimEnd() + suffix } + val number = generateSequence(2) { it + 1 }.first { named(it) !in taken } + fun place(start: Float, size: Float, steps: Int) = (start + steps * DUPLICATE_OFFSET).coerceIn(0f, (1f - size).coerceAtLeast(0f)) + fun occupied(x: Float, y: Float) = siblings.any { abs(it.x - x) < .0001f && abs(it.y - y) < .0001f && abs(it.width - width) < .0001f && abs(it.height - height) < .0001f } + val spots = (1..DUPLICATE_ATTEMPTS).flatMap { k -> DUPLICATE_DIRECTIONS.map { (dx, dy) -> place(x, width, dx * k) to place(y, height, dy * k) } } + val (px, py) = spots.firstOrNull { (cx, cy) -> !occupied(cx, cy) } ?: spots.first() + return copy(id = id, control = kind.control, name = named(number), x = px, y = py).bounded() } +internal fun InputAction.carriedTo(source: InputSurfaceElement): InputAction = + if (this is InputAction.Native && target == null && source.id.startsWith("legacy:")) InputAction.Native(source.control) else this + internal val QUICK_BUTTONS = listOf(InputCommand.STEAM, InputCommand.QUICK_ACCESS, InputCommand.KEYBOARD) val InputSurfaceElement.quickButton: InputCommand? get() = when { 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 7b6810b922..e633638787 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt @@ -110,7 +110,7 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB fun duplicate(e: InputSurfaceElement) { val copy = e.duplicate(newInputId(), elements) checkpoint() - actions = actions + (copy.id to action(e)) + actions = actions + (copy.id to action(e).carriedTo(e)) if (e.hasClick) clicks = clicks + (copy.id to clickAction(e)) elements = elements + copy selected = copy.id diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt index 9ae459c504..70f77e753e 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt @@ -216,4 +216,45 @@ class InputLayoutToolsTest { } } } + + @Test fun duplicatingLegacyElementsKeepsTheirBehaviourAndSavesValidly() { + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.SECONDARY)!!.let { it.copy(elements = InputSurfaceLayout.defaults().filter { e -> e.control in listOf(InputControl.B, InputControl.RIGHT_PAD) }) } + val base = library.preset(library.profile("game").binding(InputRole.SECONDARY)) + for (source in layout.elements) { + val copy = source.duplicate("copy", layout.elements) + assertEquals(copy.kind.control, copy.control) + val actions = mapOf(copy.id to base.action(source).carriedTo(source)) + val clicks = if (source.hasClick) mapOf(copy.id to base.clickAction(source)) else emptyMap() + val preset = base.copy(elements = actions, clicks = clicks) + library.withLayout(layout.copy(elements = layout.elements + copy)).withPreset(preset).validate() + assertEquals(InputAction.Native(source.control), preset.action(copy)) + if (source.hasClick) assertEquals(base.clickAction(source), preset.clickAction(copy)) + } + } + + @Test fun duplicateNamesNeverExceedTheNameLimit() { + for (length in 70..80) { + val source = InputSurfaceElement(InputControl.A, .1f, .1f, .1f, .1f, "a", name = "x".repeat(length)) + var siblings = listOf(source) + repeat(12) { + val copy = source.duplicate("c$it", siblings) + assertTrue("$length: ${copy.name.length}", copy.name.length <= 80) + assertTrue(copy.name.trim() == copy.name) + assertTrue(siblings.none { s -> s.name == copy.name }) + siblings = siblings + copy + } + } + } + + @Test fun repeatedDuplicationAtTheCornerNeverStacksElements() { + val source = InputSurfaceElement(InputControl.A, .75f, .75f, .25f, .25f, "a", name = "Jump") + var all = listOf(source) + repeat(10) { + val copy = all.last().duplicate("c$it", all) + assertEquals(copy, copy.bounded()) + assertTrue(all.none { e -> e.x == copy.x && e.y == copy.y }) + all = all + copy + } + } }