diff --git a/app/src/main/java/com/droiddeck/launcher/input/ControllerOverlayGeometry.kt b/app/src/main/java/com/droiddeck/launcher/input/ControllerOverlayGeometry.kt index c5d33d950..d274269d4 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/ControllerOverlayGeometry.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/ControllerOverlayGeometry.kt @@ -13,11 +13,13 @@ internal class ControllerOverlayGeometry( private val density = metrics.density private val dpi = metrics.ydpi.takeIf { it in metrics.densityDpi * .6f..metrics.densityDpi * 1.6f } ?: metrics.densityDpi.toFloat() - private val unit = minOf( - min(w / 1600f, h / 900f) * min(1f, 80f / max(h / dpi * 25.4f, 1f)) * scale, + private val fit = minOf( + min(w / 1600f, h / 900f) * min(1f, 80f / max(h / dpi * 25.4f, 1f)), (w - safe.left - safe.right).coerceAtLeast(1f) / 1200f, (h - safe.top - safe.bottom).coerceAtLeast(1f) / 900f, ) + private val unit = fit * min(scale, 1f) + private val size = fit * scale private fun anchor(value: Float, reference: Float, extent: Float, start: Int, end: Int): Float { val low = start + value * unit @@ -37,18 +39,18 @@ internal class ControllerOverlayGeometry( if (!kit.steam) return placeStandardPad(elements, kit) val leftEdge = safe.left + 52f * density val middle = (w + safe.left - safe.right) / 2f - val keyboard = Box(max(x(136f), leftEdge + 46f * unit), y(848f), 46f).takeIf { elements.any { it.id == KEYBOARD } } + val keyboard = Box(max(x(136f), leftEdge + 46f * size), y(848f), 46f).takeIf { elements.any { it.id == KEYBOARD } } val row = fitRow(listOfNotNull(keyboard, - Box(max(x(370f), leftEdge + 66f * unit), y(852f), 66f, 30f), + Box(max(x(370f), leftEdge + 66f * size), y(852f), 66f, 30f), Box(middle - 140f * unit, y(850f), 46f, 26f), Box(middle + 140f * unit, y(850f), 46f, 26f), Box(x(1230f), y(852f), 66f, 30f)), leftEdge) val (guide, select, start, qam) = row.takeLast(4) val positions = shoulders() + face(1375f, 345f, 96f) + mapOf( - InputControl.UP to Box(x(220f), y(345f) - 102.5f * unit, 55.5f, 47.5f), - InputControl.RIGHT to Box(x(220f) + 102.5f * unit, y(345f), 47.5f, 55.5f), - InputControl.DOWN to Box(x(220f), y(345f) + 102.5f * unit, 55.5f, 47.5f), - InputControl.LEFT to Box(x(220f) - 102.5f * unit, y(345f), 47.5f, 55.5f), + InputControl.UP to Box(x(220f), y(345f) - 102.5f * size, 55.5f, 47.5f), + InputControl.RIGHT to Box(x(220f) + 102.5f * size, y(345f), 47.5f, 55.5f), + InputControl.DOWN to Box(x(220f), y(345f) + 102.5f * size, 55.5f, 47.5f), + InputControl.LEFT to Box(x(220f) - 102.5f * size, y(345f), 47.5f, 55.5f), InputControl.LS to Box(x(290f), y(640f), 96f), InputControl.RS to Box(x(1310f), y(640f), 96f), InputControl.SELECT to select, @@ -85,7 +87,7 @@ internal class ControllerOverlayGeometry( private fun placeStandardPad(elements: List, kit: PadKit): List { val leftEdge = safe.left + 52f * density val middle = (w + safe.left - safe.right) / 2f - val keyboard = Box(max(x(136f), leftEdge + 46f * unit), y(848f), 46f).takeIf { elements.any { it.id == KEYBOARD } } + val keyboard = Box(max(x(136f), leftEdge + 46f * size), y(848f), 46f).takeIf { elements.any { it.id == KEYBOARD } } val controls = if (kit.playStation) listOf( InputControl.QAM to Box(x(220f), y(848f), 46f), InputControl.SELECT to Box(middle - 160f * unit, y(850f), 50f, 30f), @@ -103,15 +105,15 @@ internal class ControllerOverlayGeometry( face(1350f, 395f, 96f) + dpad(250f, 395f, 125f, kit.dpad) + mapOf( InputControl.LS to Box(x(565f), y(690f), 100f), InputControl.RS to Box(x(1035f), y(690f), 100f), - InputControl.L3 to Box(x(565f) - 135f * unit, y(690f) - 100f * unit, 42f), - InputControl.R3 to Box(x(1035f) + 135f * unit, y(690f) - 100f * unit, 42f), + InputControl.L3 to Box(x(565f) - 135f * size, y(690f) - 100f * size, 42f), + InputControl.R3 to Box(x(1035f) + 135f * size, y(690f) - 100f * size, 42f), ) } else { face(1375f, 400f, 94f) + dpad(470f, 675f, 118f, kit.dpad) + mapOf( InputControl.LS to Box(x(235f), y(400f), 104f), InputControl.RS to Box(x(1130f), y(690f), 104f), - InputControl.L3 to Box(x(235f) + 150f * unit, y(400f) - 110f * unit, 42f), - InputControl.R3 to Box(x(1130f) + 160f * unit, y(690f) + 100f * unit, 42f), + InputControl.L3 to Box(x(235f) + 150f * size, y(400f) - 110f * size, 42f), + InputControl.R3 to Box(x(1130f) + 160f * size, y(690f) + 100f * size, 42f), ) } return elements.map { element -> @@ -131,16 +133,16 @@ internal class ControllerOverlayGeometry( ) private fun face(cx: Float, cy: Float, offset: Float) = mapOf( - InputControl.Y to Box(x(cx), y(cy) - offset * unit, 58f), - InputControl.X to Box(x(cx) - offset * unit, y(cy), 58f), - InputControl.B to Box(x(cx) + offset * unit, y(cy), 58f), - InputControl.A to Box(x(cx), y(cy) + offset * unit, 58f), + InputControl.Y to Box(x(cx), y(cy) - offset * size, 58f), + InputControl.X to Box(x(cx) - offset * size, y(cy), 58f), + InputControl.B to Box(x(cx) + offset * size, y(cy), 58f), + InputControl.A to Box(x(cx), y(cy) + offset * size, 58f), ) private fun dpad(cx: Float, cy: Float, radius: Float, style: DpadStyle): Map { val inner = radius * if (style == DpadStyle.DISC) .25f else .14f val depth = (radius - inner) / 2f - val offset = (radius - depth) * unit + val offset = (radius - depth) * size val side = radius * .25f return mapOf( InputControl.UP to Box(x(cx), y(cy) - offset, side, depth), @@ -160,12 +162,12 @@ internal class ControllerOverlayGeometry( } private inner class Box(val cx: Float, val cy: Float, val hw: Float, val hh: Float = hw) { - val width get() = max(hw * 2f * unit, w * .03f) + val width get() = max(hw * 2f * size, w * .03f) fun at(cx: Float) = Box(cx, cy, hw, hh) fun place(element: InputSurfaceElement): InputSurfaceElement { - val height = max(hh * 2f * unit, h * .03f) + val height = max(hh * 2f * size, h * .03f) return element.copy( x = (cx - width / 2f).coerceIn(safe.left.toFloat(), max(safe.left.toFloat(), w - safe.right - width)) / w, y = (cy - height / 2f).coerceIn(safe.top.toFloat(), max(safe.top.toFloat(), h - safe.bottom - height)) / h, diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt index ca8838a19..9532f5177 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputLibrary.kt @@ -27,6 +27,7 @@ data class SavedInputLayout( val rotatedScale: Float = 1f, ) { val editable get() = template == InputLayoutTemplate.CUSTOM + val overlay get() = (origin ?: id).let { it == InputLibraries.OVERLAY || it.startsWith("migrated.layout.") } val quickButtons get() = QUICK_BUTTONS.filter { button -> elements.any { it.quickButton == button } } fun withQuickButton(button: InputCommand, shown: Boolean) = copy(style = style.copy(hidden = if (shown) style.hidden - button else style.hidden + button)) diff --git a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt index e7e1c58a3..888e9bc26 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/InputSurfaceLayout.kt @@ -3,6 +3,8 @@ package com.droiddeck.launcher.input import android.content.Context import org.json.JSONArray import org.json.JSONObject +import kotlin.math.max +import kotlin.math.min enum class InputElementKind(val control: InputControl) { BUTTON(InputControl.A), STICK(InputControl.LS), TRACKPAD(InputControl.LEFT_PAD), @@ -63,6 +65,27 @@ internal fun List.scaled(factor: Float): List.groupOf(e: InputSurfaceElement) = if (e.dpadPiece) filter { it.dpadPiece } else listOf(e) + +internal fun List.frame(): InputSurfaceElement { + val left = minOf { it.x }; val top = minOf { it.y } + return first().copy(x = left, y = top, width = maxOf { it.x + it.width } - left, height = maxOf { it.y + it.height } - top) +} + +internal fun List.resizedAroundCentre(factor: Float): List { + if (isEmpty()) return this + val f = frame() + val k = factor.coerceIn(.03f / minOf { min(it.width, it.height) }, 1f / max(f.width, f.height)) + val w = f.width * k; val h = f.height * k + val left = (f.x + f.width / 2 - w / 2).coerceIn(0f, 1f - w) + val top = (f.y + f.height / 2 - h / 2).coerceIn(0f, 1f - h) + return map { it.copy(x = left + (it.x - f.x) * k, y = top + (it.y - f.y) * k, width = it.width * k, height = it.height * k).bounded() } +} + internal fun List.withPositionsOf(defaults: List): List { val byId = defaults.associateBy { it.id } return map { e -> byId[e.id]?.let { d -> e.copy(x = d.x, y = d.y, width = d.width, height = d.height).bounded() } ?: e } 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 5d81a356d..a944ec28e 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/LegacyGamepadLayout.kt @@ -334,7 +334,6 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid const val STICK_REACH = 1.25f const val LEGACY_DPAD_RADIUS_DP = 26f const val LEGACY_DPAD_OFFSET_DP = 52f - val DPAD = setOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) const val KEYBOARD_ID = "builtin:keyboard" const val KEYBOARD_RADIUS_DP = 18f const val KEYBOARD_X_DP = 90f 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 3f74de743..33d951a62 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenLayout.kt @@ -12,7 +12,7 @@ import kotlin.math.roundToInt object OnScreenLayout { internal fun supportsFloatingSteamControls(controller: String, layout: SavedInputLayout?) = - layout?.id == InputLibraries.OVERLAY && + layout?.overlay == true && (controller == SessionPrefs.CONTROLLER_DECK || controller == SessionPrefs.CONTROLLER_STEAM) data class Dock(val portrait: Boolean, val grips: Boolean, val left: InputControl = InputControl.LS, val right: InputControl = InputControl.RS) { @@ -156,7 +156,7 @@ 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] } - val aspectSize = layout.aspect.takeIf { layout.id == InputLibraries.OVERLAY && it > 0f } + val aspectSize = layout.aspect.takeIf { layout.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) diff --git a/app/src/main/java/com/droiddeck/launcher/input/PadKit.kt b/app/src/main/java/com/droiddeck/launcher/input/PadKit.kt index c976d310e..750bceb5c 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadKit.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/PadKit.kt @@ -42,7 +42,7 @@ internal enum class PadKit(val face: FaceGlyphs, val dpad: DpadStyle, val stick: companion object { fun forOverlay(context: Context, layout: SavedInputLayout?): PadKit? = - if (layout?.id != InputLibraries.OVERLAY) null else when (SessionPrefs.steamController(context)) { + if (layout?.overlay != true) null else when (SessionPrefs.steamController(context)) { SessionPrefs.CONTROLLER_DECK -> STEAM_DECK SessionPrefs.CONTROLLER_STEAM -> STEAM_CONTROLLER SessionPrefs.CONTROLLER_XBOX360 -> XBOX 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 ca701cebf..8f8e592cd 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceDialog.kt @@ -97,8 +97,13 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB var aspect by mutableFloatStateOf(layout.aspect) private var scaleBase by mutableStateOf, List, Float>?>(null) private var scaleValue by mutableIntStateOf(100) + private var pieceBase by mutableStateOf, List, Int>?>(null) + private var pieceLayout: List? = null + private val piece get() = element?.let { e -> (if (elements.any { it.id == e.id }) elements else shown).groupOf(e) }.orEmpty() + val scaleRange get() = if (piece.isEmpty()) 80..125 else 50..200 private val rescales get() = gamepad?.let { pad -> elements.any { it.id in pad.ids } } == true val scale get() = when { + piece.isNotEmpty() -> pieceBase?.takeIf { it.second == piece && pieceLayout == elements }?.third ?: 100 scaleBase?.second == elements -> scaleValue rescales -> ((if (canvas.width > 0 && canvas.height > 0 && aspect > 0f && (aspect < 1f) != (canvasAspect < 1f)) OnScreenLayout.oriented(layout, canvas.width, canvas.height).scale else layoutScale) * 100).roundToInt() @@ -143,6 +148,24 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB } else base.first.scaled(factor) scaleValue = percent; scaleBase = Triple(base.first, elements, base.third) } + fun scaleSelected(context: android.content.Context, percent: Int) { + val id = selected ?: return + val adopted = adoptDisplayedFrame(context) + val base = pieceBase?.takeIf { it.second == piece && pieceLayout == elements } ?: run { + if (!adopted && canvas.width > 0 && canvas.height > 0 && shown.isNotEmpty()) { + val open = toolsExpanded + beginEdit(shown, canvasAspect) + toolsExpanded = open + } else if (!adopted) checkpoint() + val start = elements.firstOrNull { it.id == id }?.let { elements.groupOf(it) } ?: return + Triple(start, start, 100) + } + val resized = base.first.resizedAroundCentre(percent / 100f) + val byId = resized.associateBy { it.id } + elements = elements.map { byId[it.id] ?: it } + pieceBase = Triple(base.first, resized, percent) + pieceLayout = elements + } var shown by mutableStateOf>(emptyList()) private val canvasAspect get() = canvas.width.toFloat() / canvas.height.coerceAtLeast(1) @@ -337,7 +360,7 @@ private fun SurfaceCanvas(draft: SurfaceDraft, transparent: Boolean = false) { view.onBeginEdit = { e, w, h -> draft.beginEdit(e, w.toFloat() / h, if (draft.hasGamepad) GamepadScaler(view.context, w, h, draft.layout) else null) } view.onResize = { w, h -> draft.canvas = IntSize(w, h) } view.onDisplayed = { draft.shown = it } - view.onChange = { e -> if (!draft.properties) draft.elements = draft.elements.map { if (it.id == e.id) e else it } } + view.onChange = { changed -> if (!draft.properties) changed.associateBy { it.id }.let { byId -> draft.elements = draft.elements.map { byId[it.id] ?: it } } } }) } @@ -407,8 +430,8 @@ private fun SurfaceToolbar(draft: SurfaceDraft, onSave: () -> Unit, onLeave: () } PrimaryButton(stringResource(R.string.input_editor_done), compact = true, onClick = onSave) } - if (draft.elements.isNotEmpty()) SliderRow(stringResource(R.string.ctrl_size), null, draft.scale, 80..125, step = 5, - format = { "$it%" }, modifier = Modifier.testTag("input-editor-scale")) { draft.scaleAll(context, it) } + if (draft.elements.isNotEmpty()) SliderRow(stringResource(R.string.ctrl_size), null, draft.scale, draft.scaleRange, step = 5, + format = { "$it%" }, modifier = Modifier.testTag("input-editor-scale")) { if (draft.element != null) draft.scaleSelected(context, it) else draft.scaleAll(context, it) } } @OptIn(ExperimentalLayoutApi::class) 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 d3922ef0c..301747dc1 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceEditorView.kt @@ -16,6 +16,9 @@ import com.droiddeck.launcher.input.PadKit import com.droiddeck.launcher.input.SavedInputLayout import com.droiddeck.launcher.input.InputSurfaceElement import com.droiddeck.launcher.input.InputSurfaceStyle +import com.droiddeck.launcher.input.dpadPiece +import com.droiddeck.launcher.input.frame +import com.droiddeck.launcher.input.groupOf import com.droiddeck.launcher.input.snapSurfaceElement import kotlin.math.abs @@ -41,13 +44,13 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { set(value) { field = value; invalidate() } var onSelect: (String?) -> Unit = {} var onBeginEdit: (List, Int, Int) -> Unit = { _, _, _ -> } - var onChange: (InputSurfaceElement) -> Unit = {} + var onChange: (List) -> Unit = {} var onResize: (Int, Int) -> Unit = { _, _ -> } var onDisplayed: (List) -> 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 } private val radius = 14 * resources.displayMetrics.density - private var original: InputSurfaceElement? = null + private var original: List = emptyList() private var downX = 0f private var downY = 0f private var corner = 0 @@ -100,13 +103,15 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { 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 = visibleElements.firstOrNull { it.id == selected } ?: return + val e = visibleElements.firstOrNull { it.id == selected }?.let { visibleElements.groupOf(it).frame() } ?: 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 for (x in listOf(l, r)) for (y in listOf(t, b)) canvas.drawCircle(x, y, radius / 3, paint) } + private fun contains(e: InputSurfaceElement, x: Float, y: Float) = x / width in e.x..(e.x + e.width) && y / height in e.y..(e.y + e.height) + private fun clearGuides() { guideX = null; guideY = null; invalidate() } override fun onInterceptTouchEvent(event: MotionEvent) = true @@ -118,7 +123,7 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { MotionEvent.ACTION_DOWN -> { clearGuides() downX = event.x; downY = event.y; dragging = false; corner = 0 - val current = visibleElements.firstOrNull { it.id == selected } + val current = visibleElements.firstOrNull { it.id == selected }?.let { visibleElements.groupOf(it).frame() } 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 @@ -126,13 +131,16 @@ 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 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) + val touched = if (corner != 0) visibleElements.firstOrNull { it.id == selected } + else visibleElements.asReversed().firstOrNull { contains(it, downX, downY) } + ?: visibleElements.filter { it.dpadPiece }.takeIf { it.isNotEmpty() && contains(it.frame(), downX, downY) } + ?.let { pad -> pad.firstOrNull { it.id == selected } ?: pad.first() } + original = touched?.let { visibleElements.groupOf(it) }.orEmpty() + selected = touched?.id; onSelect(selected) } MotionEvent.ACTION_MOVE -> { - val e = original ?: return true + if (original.isEmpty()) return true + val e = original.frame() if (!dragging && abs(event.x - downX) + abs(event.y - downY) > ViewConfiguration.get(context).scaledTouchSlop) { dragging = true; onBeginEdit(visibleElements, width, height) } @@ -140,20 +148,24 @@ internal class InputSurfaceEditorView(context: Context) : FrameLayout(context) { val dx = (event.x - downX) / width; val dy = (event.y - downY) / height val next = if (corner == 0) e.copy(x = (e.x + dx).coerceIn(0f, 1f - e.width), y = (e.y + dy).coerceIn(0f, 1f - e.height)) else { - val left = if (corner and 1 != 0) (e.x + dx).coerceIn(0f, e.x + e.width - .03f) else e.x - val top = if (corner and 4 != 0) (e.y + dy).coerceIn(0f, e.y + e.height - .03f) else e.y - val right = if (corner and 2 != 0) (e.x + e.width + dx).coerceIn(left + .03f, 1f) else e.x + e.width - val bottom = if (corner and 8 != 0) (e.y + e.height + dy).coerceIn(top + .03f, 1f) else e.y + e.height + val minW = (.03f * e.width / original.minOf { it.width }).coerceIn(.03f, e.width.coerceAtLeast(.03f)) + val minH = (.03f * e.height / original.minOf { it.height }).coerceIn(.03f, e.height.coerceAtLeast(.03f)) + val left = if (corner and 1 != 0) (e.x + dx).coerceIn(0f, (e.x + e.width - minW).coerceAtLeast(0f)) else e.x + val top = if (corner and 4 != 0) (e.y + dy).coerceIn(0f, (e.y + e.height - minH).coerceAtLeast(0f)) else e.y + val right = if (corner and 2 != 0) (e.x + e.width + dx).coerceIn((left + minW).coerceAtMost(1f), 1f) else e.x + e.width + val bottom = if (corner and 8 != 0) (e.y + e.height + dy).coerceIn((top + minH).coerceAtMost(1f), 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, visibleElements.filter { it.id != e.id }, 9 * resources.displayMetrics.density / width, + val aligned = if (snap) snapSurfaceElement(next, visibleElements.filter { v -> original.none { it.id == v.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 - onChange(aligned?.element ?: next) + val f = aligned?.element ?: next + val sx = f.width / e.width; val sy = f.height / e.height + onChange(original.map { it.copy(x = f.x + (it.x - e.x) * sx, y = f.y + (it.y - e.y) * sy, width = it.width * sx, height = it.height * sy) }) invalidate() } - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { original = null; dragging = false; clearGuides() } + MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { original = emptyList(); dragging = false; clearGuides() } } return true } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/PadArt.kt b/app/src/main/java/com/droiddeck/launcher/ui/PadArt.kt index f92e69424..e0ae4c948 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/PadArt.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/PadArt.kt @@ -50,6 +50,7 @@ internal class PadArt(context: Context) { InputControl.LB, InputControl.RB, InputControl.LT, InputControl.RT -> shoulder(canvas, w, h, control, kit, pressed) InputControl.L4, InputControl.L5, InputControl.R4, InputControl.R5 -> grip(canvas, w, h, control.label, pressed) InputControl.L3, InputControl.R3 -> small(canvas, w, h, control.label, pressed) + InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT -> arrow(canvas, w, h, control, pressed) InputControl.SELECT, InputControl.START, InputControl.GUIDE, InputControl.QAM -> { val (shape, icon) = systemLook(kit, control) system(canvas, w, h, shape, icon, pressed) @@ -238,6 +239,19 @@ internal class PadArt(context: Context) { op(bar, Path.Op.UNION) } + fun arrow(canvas: Canvas, w: Float, h: Float, control: InputControl, pressed: Boolean) { + val cx = w / 2f; val cy = h / 2f + val s = min(w, h) / 2f - dp(2f) + val (dx, dy) = when (control) { + InputControl.UP -> 0f to -1f + InputControl.RIGHT -> 1f to 0f + InputControl.DOWN -> 0f to 1f + else -> -1f to 0f + } + glass(canvas, roundRect(cx - s, cy - s, cx + s, cy + s, s * .32f), pressed) + chevron(canvas, cx, cy, dx, dy, s * .3f, if (pressed) ON_ACCENT else INK, s * .14f) + } + /** [held] bits: up 1, right 2, down 4, left 8. */ fun dpad(canvas: Canvas, w: Float, h: Float, kit: PadKit, held: Int, chevrons: Boolean = true, ink: Int = INK) { val cx = w / 2f; val cy = h / 2f 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 79de797f0..d465b87a6 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenLayoutTest.kt @@ -514,6 +514,32 @@ class OnScreenLayoutTest { } } + @Test @Config(qualifiers = "w800dp-h450dp-land-320dpi") + fun buttonSizeGrowsAndShrinksControlsOnAScreenWhoseHeightAlreadyLimitsTheLayout() { + val (display, _) = InputLibraries.primaryDisplay(context) + val stock = gamepad() + fun at(scale: Float) = OnScreenLayout.elements(context, stock.copy(scale = scale, + elements = LegacyGamepadLayout(display, 1600, 900, scale = scale).regenerate(stock.elements)), 1600, 900, Rect()) + fun List.centre(controls: List) = controls.map { at(it) }.let { c -> + c.sumOf { (it.x + it.width / 2).toDouble() } / c.size to c.sumOf { (it.y + it.height / 2).toDouble() } / c.size } + val face = listOf(InputControl.A, InputControl.B, InputControl.X, InputControl.Y) + val dpad = listOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT) + for (kit in kits) { + SessionPrefs.setSteamController(context, kit) + val normal = at(1f) + for (scale in listOf(.8f, 1.25f)) { + val placed = at(scale) + assertEquals("$kit $scale", normal.at(InputControl.A).width * scale, placed.at(InputControl.A).width, .002f) + assertEquals("$kit $scale", normal.at(InputControl.LS).height * scale, placed.at(InputControl.LS).height, .002f) + if (scale > 1f) for (cluster in listOf(face, dpad)) { + val (x0, y0) = normal.centre(cluster); val (x1, y1) = placed.centre(cluster) + assertEquals("$kit", x0, x1, .01); assertEquals("$kit", y0, y1, .01) + } + assertInside(placed, 1600, 900) + } + } + } + @Test @Config(qualifiers = "w1280dp-h800dp-land-mdpi") fun tabletOverlaysStayInTheLowerHalfOfTheScreen() { for (kit in kits) { diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt index ed1d58f2b..cda4bbeb9 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSettingsTest.kt @@ -522,6 +522,69 @@ class InputSettingsTest { } } + @Test fun buttonSizeResizesOnlyTheSelectedControlAndEverythingWithoutASelection() { + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.PRIMARY)!! + compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.PRIMARY)) } } + fun editor() = generateSequence(listOf(ShadowDialog.getLatestDialog().window!!.decorView)) { views -> + views.filterIsInstance().flatMap { g -> (0 until g.childCount).map(g::getChildAt) }.ifEmpty { null } + }.flatten().filterIsInstance().first() + val stock = compose.runOnIdle { editor().rendered().associateBy { it.id } } + compose.onNodeWithTag("input-editor-tools").performClick() + compose.runOnIdle { editor().onSelect("legacy:a") } + val slider = compose.onNodeWithTag("input-editor-scale").performScrollTo() + slider.assert(SemanticsMatcher.expectValue(androidx.compose.ui.semantics.SemanticsProperties.StateDescription, "100%")) + slider.performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(150f) } + val one = compose.runOnIdle { editor().rendered().associateBy { it.id } } + assertEquals(stock.getValue("legacy:a").width * 1.5f, one.getValue("legacy:a").width, .002f) + for (id in listOf("legacy:b", "legacy:x", "legacy:ls")) stock.getValue(id).let { e -> one.getValue(id).let { o -> + assertEquals(id, e.x, o.x, .0001f); assertEquals(id, e.y, o.y, .0001f); assertEquals(id, e.width, o.width, .0001f) } } + compose.runOnIdle { editor().onSelect("legacy:up") } + slider.performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(150f) } + val pad = compose.runOnIdle { editor().rendered().associateBy { it.id } } + for (id in listOf("legacy:up", "legacy:down", "legacy:left", "legacy:right")) assertEquals(id, stock.getValue(id).height * 1.5f, pad.getValue(id).height, .002f) + compose.runOnIdle { assertEquals(4, editor().artworkDpad().size) } + compose.runOnIdle { editor().onSelect(null) } + slider.assert(SemanticsMatcher.expectValue(androidx.compose.ui.semantics.SemanticsProperties.StateDescription, "100%")) + slider.performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(110f) } + val all = compose.runOnIdle { editor().rendered().associateBy { it.id } } + assertEquals(stock.getValue("legacy:ls").width * 1.1f, all.getValue("legacy:ls").width, .003f) + assertEquals(stock.getValue("legacy:a").width * 1.65f, all.getValue("legacy:a").width, .004f) + compose.onNodeWithText(context.getString(R.string.input_editor_done)).performScrollTo().performClick() + compose.runOnIdle { assertEquals(1.1f, InputLibraries.read(context).layouts.first { it.id == layout.id }.scale) } + } + + @Test fun undoAfterResizingAControlAgainKeepsAnotherControlsMove() { + val library = InputLibraries.read(context) + val layout = library.layout("game", InputRole.PRIMARY)!! + compose.setContent { DroidDeckTheme { InputSurfaceDialog("game", {}, {}, role = InputRole.PRIMARY, + layoutId = layout.id, bindingsId = library.profile("game").binding(InputRole.PRIMARY)) } } + fun editor() = generateSequence(listOf(ShadowDialog.getLatestDialog().window!!.decorView)) { views -> + views.filterIsInstance().flatMap { g -> (0 until g.childCount).map(g::getChildAt) }.ifEmpty { null } + }.flatten().filterIsInstance().first() + compose.onNodeWithTag("input-editor-tools").performClick() + compose.runOnIdle { editor().onSelect("legacy:a") } + fun slider() = compose.onNodeWithTag("input-editor-scale").performScrollTo() + slider().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(150f) } + val resized = compose.runOnIdle { editor().rendered().associateBy { it.id } } + compose.runOnIdle { + val view = editor() + view.onBeginEdit(view.rendered(), view.width, view.height) + view.onChange(listOf(view.rendered().first { it.id == "legacy:b" }.let { it.copy(x = it.x - .1f) })) + } + val moved = compose.runOnIdle { editor().rendered().associateBy { it.id } } + assertEquals(resized.getValue("legacy:b").x - .1f, moved.getValue("legacy:b").x, .002f) + compose.onNodeWithTag("input-editor-tools").performClick() + compose.runOnIdle { editor().onSelect("legacy:a") } + slider().assert(SemanticsMatcher.expectValue(androidx.compose.ui.semantics.SemanticsProperties.StateDescription, "100%")) + slider().performSemanticsAction(androidx.compose.ui.semantics.SemanticsActions.SetProgress) { it(120f) } + compose.onNodeWithText(context.getString(R.string.input_editor_undo)).performScrollTo().performClick() + val undone = compose.runOnIdle { editor().rendered().associateBy { it.id } } + assertEquals(moved.getValue("legacy:b").x, undone.getValue("legacy:b").x, .0001f) + assertEquals(resized.getValue("legacy:a").width, undone.getValue("legacy:a").width, .0001f) + } + @Test fun buttonSizeShowsTheGamepadWhereItWillBeSaved() { val library = InputLibraries.read(context) val layout = library.layout("game", InputRole.PRIMARY)!! diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceEditorViewTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceEditorViewTest.kt index a6cbb2ab9..d322a387d 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceEditorViewTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceEditorViewTest.kt @@ -62,7 +62,6 @@ class InputSurfaceEditorViewTest { private fun field(target: Any, name: String) = target.javaClass.getDeclaredField(name).also { it.isAccessible = true }.get(target) private fun InputSurfaceEditorView.controls() = (field(field(this, "preview")!!, "customCanvas") as ViewGroup).let { canvas -> (0 until canvas.childCount).map(canvas::getChildAt) } - private fun InputSurfaceEditorView.artworkDpad() = field(field(this, "preview")!!, "artworkDpad") as List<*> private fun InputSurfaceEditorView.rects() = controls().map { listOf(it.left, it.top, it.width, it.height) } private fun InputSurfaceEditorView.assertControlsDrawnWhereDisplayed() { @@ -140,6 +139,25 @@ class InputSurfaceEditorViewTest { assertEquals(4, view.artworkDpad().size) } + @Test fun copiesOfTheOverlayDrawTheSameKitArtworkInTheSamePlaces() { + val source = overlay() + val copy = source.derived("copy", "Copy") + val again = copy.derived("again", "Again") + for (layout in listOf(copy, again)) { + assertEquals(PadKit.forOverlay(context, source), PadKit.forOverlay(context, layout)) + for ((w, h) in listOf(2400 to 1504, 1504 to 2400)) { + assertEquals(OnScreenLayout.editorElements(context, source, w, h), + OnScreenLayout.editorElements(context, layout, w, h)) + } + assertEquals(4, editor(layout, 2400, 1504).artworkDpad().size) + } + assertNotNull(PadKit.forOverlay(context, source.copy(id = "migrated.layout.2400x1504"))) + assertEquals(null, PadKit.forOverlay(context, this.layout)) + assertEquals(null, PadKit.forOverlay(context, this.layout.derived("custom-copy", "Copy"))) + SessionPrefs.setSteamController(context, SessionPrefs.CONTROLLER_NONE) + assertEquals(null, PadKit.forOverlay(context, copy)) + } + @Config(qualifiers = "xhdpi") @Test fun controlsAddedForTheScreenAreDrawnOnTheFirstFrameInBothOrientations() { SessionState.mode = SessionService.MODE_STEAM @@ -203,6 +221,57 @@ class InputSurfaceEditorViewTest { assertEquals(a.width, moved.width, .0001f) } + @Test fun theDpadMovesAndResizesAsOneControlFromAnyArrowOrItsCentre() { + val view = editor(overlay(), 2400, 1504) + val arrows = listOf("legacy:up", "legacy:right", "legacy:down", "legacy:left") + fun pad() = view.rendered().filter { it.id in arrows }.associateBy { it.id } + val before = pad() + assertEquals(4, view.artworkDpad().size) + val changes = mutableListOf() + view.onBeginEdit = { shown, w, h -> view.aspect = w.toFloat() / h; view.elements = shown } + view.onChange = { changes += it; view.elements = view.elements.map { e -> it.firstOrNull { c -> c.id == e.id } ?: e } } + val left = before.getValue("legacy:left"); val right = before.getValue("legacy:right") + val x = (left.x + left.width + right.x) / 2 * 2400; val y = (left.y + left.height / 2) * 1504 + view.touch(MotionEvent.ACTION_DOWN, x, y) + view.touch(MotionEvent.ACTION_MOVE, x + 240f, y - 150f) + view.touch(MotionEvent.ACTION_UP, x + 240f, y - 150f) + val moved = changes.takeLast(4).associateBy { it.id } + assertEquals(arrows.toSet(), moved.keys) + for (id in arrows) { + assertEquals(id, before.getValue(id).x + 240f / 2400, moved.getValue(id).x, .0001f) + assertEquals(id, before.getValue(id).y - 150f / 1504, moved.getValue(id).y, .0001f) + } + assertEquals(4, view.artworkDpad().size) + val down = moved.getValue("legacy:down"); val r = moved.getValue("legacy:right") + val cx = (r.x + r.width) * 2400; val cy = (down.y + down.height) * 1504 + view.touch(MotionEvent.ACTION_DOWN, cx, cy) + view.touch(MotionEvent.ACTION_MOVE, cx + 80f, cy + 80f) + view.touch(MotionEvent.ACTION_UP, cx + 80f, cy + 80f) + val resized = changes.takeLast(4).associateBy { it.id } + assertEquals(arrows.toSet(), resized.keys) + assertTrue(resized.getValue("legacy:up").width > moved.getValue("legacy:up").width) + assertEquals(4, view.artworkDpad().size) + } + + @Test fun shrinkingTheDpadStopsBeforeAnyArrowIsSmallerThanASingleControlCanBe() { + val view = editor(overlay(), 2400, 1504) + val arrows = listOf("legacy:up", "legacy:right", "legacy:down", "legacy:left") + val pad = view.rendered().filter { it.id in arrows } + val changes = mutableListOf() + view.onBeginEdit = { shown, w, h -> view.aspect = w.toFloat() / h; view.elements = shown } + view.onChange = { changes += it; view.elements = view.elements.map { e -> it.firstOrNull { c -> c.id == e.id } ?: e } } + val right = pad.maxOf { it.x + it.width } * 2400; val bottom = pad.maxOf { it.y + it.height } * 1504 + val (ux, uy) = view.centre("legacy:up") + view.touch(MotionEvent.ACTION_DOWN, ux, uy) + view.touch(MotionEvent.ACTION_UP, ux, uy) + view.touch(MotionEvent.ACTION_DOWN, right, bottom) + view.touch(MotionEvent.ACTION_MOVE, 0f, 0f) + view.touch(MotionEvent.ACTION_UP, 0f, 0f) + val shrunk = changes.takeLast(4) + assertEquals(arrows.toSet(), shrunk.map { it.id }.toSet()) + shrunk.forEach { assertTrue(it.id, it.width >= .0299f && it.height >= .0299f) } + } + @Test fun aMouseSelectsDragsAndResizesLikeATouch() { val view = editor(overlay(), 2400, 1504) val a = view.rendered().first { it.id == "legacy:a" } @@ -236,3 +305,6 @@ internal fun InputSurfaceEditorView.place(w: Int, h: Int) { @Suppress("UNCHECKED_CAST") internal fun InputSurfaceEditorView.rendered() = javaClass.getDeclaredField("visibleElements").also { it.isAccessible = true }.get(this) as List + +internal fun InputSurfaceEditorView.artworkDpad() = javaClass.getDeclaredField("preview").also { it.isAccessible = true }.get(this)!!.let { preview -> + preview.javaClass.getDeclaredField("artworkDpad").also { it.isAccessible = true }.get(preview) as List<*> }