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

@@ -176,7 +176,7 @@ internal class LegacyGamepadLayout(private val context: Context, private val wid
}
fun elements(): List<InputSurfaceElement> {
fun elements(previousStickGeometry: Boolean = false): List<InputSurfaceElement> {
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
@@ -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<InputSurfaceElement> =
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<InputSurfaceElement>): List<InputSurfaceElement> {
val byId = displayed.associateBy { it.id }
return layout.elements.map { byId[it.id] ?: it }
}
fun fit(elements: List<InputSurfaceElement>, referenceWidth: Float, referenceHeight: Float, width: Int, height: Int, safe: Rect): List<InputSurfaceElement> {
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)
@@ -59,7 +59,7 @@ import com.droiddeck.launcher.input.*
import com.droiddeck.launcher.session.SessionState
private data class SurfaceSnapshot(val elements: List<InputSurfaceElement>, val actions: Map<String, InputAction>,
val clicks: Map<String, InputAction>, val scale: Float)
val clicks: Map<String, InputAction>, 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<List<SurfaceSnapshot>>(emptyList())
var layoutScale by mutableFloatStateOf(layout.scale)
var aspect by mutableFloatStateOf(layout.aspect)
private var scaleBase by mutableStateOf<Triple<List<InputSurfaceElement>, List<InputSurfaceElement>, 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<InputSurfaceElement>, 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 } }
})
@@ -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<InputSurfaceElement> = 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<InputSurfaceElement>, 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<InputSurfaceElement> = 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