mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 04:25:38 +00:00
fix(input): send files with older Winlator key names through the converter
This commit is contained in:
1 parent
f94d5179c3
commit
2f192fddee
3 files changed
+20
-2
No files matched your search
@@ -29,6 +29,9 @@ internal object InputLayoutFile {
|
||||
)
|
||||
private val CODES: Map<String, Int> = NAMES.entries.associate { (code, name) -> name to code }
|
||||
|
||||
/** Whether [parse] keeps a binding with this name. */
|
||||
internal fun knows(binding: String) = binding in CODES
|
||||
|
||||
fun fileName(layout: SavedInputLayout) = layout.name.replace(Regex("[^A-Za-z0-9 _.-]"), "_").trim().ifEmpty { "droiddeck-layout" } + ".icp"
|
||||
|
||||
fun export(layout: SavedInputLayout, bindings: SavedInputBindings?, width: Int, height: Int): String? {
|
||||
|
||||
@@ -20,7 +20,7 @@ object WinlatorLayouts {
|
||||
fun isIcp(raw: String) = runCatching { JSONObject(raw).let { it.has("elements") && it.optJSONArray("elements") != null && it.has("cursorSpeed") } }.getOrDefault(false)
|
||||
|
||||
/**
|
||||
* Whether a file uses something only this converter keeps: mouse or gamepad bindings, trackpads or key ranges.
|
||||
* Whether a file uses something only this converter keeps: mouse or gamepad bindings, older key names, trackpads or key ranges.
|
||||
* DroidDeck's own exports send only keys, so they keep importing through [InputLayoutFile] with the sizes they were saved with.
|
||||
*/
|
||||
fun needsConversion(raw: String): Boolean = runCatching {
|
||||
@@ -28,7 +28,7 @@ object WinlatorLayouts {
|
||||
(0 until elements.length()).any { i ->
|
||||
val e = elements.optJSONObject(i) ?: return@any false
|
||||
val bindings = e.optJSONArray("bindings")?.let { a -> (0 until a.length()).map { a.optString(it) } }.orEmpty()
|
||||
e.optString("type") in setOf("TRACKPAD", "RANGE_BUTTON") || bindings.any { it.isNotBlank() && it != "NONE" && !it.startsWith("KEY_") }
|
||||
e.optString("type") in setOf("TRACKPAD", "RANGE_BUTTON") || bindings.any { it.isNotBlank() && it != "NONE" && !InputLayoutFile.knows(it) }
|
||||
}
|
||||
}.getOrDefault(false)
|
||||
|
||||
|
||||
@@ -93,6 +93,21 @@ class WinlatorLayoutsTest {
|
||||
{"type":"D_PAD","shape":"CIRCLE","bindings":["KEY_W","KEY_D","KEY_S","KEY_A"],"scale":1,"x":0.2,"y":0.5}]}"""
|
||||
assertFalse(WinlatorLayouts.needsConversion(keysOnly))
|
||||
assertTrue(WinlatorLayouts.needsConversion("""{"cursorSpeed":1,"elements":[{"type":"TRACKPAD","bindings":["MOUSE_MOVE_UP"]}]}"""))
|
||||
val aliases = """{"cursorSpeed":1,"elements":[{"type":"BUTTON","shape":"CIRCLE","bindings":["KEY_SHIFT","NONE"],"scale":1,"x":0.5,"y":0.5}]}"""
|
||||
assertTrue(WinlatorLayouts.needsConversion(aliases))
|
||||
val (layout, bindings) = WinlatorLayouts.import(aliases, "x", InputLayoutScreen.MAIN, 1920, 1080)!!
|
||||
assertEquals(InputAction.Keys(listOf(42)), bindings.elements[layout.elements.single().id])
|
||||
}
|
||||
|
||||
@Test fun droidDecksOwnExportStaysOnItsOwnImporter() {
|
||||
val element = InputSurfaceElement(InputControl.A, .4f, .4f, .1f, .1f, "jump", InputElementKind.BUTTON, "Jump", buttonShape = InputButtonShape.CIRCLE)
|
||||
val layout = SavedInputLayout("l", "Mine", listOf(element), screen = InputLayoutScreen.MAIN, aspect = 1000f / 600f)
|
||||
val preset = SavedInputBindings("b", "Mine", false, "l", elements = mapOf("jump" to InputAction.Keys(listOf(57))))
|
||||
val exported = InputLayoutFile.export(layout, preset, 1000, 600)!!
|
||||
assertFalse(WinlatorLayouts.needsConversion(exported))
|
||||
val (back, actions) = InputLayoutFile.import(context, exported, "Mine", InputLayoutScreen.MAIN, InputSurfaceStyle(), 1000, 600, 1f)!!
|
||||
val imported = back.elements.single()
|
||||
assertEquals(InputAction.Keys(listOf(57)), actions.elements[imported.id])
|
||||
}
|
||||
|
||||
@Test fun rejectsAFileWithNothingToPlace() {
|
||||
|
||||
Reference in new issue
Block a user