fix: preserve control size across landscape flips

This commit is contained in:
2026-09-13 21:23:20 +08:00
parent 70847c8b97
commit d60366ab77
17 changed files with 121 additions and 63 deletions
@@ -386,13 +386,14 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
view.setPadding(0, 0, 0, 0)
layoutInsets.gameplay.run {
mainScreen.setPadding(left, top, right, bottom)
virtualGamepad.setSafeHorizontalInsets(left, right)
virtualGamepad.setSafeHorizontalInsets(left, right, layoutInsets.gameplayRightShift)
}
layoutInsets.overlay.run {
overlayScreen.setPadding(left, top, right, bottom)
}
recordSystemEvent(
"safe_insets_game_${layoutInsets.gameplay.left}_0_${layoutInsets.gameplay.right}_0" +
"_shift_${layoutInsets.gameplayRightShift}" +
"_overlay_${layoutInsets.overlay.left}_${layoutInsets.overlay.top}_" +
"${layoutInsets.overlay.right}_${layoutInsets.overlay.bottom}",
)
@@ -28,6 +28,7 @@ internal fun computeVirtualGamepadGeometry(
maxRadius: Float,
safeLeft: Float = 0f,
safeRight: Float = 0f,
screenShift: Float = 0f,
dpadScale: Float = 1f,
dpadHorizontalOffset: Float = 0f,
dpadVerticalOffset: Float = 0f,
@@ -48,44 +49,49 @@ internal fun computeVirtualGamepadGeometry(
val unshiftedDpadLeft = unshiftedDpadX - dpadRadius * 2f
val edgeGap = baseRadius * 0.05f
val horizontalShift = min(faceRadius * 2f, (unshiftedDpadLeft - edgeGap).coerceAtLeast(0f))
val gameLeft = leftInset + sideWidth - horizontalShift
val gameRight = gameLeft + gameWidth
val baseGameLeft = leftInset + sideWidth - horizontalShift
val baseGameRight = baseGameLeft + gameWidth
val expandedRightWidth = (w - rightInset - baseGameRight).coerceAtLeast(1f)
val faceX = baseGameRight + expandedRightWidth * 0.50f
val faceGapX = min(faceRadius * 1.70f, expandedRightWidth * 0.50f - faceRadius)
val baseResetX = w - rightInset - sideWidth * 0.28f - horizontalShift
val rightmostControl = maxOf(faceX + faceGapX + faceRadius, baseResetX + utilityRadius)
val shift = screenShift.coerceIn(0f, (w - rightmostControl - edgeGap).coerceAtLeast(0f))
val gameLeft = baseGameLeft + shift
val gameRight = baseGameRight + shift
val dpadOuterExtent = dpadRadius * 2f
val dpadX = (unshiftedDpadX - horizontalShift +
val dpadX = (unshiftedDpadX - horizontalShift + shift +
dpadHorizontalOffset.coerceIn(-1f, 1f) * faceRadius)
.coerceIn(dpadOuterExtent, (w - dpadOuterExtent).coerceAtLeast(dpadOuterExtent))
val dpadY = (h * (0.61f + dpadVerticalOffset.coerceIn(-1f, 1f) * 0.12f))
.coerceIn(dpadOuterExtent, (h - dpadOuterExtent).coerceAtLeast(dpadOuterExtent))
val dpadGap = dpadRadius
val expandedRightWidth = (w - rightInset - gameRight).coerceAtLeast(1f)
val faceX = gameRight + expandedRightWidth * 0.50f
val faceY = h * 0.54f
val faceGapX = min(faceRadius * 1.70f, expandedRightWidth * 0.50f - faceRadius)
val faceGapY = faceRadius * 1.70f
val gameEdgeGap = faceRadius * 0.25f
return VirtualGamepadGeometry(
gameLeft = gameLeft,
gameRight = gameRight,
horizontalShift = horizontalShift,
horizontalShift = horizontalShift - shift,
up = VirtualControl(dpadX, dpadY - dpadGap, dpadRadius),
down = VirtualControl(dpadX, dpadY + dpadGap, dpadRadius),
left = VirtualControl(dpadX - dpadGap, dpadY, dpadRadius),
right = VirtualControl(dpadX + dpadGap, dpadY, dpadRadius),
select = VirtualControl(
leftInset + sideWidth - menuRadius - gameEdgeGap - horizontalShift,
leftInset + sideWidth - menuRadius - gameEdgeGap - horizontalShift + shift,
h * 0.89f,
menuRadius,
),
faceY = VirtualControl(faceX, faceY - faceGapY, faceRadius),
faceB = VirtualControl(faceX + faceGapX, faceY, faceRadius),
faceA = VirtualControl(faceX, faceY + faceGapY, faceRadius),
faceX = VirtualControl(faceX - faceGapX, faceY, faceRadius),
faceY = VirtualControl(faceX + shift, faceY - faceGapY, faceRadius),
faceB = VirtualControl(faceX + faceGapX + shift, faceY, faceRadius),
faceA = VirtualControl(faceX + shift, faceY + faceGapY, faceRadius),
faceX = VirtualControl(faceX - faceGapX + shift, faceY, faceRadius),
start = VirtualControl(
leftInset + sideWidth + gameWidth + menuRadius + gameEdgeGap - horizontalShift,
leftInset + sideWidth + gameWidth + menuRadius + gameEdgeGap - horizontalShift + shift,
h * 0.89f,
menuRadius,
),
pause = VirtualControl(w - rightInset - sideWidth * 0.72f - horizontalShift, h * 0.13f, utilityRadius),
reset = VirtualControl(w - rightInset - sideWidth * 0.28f - horizontalShift, h * 0.13f, utilityRadius),
pause = VirtualControl(w - rightInset - sideWidth * 0.72f - horizontalShift + shift, h * 0.13f, utilityRadius),
reset = VirtualControl(baseResetX + shift, h * 0.13f, utilityRadius),
)
}
@@ -46,6 +46,7 @@ class VirtualGamepadView(
}
private var safeLeft = 0f
private var safeRight = 0f
private var screenShift = 0f
private var directionMode = DirectionMode.EIGHT_WAY
private var dpadScale = 1.18f
private var dpadHorizontalOffset = 0f
@@ -76,10 +77,13 @@ class VirtualGamepadView(
onHorizontalShiftChanged(geometry().horizontalShift)
}
internal fun setSafeHorizontalInsets(left: Int, right: Int) {
if (safeLeft == left.toFloat() && safeRight == right.toFloat()) return
internal fun setSafeHorizontalInsets(left: Int, right: Int, rightShift: Int) {
if (safeLeft == left.toFloat() && safeRight == right.toFloat() &&
screenShift == rightShift.toFloat()
) return
safeLeft = left.toFloat()
safeRight = right.toFloat()
screenShift = rightShift.toFloat()
onHorizontalShiftChanged(geometry().horizontalShift)
invalidate()
}
@@ -277,6 +281,7 @@ class VirtualGamepadView(
maxRadius = resources.displayMetrics.density * 42f,
safeLeft = safeLeft,
safeRight = safeRight,
screenShift = screenShift,
dpadScale = dpadScale,
dpadHorizontalOffset = dpadHorizontalOffset,
dpadVerticalOffset = dpadVerticalOffset,
@@ -45,6 +45,7 @@ data class SafeInsets(val left: Int, val top: Int, val right: Int, val bottom: I
data class LayoutSafeInsets(
val gameplay: SafeInsets,
val gameplayRightShift: Int,
val overlay: SafeInsets,
)
@@ -61,16 +62,18 @@ fun resolveLayoutSafeInsets(
systemGestures: SafeInsets,
): LayoutSafeInsets {
val gameplayEdges = mergeSafeInsets(displayCutout, systemGestures)
// A landscape rotation exchanges the physical left and right edges. Keep the
// playable geometry anchored to the same coordinates in either orientation.
val gameplaySide = maxOf(gameplayEdges.left, gameplayEdges.right)
// Only the shared edge reservation should affect control size. When the
// camera is on the left, use its extra reservation to shift gameplay right
// instead of shrinking both side control areas.
val sharedSide = minOf(gameplayEdges.left, gameplayEdges.right)
return LayoutSafeInsets(
gameplay = SafeInsets(
left = gameplaySide,
left = sharedSide,
top = 0,
right = gameplaySide,
right = sharedSide,
bottom = 0,
),
gameplayRightShift = (gameplayEdges.left - sharedSide).coerceAtLeast(0),
// Menus are not touch controls: use the full width while keeping clear
// of visible top and bottom system bars.
overlay = SafeInsets(0, systemBars.top, 0, systemBars.bottom),
@@ -7,7 +7,7 @@ class BuildIdentityTest {
@Test
fun debugBuildUsesTheDocumentedIdentity() {
assertEquals("org.neslite.app.debug", BuildConfig.APPLICATION_ID)
assertEquals("0.9.12-debug", BuildConfig.VERSION_NAME)
assertEquals(9012, BuildConfig.VERSION_CODE)
assertEquals("0.9.13-debug", BuildConfig.VERSION_NAME)
assertEquals(9013, BuildConfig.VERSION_CODE)
}
}
@@ -126,28 +126,66 @@ class VirtualGamepadLayoutTest {
}
@Test
fun flippedLandscapeKeepsTheViewportAndEveryControlAtTheSameCoordinates() {
val first = resolveLayoutSafeInsets(
fun flippedLandscapeKeepsButtonSizesAndMovesGameplayAwayFromLeftCamera() {
val cameraLeft = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 0, 0, 0),
displayCutout = SafeInsets(238, 0, 0, 0),
systemGestures = SafeInsets(28, 0, 24, 0),
).gameplay
val flipped = resolveLayoutSafeInsets(
displayCutout = SafeInsets(288, 0, 0, 0),
systemGestures = SafeInsets(160, 0, 160, 0),
)
val cameraRight = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 0, 0, 0),
displayCutout = SafeInsets(0, 0, 238, 0),
systemGestures = SafeInsets(24, 0, 28, 0),
).gameplay
displayCutout = SafeInsets(0, 0, 288, 0),
systemGestures = SafeInsets(160, 0, 160, 0),
)
fun geometry(insets: SafeInsets) = computeVirtualGamepadGeometry(
fun geometry(insets: SafeInsets, shift: Int) = computeVirtualGamepadGeometry(
width = 2760f,
height = 1256f,
maxRadius = 126f,
safeLeft = insets.left.toFloat(),
safeRight = insets.right.toFloat(),
screenShift = shift.toFloat(),
dpadScale = 1.18f,
)
assertEquals(geometry(first), geometry(flipped))
val left = geometry(cameraLeft.gameplay, cameraLeft.gameplayRightShift)
val right = geometry(cameraRight.gameplay, cameraRight.gameplayRightShift)
val movement = 128f
val usableSide = (2760f - 320f - 1256f * 4f / 3f) / 2f
val originalRadius = minOf(1256f * 0.095f, usableSide * 0.16f, 126f)
assertEquals(originalRadius * 1.50f * 1.18f, left.up.radius, 0.01f)
assertEquals(originalRadius * 1.50f, left.faceA.radius, 0.01f)
assertEquals(originalRadius * 0.60f * 1.30f, left.select.radius, 0.01f)
assertEquals(originalRadius * 0.58f * 1.30f, left.pause.radius, 0.01f)
assertEquals(right.up.radius, left.up.radius, 0.01f)
assertEquals(right.faceA.radius, left.faceA.radius, 0.01f)
assertEquals(right.select.radius, left.select.radius, 0.01f)
assertEquals(right.pause.radius, left.pause.radius, 0.01f)
assertEquals(movement, left.gameLeft - right.gameLeft, 0.01f)
val leftControls = listOf(left.up, left.down, left.left, left.right, left.select,
left.faceX, left.faceY, left.faceA, left.faceB, left.start, left.pause, left.reset)
val rightControls = listOf(right.up, right.down, right.left, right.right, right.select,
right.faceX, right.faceY, right.faceA, right.faceB, right.start, right.pause, right.reset)
leftControls.zip(rightControls).forEach { (shifted, original) ->
assertEquals(movement, shifted.x - original.x, 0.01f)
assertEquals(original.radius, shifted.radius, 0.01f)
}
assertTrue(left.faceB.x + left.faceB.radius < 2760f)
}
@Test
fun rightShiftIsCappedBeforeFaceButtonsLeaveTheScreen() {
val geometry = computeVirtualGamepadGeometry(
width = 2760f,
height = 1256f,
maxRadius = 126f,
safeLeft = 160f,
safeRight = 160f,
screenShift = 999f,
dpadScale = 1.18f,
)
assertTrue(geometry.faceB.x + geometry.faceB.radius <= 2760f)
assertTrue(geometry.reset.x + geometry.reset.radius <= 2760f)
}
private fun distance(first: VirtualControl, second: VirtualControl): Float =
@@ -33,32 +33,35 @@ class SystemUiPolicyTest {
}
@Test
fun gameplayIsSymmetricAndMenusUseTheFullWidth() {
fun gameplayUsesSharedSideInsetAndShiftsForLeftCamera() {
val resolved = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 72, 0, 96),
displayCutout = SafeInsets(54, 28, 12, 0),
systemGestures = SafeInsets(24, 0, 30, 48),
)
assertEquals(SafeInsets(54, 0, 54, 0), resolved.gameplay)
assertEquals(SafeInsets(30, 0, 30, 0), resolved.gameplay)
assertEquals(24, resolved.gameplayRightShift)
assertEquals(SafeInsets(0, 72, 0, 96), resolved.overlay)
}
@Test
fun landscapeFlipKeepsGameplayInsetsAndDropsHiddenBars() {
fun landscapeFlipKeepsControlSpaceButMovesAwayFromLeftCamera() {
val first = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 0, 0, 0),
displayCutout = SafeInsets(238, 0, 0, 0),
systemGestures = SafeInsets(28, 0, 24, 0),
displayCutout = SafeInsets(288, 0, 0, 0),
systemGestures = SafeInsets(160, 0, 160, 0),
)
val flipped = resolveLayoutSafeInsets(
systemBars = SafeInsets(0, 0, 0, 0),
displayCutout = SafeInsets(0, 0, 238, 0),
systemGestures = SafeInsets(24, 0, 28, 0),
displayCutout = SafeInsets(0, 0, 288, 0),
systemGestures = SafeInsets(160, 0, 160, 0),
)
assertEquals(first.gameplay, flipped.gameplay)
assertEquals(SafeInsets(238, 0, 238, 0), first.gameplay)
assertEquals(SafeInsets(160, 0, 160, 0), first.gameplay)
assertEquals(128, first.gameplayRightShift)
assertEquals(0, flipped.gameplayRightShift)
assertEquals(SafeInsets(0, 0, 0, 0), first.overlay)
}