mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
fix(input): D-pad middle weighs every matching arm set
This commit is contained in:
1 parent
50be439943
commit
b2b03d0771
2 files changed
+26
-20
No files matched your search
@@ -64,8 +64,7 @@ class InputSurfaceView(
|
||||
private val art by lazy(LazyThreadSafetyMode.NONE) { PadArt(context) }
|
||||
private var artworkDpad = emptyList<HoldButton>()
|
||||
private val artworkDpadBounds = RectF()
|
||||
private var crossDpad = emptyList<HoldButton>()
|
||||
private val crossDpadHole = RectF()
|
||||
private var crossDpads = emptyList<Pair<List<HoldButton>, RectF>>()
|
||||
private var requestedLayout = customLayout
|
||||
init { customLayout = surfaceStyle.shown(customLayout); addView(content, LayoutParams(-1, -1)); renderCustom(pad) }
|
||||
|
||||
@@ -188,7 +187,7 @@ class InputSurfaceView(
|
||||
|
||||
private fun renderCustom(pad: PadBridge?) {
|
||||
artworkDpad = emptyList()
|
||||
crossDpad = emptyList()
|
||||
crossDpads = emptyList()
|
||||
val canvas = object : ViewGroup(context) {
|
||||
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
||||
val layout = customLayout
|
||||
@@ -256,31 +255,29 @@ class InputSurfaceView(
|
||||
private fun updateCrossDpad(canvas: ViewGroup) {
|
||||
val buttons = (0 until canvas.childCount).map(canvas::getChildAt).filterIsInstance<HoldButton>()
|
||||
val (ups, rights, downs, lefts) = DIRECTIONS.map { direction -> buttons.filter { (it.tag as? InputSurfaceElement)?.control == direction } }
|
||||
crossDpad = ups.asSequence().flatMap { up -> rights.asSequence().flatMap { right -> downs.asSequence().flatMap { down ->
|
||||
lefts.asSequence().map { left -> listOf(up, right, down, left) } } } }.firstOrNull(::hole) ?: emptyList()
|
||||
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<HoldButton>): Boolean {
|
||||
private fun hole(buttons: List<HoldButton>): RectF? {
|
||||
val (up, right, down, left) = buttons
|
||||
crossDpadHole.set(left.right.toFloat(), up.bottom.toFloat(), right.left.toFloat(), down.top.toFloat())
|
||||
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 !crossDpadHole.isEmpty() && across.first < across.second && along.first < along.second &&
|
||||
across.first < crossDpadHole.right && across.second > crossDpadHole.left &&
|
||||
along.first < crossDpadHole.bottom && along.second > crossDpadHole.top
|
||||
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? {
|
||||
if (crossDpad.isEmpty() || crossDpad.any { it.visibility != VISIBLE }) return null
|
||||
val (up, right, down, left) = crossDpad
|
||||
val originX = originX(up) - up.left
|
||||
val originY = originY(up) - up.top
|
||||
if (!crossDpadHole.contains(x - originX, y - originY)) return null
|
||||
val hx = x - originX
|
||||
val hy = y - originY
|
||||
return listOf(up, right, down, left).minBy { arm ->
|
||||
hypot(max(max(arm.left - hx, hx - arm.right), 0f), max(max(arm.top - hy, hy - arm.bottom), 0f))
|
||||
}
|
||||
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<HoldButton>, bounds: RectF): Boolean {
|
||||
|
||||
@@ -291,6 +291,15 @@ class InputSurfaceTouchTest {
|
||||
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() {
|
||||
|
||||
Reference in new issue
Block a user