mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 12:56:06 +00:00
fix(input): the layout editor shows the same placement as the session (#2783)
* fix(input): the layout editor shows the same placement as the session Editing a layout stored for one orientation while the editor is open in the other drew raw stored coordinates, so elements turned into flat, overlapping bars. Route the editor through OnScreenLayout like the on-screen controls: matched layouts are regenerated for the current screen, everything else follows the same fit, and the dirty check compares against what was shown when the editor opened. * fix(input): reset and scale in the editor frame the session saves The editor draft now carries the frame it was rebased to: the gamepad scaler is rebuilt with the editor surface's real dimensions when edits begin, so reset regenerates legacy controls for the display the user is editing instead of the primary display. Stock default positions keep their own frame aspect and are refitted into the draft frame before being applied, so reset on a differently shaped display lands where the editor shows and no longer marks an untouched layout as changed. * fix(input): refit only the default positions on reset and update the test callers --------- Co-authored-by: maxjivi05 <maxjividen@gmail.com>
This commit is contained in:
1 parent
cca84f7b6b
commit
fc5dd7f3c5
5 files changed
+29
-18
No files matched your search
@@ -138,16 +138,16 @@ object InputLibraries {
|
||||
update(context) { library -> library.layouts.firstOrNull { it.id == layoutId && it.editable }?.let { library.withLayout(it.withQuickButton(button, shown)) } ?: library }
|
||||
}.isSuccess
|
||||
|
||||
fun defaultPositions(context: Context, library: InputLibrary, layout: SavedInputLayout): List<InputSurfaceElement>? {
|
||||
fun defaultPositions(context: Context, library: InputLibrary, layout: SavedInputLayout): Pair<List<InputSurfaceElement>, Float>? {
|
||||
val origin = layout.origin ?: layout.id
|
||||
InputStockLayouts.layouts().firstOrNull { it.id == origin }?.let { return it.elements }
|
||||
InputStockLayouts.layouts().firstOrNull { it.id == origin }?.let { return it.elements to it.aspect }
|
||||
if (origin == OVERLAY || origin.startsWith("migrated.layout.")) {
|
||||
val (surfaceContext, metrics) = primaryDisplay(context)
|
||||
val size = if (origin == OVERLAY) listOf(metrics.widthPixels, metrics.heightPixels)
|
||||
else origin.removePrefix("migrated.layout.").split('x').mapNotNull { it.toIntOrNull() }
|
||||
return size.takeIf { it.size == 2 && it.all { side -> side > 0 } }?.let { LegacyGamepadLayout(surfaceContext, it[0], it[1]).elements() }
|
||||
return size.takeIf { it.size == 2 && it.all { side -> side > 0 } }?.let { LegacyGamepadLayout(surfaceContext, it[0], it[1]).elements() to it[0].toFloat() / it[1] }
|
||||
}
|
||||
return library.layouts.firstOrNull { it.id == origin && it.id != layout.id }?.elements
|
||||
return library.layouts.firstOrNull { it.id == origin && it.id != layout.id }?.let { it.elements to it.aspect }
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.droiddeck.launcher.ui
|
||||
|
||||
import android.app.Presentation
|
||||
import android.graphics.Rect
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.os.Bundle
|
||||
import android.os.Handler
|
||||
@@ -37,6 +38,7 @@ import androidx.compose.ui.layout.onSizeChanged
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.unit.IntOffset
|
||||
import androidx.compose.ui.unit.IntSize
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.roundToInt
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
@@ -69,7 +71,8 @@ private class GamepadScaler(private val context: android.content.Context, privat
|
||||
}
|
||||
|
||||
private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputBindings, val defaults: List<InputSurfaceElement>? = null,
|
||||
private val gamepad: GamepadScaler? = null, snap: Boolean = true) {
|
||||
private val defaultsFrame: Float = 0f, private var gamepad: GamepadScaler? = null, snap: Boolean = true) {
|
||||
val hasGamepad get() = gamepad != null
|
||||
var elements by mutableStateOf(layout.elements)
|
||||
var actions by mutableStateOf(preset.elements)
|
||||
var clicks by mutableStateOf(preset.clicks)
|
||||
@@ -86,30 +89,37 @@ private class SurfaceDraft(val layout: SavedInputLayout, val preset: SavedInputB
|
||||
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 }
|
||||
private val rescales get() = gamepad?.let { pad -> elements.any { it.id in pad.ids } } == true
|
||||
val scale get() = when {
|
||||
scaleBase?.second == elements -> scaleValue
|
||||
rescales -> (layoutScale * 100).roundToInt()
|
||||
else -> 100
|
||||
}
|
||||
val positioned get() = (if (rescales) gamepad!!.reset(elements) else defaults?.let { elements.withPositionsOf(it) })
|
||||
val positioned get() = (if (rescales) gamepad!!.reset(elements) else defaults?.let { elements.withPositionsOf(refit(it, defaultsFrame)) })
|
||||
?.takeIf { it != elements || rescales && layoutScale != 1f }
|
||||
|
||||
private fun refit(placed: List<InputSurfaceElement>, from: Float): List<InputSurfaceElement> {
|
||||
if (from <= 0f || aspect <= 0f || abs(from / aspect - 1f) < .01f) return placed
|
||||
return OnScreenLayout.fit(placed, from, 1f, (aspect * 1000).roundToInt().coerceAtLeast(1), 1000, Rect())
|
||||
}
|
||||
val element get() = elements.firstOrNull { it.id == selected }
|
||||
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) {
|
||||
fun beginEdit(displayed: List<InputSurfaceElement>, aspect: Float, scaler: GamepadScaler? = gamepad) {
|
||||
checkpoint()
|
||||
elements = OnScreenLayout.editorFrame(layout.copy(elements = elements), displayed)
|
||||
this.aspect = aspect
|
||||
scaler?.let { gamepad = it }
|
||||
}
|
||||
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
|
||||
elements = if (base.third > 0f && gamepad != null) {
|
||||
val pad = gamepad
|
||||
elements = if (base.third > 0f && pad != null) {
|
||||
layoutScale = factor
|
||||
val others = base.first.filterNot { it.id in gamepad.ids }.scaled(factor / base.third).associateBy { it.id }
|
||||
gamepad.rescale(base.first, base.third, factor).map { others[it.id] ?: it }
|
||||
val others = base.first.filterNot { it.id in pad.ids }.scaled(factor / base.third).associateBy { it.id }
|
||||
pad.rescale(base.first, base.third, factor).map { others[it.id] ?: it }
|
||||
} else base.first.scaled(factor)
|
||||
scaleValue = percent; scaleBase = Triple(base.first, elements, base.third)
|
||||
}
|
||||
@@ -134,7 +144,8 @@ fun InputSurfaceDialog(profile: String, onDismiss: () -> Unit, onChanged: () ->
|
||||
val (w, h) = OnScreenLayout.generatedSize(context, savedLayout, metrics.widthPixels, metrics.heightPixels) ?: (metrics.widthPixels to metrics.heightPixels)
|
||||
GamepadScaler(display, w, h)
|
||||
}
|
||||
SurfaceDraft(savedLayout, savedBindings, InputLibraries.defaultPositions(context, library, savedLayout), gamepad, ControllerPrefs.snap(context))
|
||||
val defaults = InputLibraries.defaultPositions(context, library, savedLayout)
|
||||
SurfaceDraft(savedLayout, savedBindings, defaults?.first, defaults?.second ?: 0f, gamepad, ControllerPrefs.snap(context))
|
||||
}
|
||||
val manager = remember { context.getSystemService(DisplayManager::class.java) }
|
||||
var revision by remember { mutableIntStateOf(0) }
|
||||
@@ -258,7 +269,7 @@ private fun SurfaceCanvas(draft: SurfaceDraft, transparent: Boolean = false) {
|
||||
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::beginEdit
|
||||
view.onBeginEdit = { e, w, h -> draft.beginEdit(e, w.toFloat() / h, if (draft.hasGamepad) GamepadScaler(view.context, w, h) else null) }
|
||||
view.onChange = { e -> if (!draft.properties) draft.elements = draft.elements.map { if (it.id == e.id) e else it } }
|
||||
|
||||
})
|
||||
|
||||
@@ -32,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: (List<InputSurfaceElement>, Float) -> Unit = { _, _ -> }
|
||||
var onBeginEdit: (List<InputSurfaceElement>, Int, Int) -> 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 }
|
||||
@@ -101,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(visibleElements, width.toFloat() / height)
|
||||
dragging = true; onBeginEdit(visibleElements, width, height)
|
||||
}
|
||||
if (!dragging) return true
|
||||
val dx = (event.x - downX) / width; val dy = (event.y - downY) / height
|
||||
|
||||
@@ -50,7 +50,7 @@ class InputLayoutToolsTest {
|
||||
assertEquals(InputLibraries.DECK, independent.layout(InputProfile.GAME, InputRole.SECONDARY)!!.origin)
|
||||
val stock = InputStockLayouts.layouts().first { it.id == InputLibraries.DECK }.elements
|
||||
val dragged = mine.copy(elements = mine.elements.map { it.copy(x = 0f, y = 0f) })
|
||||
assertEquals(stock, InputLibraries.defaultPositions(context, copied, dragged))
|
||||
assertEquals(stock to InputStockLayouts.layouts().first { it.id == InputLibraries.DECK }.aspect, InputLibraries.defaultPositions(context, copied, dragged))
|
||||
assertEquals(mine.elements, dragged.elements.withPositionsOf(stock))
|
||||
assertEquals(copied, InputLibraries.decode(InputLibraries.encode(copied)))
|
||||
}
|
||||
@@ -58,7 +58,7 @@ class InputLayoutToolsTest {
|
||||
@Test fun builtInGamepadDefaultsComeFromTheGamepadGenerator() {
|
||||
val library = InputLibraries.read(context)
|
||||
val gamepad = library.layouts.first { it.id == InputLibraries.OVERLAY }
|
||||
val defaults = InputLibraries.defaultPositions(context, library, gamepad)!!
|
||||
val defaults = InputLibraries.defaultPositions(context, library, gamepad)!!.first
|
||||
assertEquals(gamepad.elements.map { it.id }.toSet(), defaults.map { it.id }.toSet())
|
||||
assertNull(InputLibraries.defaultPositions(context, library, SavedInputLayout("new", "New")))
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ class InputSurfaceEditorViewTest {
|
||||
@Test fun editingStartsInTheDisplayedFrame() {
|
||||
val view = view()
|
||||
var started: Pair<List<InputSurfaceElement>, Float>? = null
|
||||
view.onBeginEdit = { elements, aspect -> started = elements to aspect }
|
||||
view.onBeginEdit = { elements, w, h -> started = elements to w.toFloat() / h }
|
||||
val (x, y) = center()
|
||||
view.touch(MotionEvent.ACTION_DOWN, x, y)
|
||||
view.touch(MotionEvent.ACTION_MOVE, x + 100f, y + 50f)
|
||||
|
||||
Reference in new issue
Block a user