diff --git a/app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt b/app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt index 988ab5f76..e8e16d712 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/GamepadGeometry.kt @@ -26,10 +26,10 @@ object GamepadGeometry { fun tall(w: Float, h: Float): Boolean = h >= w * 0.45f - fun layout(w: Float, h: Float, density: Float, portraitDock: Boolean = false): Map { + fun layout(w: Float, h: Float, density: Float, portraitDock: Boolean = false, grips: Boolean = true): Map { if (w <= 0f || h <= 0f) return emptyMap() - if (portraitDock && h >= w * 0.9f && w >= 300 * density && h >= 360 * density) { - return portraitLayout(w, h, density) + if (portraitDock && h >= w * 0.6f && w >= 300 * density && h >= 300 * density) { + return portraitLayout(w, h, density, grips) } val pad = 8 * density val buttonH = 40 * density @@ -92,38 +92,36 @@ object GamepadGeometry { return boxes } - private fun portraitLayout(w: Float, h: Float, density: Float): Map { + /** + * The panel under a portrait game: shoulders with View and Menu across the top, the back grips under them when the kit has + * them, then the d-pad and face buttons and, along the bottom edge, the sticks or trackpads, each as large as the panel allows. + */ + private fun portraitLayout(w: Float, h: Float, density: Float, grips: Boolean): Map { val pad = 8 * density val gap = 8 * density val buttonH = 44 * density - val scale = min(1f, (h - 2 * pad - 4 * gap - 3 * buttonH) / (w * 0.7f)) - val main = w * 0.4f * scale - val second = w * 0.3f * scale + val boxes = LinkedHashMap() + val topGap = 4 * density + val topW = (w - 2 * pad - 5 * topGap) / 6f + val top = listOf(Slot.LEFT_OUTER_SHOULDER, Slot.LEFT_INNER_SHOULDER, Slot.VIEW, Slot.MENU, Slot.RIGHT_INNER_SHOULDER, Slot.RIGHT_OUTER_SHOULDER) + for ((i, slot) in top.withIndex()) boxes[slot] = Box(pad + topW / 2f + i * (topW + topGap), pad + buttonH / 2f, topW, buttonH) + var areaTop = pad + buttonH + gap + if (grips) { + val gripW = (w - 2 * pad - 3 * gap) / 4f + val gripH = min(56 * density, (h - areaTop) * 0.12f).coerceAtLeast(buttonH) + for ((i, slot) in listOf(Slot.L4, Slot.L5, Slot.R5, Slot.R4).withIndex()) + boxes[slot] = Box(pad + gripW / 2f + i * (gripW + gap), areaTop + gripH / 2f, gripW, gripH) + areaTop += gripH + gap + } + val area = h - pad - areaTop - gap + val main = min(w * 0.46f, area * 0.53f) + val second = min(w * 0.42f, area - main) + val secondY = areaTop + second / 2f val mainY = h - pad - main / 2f - val secondY = mainY - main / 2f - gap - second / 2f - val shoulderY = secondY - second / 2f - gap - buttonH / 2f - val boxes = linkedMapOf( - Slot.LEFT_MAIN to Box(w * 0.25f, mainY, main, main), - Slot.RIGHT_MAIN to Box(w * 0.75f, mainY, main, main), - Slot.LEFT_SECOND to Box(w * 0.25f, secondY, second, second), - Slot.RIGHT_SECOND to Box(w * 0.75f, secondY, second, second), - ) - val utilityW = (w - 2 * pad - 3 * gap) / 4f - for ((i, slot) in middle.withIndex()) { - boxes[slot] = Box(pad + utilityW / 2f + i * (utilityW + gap), pad + buttonH / 2f, utilityW, buttonH) - } - val gripTop = pad + buttonH + gap - val gripH = min(64 * density, shoulderY - buttonH / 2f - gap - gripTop) - for ((i, slot) in listOf(Slot.L4, Slot.L5, Slot.R5, Slot.R4).withIndex()) { - boxes[slot] = Box(pad + utilityW / 2f + i * (utilityW + gap), gripTop + gripH / 2f, utilityW, gripH) - } - val panelsW = min(112 * density, w * 0.22f) - val shoulderW = (w - 2 * pad - 4 * gap - panelsW) / 4f - boxes[Slot.PANELS] = Box(w / 2f, shoulderY, panelsW, buttonH) - boxes[Slot.LEFT_OUTER_SHOULDER] = Box(pad + shoulderW / 2f, shoulderY, shoulderW, buttonH) - boxes[Slot.LEFT_INNER_SHOULDER] = Box(pad + shoulderW * 1.5f + gap, shoulderY, shoulderW, buttonH) - boxes[Slot.RIGHT_INNER_SHOULDER] = Box(w - pad - shoulderW * 1.5f - gap, shoulderY, shoulderW, buttonH) - boxes[Slot.RIGHT_OUTER_SHOULDER] = Box(w - pad - shoulderW / 2f, shoulderY, shoulderW, buttonH) + boxes[Slot.LEFT_SECOND] = Box(w * 0.25f, secondY, second, second) + boxes[Slot.RIGHT_SECOND] = Box(w * 0.75f, secondY, second, second) + boxes[Slot.LEFT_MAIN] = Box(w * 0.25f, mainY, main, main) + boxes[Slot.RIGHT_MAIN] = Box(w * 0.75f, mainY, main, main) return boxes } } diff --git a/app/src/main/java/com/droiddeck/launcher/input/KitLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/KitLayout.kt index 14e9d45af..e4d436a7b 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/KitLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/KitLayout.kt @@ -8,8 +8,8 @@ import kotlin.math.min /** * Places a virtual controller's default on-screen controls. Each kit is drawn on a 1600 × 900 reference screen; controls keep * their distance from the nearest edge, so wider screens spread them apart instead of stretching them. Clusters such as the face - * buttons are placed by their centre so they move as one. In portrait the whole pad sits along the bottom edge, and the row left of - * the bottom edge always starts after DroidDeck's corner button. + * buttons are placed by their centre so they move as one, and the row along the bottom-left edge always starts after DroidDeck's + * corner button. */ object KitLayout { const val DPAD_ID = "kit:dpad" @@ -19,11 +19,12 @@ object KitLayout { fun isKit(id: String) = id.startsWith("legacy:") || id == DPAD_ID || id == KEYBOARD_ID private const val REF_W = 1600f private const val REF_H = 900f - private const val PORTRAIT_W = 1200f /** The widest pair of side controls on any kit: a larger size never pushes the two halves, or the rows, into each other. */ private const val MIN_SPAN = 1200f private const val MAX_HEIGHT_MM = 80f private const val CORNER_DP = 52f + private const val ROW_DP = 60f + private const val BUTTON_DP = 44f private const val GAP_DP = 8f private class Spec(val control: InputControl, val cx: Float, val cy: Float, val hw: Float, val hh: Float = hw, val id: String = "legacy:${control.id}", @@ -47,7 +48,7 @@ object KitLayout { kit.steam -> shoulders() + listOf( Spec(InputControl.L4, 30f, 470f, 30f, 58f, shape = InputButtonShape.ROUNDED), Spec(InputControl.L5, 30f, 590f, 30f, 58f, shape = InputButtonShape.ROUNDED), Spec(InputControl.R4, 1570f, 470f, 30f, 58f, shape = InputButtonShape.ROUNDED), Spec(InputControl.R5, 1570f, 590f, 30f, 58f, shape = InputButtonShape.ROUNDED), - dpad(215f, 330f, 100f)) + face(1385f, 330f, 50f, 82f) + listOf( + dpad(215f, 330f, 112f)) + face(1385f, 330f, 56f, 92f) + listOf( Spec(InputControl.LS, 290f, 610f, 96f), Spec(InputControl.RS, 1310f, 610f, 96f), Spec(InputControl.LEFT_PAD, 480f, 345f, 95f, shape = InputButtonShape.ROUNDED), Spec(InputControl.RIGHT_PAD, 1120f, 345f, 95f, shape = InputButtonShape.ROUNDED), Spec(InputControl.SELECT, 660f, 150f, 46f, 26f, shape = InputButtonShape.PILL), Spec(InputControl.START, 940f, 150f, 46f, 26f, shape = InputButtonShape.PILL), @@ -67,16 +68,15 @@ object KitLayout { /** The default controls for [kit] on a [width] × [height] screen. */ fun elements(context: Context, kit: PadKit, width: Int, height: Int, safe: Rect = Rect(), scale: Float = 1f): List { if (width <= 0 || height <= 0) return emptyList() + if (height > width) return portrait(context, kit, width, height, safe, scale) val w = width.toFloat(); val h = height.toFloat() - val portrait = height > width - val unit = (min(w / if (portrait) PORTRAIT_W else REF_W, h / REF_H) * cap(context, min(width, height)) * scale).coerceAtMost(min(w / MIN_SPAN, h / REF_H)) + val unit = (min(w / REF_W, h / REF_H) * cap(context, min(width, height)) * scale).coerceAtMost(min(w / MIN_SPAN, h / REF_H)) fun x(ref: Float) = when { ref < REF_W / 3f -> safe.left + ref * unit ref > REF_W * 2f / 3f -> w - safe.right - (REF_W - ref) * unit else -> w / 2f + (ref - REF_W / 2f) * unit } fun y(ref: Float) = when { - portrait -> h - safe.bottom - (REF_H - ref) * unit ref < REF_H / 3f -> safe.top + ref * unit ref > REF_H * 2f / 3f -> h - safe.bottom - (REF_H - ref) * unit else -> h / 2f + (ref - REF_H / 2f) * unit @@ -95,10 +95,49 @@ object KitLayout { } } + /** + * A portrait surface gets the same pad as the panel under the game, in the lower part of the screen, with the keyboard, + * Steam, Quick Access and stick-click buttons in a row along the bottom edge that starts after DroidDeck's corner button. + */ + private fun portrait(context: Context, kit: PadKit, width: Int, height: Int, safe: Rect, scale: Float): List { + val w = width.toFloat(); val h = height.toFloat() + val density = context.resources.displayMetrics.density + val row = ROW_DP * density + val inner = (w - safe.left - safe.right).coerceAtLeast(1f) + val area = (min(h * .6f, inner * 1.15f) * scale).coerceAtMost(h - safe.top - safe.bottom - row).coerceAtLeast(1f) + val top = h - safe.bottom - row - area + val pad = dock(context, kit, inner.toInt(), area.toInt(), true, InputControl.LS, if (kit.steam) InputControl.RIGHT_PAD else InputControl.RS).map { e -> + e.copy(x = (safe.left + e.x * inner) / w, y = (top + e.y * area) / h, width = e.width * inner / w, height = e.height * area / h, + floating = e.kind == InputElementKind.STICK) + } + val specs = specs(kit) + fun spec(control: InputControl, id: String = "legacy:${control.id}") = specs.firstOrNull { it.control == control && it.id == id } + val size = BUTTON_DP * density + val cy = h - safe.bottom - row / 2f + val placed = mutableListOf() + fun put(s: Spec, cx: Float) { + val bw = size * s.hw / s.hh + placed += InputSurfaceElement(s.control, (cx - bw / 2) / w, (cy - size / 2) / h, bw / w, size / h, s.id, s.kind, + if (s.name == s.control.label) kit.name(s.control) else s.name, buttonShape = s.shape, icon = s.icon) + } + val gap = GAP_DP * density + var left = safe.left + CORNER_DP * density + for (s in listOfNotNull(spec(InputControl.A, KEYBOARD_ID), spec(InputControl.GUIDE).takeIf { kit.steam }, spec(InputControl.L3))) { + val bw = size * s.hw / s.hh + put(s, left + bw / 2); left += bw + gap + } + var right = w - safe.right - gap + for (s in listOfNotNull(spec(InputControl.QAM), spec(InputControl.GUIDE).takeIf { !kit.steam }, spec(InputControl.R3))) { + val bw = size * s.hw / s.hh + put(s, right - bw / 2); right -= bw + gap + } + return pad + placed + } + /** Controls for the panel below the game, in [GamepadGeometry]'s slots. */ fun dock(context: Context, kit: PadKit, width: Int, height: Int, portrait: Boolean, left: InputControl, right: InputControl): List { val w = width.toFloat(); val h = height.toFloat() - val boxes = GamepadGeometry.layout(w, h, context.resources.displayMetrics.density, portrait) + val boxes = GamepadGeometry.layout(w, h, context.resources.displayMetrics.density, portrait, grips = kit.steam) val placed = mutableListOf() fun put(control: InputControl, box: GamepadGeometry.Box?, shape: InputButtonShape, id: String = "legacy:${control.id}", kind: InputElementKind = InputElementKind.from(control), name: String = kit.name(control)) { @@ -106,9 +145,11 @@ object KitLayout { placed += InputSurfaceElement(control, b.left / w, b.top / h, b.width / w, b.height / h, id, kind, name, floating = false, buttonShape = shape).bounded() } fun square(box: GamepadGeometry.Box?) = box?.let { val side = min(it.width, it.height); GamepadGeometry.Box(it.cx, it.cy, side, side) } - put(left, square(boxes[GamepadGeometry.Slot.LEFT_MAIN]), InputButtonShape.CIRCLE) + val leftMain = if (kit.steam || left != InputControl.LEFT_PAD) left else InputControl.LS + val rightMain = if (kit.steam || right != InputControl.RIGHT_PAD) right else InputControl.RS + put(leftMain, square(boxes[GamepadGeometry.Slot.LEFT_MAIN]), InputButtonShape.CIRCLE) put(InputControl.LS, square(boxes[GamepadGeometry.Slot.LEFT_SECOND]), InputButtonShape.ROUNDED, DPAD_ID, InputElementKind.DPAD, "D-pad") - put(right, square(boxes[GamepadGeometry.Slot.RIGHT_MAIN]), InputButtonShape.CIRCLE) + put(rightMain, square(boxes[GamepadGeometry.Slot.RIGHT_MAIN]), InputButtonShape.CIRCLE) square(boxes[GamepadGeometry.Slot.RIGHT_SECOND])?.let { b -> val r = b.width * .17f; val off = b.width * .3f listOf(InputControl.Y to (0f to -1f), InputControl.B to (1f to 0f), InputControl.A to (0f to 1f), InputControl.X to (-1f to 0f)).forEach { (c, d) -> diff --git a/app/src/test/java/com/droiddeck/launcher/input/KitLayoutTest.kt b/app/src/test/java/com/droiddeck/launcher/input/KitLayoutTest.kt index 373eea937..35bf1f28b 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/KitLayoutTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/KitLayoutTest.kt @@ -176,8 +176,9 @@ class KitLayoutTest { val face = listOf("legacy:y", "legacy:x", "legacy:b", "legacy:a").map { centre(e.getValue(it), w, h) } val cx = face.map { it.first }.average().toFloat(); val cy = face.map { it.second }.average().toFloat() val radius = e.getValue("legacy:a").height * h / 2 - for ((x, y) in face) assertEquals("$kit $w x $h", radius * ratio.getValue(kit), hypot(x - cx, y - cy), 1f) - if (kit == PadKit.XBOX) { + val spread = if (h > w) .3f / .17f else ratio.getValue(kit) + for ((x, y) in face) assertEquals("$kit $w x $h", radius * spread, hypot(x - cx, y - cy), 1f) + if (kit == PadKit.XBOX && h <= w) { val (lx, ly) = centre(e.getValue("legacy:ls"), w, h); val (cx3, cy3) = centre(e.getValue("legacy:l3"), w, h) val unit = e.getValue("legacy:ls").height * h / 208f assertEquals("$w x $h", hypot(150f, 110f) * unit, hypot(cx3 - lx, cy3 - ly), 1f) @@ -207,18 +208,19 @@ class KitLayoutTest { } } - @Test fun portraitSurfacesKeepEveryControlInTheBottomBlock() { - for (kit in PadKit.entries) for ((w, h) in listOf(1080 to 1920, 1200 to 2000)) { + @Test fun portraitSurfacesPutTheWholePadInTheLowerPartAboveABottomRow() { + val density = context.resources.displayMetrics.density + for (kit in PadKit.entries) for ((w, h) in listOf(1080 to 1920, 1200 to 2000, 1080 to 2400)) { val elements = KitLayout.elements(context, kit, w, h) - val unit = w / 1200f - val top = elements.minOf { it.y } * h - assertTrue("$kit $w x $h top $top", top >= h - 900 * unit - .5f) - assertEquals("$kit $w x $h", h.toFloat(), elements.maxOf { (it.y + it.height) * h }, 900 * unit * .1f) - val tall = KitLayout.elements(context, kit, w, h + 100).associateBy { it.id } - for (e in elements.filter { it.height > .031f && tall.getValue(it.id).height > .031f }) { - val t = tall.getValue(e.id) - assertEquals("$kit ${e.id}", (1 - e.y - e.height / 2) * h, (1 - t.y - t.height / 2) * (h + 100), 1f) - } + val row = 60 * density + val area = minOf(h * .6f, w * 1.15f) + assertTrue("$kit $w x $h", elements.minOf { it.y } * h >= h - row - area - .5f) + assertTrue("$kit $w x $h", elements.maxOf { (it.y + it.height) * h } <= h + .5f) + val bottomRow = elements.filter { (it.y + it.height / 2) * h > h - row } + assertTrue("$kit $w x $h", bottomRow.any { it.id == KitLayout.KEYBOARD_ID } && bottomRow.any { it.control == InputControl.QAM }) + assertTrue("$kit $w x $h", bottomRow.minOf { it.x } * w >= 52 * density - .5f) + val stick = elements.first { it.id == "legacy:ls" } + assertTrue("$kit $w x $h", stick.width * w >= w * .3f) } } @@ -238,4 +240,12 @@ class KitLayoutTest { assertTrue(placed.none { it.floating }) } } + + @Test fun onlySteamKitsPutTrackpadsInTheDockedStrip() { + for (kit in PadKit.entries) { + val placed = KitLayout.dock(context, kit, 1080, 1300, true, InputControl.LS, InputControl.RIGHT_PAD).map { it.control } + assertEquals(kit.toString(), kit.steam, InputControl.RIGHT_PAD in placed) + assertEquals(kit.toString(), !kit.steam, InputControl.RS in placed) + } + } } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt index dbb153bce..360af6f63 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/GamepadGeometryTest.kt @@ -94,7 +94,7 @@ class GamepadGeometryTest { @Test fun portraitDockPlacesAllControlsWithinThePanel() { for (panel in portraitPanels) { val b = portraitBoxes(panel) - assertEquals(panel.name, Slot.values().toSet(), b.keys) + assertEquals(panel.name, Slot.values().toSet() - setOf(Slot.STEAM, Slot.QAM, Slot.PANELS), b.keys) for ((slot, box) in b) { val where = "${panel.name}: $slot $box" assertTrue(where, box.left >= 0f && box.top >= 0f) @@ -126,16 +126,35 @@ class GamepadGeometryTest { } } - @Test fun portraitDockMovesUtilitiesAndGripsAboveTheGamepad() { + @Test fun portraitDockPutsShouldersAndMenusInOneRowAboveTheGripsAndTheGamepad() { for (panel in portraitPanels) { val b = portraitBoxes(panel) - assertEquals(panel.name, 8 * panel.density, b.getValue(Slot.VIEW).top, 0.5f) + val row = listOf(Slot.LEFT_OUTER_SHOULDER, Slot.LEFT_INNER_SHOULDER, Slot.VIEW, Slot.MENU, Slot.RIGHT_INNER_SHOULDER, Slot.RIGHT_OUTER_SHOULDER) + for (slot in row) assertEquals("${panel.name}: $slot", 8 * panel.density, b.getValue(slot).top, 0.5f) + assertEquals(panel.name, row, row.sortedBy { b.getValue(it).cx }) assertTrue(panel.name, b.getValue(Slot.MENU).bottom < b.getValue(Slot.L4).top) - assertTrue(panel.name, b.getValue(Slot.L4).bottom < b.getValue(Slot.LEFT_OUTER_SHOULDER).top) - assertTrue(panel.name, b.getValue(Slot.LEFT_OUTER_SHOULDER).bottom < b.getValue(Slot.LEFT_SECOND).top) + assertTrue(panel.name, b.getValue(Slot.L4).bottom < b.getValue(Slot.LEFT_SECOND).top) + assertTrue(panel.name, b.getValue(Slot.LEFT_SECOND).bottom < b.getValue(Slot.LEFT_MAIN).top) } } + @Test fun aPortraitDockWithoutGripsGivesTheirRowToTheGamepad() { + for (panel in portraitPanels) { + val with = portraitBoxes(panel) + val without = GamepadGeometry.layout(panel.width.toFloat(), panel.controlsHeight.toFloat(), panel.density, portraitDock = true, grips = false) + assertTrue(panel.name, grips.none { it in without }) + assertTrue(panel.name, without.getValue(Slot.LEFT_SECOND).top < with.getValue(Slot.LEFT_SECOND).top) + assertTrue(panel.name, without.getValue(Slot.LEFT_SECOND).width >= with.getValue(Slot.LEFT_SECOND).width) + } + } + + @Test fun aShorterPortraitDockStillGetsTheUprightLayout() { + val (w, h, density) = Triple(1080f, 1010f, 369 / 160f) + val b = GamepadGeometry.layout(w, h, density, portraitDock = true, grips = false) + assertEquals(8 * density, b.getValue(Slot.VIEW).top, 0.5f) + assertTrue(b.getValue(Slot.RIGHT_SECOND).width >= w * 0.3f) + } + @Test fun shortPanelsKeepTheExistingLayoutEvenWhenPortraitDockIsRequested() { for ((width, height, density) in listOf(Triple(1920f, 1080f, 2f), Triple(720f, 600f, 2.3f))) { assertEquals(GamepadGeometry.layout(width, height, density),