From 2c95ebd22d416190169ca0ed5b847d6747945c0b Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Tue, 1 Dec 2026 22:15:02 -0500 Subject: [PATCH] fix(input): the middle of a crossed on-screen D-pad presses the nearest arm (#3558) * fix(input): the middle of a crossed on-screen D-pad presses the nearest arm Four direction buttons laid out as a cross left an empty square in the middle, so a touch there fell through to the mouse. A touch inside the cross now picks the arm on that side, for the stock pad and any custom layout with the same shape. * fix(input): take the D-pad middle for hand-placed arms that are not exactly centred * fix(input): limit the D-pad fallback to its middle and find the cross among duplicate arms * fix(input): D-pad middle picks the nearest arm and needs overlapping opposite arms * fix(input): D-pad middle measures the distance to each arm itself * fix(input): D-pad middle weighs every matching arm set --- .../droiddeck/launcher/ui/InputSurfaceView.kt | 39 +++++++++- .../launcher/ui/InputSurfaceTouchTest.kt | 75 +++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt index f06c43851c..e0d0207ff3 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/InputSurfaceView.kt @@ -44,6 +44,7 @@ import com.droiddeck.launcher.session.SessionPrefs import kotlin.math.abs import kotlin.math.atan2 import kotlin.math.hypot +import kotlin.math.max import kotlin.math.min @SuppressLint("ViewConstructor") @@ -63,6 +64,7 @@ class InputSurfaceView( private val art by lazy(LazyThreadSafetyMode.NONE) { PadArt(context) } private var artworkDpad = emptyList() private val artworkDpadBounds = RectF() + private var crossDpads = emptyList, RectF>>() private var requestedLayout = customLayout init { customLayout = surfaceStyle.shown(customLayout); addView(content, LayoutParams(-1, -1)); renderCustom(pad) } @@ -185,6 +187,7 @@ class InputSurfaceView( private fun renderCustom(pad: PadBridge?) { artworkDpad = emptyList() + crossDpads = emptyList() val canvas = object : ViewGroup(context) { override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { val layout = customLayout @@ -203,6 +206,7 @@ class InputSurfaceView( child.layout(x, y, x + child.measuredWidth, y + child.measuredHeight) } updateArtworkDpad(this) + updateCrossDpad(this) } override fun dispatchDraw(canvas: Canvas) { @@ -244,11 +248,39 @@ class InputSurfaceView( canvas.getChildAt(index) as? HoldButton } val previous = artworkDpad - artworkDpad = if (crossed(buttons)) buttons else emptyList() + artworkDpad = if (crossed(buttons, artworkDpadBounds)) buttons else emptyList() if (artworkDpad != previous) (previous + artworkDpad).forEach { it.invalidate() } } - private fun crossed(buttons: List): Boolean { + private fun updateCrossDpad(canvas: ViewGroup) { + val buttons = (0 until canvas.childCount).map(canvas::getChildAt).filterIsInstance() + val (ups, rights, downs, lefts) = DIRECTIONS.map { direction -> buttons.filter { (it.tag as? InputSurfaceElement)?.control == direction } } + crossDpads = ups.flatMap { up -> rights.flatMap { right -> downs.flatMap { down -> + lefts.mapNotNull { left -> listOf(up, right, down, left).let { arms -> hole(arms)?.let { arms to it } } } } } } + } + + private fun hole(buttons: List): RectF? { + val (up, right, down, left) = buttons + val hole = RectF(left.right.toFloat(), up.bottom.toFloat(), right.left.toFloat(), down.top.toFloat()) + val across = max(up.left, down.left) to min(up.right, down.right) + val along = max(left.top, right.top) to min(left.bottom, right.bottom) + return hole.takeIf { + !it.isEmpty && across.first < across.second && along.first < along.second && + across.first < it.right && across.second > it.left && along.first < it.bottom && along.second > it.top + } + } + + private fun crossTarget(x: Float, y: Float): HoldButton? { + val first = crossDpads.firstOrNull()?.first?.first() ?: return null + val hx = x - (originX(first) - first.left) + val hy = y - (originY(first) - first.top) + return crossDpads.filter { (arms, hole) -> arms.all { it.visibility == VISIBLE } && hole.contains(hx, hy) } + .flatMap { it.first }.distinct().minByOrNull { arm -> + hypot(max(max(arm.left - hx, hx - arm.right), 0f), max(max(arm.top - hy, hy - arm.bottom), 0f)) + } + } + + private fun crossed(buttons: List, bounds: RectF): Boolean { if (buttons.size != DIRECTIONS.size) return false val (up, right, down, left) = buttons val centerX = (left.left + right.right) / 2f @@ -258,7 +290,7 @@ class InputSurfaceView( kotlin.math.abs((left.top + left.bottom) / 2f - centerY) > 2f || kotlin.math.abs((right.top + right.bottom) / 2f - centerY) > 2f || up.bottom >= down.top || left.right >= right.left) return false - artworkDpadBounds.set(left.left.toFloat(), up.top.toFloat(), right.right.toFloat(), down.bottom.toFloat()) + bounds.set(left.left.toFloat(), up.top.toFloat(), right.right.toFloat(), down.bottom.toFloat()) return true } @@ -284,6 +316,7 @@ class InputSurfaceView( val views = (canvas.childCount - 1 downTo 0).map(canvas::getChildAt).filter { it.visibility == VISIBLE } views.firstOrNull { !(it is StickView && it.adaptive) && hits(it, x, y, 0f) }?.let { return it } views.filter { it is HoldButton && hits(it, x, y, slack(it)) }.minByOrNull { distance(it, x, y) }?.let { return it } + crossTarget(x, y)?.let { return it } if (views.any { it is HoldButton && hits(it, x, y, slack(it) + ADAPTIVE_CLEARANCE_PX) }) return null return views.filter { it is StickView && it.adaptive && it !in owners.values && (hits(it, x, y, 0f) || it.captures(x - originX(it), y - originY(it))) } .minByOrNull { distance(it, x, y) } diff --git a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt index cd92665cc2..a42a0bf76c 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/InputSurfaceTouchTest.kt @@ -227,6 +227,81 @@ class InputSurfaceTouchTest { touch(MotionEvent.ACTION_UP, 1 to (750f to 285f)) } + @Test fun theMiddleOfACrossedDpadPressesTheNearestArmInsteadOfFallingThrough() { + val up = InputSurfaceElement(InputControl.UP, .45f, .1f, .1f, .15f) + val down = InputSurfaceElement(InputControl.DOWN, .45f, .45f, .1f, .15f) + show(up, down, InputSurfaceElement(InputControl.LEFT, .3f, .25f, .15f, .2f), + InputSurfaceElement(InputControl.RIGHT, .55f, .25f, .15f, .2f), a) + val missed = mutableListOf() + view.onTouchMiss = { missed += it.actionMasked } + touch(MotionEvent.ACTION_DOWN, 1 to (500f to 170f)) + assertEquals(-1, state().hatY()) + touch(MotionEvent.ACTION_MOVE, 1 to (540f to 215f)) + assertEquals(1, state().hatX()) + assertEquals(0, state().hatY()) + touch(MotionEvent.ACTION_UP, 1 to (540f to 215f)) + assertEquals(0, state().hatX()) + assertTrue(missed.isEmpty()) + assertFalse(view.takesTouchAt(250f, 210f)) + assertFalse(view.takesTouchAt(800f, 500f)) + assertFalse(view.takesTouchAt(350f, 100f)) + show(up, down) + assertFalse(view.takesTouchAt(500f, 210f)) + view.onTouchMiss = null + } + + @Test fun aHandPlacedDpadWithOffArmsStillTakesItsMiddle() { + show(InputSurfaceElement(InputControl.UP, .42f, .1f, .1f, .15f), + InputSurfaceElement(InputControl.DOWN, .42f, .45f, .1f, .15f), + InputSurfaceElement(InputControl.LEFT, .3f, .27f, .15f, .15f), + InputSurfaceElement(InputControl.RIGHT, .5f, .25f, .15f, .2f)) + touch(MotionEvent.ACTION_DOWN, 1 to (475f to 225f)) + assertTrue(state().hatX() != 0 || state().hatY() != 0) + touch(MotionEvent.ACTION_UP, 1 to (475f to 225f)) + assertFalse(view.takesTouchAt(750f, 225f)) + } + + @Test fun aTallGapPicksTheNearestArmAndSplitArmsAreNoCross() { + show(InputSurfaceElement(InputControl.UP, .45f, .1f, .1f, .15f), + InputSurfaceElement(InputControl.DOWN, .45f, .65f, .1f, .15f), + InputSurfaceElement(InputControl.LEFT, .3f, .35f, .15f, .2f), + InputSurfaceElement(InputControl.RIGHT, .55f, .35f, .15f, .2f)) + touch(MotionEvent.ACTION_DOWN, 1 to (530f to 290f)) + assertEquals(1, state().hatX()) + assertEquals(0, state().hatY()) + touch(MotionEvent.ACTION_UP, 1 to (530f to 290f)) + touch(MotionEvent.ACTION_DOWN, 1 to (530f to 180f)) + assertEquals(-1, state().hatY()) + assertEquals(0, state().hatX()) + touch(MotionEvent.ACTION_UP, 1 to (530f to 180f)) + show(InputSurfaceElement(InputControl.UP, .2f, .1f, .1f, .15f), + InputSurfaceElement(InputControl.DOWN, .7f, .6f, .1f, .15f), + InputSurfaceElement(InputControl.LEFT, .1f, .35f, .15f, .2f), + InputSurfaceElement(InputControl.RIGHT, .8f, .35f, .15f, .2f)) + assertFalse(view.takesTouchAt(500f, 300f)) + } + + @Test fun aSecondUpButtonDoesNotHideTheCross() { + show(InputSurfaceElement(InputControl.UP, .1f, .7f, .1f, .15f, "extra-up"), + InputSurfaceElement(InputControl.UP, .45f, .1f, .1f, .15f), + InputSurfaceElement(InputControl.DOWN, .45f, .45f, .1f, .15f), + InputSurfaceElement(InputControl.LEFT, .3f, .25f, .15f, .2f), + InputSurfaceElement(InputControl.RIGHT, .55f, .25f, .15f, .2f)) + touch(MotionEvent.ACTION_DOWN, 1 to (500f to 170f)) + assertEquals(-1, state().hatY()) + touch(MotionEvent.ACTION_UP, 1 to (500f to 170f)) + assertEquals(0, state().hatY()) + show(InputSurfaceElement(InputControl.UP, .45f, 0f, .1f, .05f, "extra-up"), + InputSurfaceElement(InputControl.UP, .45f, .1f, .1f, .15f), + InputSurfaceElement(InputControl.DOWN, .45f, .45f, .1f, .15f), + InputSurfaceElement(InputControl.LEFT, .3f, .25f, .15f, .2f), + InputSurfaceElement(InputControl.RIGHT, .55f, .25f, .15f, .2f)) + touch(MotionEvent.ACTION_DOWN, 1 to (490f to 170f)) + assertEquals(-1, state().hatY()) + assertEquals(0, state().hatX()) + touch(MotionEvent.ACTION_UP, 1 to (490f to 170f)) + } + @Test fun adaptiveSticksCaptureAroundThemAndTheLeftOneToTheEdge() { val left = InputSurfaceElement(InputControl.LS, .3f, .3f, .2f, .4f, hideWhenIdle = true) val right = InputSurfaceElement(InputControl.RS, .6f, .3f, .2f, .4f, hideWhenIdle = true)