diff --git a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt index 1a8987bb58..31dd01fde1 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt +++ b/app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt @@ -45,11 +45,11 @@ class OnScreenControls(context: Context, private val pad: PadBridge?, private va val halfW get() = if (wide) radius * SHOULDER_WIDTH else radius val halfH get() = if (wide) radius * SHOULDER_HEIGHT else radius - fun contains(x: Float, y: Float, r: Float): Boolean { + fun contains(x: Float, y: Float, r: Float, padding: Float = 0f): Boolean { val dx = x - cx val dy = y - cy - if (wide) return abs(dx) <= halfW + r * 0.25f && abs(dy) <= halfH + r * 0.25f - val hit = r * 1.25f + if (wide) return abs(dx) <= halfW + r * 0.25f + padding && abs(dy) <= halfH + r * 0.25f + padding + val hit = r * 1.25f + padding return dx * dx + dy * dy <= hit * hit } @@ -516,8 +516,17 @@ class OnScreenControls(context: Context, private val pad: PadBridge?, private va 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 } + if (controls.any { it.stick < 0 && isVisible(it) && it.contains(x, y, it.radius, 5f) }) return null + return controls.filter { + val dx = x - it.cx + val dy = y - it.cy + val reach = it.radius * 1.4f + it.stick >= 0 && isVisible(it) && it.pressedBy == -1 && dx * dx + dy * dy <= reach * reach + }.minByOrNull { + val dx = x - it.cx + val dy = y - it.cy + dx * dx + dy * dy + } } private fun isVisible(control: Control): Boolean = when { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt b/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt index 155029ed53..ee5ffd4dfa 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ControllerSettings.kt @@ -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", "Sticks stay hidden until you touch their side of the screen, then hide when released", c.adaptiveSticks, onChange = a.onAdaptiveSticks) + ToggleRow(host, "controller-adaptive", "Adaptive sticks", "Sticks appear when touched near their saved positions and 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() } diff --git a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt index 2305105897..f82ccd7863 100644 --- a/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/input/OnScreenControlsTest.kt @@ -4,6 +4,8 @@ import android.content.Context import android.graphics.Bitmap import android.graphics.Canvas import android.view.MotionEvent +import android.view.View +import android.widget.FrameLayout import org.junit.Assert.* import org.junit.Before import org.junit.Test @@ -24,6 +26,7 @@ class OnScreenControlsTest { @Before fun setUp() { context = RuntimeEnvironment.getApplication() context.getSharedPreferences("controller", Context.MODE_PRIVATE).edit().clear().commit() + ControllerPrefs.setLayout(context, 1200, 800, mapOf("ls" to (0.45f to 0.5f), "rs" to (0.55f to 0.5f))) view = OnScreenControls(context, null) view.layout(0, 0, 1200, 800) } @@ -93,7 +96,7 @@ class OnScreenControlsTest { 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))) + assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 200f))) touch(MotionEvent.ACTION_DOWN, 1 to (x + 10f to y)) assertEquals(x, value(left, "ax"), 0f) assertEquals(10f, value(left, "kx"), 0f) @@ -110,7 +113,60 @@ class OnScreenControlsTest { 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))) + assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 200f))) + } + + @Test fun adaptiveActivationIsLimitedToExpandedSavedCircle() { + val left = control("ls") + val x = value(left, "cx") + val y = value(left, "cy") + val reach = value(left, "radius") * 1.4f + assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x to y - reach - 1f))) + assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to y - reach + 1f))) + assertEquals(y - reach + 1f, value(left, "ay"), 0.001f) + touch(MotionEvent.ACTION_UP, 1 to (x to y - reach + 1f)) + assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x - reach to y - reach))) + assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (600f to 200f))) + } + + @Test fun touchesOutsideActivationAreasStayWithTrackpad() { + val received = mutableListOf() + val trackpad = View(context).apply { + setOnTouchListener { _, event -> received.add(event.actionMasked); true } + } + val root = FrameLayout(context).apply { + addView(trackpad, FrameLayout.LayoutParams(1200, 800)) + addView(view, FrameLayout.LayoutParams(1200, 800)) + measure(View.MeasureSpec.makeMeasureSpec(1200, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(800, View.MeasureSpec.EXACTLY)) + layout(0, 0, 1200, 800) + } + for ((action, position) in listOf( + MotionEvent.ACTION_DOWN to (600f to 200f), + MotionEvent.ACTION_MOVE to (540f to 400f), + MotionEvent.ACTION_UP to (540f to 400f), + )) { + val event = MotionEvent.obtain(1000L, time++, action, position.first, position.second, 0) + try { assertTrue(root.dispatchTouchEvent(event)) } finally { event.recycle() } + } + assertEquals(listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP), received) + assertEquals(-1, field(control("ls"), "pressedBy")) + } + + @Test fun adaptiveActivationLeavesFivePixelsAroundButtonHitTargets() { + for (id in listOf("a", "rb")) { + val button = control(id) + val radius = value(button, "radius") + val edge = value(button, "cy") - radius * if (id == "rb") 1.05f else 1.25f + val x = value(button, "cx") + val left = control("ls") + left.javaClass.getDeclaredField("cx").apply { isAccessible = true }.setFloat(left, x) + left.javaClass.getDeclaredField("cy").apply { isAccessible = true }.setFloat(left, edge - 10f) + assertFalse(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 4f))) + assertEquals(-1, field(left, "pressedBy")) + assertTrue(touch(MotionEvent.ACTION_DOWN, 1 to (x to edge - 6f))) + assertEquals(1, field(left, "pressedBy")) + touch(MotionEvent.ACTION_UP, 1 to (x to edge - 6f)) + } } private fun control(id: String): Any = (field(view, "controls") as List<*>).first { field(it!!, "id") == id }!!