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 33d951a62..ad5d8e270 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt @@ -31,17 +31,20 @@ object OnScreenLayout { private const val TOLERANCE = .01f private const val KEYBOARD_ID = "builtin:keyboard" private val REAR = setOf(InputControl.L4, InputControl.L5, InputControl.R4, InputControl.R5) + private val BAR = setOf(InputControl.GUIDE, InputControl.QAM) fun elements(context: Context, saved: SavedInputLayout?, width: Int, height: Int, safe: Rect, buttonsOnly: Boolean = false, dock: Dock? = null, rearButtons: Boolean? = null, trackpads: Boolean? = null): List { if (saved == null) return emptyList() if (width <= 0 || height <= 0) return saved.elements - val layout = oriented(saved, width, height) + val layout = oriented(saved, dock?.portrait ?: (height > width)) val stored = layout.elements val generator = LegacyGamepadLayout(context, width, height, safe, layout.scale) val (reference, matched) = reference(context, layout, width, height) if (buttonsOnly && dock == null) return generator.buttonsOnly(stored) + fit(stored.filter { it.alwaysShown }, reference.first, reference.second, width, height, safe) + if (dock?.portrait == true && !dock.grips && !matched && layout.aspect > 0f && layout.aspect < 1f && stored.any(::isGamepad)) + return cluster(stored.filterNot { it.alwaysShown || isGamepad(it) && (it.id == KEYBOARD_ID || it.control in BAR) }, layout.aspect, width, height, safe) val generated = matched || PadKit.forOverlay(context, layout) != null && stored.any(::isGamepad) && (dock != null || height > width && layout.aspect >= 1f || height <= width && layout.aspect > 0f && layout.aspect < 1f) val gamepad = if (generated) stored.filter(::isGamepad) else emptyList() @@ -70,8 +73,9 @@ object OnScreenLayout { .place(ordered, lowerShortcuts = generated, rearButtons = rearButtons, trackpads = trackpads) } - fun oriented(layout: SavedInputLayout, width: Int, height: Int): SavedInputLayout { - val portrait = height > width + fun oriented(layout: SavedInputLayout, width: Int, height: Int): SavedInputLayout = oriented(layout, height > width) + + private fun oriented(layout: SavedInputLayout, portrait: Boolean): SavedInputLayout { if (layout.rotated.isEmpty() || layout.aspect <= 0f || (layout.aspect < 1f) == portrait || (layout.rotatedAspect < 1f) != portrait) return layout val rotated = layout.rotated.associateBy { it.id } return layout.copy(elements = layout.elements.map { e -> rotated[e.id]?.let { e.copy(x = it.x, y = it.y, width = it.width, height = it.height) } ?: e } + @@ -131,6 +135,23 @@ object OnScreenLayout { } } + private fun cluster(elements: List, aspect: Float, width: Int, height: Int, safe: Rect): List { + if (elements.isEmpty()) return elements + val left = elements.minOf { it.x } * aspect + val top = elements.minOf { it.y } + val spanX = (elements.maxOf { it.x + it.width } * aspect - left).coerceAtLeast(.0001f) + val spanY = (elements.maxOf { it.y + it.height } - top).coerceAtLeast(.0001f) + val w = (width - safe.left - safe.right).toFloat().coerceAtLeast(1f) + val h = (height - safe.top - safe.bottom).toFloat().coerceAtLeast(1f) + val scale = minOf(w / aspect, w / spanX, h / spanY) + val originX = safe.left + (w - spanX * scale) / 2f - left * scale + val originY = safe.top + (h - spanY * scale) / 2f - top * scale + return elements.map { e -> + e.copy(x = (originX + e.x * aspect * scale) / width, y = (originY + e.y * scale) / height, + width = e.width * aspect * scale / width, height = e.height * scale / height) + } + } + 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/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt index d465b87a6..1fa557b6d 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt @@ -307,6 +307,51 @@ class OnScreenLayoutTest { assertEquals(.1f * 2208f / 1768 / .2f, fitted.width * 2208 / (fitted.height * 800), .01f) } + @Test fun controlsEditedInPortraitKeepTheirPlacesInThePanelBelowTheGame() { + SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_NONE) + val (w, h) = 1080 to 2400 + val stock = gamepad() + val shown = OnScreenLayout.editorElements(context, stock, w, h) + val moved = shown.map { if (it.id == "legacy:a") it.copy(x = .05f) else if (it.id == "legacy:ls") it.copy(x = .6f) else it } + val edited = OnScreenLayout.withFrame(context, stock, OnScreenLayout.editorFrame(stock, moved), w.toFloat() / h) + val shownA = shown.first { it.id == "legacy:a" } + for (panelHeight in listOf(1100, 800)) { + val placed = OnScreenLayout.elements(context, edited, w, panelHeight, Rect(), dock = OnScreenLayout.Dock(true, false)) + val a = placed.first { it.id == "legacy:a" } + val ls = placed.first { it.id == "legacy:ls" } + val b = placed.first { it.id == "legacy:b" } + assertTrue(a.x < .2f) + assertTrue(ls.x > .5f) + assertTrue(b.x > a.x) + assertNull(placed.firstOrNull { it.id == "builtin:keyboard" || it.control == InputControl.GUIDE || it.control == InputControl.QAM }) + assertInside(placed, w, panelHeight) + assertEquals(shownA.width * w / (shownA.height * h), a.width * w / (a.height * panelHeight), .01f) + val untouched = OnScreenLayout.elements(context, stock, w, panelHeight, Rect(), dock = OnScreenLayout.Dock(true, false)) + assertTrue(untouched.first { it.id == "legacy:a" }.x > .5f) + assertTrue(untouched.first { it.id == "legacy:ls" }.x < .5f) + } + val landscape = OnScreenLayout.elements(context, edited, 2400, 1080, Rect()) + assertTrue(landscape.first { it.id == "legacy:a" }.x > .5f) + SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_DECK) + val deck = OnScreenLayout.elements(context, edited, w, 1100, Rect(), dock = OnScreenLayout.Dock.deck(true, null), trackpads = true) + assertTrue(deck.none { it.id == "legacy:rs" }) + assertTrue(deck.any { it.kind == InputElementKind.TRACKPAD && it.x + it.width / 2f > .5f }) + SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_NONE) + val tiny = stock.copy(aspect = .45f, elements = stock.elements.map { + when (it.id) { + "legacy:a" -> it.copy(x = .05f, y = .05f, width = .1f, height = .045f) + "legacy:b" -> it.copy(x = .16f, y = .05f, width = .1f, height = .045f) + "legacy:ls" -> it.copy(x = .8f, y = .85f, width = .15f, height = .1f) + else -> it + } + }, rotated = emptyList()) + val short = OnScreenLayout.elements(context, tiny, w, 385, Rect(), dock = OnScreenLayout.Dock(true, false)) + val sa = short.first { it.id == "legacy:a" } + val sb = short.first { it.id == "legacy:b" } + assertTrue(sa.x + sa.width <= sb.x) + assertEquals(1f, sa.width * w / (sa.height * 385), .01f) + } + @Test fun thePortraitAndBelowGameKitsKeepDirectionalArtClearOfTheSticksAndFaceButtons() { val directions = setOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) val face = setOf(InputControl.A, InputControl.B, InputControl.X, InputControl.Y)