fix(input): the controls editor shows the layout the game uses (#2339)

* fix(input): align layout editor with game controls

* fix(input): regenerate built-in layouts for measured surfaces

* fix(input): preserve edge stick travel bounds

* Drop editor strings now translated on main

* fix(input): keep editor frame, undo state and previous edge sticks consistent

* fix(input): preserve draft controls and aspect in editor
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-17 11:23:34 -05:00
1 parent 2ff890f0ec
commit f84e9d5191
9 files changed
+245 -25

No files matched your search

@@ -162,6 +162,23 @@ class InputLayoutToolsTest {
assertEquals(label, emptyList<Pair<String, String>>(), overlapping(elements, width, height))
}
private fun assertStickView(elements: List<InputSurfaceElement>, width: Int, height: Int, label: String) {
for (control in listOf(InputControl.LS, InputControl.RS)) {
val stick = elements.first { it.control == control }
assertTrue("$label ${stick.id} keeps its widened travel width", stick.width * width >= stick.height * height * 1.25f - 1f)
assertTrue("$label ${stick.id} stays on screen", stick.x >= 0f && stick.x + stick.width <= 1.0001f)
}
}
@Test fun sticksKeepTheirFullViewAtSurfaceEdges() {
for ((width, height) in listOf(2608 to 1200, 1920 to 1080, 1280 to 960)) {
ControllerPrefs.setLayout(context, width, height, mapOf("ls" to (0f to .5f), "rs" to (1f to .5f)))
val elements = LegacyGamepadLayout(context, width, height).elements()
assertStickView(elements, width, height, "$width x $height")
ControllerPrefs.resetLayout(context, width, height)
}
}
@Test @Config(qualifiers = "w833dp-h468dp-land-369dpi")
fun buttonSizeKeepsTheGamepadShapeAtHandheldDensity() {
for ((w, h) in listOf(1920 to 1080, 1920 to 1200)) {
@@ -191,4 +208,10 @@ class InputLayoutToolsTest {
}
}
}
@Test @Config(qualifiers = "w745dp-h343dp-land-420dpi")
fun wideHighDensityGamepadKeepsItsShape() {
val elements = LegacyGamepadLayout(context, 2608, 1200).elements()
assertGamepadShape(elements, 2608, 1200, "2608 x 1200")
}
}
@@ -49,6 +49,83 @@ class OnScreenLayoutTest {
}
}
@Test @Config(qualifiers = "w745dp-h343dp-land-420dpi")
fun aBuiltInGamepadSavedAtTheDisplaySizeRegeneratesForTheMeasuredSurface() {
val saved = gamepad().copy(elements = LegacyGamepadLayout(context, 2608, 1200).elements(), aspect = 2608f / 1200)
val placed = OnScreenLayout.elements(context, saved, 2048, 1200, Rect())
assertEquals(LegacyGamepadLayout(context, 2048, 1200).regenerate(saved.elements), placed)
}
@Test @Config(qualifiers = "w745dp-h343dp-land-420dpi")
fun previouslyGeneratedEdgeSticksRegenerateWithoutOverridingCustomGeometry() {
val w = 2608
val h = 1200
ControllerPrefs.setLayout(context, w, h, mapOf("ls" to (0f to .5f), "rs" to (1f to .5f)))
val generator = LegacyGamepadLayout(context, w, h)
val old = generator.elements(previousStickGeometry = true)
val current = generator.elements()
assertTrue(old.filter { it.control == InputControl.LS || it.control == InputControl.RS }.zip(current.filter { it.control == InputControl.LS || it.control == InputControl.RS }).any { (a, b) -> kotlin.math.abs(a.width - b.width) > .01f })
val saved = gamepad().copy(elements = old, aspect = w.toFloat() / h)
assertEquals(LegacyGamepadLayout(context, 2048, h).regenerate(old), OnScreenLayout.elements(context, saved, 2048, h, Rect()))
val custom = saved.copy(elements = old.map { if (it.id == "legacy:a") it.copy(x = .4f) else it })
assertEquals(OnScreenLayout.fit(custom.elements, custom.aspect, 1f, 2048, h, Rect()), OnScreenLayout.elements(context, custom, 2048, h, Rect()))
}
@Test fun savingTheEditorFrameKeepsEveryControlWhereTheEditorShowsIt() {
val w = 2608
val h = 1200
val layout = SavedInputLayout("custom", "Custom", listOf(
InputSurfaceElement(InputControl.A, .1f, .2f, .1f, .2f, "custom:a"),
InputSurfaceElement(InputControl.B, .7f, .6f, .1f, .2f, "custom:b")), aspect = 16f / 9f)
val displayed = OnScreenLayout.editorElements(context, layout, w, h, edited = true)
val moved = displayed.map { if (it.id == "custom:a") it.copy(x = .4f) else it }
val frame = OnScreenLayout.editorFrame(layout, moved + InputSurfaceElement(InputControl.X, .5f, .5f, .1f, .1f, "builtin:keyboard"))
assertEquals(layout.elements.map { it.id }, frame.map { it.id })
val saved = layout.copy(elements = frame, aspect = w.toFloat() / h)
val game = OnScreenLayout.elements(context, saved, w, h, Rect())
val editor = OnScreenLayout.editorElements(context, saved, w, h, edited = true)
for (shown in moved) {
assertArrayEquals(px(shown, w, h), px(game.first { it.id == shown.id }, w, h), .001f)
assertArrayEquals(px(shown, w, h), px(editor.first { it.id == shown.id }, w, h), .001f)
}
}
@Test fun editorAndGameUseTheSamePixelRectsOnWideAndStandardScreens() {
val layout = gamepad()
for ((w, h) in listOf(2000 to 900, 1600 to 900)) {
val game = OnScreenLayout.elements(context, layout, w, h, Rect())
val editor = OnScreenLayout.editorElements(context, layout, w, h)
assertEquals(game.map { it.id }, editor.map { it.id })
game.zip(editor).forEach { (gameElement, editorElement) ->
assertArrayEquals(px(gameElement, w, h), px(editorElement, w, h), .001f)
}
}
}
@Test fun editedGeneratedAndCustomLayoutsKeepTheirPixelRectsAcrossRefreshes() {
for ((w, h) in listOf(2000 to 900, 1600 to 900)) {
val generated = gamepad()
val generatedEdit = generated.elements.map { if (it.id == "legacy:a") it.copy(x = .3f, y = .4f, width = .12f, height = .12f) else it }
val generatedSaved = generated.copy(elements = generatedEdit, aspect = w.toFloat() / h)
val generatedPixels = px(OnScreenLayout.elements(context, generatedSaved, w, h, Rect()).first { it.id == "legacy:a" }, w, h)
val generatedRefresh = px(OnScreenLayout.editorElements(context, generatedSaved, w, h, edited = true).first { it.id == "legacy:a" }, w, h)
assertArrayEquals(generatedPixels, generatedRefresh, .001f)
val custom = SavedInputLayout("custom", "Custom", listOf(InputSurfaceElement(InputControl.A, .3f, .4f, .12f, .12f)), aspect = w.toFloat() / h)
val customRefresh = OnScreenLayout.editorElements(context, custom, w, h, edited = true)
assertArrayEquals(px(customRefresh.single(), w, h), px(OnScreenLayout.editorElements(context, custom.copy(elements = customRefresh), w, h, edited = true).single(), w, h), .001f)
}
}
@Test fun resetPositionsRestoresGeneratedGeometryAfterAnEdit() {
for ((w, h) in listOf(2000 to 900, 1600 to 900)) {
val original = gamepad()
val moved = original.copy(elements = original.elements.map { if (it.id == "legacy:a") it.copy(x = .3f) else it }, aspect = w.toFloat() / h)
val reset = LegacyGamepadLayout(context, w, h).regenerate(moved.elements)
assertEquals(LegacyGamepadLayout(context, w, h).regenerate(original.elements), OnScreenLayout.elements(context, moved.copy(elements = reset), w, h, Rect()))
}
}
@Test fun regenerationKeepsTheUsersButtonStylesAndRemovedButtons() {
val layout = gamepad().let { g -> g.copy(elements = g.elements.filterNot { it.id == "legacy:y" }.map { if (it.id == "legacy:a") it.copy(opacity = .4f, icon = 0, name = "Jump") else it }) }
val placed = OnScreenLayout.elements(context, layout, 2000, 1000, Rect())
@@ -0,0 +1,76 @@
package com.droiddeck.launcher.ui
import android.view.MotionEvent
import com.droiddeck.launcher.input.InputControl
import com.droiddeck.launcher.input.InputSurfaceElement
import com.droiddeck.launcher.input.OnScreenLayout
import com.droiddeck.launcher.input.SavedInputLayout
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34], manifest = Config.NONE)
class InputSurfaceEditorViewTest {
private val context get() = RuntimeEnvironment.getApplication()
private val layout = SavedInputLayout("custom", "Custom", listOf(InputSurfaceElement(InputControl.A, .1f, .2f, .2f, .2f)), aspect = 2f)
private fun view() = InputSurfaceEditorView(context).apply {
layout(0, 0, 1000, 1000)
sourceLayout = layout
aspect = layout.aspect
elements = layout.elements
snap = false
}
private fun center() = OnScreenLayout.editorElements(context, layout, 1000, 1000).first().let { (it.x + it.width / 2) * 1000 to (it.y + it.height / 2) * 1000 }
@Suppress("UNCHECKED_CAST")
private fun InputSurfaceEditorView.rendered() = javaClass.getDeclaredField("visibleElements").also { it.isAccessible = true }.get(this) as List<InputSurfaceElement>
private fun InputSurfaceEditorView.touch(action: Int, x: Float, y: Float) {
MotionEvent.obtain(0, 10, action, x, y, 0).also { onTouchEvent(it); it.recycle() }
}
@Test fun editingStartsInTheDisplayedFrame() {
val view = view()
var started: Pair<List<InputSurfaceElement>, Float>? = null
view.onBeginEdit = { elements, aspect -> started = elements to aspect }
val (x, y) = center()
view.touch(MotionEvent.ACTION_DOWN, x, y)
view.touch(MotionEvent.ACTION_MOVE, x + 100f, y + 50f)
val (elements, aspect) = requireNotNull(started)
assertEquals(1f, aspect, .0001f)
assertEquals(OnScreenLayout.editorElements(context, layout, 1000, 1000), elements)
}
@Test fun undoRestoresTheSavedFrameForHitTesting() {
val view = view()
var selection: String? = null
view.onSelect = { selection = it }
view.elements = layout.elements.map { it.copy(x = .5f) }
view.elements = layout.elements
val (x, y) = center()
view.touch(MotionEvent.ACTION_DOWN, x, y)
assertEquals("legacy:a", selection)
}
@Test fun editorFramePreservesAddedAndRemovedDraftElements() {
val added = layout.elements + InputSurfaceElement(InputControl.B, .6f, .2f, .2f, .2f, id = "added")
val removed = layout.copy(elements = emptyList())
assertEquals(added, OnScreenLayout.editorFrame(layout.copy(elements = added), added))
assertEquals(emptyList<InputSurfaceElement>(), OnScreenLayout.editorFrame(removed, emptyList()))
}
@Test fun savedAspectRemainsStableForRenamesAndResizes() {
val view = view()
val renamed = layout.elements.map { it.copy(name = "Renamed") }
view.elements = renamed
view.aspect = 2f
assertEquals(OnScreenLayout.editorElements(context, layout.copy(elements = renamed, aspect = 2f), 1000, 1000), view.rendered())
view.layout(0, 0, 800, 1000)
assertEquals(OnScreenLayout.editorElements(context, layout.copy(elements = renamed, aspect = 2f), 800, 1000), view.rendered())
}
}