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 6fb2f3d570..87f4e9d978 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/WinlatorLayouts.kt @@ -107,7 +107,8 @@ object WinlatorLayouts { val actions = mutableMapOf() fun place(kind: InputElementKind, name: String, cx: Float, cy: Float, halfW: Float, halfH: Float, action: InputAction, shape: InputButtonShape = InputButtonShape.ROUNDED, toggle: Boolean = false) { - if (elements.size >= 64) return + // 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), toggle = toggle && (kind == InputElementKind.BUTTON || kind == InputElementKind.TRIGGER), buttonShape = shape).bounded() @@ -146,13 +147,20 @@ object WinlatorLayouts { val across = bindings.size.coerceIn(1, range.size) val vertical = e.optInt("orientation", 0) == 1 val step = 4 * scale * .01f - for ((n, name) in range.withIndex()) { - val code = keys[name] ?: continue + val centres = range.indices.map { n -> val along = (n % across - (across - 1) / 2f) * step val line = (n / across) * step - val x = if (vertical) cx + line else cx + along - val y = if (vertical) cy + along * aspect else cy + line * aspect - place(InputElementKind.BUTTON, label("KEY_$name"), x, y, 2 * scale, 2 * scale, InputAction.Keys(listOf(code)), shape) + 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) + 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, + 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 ca96e9eb33..b3410c463a 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/WinlatorLayoutsTest.kt @@ -110,6 +110,23 @@ 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 aFileNeedingMoreThan64ControlsFailsInsteadOfDroppingSome() { + val buttons = (0 until 60).joinToString(",") { """{"type":"BUTTON","shape":"CIRCLE","bindings":["KEY_A"],"scale":0.5,"x":0.5,"y":0.5}""" } + val raw = """{"cursorSpeed":1,"elements":[$buttons,{"type":"RANGE_BUTTON","bindings":["NONE","NONE","NONE","NONE"],"scale":1,"x":0.5,"y":0.1,"range":"FROM_F1_TO_F12"}]}""" + assertNull(WinlatorLayouts.import(raw, "x", InputLayoutScreen.MAIN, 1920, 1080)) + } + @Test fun aKeyboardTrackpadKeepsItsKeys() { val raw = """{"cursorSpeed":1,"elements":[{"type":"TRACKPAD","shape":"CIRCLE","bindings":["KEY_W","KEY_D","KEY_S","KEY_A"],"scale":1,"x":0.5,"y":0.5}]}""" val (layout, bindings) = WinlatorLayouts.import(raw, "x", InputLayoutScreen.MAIN, 1920, 1080)!!