diff --git a/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt b/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt index 87f4e9d978..4994636e3c 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt @@ -10,6 +10,8 @@ import java.net.URLEncoder object WinlatorLayouts { private const val BASE = "https://raw.githubusercontent.com/brunodev85/winlator/main/input_controls/" const val INDEX_URL = BASE + "index.txt" + /** The smallest control side, as [InputSurfaceElement.bounded] allows. */ + private const val MIN_SIZE = .03f fun fileUrl(name: String) = BASE + URLEncoder.encode(name, "UTF-8").replace("+", "%20") @@ -105,16 +107,20 @@ object WinlatorLayouts { val aspect = (width.coerceAtLeast(1).toFloat() / height.coerceAtLeast(1)).coerceIn(.05f, 20f) val elements = mutableListOf() val actions = mutableMapOf() - fun place(kind: InputElementKind, name: String, cx: Float, cy: Float, halfW: Float, halfH: Float, action: InputAction, + fun placeRect(kind: InputElementKind, name: String, x: Float, y: Float, w: Float, h: Float, action: InputAction, shape: InputButtonShape = InputButtonShape.ROUNDED, toggle: Boolean = false) { // Too many controls fails the import instead of leaving some out. require(elements.size < 64) - val w = halfW * .01f; val h = halfH * .01f * aspect - val element = InputSurfaceElement(kind.control, cx - w, cy - h, 2 * w, 2 * h, newInputId(), kind, name.ifBlank { "?" }.take(80), + val element = InputSurfaceElement(kind.control, x, y, w, h, newInputId(), kind, name.ifBlank { "?" }.take(80), toggle = toggle && (kind == InputElementKind.BUTTON || kind == InputElementKind.TRIGGER), buttonShape = shape).bounded() elements += element actions[element.id] = action.takeIf { it.validFor(element) } ?: InputAction.Disabled } + fun place(kind: InputElementKind, name: String, cx: Float, cy: Float, halfW: Float, halfH: Float, action: InputAction, + shape: InputButtonShape = InputButtonShape.ROUNDED, toggle: Boolean = false) { + val w = halfW * .01f; val h = halfH * .01f * aspect + placeRect(kind, name, cx - w, cy - h, 2 * w, 2 * h, action, shape, toggle) + } for (i in 0 until minOf(source.length(), 128)) { val e = source.optJSONObject(i) ?: continue val bindings = e.optJSONArray("bindings")?.let { a -> (0 until a.length()).map { a.optString(it) } }.orEmpty() @@ -146,20 +152,21 @@ object WinlatorLayouts { val range = ranges[e.optString("range", "FROM_A_TO_Z")] ?: continue val across = bindings.size.coerceIn(1, range.size) val vertical = e.optInt("orientation", 0) == 1 - val step = 4 * scale * .01f - val centres = range.indices.map { n -> - val along = (n % across - (across - 1) / 2f) * step - val line = (n / across) * step - if (vertical) cx + line to cy + along * aspect else cx + along to cy + line * aspect - } - // The whole block moves to fit on screen, so rows never get pushed onto each other at an edge. - val halfX = step / 2; val halfY = step / 2 * aspect - fun shift(low: Float, high: Float) = when { low < 0f -> -low; high > 1f -> 1f - high; else -> 0f } - val dx = shift(centres.minOf { it.first } - halfX, centres.maxOf { it.first } + halfX) - val dy = shift(centres.minOf { it.second } - halfY, centres.maxOf { it.second } + halfY) + // Cells are never smaller than the smallest control, and a block bigger than the screen shrinks to fit it, + // so the keys never overlap; then the whole block moves onto the screen. + val lines = (range.size + across - 1) / across + val cols = if (vertical) lines else across + val rows = if (vertical) across else lines + val cellW = maxOf(4 * scale * .01f, MIN_SIZE).coerceAtMost(1f / cols) + val cellH = maxOf(4 * scale * .01f * aspect, MIN_SIZE).coerceAtMost(1f / rows) + val blockW = cols * cellW; val blockH = rows * cellH + val left = (if (vertical) cx - cellW / 2 else cx - blockW / 2).coerceIn(0f, (1f - blockW).coerceAtLeast(0f)) + val top = (if (vertical) cy - blockH / 2 else cy - cellH / 2).coerceIn(0f, (1f - blockH).coerceAtLeast(0f)) for ((n, name) in range.withIndex()) { val code = keys[name] ?: continue - place(InputElementKind.BUTTON, label("KEY_$name"), centres[n].first + dx, centres[n].second + dy, 2 * scale, 2 * scale, + val col = if (vertical) n / across else n % across + val row = if (vertical) n % across else n / across + placeRect(InputElementKind.BUTTON, label("KEY_$name"), left + col * cellW, top + row * cellH, cellW, cellH, InputAction.Keys(listOf(code)), shape) } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt index b3410c463a..f473d6a607 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt @@ -110,14 +110,20 @@ class WinlatorLayoutsTest { assertEquals(InputAction.Keys(listOf(57)), actions.elements[imported.id]) } - @Test fun aRangeAtTheScreenEdgeKeepsEveryKeySeparate() { - val raw = """{"cursorSpeed":1,"elements":[{"type":"RANGE_BUTTON","shape":"CIRCLE","bindings":["NONE","NONE","NONE","NONE"],"scale":1,"x":0.5,"y":0.95,"range":"FROM_F1_TO_F12"}]}""" - val (layout, _) = WinlatorLayouts.import(raw, "x", InputLayoutScreen.MAIN, 2400, 1080)!! - assertEquals(12, layout.elements.size) - assertTrue(layout.elements.all { it.y >= 0f && it.y + it.height <= 1.0001f }) - for (a in layout.elements) for (b in layout.elements) if (a !== b) { - val overlap = a.x < b.x + b.width - 1e-4f && b.x < a.x + a.width - 1e-4f && a.y < b.y + b.height - 1e-4f && b.y < a.y + a.height - 1e-4f - assertFalse("${a.name} overlaps ${b.name}", overlap) + @Test fun aRangeKeepsEveryKeySeparateAndOnScreenAtAnyScaleOrPlace() { + for ((scale, y, orientation, range) in listOf( + listOf(1, .95, 0, "FROM_F1_TO_F12"), listOf(.5, .5, 0, "FROM_F1_TO_F12"), listOf(5, .5, 0, "FROM_F1_TO_F12"), + listOf(5, .9, 1, "FROM_A_TO_Z"), listOf(.3, .02, 1, "FROM_0_TO_9"))) { + val raw = """{"cursorSpeed":1,"elements":[{"type":"RANGE_BUTTON","shape":"CIRCLE","bindings":["NONE","NONE","NONE","NONE"], + "scale":$scale,"x":0.98,"y":$y,"orientation":$orientation,"range":"$range"}]}""" + val (layout, _) = WinlatorLayouts.import(raw, "x", InputLayoutScreen.MAIN, 2400, 1080)!! + val case = "$scale $y $orientation $range" + assertEquals(case, if (range == "FROM_A_TO_Z") 26 else if (range == "FROM_0_TO_9") 10 else 12, layout.elements.size) + assertTrue(case, layout.elements.all { it.x >= 0f && it.y >= 0f && it.x + it.width <= 1.0001f && it.y + it.height <= 1.0001f }) + for (a in layout.elements) for (b in layout.elements) if (a !== b) { + val overlap = a.x < b.x + b.width - 1e-4f && b.x < a.x + a.width - 1e-4f && a.y < b.y + b.height - 1e-4f && b.y < a.y + a.height - 1e-4f + assertFalse("$case: ${a.name} overlaps ${b.name}", overlap) + } } }