fix(input): kit scale fits the safe area and oversized insets are capped

This commit is contained in:
Kurt Himebauch committed 2026-11-18 23:52:00 -05:00
1 parent c664b6cfbf
commit 968828afbd
2 files changed
+19 -4

No files matched your search

@@ -41,6 +41,8 @@ object KitLayout {
/** The widest pair of side controls on any kit: a larger size never pushes the two halves, or the rows, into each other. */
private const val MIN_SPAN = 1200f
private const val MAX_HEIGHT_MM = 80f
/** A cutout never takes more than this fraction of a side; larger insets would leave no room for a usable pad. */
private const val MAX_INSET = 6
private const val CORNER_DP = 52f
private const val ROW_DP = 60f
private const val BUTTON_DP = 44f
@@ -85,11 +87,13 @@ object KitLayout {
}
/** The default controls for [kit] on a [width] × [height] screen. */
fun elements(context: Context, kit: PadKit, width: Int, height: Int, safe: Rect = Rect(), scale: Float = 1f): List<InputSurfaceElement> {
fun elements(context: Context, kit: PadKit, width: Int, height: Int, inset: Rect = Rect(), scale: Float = 1f): List<InputSurfaceElement> {
if (width <= 0 || height <= 0) return emptyList()
val safe = Rect(inset.left.coerceIn(0, width / MAX_INSET), inset.top.coerceIn(0, height / MAX_INSET),
inset.right.coerceIn(0, width / MAX_INSET), inset.bottom.coerceIn(0, height / MAX_INSET))
if (height > width) return portrait(context, kit, width, height, safe, scale)
val w = width.toFloat(); val h = height.toFloat()
val unit = (min(w / REF_W, h / REF_H) * cap(context, min(width, height)) * scale).coerceAtMost(min(w / MIN_SPAN, h / REF_H))
val unit = (min(w / REF_W, h / REF_H) * cap(context, min(width, height)) * scale).coerceAtMost(min((w - safe.left - safe.right).coerceAtLeast(1f) / MIN_SPAN, (h - safe.top - safe.bottom).coerceAtLeast(1f) / REF_H))
fun x(ref: Float) = when {
ref < REF_W / 3f -> safe.left + ref * unit
ref > REF_W * 2f / 3f -> w - safe.right - (REF_W - ref) * unit
@@ -98,7 +98,7 @@ class KitLayoutTest {
}
@Test fun safeInsetsMoveEdgeControlsInByTheInsetOnly() {
val safe = Rect(120, 30, 60, 40)
val safe = Rect(120, 0, 60, 0)
for (kit in PadKit.entries) {
val plain = KitLayout.elements(context, kit, 2400, 1080).associateBy { it.id }
val inset = KitLayout.elements(context, kit, 2400, 1080, safe)
@@ -108,7 +108,6 @@ class KitLayoutTest {
}
val a = plain.getValue("legacy:lt"); val b = inset.single { it.id == "legacy:lt" }
assertEquals(safe.left.toFloat(), (b.x - a.x) * 2400, .5f)
assertEquals(safe.top.toFloat(), (b.y - a.y) * 1080, .5f)
val r = plain.getValue("legacy:rt"); val s = inset.single { it.id == "legacy:rt" }
assertEquals(-safe.right.toFloat(), (s.x - r.x) * 2400, .5f)
}
@@ -282,4 +281,16 @@ class KitLayoutTest {
for (m in middle) assertTrue("$kit $top ${m.id}", m.y * 900 >= (lb.y + lb.height) * 900 - .5f || m.x * 1600 >= (lb.x + lb.width) * 1600)
}
}
@Test fun insetsThatLeaveLessRoomShrinkTheKitInsteadOfOverlappingIt() {
for (kit in PadKit.entries) for ((w, h, safe) in listOf(Triple(1600, 900, Rect(0, 60, 0, 0)), Triple(1600, 900, Rect(0, 730, 0, 0)),
Triple(1920, 1080, Rect(80, 120, 80, 90)), Triple(2400, 1080, Rect(120, 30, 60, 40)))) {
val elements = KitLayout.elements(context, kit, w, h, safe)
assertEquals("$kit $w x $h $safe", emptyList<Pair<String, String>>(), overlapping(elements, w, h))
if (safe.top <= h / 6) for (e in elements) {
val (x, y, ew, eh) = px(e, w, h).toList()
assertTrue("$kit $safe ${e.id}", x >= safe.left - .5f && y >= safe.top - .5f && x + ew <= w - safe.right + .5f && y + eh <= h - safe.bottom + .5f)
}
}
}
}