fix(input): import whole Winlator key ranges and keyboard trackpads

This commit is contained in:
Kurt Himebauch committed 2026-12-04 14:53:13 -05:00
1 parent 7e1d6a3688
commit 8e7cf7f683
2 files changed
+21 -9

No files matched your search

@@ -136,17 +136,23 @@ object WinlatorLayouts {
}
"D_PAD" -> place(InputElementKind.DPAD, text.ifEmpty { "D-pad" }, cx, cy, 7 * scale, 7 * scale, directionAction(bindings, InputElementKind.DPAD))
"STICK" -> place(InputElementKind.STICK, text.ifEmpty { "Stick" }, cx, cy, 6 * scale, 6 * scale, directionAction(bindings, InputElementKind.STICK))
// Key directions stay keys; a trackpad sending gamepad sticks has no trackpad equivalent here, so it moves the pointer.
"TRACKPAD" -> place(InputElementKind.TRACKPAD, text.ifEmpty { "Trackpad" }, cx, cy, 6 * scale, 6 * scale,
directionAction(bindings, InputElementKind.TRACKPAD).takeIf { it !is InputAction.Directions } ?: InputAction.Pointer())
directionAction(bindings, InputElementKind.TRACKPAD).takeIf { it is InputAction.Directions || it is InputAction.Pointer } ?: InputAction.Pointer())
"RANGE_BUTTON" -> {
// Winlator scrolls a window of [bindings.size] keys through the range; every key gets its own button here,
// in rows (or columns) as long as that window.
val range = ranges[e.optString("range", "FROM_A_TO_Z")] ?: continue
val count = bindings.size.coerceIn(1, range.size)
val across = bindings.size.coerceIn(1, range.size)
val vertical = e.optInt("orientation", 0) == 1
for (n in 0 until count) {
val offset = (n - (count - 1) / 2f) * 4 * scale * .01f
val code = keys[range[n]] ?: continue
place(InputElementKind.BUTTON, label("KEY_" + range[n]), cx + if (vertical) 0f else offset, cy + if (vertical) offset * aspect else 0f,
2 * scale, 2 * scale, InputAction.Keys(listOf(code)), shape)
val step = 4 * scale * .01f
for ((n, name) in range.withIndex()) {
val code = keys[name] ?: continue
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)
}
}
}
@@ -35,7 +35,7 @@ class WinlatorLayoutsTest {
@Test fun convertsEachKindOfControl() {
val (layout, bindings) = WinlatorLayouts.import(profile, "fallback", InputLayoutScreen.MAIN, 2400, 1080)!!
assertEquals("Shooter", layout.name)
assertEquals(10, layout.elements.size)
assertEquals(18, layout.elements.size)
assertTrue(layout.elements.all { it.x in 0f..1f && it.y in 0f..1f && it.x + it.width <= 1.001f && it.y + it.height <= 1.001f })
fun action(name: String) = bindings.elements.getValue(layout.elements.first { it.name == name }.id)
@@ -52,7 +52,7 @@ class WinlatorLayoutsTest {
assertEquals(InputAction.Native(InputControl.RS), bindings.elements[stick.id])
val pad = layout.elements.single { it.kind == InputElementKind.TRACKPAD }
assertEquals(InputAction.Pointer(), bindings.elements[pad.id])
assertEquals(listOf("F1", "F2", "F3", "F4"), layout.elements.filter { it.name.matches(Regex("F[0-9]+")) }.map { it.name })
assertEquals((1..12).map { "F$it" }, layout.elements.filter { it.name.matches(Regex("F[0-9]+")) }.map { it.name })
assertEquals(InputAction.Keys(listOf(59)), action("F1"))
val dpadWide = dpad.width * 2400
@@ -110,6 +110,12 @@ class WinlatorLayoutsTest {
assertEquals(InputAction.Keys(listOf(57)), actions.elements[imported.id])
}
@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)!!
assertEquals(InputAction.Directions(17, 32, 31, 30), bindings.elements[layout.elements.single().id])
}
@Test fun rejectsAFileWithNothingToPlace() {
assertNull(WinlatorLayouts.import("""{"cursorSpeed":1,"elements":[]}""", "x", InputLayoutScreen.MAIN, 100, 100))
}