fix(input): take the D-pad middle for hand-placed arms that are not exactly centred

This commit is contained in:
maxjivi05 committed 2026-12-01 20:58:20 -05:00
1 parent a67860a8f5
commit eeb29ae714
2 files changed
+26 -4

No files matched your search

@@ -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")
@@ -65,6 +66,7 @@ class InputSurfaceView(
private val artworkDpadBounds = RectF()
private var crossDpad = emptyList<HoldButton>()
private val crossDpadBounds = RectF()
private val crossDpadHole = RectF()
private var requestedLayout = customLayout
init { customLayout = surfaceStyle.shown(customLayout); addView(content, LayoutParams(-1, -1)); renderCustom(pad) }
@@ -257,7 +259,16 @@ class InputSurfaceView(
(0 until canvas.childCount).map(canvas::getChildAt)
.firstOrNull { it is HoldButton && (it.tag as? InputSurfaceElement)?.control == direction } as? HoldButton
}
crossDpad = if (crossed(buttons, crossDpadBounds)) buttons else emptyList()
crossDpad = emptyList()
if (buttons.size != DIRECTIONS.size) return
val (up, right, down, left) = buttons
crossDpadHole.set(left.right.toFloat(), up.bottom.toFloat(), right.left.toFloat(), down.top.toFloat())
if (crossDpadHole.isEmpty() || max(up.left, down.left) >= crossDpadHole.right || min(up.right, down.right) <= crossDpadHole.left ||
max(left.top, right.top) >= crossDpadHole.bottom || min(left.bottom, right.bottom) <= crossDpadHole.top) return
crossDpadBounds.set(left.left.toFloat(), up.top.toFloat(), right.right.toFloat(), down.bottom.toFloat())
crossDpadBounds.union(min(up.left, down.left).toFloat(), min(left.top, right.top).toFloat(),
max(up.right, down.right).toFloat(), max(left.bottom, right.bottom).toFloat())
crossDpad = buttons
}
private fun crossTarget(x: Float, y: Float): HoldButton? {
@@ -266,9 +277,9 @@ class InputSurfaceView(
val originX = originX(up) - up.left
val originY = originY(up) - up.top
if (!crossDpadBounds.contains(x - originX, y - originY)) return null
val dx = x - originX - crossDpadBounds.centerX()
val dy = y - originY - crossDpadBounds.centerY()
return if (kotlin.math.abs(dx) > kotlin.math.abs(dy)) { if (dx > 0f) right else left } else if (dy > 0f) down else up
val dx = x - originX - crossDpadHole.centerX()
val dy = y - originY - crossDpadHole.centerY()
return if (abs(dx) > abs(dy)) { if (dx > 0f) right else left } else if (dy > 0f) down else up
}
private fun crossed(buttons: List<HoldButton>, bounds: RectF): Boolean {
@@ -249,6 +249,17 @@ class InputSurfaceTouchTest {
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 adaptiveSticksCaptureAroundThemAndTheLeftOneToTheEdge() {
val left = InputSurfaceElement(InputControl.LS, .3f, .3f, .2f, .4f, hideWhenIdle = true)
val right = InputSurfaceElement(InputControl.RS, .6f, .3f, .2f, .4f, hideWhenIdle = true)