mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
Read and write Winlator layouts as landscape whichever way the device is held
This commit is contained in:
1 parent
c89ac81869
commit
b99cbb6bab
3 files changed
+20
-3
No files matched your search
@@ -32,9 +32,13 @@ internal object InputLayoutFile {
|
||||
/** Whether [parse] keeps a binding with this name. */
|
||||
internal fun knows(binding: String) = binding in CODES
|
||||
|
||||
/** Winlator lays out every .icp for a landscape screen, whichever way the device is held when it is read or written. */
|
||||
internal fun landscape(width: Int, height: Int) = if (height > width) height to width else width to height
|
||||
|
||||
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? {
|
||||
fun export(layout: SavedInputLayout, bindings: SavedInputBindings?, screenWidth: Int, screenHeight: Int): String? {
|
||||
val (width, height) = landscape(screenWidth, screenHeight)
|
||||
val snap = width.coerceAtLeast(1) / 100f
|
||||
val elements = JSONArray()
|
||||
for (e in layout.elements) {
|
||||
@@ -116,7 +120,8 @@ internal object InputLayoutFile {
|
||||
}
|
||||
|
||||
fun import(context: Context, raw: String, fallbackName: String, screen: InputLayoutScreen, style: InputSurfaceStyle,
|
||||
width: Int, height: Int, density: Float, id: String = newInputId()): Pair<SavedInputLayout, SavedInputBindings>? {
|
||||
screenWidth: Int, screenHeight: Int, density: Float, id: String = newInputId()): Pair<SavedInputLayout, SavedInputBindings>? {
|
||||
val (width, height) = landscape(screenWidth, screenHeight)
|
||||
val keys = parse(raw, width, density)?.takeIf { it.isNotEmpty() } ?: return null
|
||||
val converted = runCatching { LegacyScreenKeyMigration.convert(context, keys, width, height, density) }.getOrNull() ?: return null
|
||||
val name = runCatching { JSONObject(raw).optString("name") }.getOrNull().orEmpty().trim().take(80).ifEmpty { fallbackName.trim().take(80) }
|
||||
|
||||
@@ -101,9 +101,10 @@ object WinlatorLayouts {
|
||||
}.lowercase().replaceFirstChar { it.uppercase() }.take(80)
|
||||
|
||||
/** Converts an `.icp` file for a [width]×[height] screen into a layout and the actions of its controls. */
|
||||
fun import(raw: String, fallbackName: String, screen: InputLayoutScreen, width: Int, height: Int,
|
||||
fun import(raw: String, fallbackName: String, screen: InputLayoutScreen, screenWidth: Int, screenHeight: Int,
|
||||
id: String = newInputId(), bindingsId: String = newInputId()): Pair<SavedInputLayout, SavedInputBindings>? = runCatching {
|
||||
require(raw.length <= 1_000_000)
|
||||
val (width, height) = InputLayoutFile.landscape(screenWidth, screenHeight)
|
||||
val doc = JSONObject(raw)
|
||||
val source = doc.getJSONArray("elements")
|
||||
val aspect = (width.coerceAtLeast(1).toFloat() / height.coerceAtLeast(1)).coerceIn(.05f, 20f)
|
||||
|
||||
@@ -32,6 +32,17 @@ class WinlatorLayoutsTest {
|
||||
assertFalse(WinlatorLayouts.isIcp("not json"))
|
||||
}
|
||||
|
||||
@Test fun aLayoutImportedInPortraitMatchesTheLandscapeImport() {
|
||||
val landscape = WinlatorLayouts.import(profile, "x", InputLayoutScreen.MAIN, 2400, 1080, id = "l", bindingsId = "b")!!
|
||||
val portrait = WinlatorLayouts.import(profile, "x", InputLayoutScreen.MAIN, 1080, 2400, id = "l", bindingsId = "b")!!
|
||||
assertEquals(2400f / 1080f, portrait.first.aspect, .001f)
|
||||
assertEquals(landscape.first.elements.map { it.copy(id = "") }, portrait.first.elements.map { it.copy(id = "") })
|
||||
val exported = InputLayoutFile.export(landscape.first, landscape.second, 1000, 600)!!
|
||||
assertEquals(exported, InputLayoutFile.export(landscape.first, landscape.second, 600, 1000))
|
||||
val back = InputLayoutFile.import(context, exported, "x", InputLayoutScreen.MAIN, InputSurfaceStyle(), 600, 1000, 2f)!!
|
||||
assertTrue(back.first.aspect > 1f)
|
||||
}
|
||||
|
||||
@Test fun convertsEachKindOfControl() {
|
||||
val (layout, bindings) = WinlatorLayouts.import(profile, "fallback", InputLayoutScreen.MAIN, 2400, 1080)!!
|
||||
assertEquals("Shooter", layout.name)
|
||||
|
||||
Reference in new issue
Block a user