fix(input): keep built-in styling and stick reach on duplicated controls

Built-in status is now a saved property of the element instead of being
read from its id, so a duplicate renders and responds like its source.
This commit is contained in:
Kurt Himebauch committed 2026-11-11 18:57:45 +00:00
1 parent cc29854356
commit 7c5cdac9e5
4 files changed
+37 -6

No files matched your search

@@ -304,7 +304,7 @@ object InputLibraries {
for (e in layout.elements) elements.put(JSONObject().put("id", e.id).put("kind", e.kind.name)
.put("control", e.control.id).put("name", e.name).put("x", e.x.toDouble()).put("y", e.y.toDouble())
.put("width", e.width.toDouble()).put("height", e.height.toDouble()).put("floating", e.floating).put("hideWhenIdle", e.hideWhenIdle).put("mouseGestures", e.mouseGestures).put("toggle", e.toggle).put("icon", e.icon).put("image", e.image)
.put("elementOpacity", e.opacity.toDouble()).put("buttonShape", e.buttonShape.name).put("showLabel", e.showLabel).put("alwaysShown", e.alwaysShown))
.put("elementOpacity", e.opacity.toDouble()).put("buttonShape", e.buttonShape.name).put("showLabel", e.showLabel).put("alwaysShown", e.alwaysShown).put("builtIn", e.builtIn))
layouts.put(JSONObject().put("id", layout.id).put("name", layout.name).put("template", layout.template.name).put("elements", elements)
.put("tint", layout.style.tint ?: JSONObject.NULL).put("opacity", layout.style.opacity).put("screen", layout.screen.name).put("aspect", layout.aspect.toDouble()).put("scale", layout.scale.toDouble())
.put("hidden", JSONArray(layout.style.hidden.map { it.name }.sorted())).put("origin", layout.origin ?: JSONObject.NULL))
@@ -341,11 +341,13 @@ object InputLibraries {
val doc = JSONObject(raw); require(doc.getInt("version") in 2..VERSION)
val layouts = doc.getJSONArray("layouts").objects(256).map { layout ->
val elements = layout.getJSONArray("elements").objects(64).map { e ->
InputSurfaceElement(requireNotNull(InputControl.fromId(e.getString("control"))), e.getDouble("x").toFloat(),
val control = requireNotNull(InputControl.fromId(e.getString("control")))
InputSurfaceElement(control, e.getDouble("x").toFloat(),
e.getDouble("y").toFloat(), e.getDouble("width").toFloat(), e.getDouble("height").toFloat(),
e.getString("id"), InputElementKind.valueOf(e.getString("kind")), e.getString("name"),
e.optBoolean("floating", true), e.optBoolean("hideWhenIdle", false), e.optBoolean("mouseGestures", false), e.optBoolean("toggle", false), e.optInt("icon", 0), e.optString("image", ""),
e.optDouble("elementOpacity", 1.0).toFloat(), InputButtonShape.valueOf(e.optString("buttonShape", InputButtonShape.ROUNDED.name)), e.optBoolean("showLabel", true), e.optBoolean("alwaysShown", false))
e.optDouble("elementOpacity", 1.0).toFloat(), InputButtonShape.valueOf(e.optString("buttonShape", InputButtonShape.ROUNDED.name)), e.optBoolean("showLabel", true), e.optBoolean("alwaysShown", false),
e.optBoolean("builtIn", isBuiltInId(e.getString("id"), control)))
}
SavedInputLayout(layout.getString("id"), layout.getString("name"), elements, InputLayoutTemplate.valueOf(layout.getString("template")),
InputSurfaceStyle(if (!layout.has("tint")) ControllerPrefs.STEAM_BLUE else if (layout.isNull("tint")) null else layout.getInt("tint"), layout.optInt("opacity", 100),
@@ -36,13 +36,17 @@ data class InputSurfaceElement(
val buttonShape: InputButtonShape = InputButtonShape.ROUNDED,
val showLabel: Boolean = true,
val alwaysShown: Boolean = false,
val builtIn: Boolean = isBuiltInId(id, control),
) {
val classicStyle get() = builtIn && (buttonShape == InputButtonShape.CIRCLE || buttonShape == InputButtonShape.PILL)
val hasClick get() = kind == InputElementKind.STICK || kind == InputElementKind.TRACKPAD
fun bounded() = copy(x = x.coerceIn(0f, 0.97f), y = y.coerceIn(0f, 0.97f),
width = width.coerceIn(0.03f, (1f - x.coerceIn(0f, 0.97f)).coerceAtLeast(0.03f)), height = height.coerceIn(0.03f, (1f - y.coerceIn(0f, 0.97f)).coerceAtLeast(0.03f)))
}
internal fun isBuiltInId(id: String, control: InputControl) = id == "legacy:${control.id}" || id == "builtin:keyboard"
private const val DUPLICATE_OFFSET = .04f
private const val DUPLICATE_ATTEMPTS = 25
private val DUPLICATE_DIRECTIONS = listOf(1 to 1, -1 to -1, 1 to -1, -1 to 1)
@@ -356,7 +356,7 @@ class InputSurfaceView(
canvas.drawBitmap(it, null, imageRect, labelPaint)
return
}
if (classic && element.id == "builtin:keyboard" && element.icon == InputButtonIcons.KEYBOARD) { drawKeyboard(canvas, cx, height / 2f, lit); return }
if (classic && element.icon == InputButtonIcons.KEYBOARD) { drawKeyboard(canvas, cx, height / 2f, lit); return }
if (element.icon != 0 && InputButtonIcons.draw(canvas, element.icon, cx, height / 2f, min(width, height) * .68f, labelPaint, symbol)) return
if (!element.showLabel) return
labelPaint.style = Paint.Style.FILL
@@ -704,8 +704,7 @@ class InputSurfaceView(
private fun adaptive(element: InputSurfaceElement) = element.kind == InputElementKind.STICK && element.floating && element.hideWhenIdle
private fun classic(element: InputSurfaceElement) = (element.id == "legacy:${element.control.id}" || element.id == "builtin:keyboard") &&
(element.buttonShape == InputButtonShape.CIRCLE || element.buttonShape == InputButtonShape.PILL)
private fun classic(element: InputSurfaceElement) = element.classicStyle
private val DIRECTIONS = listOf(InputControl.UP, InputControl.RIGHT, InputControl.DOWN, InputControl.LEFT)
@@ -286,4 +286,30 @@ class InputLayoutToolsTest {
all = all + copy
}
}
@Test fun duplicateOfBuiltInControlKeepsClassicStyle() {
for ((control, shape) in listOf(InputControl.LS to InputButtonShape.CIRCLE, InputControl.SELECT to InputButtonShape.PILL, InputControl.A to InputButtonShape.CIRCLE)) {
val source = InputSurfaceElement(control, .3f, .3f, .2f, .2f, buttonShape = shape)
val copy = source.duplicate("copy", listOf(source))
assertTrue(source.classicStyle)
assertEquals(source.classicStyle, copy.classicStyle)
assertNotEquals(source.id, copy.id)
}
}
@Test fun customControlsAreNeverClassic() {
val custom = InputSurfaceElement(InputControl.A, .3f, .3f, .2f, .2f, "custom", buttonShape = InputButtonShape.CIRCLE)
assertFalse(custom.classicStyle)
assertFalse(custom.duplicate("copy", listOf(custom)).classicStyle)
}
@Test fun classicStyleSurvivesSavingAndRoundedShapesStayPlain() {
val source = InputSurfaceElement(InputControl.B, .3f, .3f, .2f, .2f, buttonShape = InputButtonShape.PILL)
val copy = source.duplicate("copy", listOf(source))
val library = InputLibraries.read(context)
val layout = library.layout("game", InputRole.SECONDARY)!!.copy(elements = listOf(source, copy))
val restored = InputLibraries.decode(InputLibraries.encode(library.withLayout(layout)))!!.layout("game", InputRole.SECONDARY)!!.elements
assertEquals(listOf(true, true), restored.map { it.classicStyle })
assertFalse(source.copy(buttonShape = InputButtonShape.ROUNDED).duplicate("r", listOf(source)).classicStyle)
}
}