mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
Controller editor: snap controls into line while dragging (#490)
This commit is contained in:
1 parent
c1433ac0b3
commit
1959b1a875
11 files changed
+191
-24
No files matched your search
@@ -10,16 +10,23 @@ import androidx.activity.ComponentActivity
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.selection.toggleable
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.semantics.Role
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.compose.ui.unit.dp
|
||||
@@ -48,19 +55,27 @@ class ControllerEditorActivity : ComponentActivity() {
|
||||
val bar = ComposeView(this).apply {
|
||||
setContent {
|
||||
DroidDeckTheme {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
||||
var snap by remember { mutableStateOf(ControllerPrefs.read(context).snap) }
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(5.dp),
|
||||
modifier = Modifier
|
||||
.background(MaterialTheme.colorScheme.surface.copy(alpha = 0.9f), RoundedCornerShape(50))
|
||||
.padding(start = 14.dp, end = 5.dp, top = 5.dp, bottom = 5.dp),
|
||||
.background(MaterialTheme.colorScheme.surface.copy(alpha = 0.9f), RoundedCornerShape(20.dp))
|
||||
.padding(start = 5.dp, end = 5.dp, top = 8.dp, bottom = 5.dp),
|
||||
) {
|
||||
Text(stringResource(R.string.ctrl_editor_drag), fontSize = 12.sp, color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(end = 4.dp))
|
||||
EditorButton(stringResource(R.string.ctrl_reset), false) { controls.resetLayout() }
|
||||
EditorButton(stringResource(R.string.common_cancel), false) { finish() }
|
||||
EditorButton(stringResource(R.string.ctrl_editor_save), true) {
|
||||
controls.saveLayout()
|
||||
finish()
|
||||
Text(stringResource(R.string.ctrl_editor_drag), fontSize = 12.sp, color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
EditorButton(stringResource(R.string.ctrl_editor_snap), false, selected = snap) {
|
||||
snap = !snap
|
||||
ControllerPrefs.setSnap(context, snap)
|
||||
controls.setSnap(snap)
|
||||
}
|
||||
EditorButton(stringResource(R.string.ctrl_reset), false) { controls.resetLayout() }
|
||||
EditorButton(stringResource(R.string.common_cancel), false) { finish() }
|
||||
EditorButton(stringResource(R.string.ctrl_editor_save), true) {
|
||||
controls.saveLayout()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,14 +86,27 @@ class ControllerEditorActivity : ComponentActivity() {
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun EditorButton(label: String, primary: Boolean, onClick: () -> Unit) {
|
||||
private fun EditorButton(label: String, primary: Boolean, selected: Boolean? = null, onClick: () -> Unit) {
|
||||
val pal = LocalPalette.current
|
||||
Text(
|
||||
label, fontSize = 12.sp, fontWeight = FontWeight.SemiBold,
|
||||
color = if (primary) pal.onSignal else MaterialTheme.colorScheme.onBackground,
|
||||
color = when {
|
||||
primary -> pal.onSignal
|
||||
selected == true -> pal.signal
|
||||
else -> MaterialTheme.colorScheme.onBackground
|
||||
},
|
||||
modifier = Modifier.clip(RoundedCornerShape(50))
|
||||
.background(if (primary) pal.signal else Color.White.copy(alpha = 0.06f))
|
||||
.clickable(onClick = onClick)
|
||||
.background(
|
||||
when {
|
||||
primary -> pal.signal
|
||||
selected == true -> pal.signal.copy(alpha = 0.18f)
|
||||
else -> Color.White.copy(alpha = 0.06f)
|
||||
},
|
||||
)
|
||||
.then(
|
||||
if (selected == null) Modifier.clickable(onClick = onClick)
|
||||
else Modifier.toggleable(value = selected, role = Role.Switch, onValueChange = { onClick() }),
|
||||
)
|
||||
.padding(horizontal = 12.dp, vertical = 6.dp),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ object ControllerPrefs {
|
||||
val steamButton: Boolean,
|
||||
val qamButton: Boolean,
|
||||
val keyboardButton: Boolean,
|
||||
val snap: Boolean,
|
||||
val customLayout: Boolean,
|
||||
val mapping: Map<String, String>,
|
||||
)
|
||||
@@ -64,6 +65,7 @@ object ControllerPrefs {
|
||||
steamButton = p.getBoolean("steamButton", true),
|
||||
qamButton = p.getBoolean("qamButton", true),
|
||||
keyboardButton = p.getBoolean("keyboardButton", true),
|
||||
snap = p.getBoolean("snap", true),
|
||||
customLayout = p.all.keys.any { it.startsWith("layout.") },
|
||||
mapping = mappable.associate { (id, _) -> id to target(context, id) },
|
||||
)
|
||||
@@ -119,6 +121,10 @@ object ControllerPrefs {
|
||||
prefs(context).edit().putBoolean("keyboardButton", on).apply()
|
||||
}
|
||||
|
||||
fun setSnap(context: Context, on: Boolean) {
|
||||
prefs(context).edit().putBoolean("snap", on).apply()
|
||||
}
|
||||
|
||||
fun layout(context: Context, width: Int, height: Int): Map<String, Pair<Float, Float>> {
|
||||
val raw = prefs(context).getString("layout.${width}x$height", null) ?: return emptyMap()
|
||||
return raw.split(';').mapNotNull { entry ->
|
||||
|
||||
@@ -9,6 +9,7 @@ import android.graphics.Path
|
||||
import android.graphics.Rect
|
||||
import android.graphics.RectF
|
||||
import android.os.Build
|
||||
import android.view.HapticFeedbackConstants
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.WindowInsets
|
||||
@@ -95,8 +96,11 @@ class OnScreenControls(
|
||||
private var quickPressedBy = -1
|
||||
private var keyboardPressedBy = -1
|
||||
private var settings = ControllerPrefs.read(context)
|
||||
private var snap = settings.snap
|
||||
private var safe = Rect()
|
||||
private var selected: String? = null
|
||||
private var snapX: Float? = null
|
||||
private var snapY: Float? = null
|
||||
private var grabX = 0f
|
||||
private var grabY = 0f
|
||||
private var ignoreSaved = false
|
||||
@@ -138,10 +142,18 @@ class OnScreenControls(
|
||||
fun reload() {
|
||||
releaseAll()
|
||||
settings = ControllerPrefs.read(context)
|
||||
setSnap(settings.snap)
|
||||
applySettings()
|
||||
relayout()
|
||||
}
|
||||
|
||||
private fun clearSnapGuides() {
|
||||
if (snapX == null && snapY == null) return
|
||||
snapX = null
|
||||
snapY = null
|
||||
invalidate()
|
||||
}
|
||||
|
||||
private fun applySettings() {
|
||||
for (control in controls) control.target = settings.mapping[control.id] ?: control.id
|
||||
val tint = settings.tint
|
||||
@@ -179,6 +191,11 @@ class OnScreenControls(
|
||||
invalidate()
|
||||
}
|
||||
|
||||
fun setSnap(on: Boolean) {
|
||||
snap = on
|
||||
if (!on) clearSnapGuides()
|
||||
}
|
||||
|
||||
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
||||
super.onSizeChanged(w, h, oldw, oldh)
|
||||
relayout()
|
||||
@@ -412,15 +429,18 @@ class OnScreenControls(
|
||||
}
|
||||
}
|
||||
if (editing) {
|
||||
val group = selected ?: return
|
||||
val (x, y) = centre(group)
|
||||
stroke.color = heldStroke
|
||||
val single = members(group).singleOrNull()
|
||||
if (single?.wide == true) {
|
||||
val pad = dp(6f)
|
||||
box.set(x - single.halfW - pad, y - single.halfH - pad, x + single.halfW + pad, y + single.halfH + pad)
|
||||
canvas.drawRoundRect(box, single.halfH, single.halfH, stroke)
|
||||
} else canvas.drawCircle(x, y, extent(group) + dp(6f), stroke)
|
||||
snapX?.let { canvas.drawLine(it, safe.top.toFloat(), it, (height - safe.bottom).toFloat(), stroke) }
|
||||
snapY?.let { canvas.drawLine(safe.left.toFloat(), it, (width - safe.right).toFloat(), it, stroke) }
|
||||
selected?.let { group ->
|
||||
val (x, y) = centre(group)
|
||||
val single = members(group).singleOrNull()
|
||||
if (single?.wide == true) {
|
||||
val pad = dp(6f)
|
||||
box.set(x - single.halfW - pad, y - single.halfH - pad, x + single.halfW + pad, y + single.halfH + pad)
|
||||
canvas.drawRoundRect(box, single.halfH, single.halfH, stroke)
|
||||
} else canvas.drawCircle(x, y, extent(group) + dp(6f), stroke)
|
||||
}
|
||||
} else if (!buttonsOnly) {
|
||||
val (x, y) = quickCenter()
|
||||
val radius = dp(18f)
|
||||
@@ -587,6 +607,7 @@ class OnScreenControls(
|
||||
private fun onEditTouch(event: MotionEvent): Boolean {
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
clearSnapGuides()
|
||||
val control = controlAt(event.x, event.y)
|
||||
selected = control?.group
|
||||
val group = selected ?: run { invalidate(); return false }
|
||||
@@ -598,14 +619,29 @@ class OnScreenControls(
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
val group = selected ?: return true
|
||||
ignoreSaved = false
|
||||
put(group, event.x + grabX, event.y + grabY)
|
||||
val rawX = event.x + grabX
|
||||
val rawY = event.y + grabY
|
||||
val others = groups.filter { it != group && members(it).any(::isVisible) }.map { centre(it) }
|
||||
val nextX = if (snap) closestSnap(rawX, others.map { it.first } + width / 2f) else null
|
||||
val nextY = if (snap) closestSnap(rawY, others.map { it.second }) else null
|
||||
put(group, nextX ?: rawX, nextY ?: rawY)
|
||||
clamp(group)
|
||||
val (x, y) = centre(group)
|
||||
val visibleX = nextX?.takeIf { abs(x - it) <= SNAP_TOLERANCE_PX }
|
||||
val visibleY = nextY?.takeIf { abs(y - it) <= SNAP_TOLERANCE_PX }
|
||||
if ((visibleX != null && visibleX != snapX) || (visibleY != null && visibleY != snapY)) performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK)
|
||||
snapX = visibleX
|
||||
snapY = visibleY
|
||||
invalidate()
|
||||
}
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> clearSnapGuides()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun closestSnap(position: Float, targets: List<Float>): Float? =
|
||||
targets.minByOrNull { abs(position - it) }?.takeIf { abs(position - it) <= dp(SNAP_DP) }
|
||||
|
||||
private fun controlAt(x: Float, y: Float): Control? =
|
||||
controls.firstOrNull {
|
||||
!quickHidden && isVisible(it) && (editing || !settings.adaptiveSticks || it.stick < 0) && it.contains(x, y, it.radius)
|
||||
@@ -715,6 +751,8 @@ class OnScreenControls(
|
||||
const val PHONE_MAX_HEIGHT_MM = 90f
|
||||
const val SHOULDER_WIDTH = 1.45f
|
||||
const val SHOULDER_HEIGHT = 0.8f
|
||||
const val SNAP_DP = 9f
|
||||
const val SNAP_TOLERANCE_PX = 1f
|
||||
val shoulderIds = setOf("lb", "rb", "lt", "rt")
|
||||
val directions = listOf("up", "right", "down", "left")
|
||||
}
|
||||
|
||||
@@ -1592,6 +1592,7 @@
|
||||
<!-- The on-screen button is not shown -->
|
||||
<string name="ctrl_target_hidden">Oculto</string>
|
||||
<string name="ctrl_editor_drag">Arrastra para mover</string>
|
||||
<string name="ctrl_editor_snap">Ajustar</string>
|
||||
<string name="ctrl_editor_save">Guardar</string>
|
||||
|
||||
<!-- What the second screen shows during a session -->
|
||||
|
||||
@@ -1556,6 +1556,7 @@
|
||||
<!-- The on-screen button is not shown -->
|
||||
<string name="ctrl_target_hidden">非表示</string>
|
||||
<string name="ctrl_editor_drag">ドラッグして移動</string>
|
||||
<string name="ctrl_editor_snap">スナップ</string>
|
||||
<string name="ctrl_editor_save">保存</string>
|
||||
|
||||
<!-- What the second screen shows during a session -->
|
||||
|
||||
@@ -1556,6 +1556,7 @@
|
||||
<!-- The on-screen button is not shown -->
|
||||
<string name="ctrl_target_hidden">숨김</string>
|
||||
<string name="ctrl_editor_drag">드래그하여 이동</string>
|
||||
<string name="ctrl_editor_snap">맞춤</string>
|
||||
<string name="ctrl_editor_save">저장</string>
|
||||
|
||||
<!-- What the second screen shows during a session -->
|
||||
|
||||
@@ -1556,6 +1556,7 @@
|
||||
<!-- The on-screen button is not shown -->
|
||||
<string name="ctrl_target_hidden">隐藏</string>
|
||||
<string name="ctrl_editor_drag">拖动以移动</string>
|
||||
<string name="ctrl_editor_snap">对齐</string>
|
||||
<string name="ctrl_editor_save">保存</string>
|
||||
|
||||
<!-- What the second screen shows during a session -->
|
||||
|
||||
@@ -1556,6 +1556,7 @@
|
||||
<!-- The on-screen button is not shown -->
|
||||
<string name="ctrl_target_hidden">隱藏</string>
|
||||
<string name="ctrl_editor_drag">拖曳以移動</string>
|
||||
<string name="ctrl_editor_snap">對齊</string>
|
||||
<string name="ctrl_editor_save">儲存</string>
|
||||
|
||||
<!-- What the second screen shows during a session -->
|
||||
|
||||
@@ -1556,6 +1556,7 @@
|
||||
<!-- The on-screen button is not shown -->
|
||||
<string name="ctrl_target_hidden">隱藏</string>
|
||||
<string name="ctrl_editor_drag">拖曳以移動</string>
|
||||
<string name="ctrl_editor_snap">對齊</string>
|
||||
<string name="ctrl_editor_save">儲存</string>
|
||||
|
||||
<!-- What the second screen shows during a session -->
|
||||
|
||||
@@ -1533,6 +1533,7 @@
|
||||
<!-- The on-screen button is not shown -->
|
||||
<string name="ctrl_target_hidden">Hidden</string>
|
||||
<string name="ctrl_editor_drag">Drag to move</string>
|
||||
<string name="ctrl_editor_snap">Snap</string>
|
||||
<string name="ctrl_editor_save">Save</string>
|
||||
<!-- What the second screen shows during a session -->
|
||||
<string name="second_mode_none">None</string>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.sqrt
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
@@ -32,6 +33,88 @@ class OnScreenControlsTest {
|
||||
view.layout(0, 0, 1200, 800)
|
||||
}
|
||||
|
||||
@Test fun editorSnapsToOtherGroupCentreLines() {
|
||||
editor()
|
||||
val button = control("lb")
|
||||
val target = control("rb")
|
||||
val x = value(button, "cx")
|
||||
val y = value(button, "cy")
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (x to y))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (x to y + 5f))
|
||||
assertEquals(value(target, "cy"), value(button, "cy"), 0f)
|
||||
assertEquals(value(target, "cy"), nullableFloat(view, "snapY")!!, 0f)
|
||||
touch(MotionEvent.ACTION_UP, 1 to (x to y + 5f))
|
||||
assertNull(nullableFloat(view, "snapY"))
|
||||
}
|
||||
|
||||
@Test fun editorSnapsToClusterCentresNotTheirOuterButtons() {
|
||||
editor()
|
||||
val button = control("lb")
|
||||
val x = value(button, "cx")
|
||||
val row = value(control("y"), "cy") + 3f
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (x to value(button, "cy")))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (x to row))
|
||||
assertEquals(row, value(button, "cy"), 0f)
|
||||
assertNull(nullableFloat(view, "snapY"))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (x to value(control("x"), "cy") + 3f))
|
||||
assertEquals(value(control("x"), "cy"), value(button, "cy"), 0f)
|
||||
touch(MotionEvent.ACTION_UP, 1 to (x to row))
|
||||
}
|
||||
|
||||
@Test fun editorDoesNotSnapBeyondDistanceAndCanDragFreeFromGuide() {
|
||||
editor()
|
||||
val button = control("lb")
|
||||
val x = value(button, "cx")
|
||||
val y = value(button, "cy")
|
||||
val otherYs = (field(view, "controls") as List<*>)
|
||||
.filter { field(it!!, "group") != "lb" }
|
||||
.map { value(it!!, "cy") }
|
||||
val freeY = (100..700 step 31).map(Int::toFloat).first {
|
||||
otherYs.minOf { targetY -> abs(it - targetY) } > 12f
|
||||
}
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (x to y))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (x to freeY))
|
||||
assertEquals(freeY, value(button, "cy"), 0f)
|
||||
assertNull(nullableFloat(view, "snapY"))
|
||||
val target = value(control("rb"), "cy")
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (x to target + 5f))
|
||||
assertEquals(target, value(button, "cy"), 0f)
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (x to freeY))
|
||||
assertEquals(freeY, value(button, "cy"), 0f)
|
||||
assertNull(nullableFloat(view, "snapY"))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (x to freeY))
|
||||
}
|
||||
|
||||
@Test fun editorSnapsToScreenCentreAndCanDisableSnapping() {
|
||||
editor()
|
||||
val button = control("lb")
|
||||
val y = value(button, "cy")
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (value(button, "cx") to y))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (605f to y))
|
||||
assertEquals(600f, value(button, "cx"), 0f)
|
||||
assertEquals(600f, nullableFloat(view, "snapX")!!, 0f)
|
||||
touch(MotionEvent.ACTION_UP, 1 to (605f to y))
|
||||
|
||||
ControllerPrefs.setSnap(context, false)
|
||||
editor()
|
||||
val unsnapped = control("lb")
|
||||
val x = value(unsnapped, "cx")
|
||||
val startY = value(unsnapped, "cy")
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (x to startY))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (605f to startY))
|
||||
assertEquals(605f, value(unsnapped, "cx"), 0f)
|
||||
assertNull(nullableFloat(view, "snapX"))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (605f to startY))
|
||||
}
|
||||
|
||||
@Test fun editorSnapPreferenceDefaultsOnAndCanBeChanged() {
|
||||
assertTrue(ControllerPrefs.read(context).snap)
|
||||
ControllerPrefs.setSnap(context, false)
|
||||
assertFalse(ControllerPrefs.read(context).snap)
|
||||
ControllerPrefs.resetAll(context)
|
||||
assertTrue(ControllerPrefs.read(context).snap)
|
||||
}
|
||||
|
||||
@Test fun adaptiveStickHidesUntilTouchedAndReturnsToNeutralOnRelease() {
|
||||
val left = control("ls")
|
||||
assertEquals(0, pixel(value(left, "cx"), value(left, "cy")))
|
||||
@@ -171,6 +254,10 @@ class OnScreenControlsTest {
|
||||
}
|
||||
|
||||
private fun control(id: String): Any = (field(view, "controls") as List<*>).first { field(it!!, "id") == id }!!
|
||||
private fun editor() {
|
||||
view = OnScreenControls(context, null, editing = true)
|
||||
view.layout(0, 0, 1200, 800)
|
||||
}
|
||||
|
||||
@Test fun keyboardShortcutWorksWithHiddenPadAndButtonsOnly() {
|
||||
var opened = 0
|
||||
@@ -236,6 +323,7 @@ class OnScreenControlsTest {
|
||||
}
|
||||
}
|
||||
private fun field(owner: Any, name: String): Any = owner.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(owner)!!
|
||||
private fun nullableFloat(owner: Any, name: String) = owner.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(owner) as Float?
|
||||
private fun value(owner: Any, name: String) = field(owner, name) as Float
|
||||
|
||||
private fun pixel(x: Float, y: Float): Int {
|
||||
|
||||
Reference in new issue
Block a user