mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
Keep a portrait-only layout's frame through export and import
This commit is contained in:
1 parent
b99cbb6bab
commit
68954badee
2 files changed
+27
-4
No files matched your search
@@ -35,13 +35,18 @@ internal object InputLayoutFile {
|
||||
/** 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
|
||||
|
||||
/** Marks a DroidDeck export of a layout that has only a portrait arrangement; Winlator ignores unknown fields. */
|
||||
private const val PORTRAIT = "droiddeckPortrait"
|
||||
|
||||
fun fileName(layout: SavedInputLayout) = layout.name.replace(Regex("[^A-Za-z0-9 _.-]"), "_").trim().ifEmpty { "droiddeck-layout" } + ".icp"
|
||||
|
||||
fun export(layout: SavedInputLayout, bindings: SavedInputBindings?, screenWidth: Int, screenHeight: Int): String? {
|
||||
val (width, height) = landscape(screenWidth, screenHeight)
|
||||
val portraitOnly = layout.aspect > 0f && layout.aspect < 1f && (layout.rotated.isEmpty() || layout.rotatedAspect <= 1f)
|
||||
val arrangement = if (layout.aspect > 0f && layout.aspect < 1f && !portraitOnly) layout.rotated else layout.elements
|
||||
val (width, height) = landscape(screenWidth, screenHeight).let { (w, h) -> if (portraitOnly) h to w else w to h }
|
||||
val snap = width.coerceAtLeast(1) / 100f
|
||||
val elements = JSONArray()
|
||||
for (e in layout.elements) {
|
||||
for (e in arrangement) {
|
||||
val action = bindings?.action(e) ?: InputAction.Disabled
|
||||
val directional = e.kind == InputElementKind.DPAD || e.kind == InputElementKind.STICK
|
||||
val codes = when {
|
||||
@@ -76,7 +81,8 @@ internal object InputLayoutFile {
|
||||
.apply { if (e.opacity < 1f) put("opacity", e.opacity.toDouble()) })
|
||||
}
|
||||
if (elements.length() == 0) return null
|
||||
return JSONObject().put("id", 0).put("name", layout.name).put("cursorSpeed", 1.5).put("elements", elements).toString()
|
||||
return JSONObject().put("id", 0).put("name", layout.name).put("cursorSpeed", 1.5).put("elements", elements)
|
||||
.apply { if (portraitOnly) put(PORTRAIT, true) }.toString()
|
||||
}
|
||||
|
||||
fun parse(raw: String, width: Int, density: Float): List<LegacyScreenKey>? {
|
||||
@@ -121,7 +127,8 @@ internal object InputLayoutFile {
|
||||
|
||||
fun import(context: Context, raw: String, fallbackName: String, screen: InputLayoutScreen, style: InputSurfaceStyle,
|
||||
screenWidth: Int, screenHeight: Int, density: Float, id: String = newInputId()): Pair<SavedInputLayout, SavedInputBindings>? {
|
||||
val (width, height) = landscape(screenWidth, screenHeight)
|
||||
val portrait = runCatching { JSONObject(raw).optBoolean(PORTRAIT) }.getOrDefault(false)
|
||||
val (width, height) = landscape(screenWidth, screenHeight).let { (w, h) -> if (portrait) h to w else w to h }
|
||||
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) }
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import kotlin.math.abs
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34], manifest = Config.NONE)
|
||||
@@ -43,6 +44,21 @@ class WinlatorLayoutsTest {
|
||||
assertTrue(back.first.aspect > 1f)
|
||||
}
|
||||
|
||||
@Test fun aPortraitOnlyLayoutKeepsItsFrameThroughExportAndImport() {
|
||||
// A 100 px circle on a 600×1000 portrait screen.
|
||||
val circle = InputSurfaceElement(InputControl.A, .4f, .5f, 100f / 600f, 100f / 1000f, "jump", InputElementKind.BUTTON, "Jump",
|
||||
buttonShape = InputButtonShape.CIRCLE)
|
||||
val tall = SavedInputLayout("l", "Tall", listOf(circle), screen = InputLayoutScreen.MAIN, aspect = .6f)
|
||||
val preset = SavedInputBindings("b", "Tall", false, "l", elements = mapOf("jump" to InputAction.Keys(listOf(57))))
|
||||
val exported = InputLayoutFile.export(tall, preset, 1000, 600)!!
|
||||
assertEquals(exported, InputLayoutFile.export(tall, preset, 600, 1000))
|
||||
val back = InputLayoutFile.import(context, exported, "x", InputLayoutScreen.MAIN, InputSurfaceStyle(), 1000, 600, 1f)!!.first
|
||||
assertEquals(.6f, back.aspect, .001f)
|
||||
val again = back.elements.single()
|
||||
assertEquals(circle.height, again.height, circle.height * .1f)
|
||||
assertEquals(circle.x + circle.width / 2, again.x + again.width / 2, .01f)
|
||||
}
|
||||
|
||||
@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