mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
Hide idle adaptive sticks and activate them at touch positions
This commit is contained in:
1 parent
da0775913f
commit
7d0e6b134b
4 files changed
+154
-5
No files matched your search
@@ -350,6 +350,7 @@ class OnScreenControls(context: Context, private val pad: PadBridge?, private va
|
||||
override fun onDraw(canvas: Canvas) {
|
||||
for (control in controls) {
|
||||
if (!isVisible(control)) continue
|
||||
if (control.stick >= 0 && settings.adaptiveSticks && !editing && control.pressedBy == -1) continue
|
||||
val held = control.pressedBy != -1
|
||||
val radius = control.radius
|
||||
if (control.stick >= 0) {
|
||||
@@ -426,10 +427,13 @@ class OnScreenControls(context: Context, private val pad: PadBridge?, private va
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> {
|
||||
val index = event.actionIndex
|
||||
val control = controlAt(event.getX(index), event.getY(index)) ?: return false
|
||||
val x = event.getX(index)
|
||||
val y = event.getY(index)
|
||||
val control = controlAt(x, y) ?: adaptiveStickAt(x, y) ?: return false
|
||||
if (control.pressedBy != -1) return true
|
||||
control.pressedBy = event.getPointerId(index)
|
||||
if (control.stick >= 0) {
|
||||
control.clicked = settings.stickClick && event.eventTime - control.lastUp < DOUBLE_TAP_MS
|
||||
control.clicked = settings.stickClick && control.lastUp > 0L && event.eventTime - control.lastUp < DOUBLE_TAP_MS
|
||||
val adaptive = settings.adaptiveSticks
|
||||
control.ax = if (adaptive) event.getX(index) else control.cx
|
||||
control.ay = if (adaptive) event.getY(index) else control.cy
|
||||
@@ -468,7 +472,7 @@ class OnScreenControls(context: Context, private val pad: PadBridge?, private va
|
||||
for (control in controls) {
|
||||
if (control.pressedBy == pointer || event.actionMasked == MotionEvent.ACTION_CANCEL) {
|
||||
if (control.pressedBy != -1) changed = true
|
||||
if (control.stick >= 0 && control.pressedBy != -1) control.lastUp = if (control.clicked) 0L else event.eventTime
|
||||
if (control.stick >= 0 && control.pressedBy != -1) control.lastUp = if (control.clicked || event.actionMasked == MotionEvent.ACTION_CANCEL) 0L else event.eventTime
|
||||
control.pressedBy = -1
|
||||
control.clicked = false
|
||||
if (control.stick >= 0 && (control.kx != 0f || control.ky != 0f)) control.dirty = true
|
||||
@@ -505,7 +509,16 @@ class OnScreenControls(context: Context, private val pad: PadBridge?, private va
|
||||
}
|
||||
|
||||
private fun controlAt(x: Float, y: Float): Control? =
|
||||
controls.firstOrNull { isVisible(it) && it.contains(x, y, it.radius) }
|
||||
controls.firstOrNull {
|
||||
isVisible(it) && (editing || !settings.adaptiveSticks || it.stick < 0) && it.contains(x, y, it.radius)
|
||||
}
|
||||
|
||||
private fun adaptiveStickAt(x: Float, y: Float): Control? {
|
||||
if (!settings.adaptiveSticks || editing || buttonsOnly) return null
|
||||
if (x < safe.left || x >= width - safe.right || y < safe.top || y >= height - safe.bottom) return null
|
||||
val side = if (x < (safe.left + width - safe.right) / 2f) 0 else 1
|
||||
return controls.firstOrNull { it.stick == side && isVisible(it) && it.pressedBy == -1 }
|
||||
}
|
||||
|
||||
private fun isVisible(control: Control): Boolean = when {
|
||||
buttonsOnly && !editing -> control.id == "guide" || control.id == "qam"
|
||||
@@ -574,6 +587,7 @@ class OnScreenControls(context: Context, private val pad: PadBridge?, private va
|
||||
controls.forEach {
|
||||
it.pressedBy = -1
|
||||
it.clicked = false
|
||||
it.lastUp = 0L
|
||||
if (it.stick >= 0 && (it.kx != 0f || it.ky != 0f)) it.dirty = true
|
||||
it.kx = 0f; it.ky = 0f
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ fun ColumnScope.ControllerRows(host: MenuHost, oscMode: String, c: ControllerPre
|
||||
ChoiceRow(host, "controller-opacity", "Opacity", null, ControllerPrefs.opacities.map { it to "$it%" }, c.opacity, onPick = a.onOpacity)
|
||||
ChoiceRow(host, "controller-size", "Button size", "100% keeps the standard size", ControllerPrefs.sizes.map { it to "$it%" }, c.size, onPick = a.onSize)
|
||||
ToggleRow(host, "controller-stick-click", "Stick click", "Double-tap a stick and hold for L3 or R3", c.stickClick, onChange = a.onStickClick)
|
||||
ToggleRow(host, "controller-adaptive", "Adaptive sticks", "A stick centers where your thumb lands and returns home when you let go", c.adaptiveSticks, onChange = a.onAdaptiveSticks)
|
||||
ToggleRow(host, "controller-adaptive", "Adaptive sticks", "Sticks stay hidden until you touch their side of the screen, then hide when released", c.adaptiveSticks, onChange = a.onAdaptiveSticks)
|
||||
SettingsRow("Layout", if (c.customLayout) "Custom positions saved" else "Placed for this screen's size and your grip") {
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
SecondaryButton("Edit") { a.onEditLayout() }
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.view.MotionEvent
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.GraphicsMode
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [28], manifest = Config.NONE, qualifiers = "mdpi")
|
||||
@GraphicsMode(GraphicsMode.Mode.NATIVE)
|
||||
class OnScreenControlsTest {
|
||||
private lateinit var context: Context
|
||||
private lateinit var view: OnScreenControls
|
||||
private var time = 1000L
|
||||
|
||||
@Before fun setUp() {
|
||||
context = RuntimeEnvironment.getApplication()
|
||||
context.getSharedPreferences("controller", Context.MODE_PRIVATE).edit().clear().commit()
|
||||
view = OnScreenControls(context, null)
|
||||
view.layout(0, 0, 1200, 800)
|
||||
}
|
||||
|
||||
@Test fun adaptiveStickHidesUntilTouchedAndReturnsToNeutralOnRelease() {
|
||||
val left = control("ls")
|
||||
assertEquals(0, pixel(value(left, "cx"), value(left, "cy")))
|
||||
assertTrue(touch(MotionEvent.ACTION_DOWN, 7 to (540f to 400f)))
|
||||
assertEquals(7, field(left, "pressedBy"))
|
||||
assertEquals(540f, value(left, "ax"), 0f)
|
||||
assertEquals(400f, value(left, "ay"), 0f)
|
||||
assertNotEquals(0, pixel(540f, 400f))
|
||||
touch(MotionEvent.ACTION_MOVE, 7 to (580f to 420f))
|
||||
assertTrue(value(left, "kx") > 0f)
|
||||
assertTrue(value(left, "ky") > 0f)
|
||||
touch(MotionEvent.ACTION_UP, 7 to (580f to 420f))
|
||||
assertEquals(-1, field(left, "pressedBy"))
|
||||
assertEquals(0f, value(left, "kx"), 0f)
|
||||
assertEquals(0f, value(left, "ky"), 0f)
|
||||
assertEquals(0, pixel(540f, 400f))
|
||||
}
|
||||
|
||||
@Test fun simultaneousSticksKeepTheirPointersAndReleaseIndependently() {
|
||||
val left = control("ls")
|
||||
val right = control("rs")
|
||||
touch(MotionEvent.ACTION_DOWN, 7 to (540f to 400f))
|
||||
touch(MotionEvent.ACTION_POINTER_DOWN or (1 shl 8), 7 to (540f to 400f), 9 to (660f to 400f))
|
||||
assertEquals(7, field(left, "pressedBy"))
|
||||
assertEquals(9, field(right, "pressedBy"))
|
||||
touch(MotionEvent.ACTION_POINTER_DOWN or (2 shl 8), 7 to (540f to 400f), 9 to (660f to 400f), 11 to (500f to 400f))
|
||||
assertEquals(7, field(left, "pressedBy"))
|
||||
touch(MotionEvent.ACTION_POINTER_UP or (2 shl 8), 7 to (540f to 400f), 9 to (660f to 400f), 11 to (500f to 400f))
|
||||
touch(MotionEvent.ACTION_MOVE, 7 to (700f to 400f), 9 to (500f to 400f))
|
||||
assertTrue(value(left, "kx") > 0f)
|
||||
assertTrue(value(right, "kx") < 0f)
|
||||
touch(MotionEvent.ACTION_POINTER_UP, 7 to (700f to 400f), 9 to (500f to 400f))
|
||||
assertEquals(-1, field(left, "pressedBy"))
|
||||
assertEquals(9, field(right, "pressedBy"))
|
||||
touch(MotionEvent.ACTION_CANCEL, 9 to (500f to 400f))
|
||||
for (stick in listOf(left, right)) {
|
||||
assertEquals(-1, field(stick, "pressedBy"))
|
||||
assertEquals(0f, value(stick, "kx"), 0f)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun cancelledTouchDoesNotBecomeAStickClick() {
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f))
|
||||
touch(MotionEvent.ACTION_CANCEL, 1 to (540f to 400f))
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f))
|
||||
assertEquals(false, field(control("ls"), "clicked"))
|
||||
touch(MotionEvent.ACTION_UP, 1 to (540f to 400f))
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f))
|
||||
assertEquals(true, field(control("ls"), "clicked"))
|
||||
}
|
||||
|
||||
@Test fun buttonsTakePriorityOverAdaptiveRegions() {
|
||||
val button = control("a")
|
||||
touch(MotionEvent.ACTION_DOWN, 3 to (value(button, "cx") to value(button, "cy")))
|
||||
assertEquals(3, field(button, "pressedBy"))
|
||||
assertEquals(-1, field(control("rs"), "pressedBy"))
|
||||
}
|
||||
|
||||
@Test fun fixedSticksAndEditorRemainVisibleAtSavedPositions() {
|
||||
ControllerPrefs.setAdaptiveSticks(context, false)
|
||||
view.reload()
|
||||
val left = control("ls")
|
||||
val x = value(left, "cx")
|
||||
val y = value(left, "cy")
|
||||
assertNotEquals(0, pixel(x, y))
|
||||
assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)))
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (x + 10f to y))
|
||||
assertEquals(x, value(left, "ax"), 0f)
|
||||
assertEquals(10f, value(left, "kx"), 0f)
|
||||
ControllerPrefs.setAdaptiveSticks(context, true)
|
||||
view = OnScreenControls(context, null, editing = true)
|
||||
view.layout(0, 0, 1200, 800)
|
||||
assertNotEquals(0, pixel(value(control("ls"), "cx"), value(control("ls"), "cy")))
|
||||
}
|
||||
|
||||
@Test fun buttonsOnlyAndReloadReleaseAdaptiveSticks() {
|
||||
touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f))
|
||||
touch(MotionEvent.ACTION_MOVE, 1 to (570f to 400f))
|
||||
view.reload()
|
||||
assertEquals(-1, field(control("ls"), "pressedBy"))
|
||||
assertEquals(0f, value(control("ls"), "kx"), 0f)
|
||||
view.setButtonsOnly(true)
|
||||
assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (540f to 400f)))
|
||||
}
|
||||
|
||||
private fun control(id: String): Any = (field(view, "controls") as List<*>).first { field(it!!, "id") == id }!!
|
||||
private fun field(owner: Any, name: String): Any = owner.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(owner)!!
|
||||
private fun value(owner: Any, name: String) = field(owner, name) as Float
|
||||
|
||||
private fun pixel(x: Float, y: Float): Int {
|
||||
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
|
||||
view.draw(Canvas(bitmap))
|
||||
val color = bitmap.getPixel(x.toInt(), y.toInt())
|
||||
bitmap.recycle()
|
||||
return color
|
||||
}
|
||||
|
||||
private fun touch(action: Int, vararg pointers: Pair<Int, Pair<Float, Float>>): Boolean {
|
||||
val properties = pointers.map { (id, _) -> MotionEvent.PointerProperties().apply { this.id = id; toolType = MotionEvent.TOOL_TYPE_FINGER } }.toTypedArray()
|
||||
val coords = pointers.map { (_, position) -> MotionEvent.PointerCoords().apply { x = position.first; y = position.second; pressure = 1f; size = 1f } }.toTypedArray()
|
||||
val event = MotionEvent.obtain(1000L, time++, action, pointers.size, properties, coords, 0, 0, 1f, 1f, 0, 0, 0, 0)
|
||||
return try { view.onTouchEvent(event) } finally { event.recycle() }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user