From f84e9d519113bbcd268cd141d3f1b3eb29396357 Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Tue, 17 Nov 2026 11:23:34 -0500 Subject: [PATCH] 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 --- .../launcher/input/LegacyGamepadLayout.kt | 16 +++- .../launcher/input/OnScreenLayout.kt | 16 +++- .../launcher/ui/InputSurfaceDialog.kt | 24 +++--- .../launcher/ui/InputSurfaceEditorView.kt | 34 ++++++-- app/src/main/res/values-ro/input_library.xml | 2 +- app/src/main/res/values-uk/input_library.xml | 2 +- .../launcher/input/InputLayoutToolsTest.kt | 23 ++++++ .../launcher/input/OnScreenLayoutTest.kt | 77 +++++++++++++++++++ .../launcher/ui/InputSurfaceEditorViewTest.kt | 76 ++++++++++++++++++ 9 files changed, 245 insertions(+), 25 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceEditorViewTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt index e281a2b741..2a36489e85 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt @@ -176,7 +176,7 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid } - fun elements(): List { + fun elements(previousStickGeometry: Boolean = false): List { fit = fitScale() controls.forEach { it.radius = scaled(it.radiusDp) } placeAuto(width.toFloat(), height.toFloat()) @@ -189,7 +189,7 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid }.map { c -> InputSurfaceElement(requireNotNull(InputControl.fromId(c.id)), (c.cx - c.halfW) / width, (c.cy - c.halfH) / height, 2 * c.halfW / width, 2 * c.halfH / height, floating = settings.adaptiveSticks, hideWhenIdle = c.stick >= 0 && settings.adaptiveSticks, - buttonShape = if (c.id in shoulderIds) InputButtonShape.PILL else InputButtonShape.CIRCLE).let(::widenStick).bounded() + buttonShape = if (c.id in shoulderIds) InputButtonShape.PILL else InputButtonShape.CIRCLE).let { if (previousStickGeometry) widenStickPreviously(it) else widenStick(it) }.bounded() }.toMutableList() if (settings.keyboardButton) result += keyboard() return result @@ -315,7 +315,7 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid return e.copy(x = (cx - radius) / width, y = (cy - radius) / height, width = 2 * radius / width, height = 2 * radius / height) } - private fun widenStick(e: InputSurfaceElement): InputSurfaceElement { + private fun widenStickPreviously(e: InputSurfaceElement): InputSurfaceElement { val extra = e.width * (STICK_REACH - 1f) return when (e.control) { InputControl.LS -> (e.x - extra).coerceAtLeast(0f).let { x -> e.copy(x = x, width = e.x + e.width - x) } @@ -323,6 +323,16 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid else -> e } } + + private fun widenStick(e: InputSurfaceElement): InputSurfaceElement { + val extra = e.width * (STICK_REACH - 1f) + val width = e.width + extra + return when (e.control) { + InputControl.LS -> e.copy(x = (e.x - extra).coerceIn(0f, (1f - width).coerceAtLeast(0f)), width = width) + InputControl.RS -> e.copy(x = e.x.coerceIn(0f, (1f - width).coerceAtLeast(0f)), width = width) + else -> e + } + } private companion object { const val MIN_FACE_MM = 9.5f const val FULL_SIZE_HEIGHT_MM = 85f diff --git a/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt index 3935c1a565..9fbb01c7ad 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt @@ -5,6 +5,7 @@ import android.graphics.Rect import kotlin.math.abs import kotlin.math.max import kotlin.math.min +import kotlin.math.roundToInt object OnScreenLayout { data class Dock(val portrait: Boolean, val grips: Boolean, val left: InputControl = InputControl.LS, val right: InputControl = InputControl.RS) { @@ -41,6 +42,14 @@ object OnScreenLayout { return stored.mapNotNull { byId[it.id] } + placed.filter { p -> stored.none { it.id == p.id } } } + fun editorElements(context: Context, layout: SavedInputLayout, width: Int, height: Int, edited: Boolean = false): List = + elements(context, if (edited && width > 0 && height > 0) layout.copy(aspect = width.toFloat() / height) else layout, width, height, Rect()) + + fun editorFrame(layout: SavedInputLayout, displayed: List): List { + val byId = displayed.associateBy { it.id } + return layout.elements.map { byId[it.id] ?: it } + } + fun fit(elements: List, referenceWidth: Float, referenceHeight: Float, width: Int, height: Int, safe: Rect): List { val w = width.toFloat() val h = height.toFloat() @@ -77,8 +86,11 @@ object OnScreenLayout { if (gamepad.any { it.id.startsWith("legacy:") }) { val named = layout.id.removePrefix("migrated.layout.").takeIf { it != layout.id }?.split('x')?.mapNotNull { it.toIntOrNull() } ?.takeIf { it.size == 2 && it.all { size -> size > 0 } }?.let { it[0] to it[1] } - for ((w, h) in listOfNotNull(named, width to height, long to short, short to long).distinct()) { - if (matches(gamepad, LegacyGamepadLayout(display, w, h, scale = layout.scale).elements())) return (w.toFloat() to h.toFloat()) to true + val aspectSize = layout.aspect.takeIf { layout.id == InputLibraries.OVERLAY && it > 0f } + ?.let { (it * height).roundToInt().coerceAtLeast(1) to height } + for ((w, h) in listOfNotNull(named, width to height, aspectSize, long to short, short to long).distinct()) { + val generator = LegacyGamepadLayout(display, w, h, scale = layout.scale) + if (matches(gamepad, generator.elements()) || matches(gamepad, generator.elements(previousStickGeometry = true))) return (w.toFloat() to h.toFloat()) to true } } val aspect = layout.aspect.takeIf { it > 0f } ?: (long.toFloat() / short) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt index ddf5838be8..065b2183ff 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt @@ -59,7 +59,7 @@ import com.droiddeck.launcher.input.* import com.droiddeck.launcher.session.SessionState private data class SurfaceSnapshot(val elements: List, val actions: Map, - val clicks: Map, val scale: Float) + val clicks: Map, val scale: Float, val aspect: Float) private class GamepadScaler(private val context: android.content.Context, private val width: Int, private val height: Int) { private fun generator(scale: Float) = LegacyGamepadLayout(context, width, height, scale = scale) @@ -83,6 +83,7 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB var clickProperties by mutableStateOf(false) var history by mutableStateOf>(emptyList()) var layoutScale by mutableFloatStateOf(layout.scale) + var aspect by mutableFloatStateOf(layout.aspect) private var scaleBase by mutableStateOf, List, Float>?>(null) private var scaleValue by mutableIntStateOf(100) private val rescales get() = gamepad != null && elements.any { it.id in gamepad.ids } @@ -94,9 +95,14 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB val positioned get() = (if (rescales) gamepad!!.reset(elements) else defaults?.let { elements.withPositionsOf(it) }) ?.takeIf { it != elements || rescales && layoutScale != 1f } val element get() = elements.firstOrNull { it.id == selected } - val dirty get() = elements != layout.elements || actions != preset.elements || clicks != preset.clicks || layoutScale != layout.scale - fun checkpoint() { history = (history + SurfaceSnapshot(elements, actions, clicks, layoutScale)).takeLast(40) } - fun undo() { history.lastOrNull()?.let { elements = it.elements; actions = it.actions; clicks = it.clicks; layoutScale = it.scale; history = history.dropLast(1) } } + val dirty get() = elements != layout.elements || actions != preset.elements || clicks != preset.clicks || layoutScale != layout.scale || aspect != layout.aspect + fun checkpoint() { history = (history + SurfaceSnapshot(elements, actions, clicks, layoutScale, aspect)).takeLast(40) } + fun undo() { history.lastOrNull()?.let { elements = it.elements; actions = it.actions; clicks = it.clicks; layoutScale = it.scale; aspect = it.aspect; history = history.dropLast(1) } } + fun beginEdit(displayed: List, aspect: Float) { + checkpoint() + elements = OnScreenLayout.editorFrame(layout.copy(elements = elements), displayed) + this.aspect = aspect + } fun scaleAll(percent: Int) { val base = scaleBase?.takeIf { it.second == elements } ?: Triple(elements, elements, if (rescales) layoutScale else 0f).also { checkpoint() } val factor = percent / 100f @@ -112,10 +118,6 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB fun clickAction(e: InputSurfaceElement) = preset.copy(elements = actions, clicks = clicks).clickAction(e) } -@Suppress("DEPRECATION") -private fun displayAspect(display: Display): Float? = android.util.DisplayMetrics().also(display::getRealMetrics) - .takeIf { it.widthPixels > 0 && it.heightPixels > 0 }?.let { (it.widthPixels.toFloat() / it.heightPixels).coerceIn(.05f, 20f) } - @Composable fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () -> Unit, role: InputRole = InputRole.SECONDARY, layoutId: String? = null, bindingsId: String? = null) { @@ -153,7 +155,7 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () -> InputLibraries.update(context) { latest -> val l = latest.layouts.first { it.id == savedLayout.id } val p = latest.preset(savedBindings.id) - latest.withLayout(l.copy(elements = draft.elements, aspect = target?.let(::displayAspect) ?: l.aspect, scale = draft.layoutScale)).withPreset(p.copy(elements = draft.actions.filterKeys { id -> draft.elements.any { it.id == id } }, + latest.withLayout(l.copy(elements = draft.elements, aspect = draft.aspect, scale = draft.layoutScale)).withPreset(p.copy(elements = draft.actions.filterKeys { id -> draft.elements.any { it.id == id } }, clicks = draft.clicks.filterKeys { id -> draft.elements.any { it.id == id && it.hasClick } })) } }.onSuccess { onChanged(); onDismiss() }.onFailure { failure = it.message ?: context.getString(R.string.input_save_failed) } @@ -253,10 +255,10 @@ private fun SurfaceCanvas(draft: SurfaceDraft, transparent: Boolean = false) { AndroidView(factory = { InputSurfaceEditorView(it) }, modifier = Modifier.fillMaxSize().testTag("input-layout-canvas"), update = { view -> view.setBackgroundColor(if (transparent) android.graphics.Color.TRANSPARENT else android.graphics.Color.rgb(10, 11, 13)) - view.style = draft.layout.style.copy(hidden = emptySet()); view.elements = draft.elements; view.selected = draft.selected; view.snap = draft.snap + view.style = draft.layout.style.copy(hidden = emptySet()); view.sourceLayout = draft.layout; view.aspect = draft.aspect; view.elements = draft.elements; view.selected = draft.selected; view.snap = draft.snap view.isEnabled = !draft.properties && !draft.adding view.onSelect = { if (!draft.properties) draft.selected = it } - view.onBeginEdit = draft::checkpoint + view.onBeginEdit = draft::beginEdit view.onChange = { e -> if (!draft.properties) draft.elements = draft.elements.map { if (it.id == e.id) e else it } } }) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt index d03eaa6e9f..e71aba6a59 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt @@ -9,6 +9,8 @@ import android.view.HapticFeedbackConstants import android.view.MotionEvent import android.view.ViewConfiguration import android.widget.FrameLayout +import com.droiddeck.launcher.input.OnScreenLayout +import com.droiddeck.launcher.input.SavedInputLayout import com.droiddeck.launcher.input.InputSurfaceElement import com.droiddeck.launcher.input.InputSurfaceStyle import com.droiddeck.launcher.input.snapSurfaceElement @@ -16,7 +18,11 @@ import kotlin.math.abs internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { var elements: List = emptyList() - set(value) { field = value; preview.updateCustomLayout(value); invalidate() } + set(value) { field = value; refreshElements(); invalidate() } + var aspect = 1f + set(value) { field = value; refreshElements(); invalidate() } + var sourceLayout: SavedInputLayout? = null + set(value) { field = value; refreshElements(); invalidate() } var style = InputSurfaceStyle() set(value) { field = value; preview.updateStyle(value) } var snap = true @@ -26,7 +32,7 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { var selected: String? = null set(value) { field = value; invalidate() } var onSelect: (String?) -> Unit = {} - var onBeginEdit: () -> Unit = {} + var onBeginEdit: (List, Float) -> Unit = { _, _ -> } var onChange: (InputSurfaceElement) -> Unit = {} private val preview = InputSurfaceView(context) private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.rgb(26, 159, 255); strokeWidth = 2 * resources.displayMetrics.density } @@ -37,17 +43,31 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { private var corner = 0 private var dragging = false + private var visibleElements: List = emptyList() + init { setBackgroundColor(Color.rgb(10, 11, 13)) addView(preview, LayoutParams(-1, -1)) } + private fun refreshElements() { + visibleElements = sourceLayout?.let { layout -> + OnScreenLayout.editorElements(context, layout.copy(elements = elements, aspect = aspect), width, height) + } ?: elements + preview.updateCustomLayout(visibleElements) + } + + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { + super.onSizeChanged(w, h, oldw, oldh) + refreshElements() + } + override fun dispatchDraw(canvas: Canvas) { super.dispatchDraw(canvas) paint.style = Paint.Style.STROKE guideX?.let { canvas.drawLine(width * it, 0f, width * it, height.toFloat(), paint) } guideY?.let { canvas.drawLine(0f, height * it, width.toFloat(), height * it, paint) } - val e = elements.firstOrNull { it.id == selected } ?: return + val e = visibleElements.firstOrNull { it.id == selected } ?: return val l = width * e.x; val t = height * e.y; val r = l + width * e.width; val b = t + height * e.height paint.style = Paint.Style.STROKE; canvas.drawRect(l, t, r, b, paint) paint.style = Paint.Style.FILL @@ -65,7 +85,7 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { MotionEvent.ACTION_DOWN -> { clearGuides() downX = event.x; downY = event.y; dragging = false; corner = 0 - val current = elements.firstOrNull { it.id == selected } + val current = visibleElements.firstOrNull { it.id == selected } if (current != null) { val left = width * current.x; val top = height * current.y val right = left + width * current.width; val bottom = top + height * current.height @@ -73,7 +93,7 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { val vertical = if (abs(downY - top) < radius) 4 else if (abs(downY - bottom) < radius) 8 else 0 if (horizontal != 0 && vertical != 0) corner = horizontal or vertical } - original = if (corner != 0) current else elements.asReversed().firstOrNull { + original = if (corner != 0) current else visibleElements.asReversed().firstOrNull { downX / width in it.x..(it.x + it.width) && downY / height in it.y..(it.y + it.height) } selected = original?.id; onSelect(selected) @@ -81,7 +101,7 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { MotionEvent.ACTION_MOVE -> { val e = original ?: return true if (!dragging && abs(event.x - downX) + abs(event.y - downY) > ViewConfiguration.get(context).scaledTouchSlop) { - dragging = true; onBeginEdit() + dragging = true; onBeginEdit(visibleElements, width.toFloat() / height) } if (!dragging) return true val dx = (event.x - downX) / width; val dy = (event.y - downY) / height @@ -93,7 +113,7 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { val bottom = if (corner and 8 != 0) (e.y + e.height + dy).coerceIn(top + .03f, 1f) else e.y + e.height e.copy(x = left, y = top, width = right - left, height = bottom - top).bounded() } - val aligned = if (snap) snapSurfaceElement(next, elements.filter { it.id != e.id }, 9 * resources.displayMetrics.density / width, + val aligned = if (snap) snapSurfaceElement(next, visibleElements.filter { it.id != e.id }, 9 * resources.displayMetrics.density / width, 9 * resources.displayMetrics.density / height, corner) else null if (aligned != null && (aligned.x != null && aligned.x != guideX || aligned.y != null && aligned.y != guideY)) performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK) guideX = aligned?.x; guideY = aligned?.y diff --git a/app/src/main/res/values-ro/input_library.xml b/app/src/main/res/values-ro/input_library.xml index e277e65abf..5a3648d509 100644 --- a/app/src/main/res/values-ro/input_library.xml +++ b/app/src/main/res/values-ro/input_library.xml @@ -29,7 +29,7 @@ Cruce Trăgaci Gata - Anulează + Anulează ultima acțiune + Adaugă Trage pentru a muta. Trage de un colț pentru a redimensiona. Atinge Proprietăți pentru a atribui o acțiune. Editare pe ecranul secundar diff --git a/app/src/main/res/values-uk/input_library.xml b/app/src/main/res/values-uk/input_library.xml index c00c0b1a39..f2504dde51 100644 --- a/app/src/main/res/values-uk/input_library.xml +++ b/app/src/main/res/values-uk/input_library.xml @@ -29,7 +29,7 @@ Хрестовина Тригер Готово - Скасувати + Скасувати дію + Додати Перетягніть, щоб перемістити. Потягніть за кут, щоб змінити розмір. Торкніться «Властивості», щоб призначити дію. Редагування на другому екрані diff --git a/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt index 44ad82b904..a7a1138957 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/InputLayoutToolsTest.kt @@ -162,6 +162,23 @@ class InputLayoutToolsTest { assertEquals(label, emptyList>(), overlapping(elements, width, height)) } + private fun assertStickView(elements: List, 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") + } } diff --git a/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt index 0a95382937..cfe027586a 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt @@ -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()) diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceEditorViewTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceEditorViewTest.kt new file mode 100644 index 0000000000..c74e5be155 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceEditorViewTest.kt @@ -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 + + 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, 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(), 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()) + } +}