feat: add configurable four and eight way controls
This commit is contained in:
@@ -64,6 +64,7 @@ import org.neslite.app.input.ControllerProfile
|
||||
import org.neslite.app.input.ControllerProfileJson
|
||||
import org.neslite.app.input.ControllerProfileResolver
|
||||
import org.neslite.app.input.ControllerProfileStore
|
||||
import org.neslite.app.input.DirectionMode
|
||||
import org.neslite.app.input.GameAction
|
||||
import org.neslite.app.input.InputBinding
|
||||
import org.neslite.app.input.InputRouter
|
||||
@@ -75,6 +76,7 @@ import org.neslite.app.save.SaveCatalogReader
|
||||
import org.neslite.app.save.SaveSlotSummary
|
||||
import org.neslite.app.settings.AppSettings
|
||||
import org.neslite.app.settings.AppSettingsStore
|
||||
import org.neslite.app.settings.VirtualDpadSize
|
||||
import org.neslite.app.settings.shouldShowVirtualGamepad
|
||||
import org.neslite.app.system.BackDecision
|
||||
import org.neslite.app.system.CrashLoopGuard
|
||||
@@ -1087,6 +1089,87 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
renderSettings()
|
||||
}
|
||||
})
|
||||
settingsPanel.addView(section("屏幕方向键与手柄摇杆"))
|
||||
settingsPanel.addView(Button(this).apply {
|
||||
text = if (appSettings.directionMode == DirectionMode.EIGHT_WAY) {
|
||||
"方向输入:八方向"
|
||||
} else {
|
||||
"方向输入:四方向"
|
||||
}
|
||||
setOnClickListener {
|
||||
appSettings = appSettings.copy(
|
||||
directionMode = if (appSettings.directionMode == DirectionMode.EIGHT_WAY) {
|
||||
DirectionMode.FOUR_WAY
|
||||
} else {
|
||||
DirectionMode.EIGHT_WAY
|
||||
},
|
||||
)
|
||||
applyAppSettings()
|
||||
persistAppSettings()
|
||||
renderSettings()
|
||||
}
|
||||
})
|
||||
settingsPanel.addView(body("八方向允许斜向组合;四方向在斜推时保留最近明确输入的横向或纵向。"))
|
||||
settingsPanel.addView(Button(this).apply {
|
||||
text = "方向键大小:${appSettings.virtualDpadSize.label}"
|
||||
setOnClickListener {
|
||||
appSettings = appSettings.copy(virtualDpadSize = appSettings.virtualDpadSize.next())
|
||||
applyAppSettings()
|
||||
persistAppSettings()
|
||||
renderSettings()
|
||||
}
|
||||
})
|
||||
settingsPanel.addView(
|
||||
body("方向键水平位置:${(appSettings.virtualDpadHorizontalOffset * 100).toInt()}%"),
|
||||
)
|
||||
settingsPanel.addView(SeekBar(this).apply {
|
||||
max = 200
|
||||
progress = ((appSettings.virtualDpadHorizontalOffset + 1f) * 100f).toInt()
|
||||
setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
||||
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {}
|
||||
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
|
||||
override fun onStopTrackingTouch(seekBar: SeekBar?) {
|
||||
appSettings = appSettings.copy(
|
||||
virtualDpadHorizontalOffset = ((seekBar?.progress ?: 100) - 100) / 100f,
|
||||
)
|
||||
applyAppSettings()
|
||||
persistAppSettings()
|
||||
renderSettings()
|
||||
}
|
||||
})
|
||||
})
|
||||
settingsPanel.addView(
|
||||
body("方向键垂直位置:${(appSettings.virtualDpadVerticalOffset * 100).toInt()}%"),
|
||||
)
|
||||
settingsPanel.addView(SeekBar(this).apply {
|
||||
max = 200
|
||||
progress = ((appSettings.virtualDpadVerticalOffset + 1f) * 100f).toInt()
|
||||
setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
||||
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {}
|
||||
override fun onStartTrackingTouch(seekBar: SeekBar?) {}
|
||||
override fun onStopTrackingTouch(seekBar: SeekBar?) {
|
||||
appSettings = appSettings.copy(
|
||||
virtualDpadVerticalOffset = ((seekBar?.progress ?: 100) - 100) / 100f,
|
||||
)
|
||||
applyAppSettings()
|
||||
persistAppSettings()
|
||||
renderSettings()
|
||||
}
|
||||
})
|
||||
})
|
||||
settingsPanel.addView(Button(this).apply {
|
||||
text = "恢复方向键默认大小与位置"
|
||||
setOnClickListener {
|
||||
appSettings = appSettings.copy(
|
||||
virtualDpadSize = VirtualDpadSize.LARGE,
|
||||
virtualDpadHorizontalOffset = 0f,
|
||||
virtualDpadVerticalOffset = 0f,
|
||||
)
|
||||
applyAppSettings()
|
||||
persistAppSettings()
|
||||
renderSettings()
|
||||
}
|
||||
})
|
||||
settingsPanel.addView(Button(this).apply {
|
||||
text = "手动设置手柄按键、摇杆与功能键"
|
||||
setOnClickListener {
|
||||
@@ -1117,12 +1200,24 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
|
||||
private fun applyAppSettings() {
|
||||
muted = appSettings.muted
|
||||
if (::virtualGamepad.isInitialized) {
|
||||
virtualGamepad.configureDpad(
|
||||
mode = appSettings.directionMode,
|
||||
scale = appSettings.virtualDpadSize.scale,
|
||||
horizontalOffset = appSettings.virtualDpadHorizontalOffset,
|
||||
verticalOffset = appSettings.virtualDpadVerticalOffset,
|
||||
)
|
||||
}
|
||||
val directionState = inputRouter.setEightDirectionEnabled(
|
||||
appSettings.directionMode == DirectionMode.EIGHT_WAY,
|
||||
)
|
||||
if (::emulatorSurface.isInitialized) {
|
||||
emulatorSurface.integerScale = appSettings.integerScale
|
||||
emulatorSurface.linearFilter = appSettings.linearFilter
|
||||
}
|
||||
updateVirtualGamepadVisibility()
|
||||
if (activeSessionId != 0L) {
|
||||
applyRoutedInput(directionState)
|
||||
NativeBridge.command(activeSessionId, "volume:${appSettings.volume}")
|
||||
NativeBridge.command(activeSessionId, if (appSettings.muted) "mute:on" else "mute:off")
|
||||
}
|
||||
|
||||
@@ -28,6 +28,9 @@ internal fun computeVirtualGamepadGeometry(
|
||||
maxRadius: Float,
|
||||
safeLeft: Float = 0f,
|
||||
safeRight: Float = 0f,
|
||||
dpadScale: Float = 1f,
|
||||
dpadHorizontalOffset: Float = 0f,
|
||||
dpadVerticalOffset: Float = 0f,
|
||||
): VirtualGamepadGeometry {
|
||||
val w = width.coerceAtLeast(1f)
|
||||
val h = height.coerceAtLeast(1f)
|
||||
@@ -37,41 +40,46 @@ internal fun computeVirtualGamepadGeometry(
|
||||
val gameWidth = min(usableWidth, h * 4f / 3f)
|
||||
val sideWidth = ((usableWidth - gameWidth) / 2f).coerceAtLeast(1f)
|
||||
val baseRadius = min(min(h * 0.095f, sideWidth * 0.16f), maxRadius)
|
||||
val radius = baseRadius * 1.50f
|
||||
val faceRadius = baseRadius * 1.50f
|
||||
val dpadRadius = faceRadius * dpadScale.coerceIn(0.85f, 1.50f)
|
||||
val menuRadius = baseRadius * 0.60f * 1.30f
|
||||
val utilityRadius = baseRadius * 0.58f * 1.30f
|
||||
val unshiftedDpadX = leftInset + sideWidth * 0.50f
|
||||
val unshiftedDpadLeft = unshiftedDpadX - radius * 2f
|
||||
val edgeGap = baseRadius * 0.10f
|
||||
val horizontalShift = min(radius * 2f, (unshiftedDpadLeft - edgeGap).coerceAtLeast(0f))
|
||||
val unshiftedDpadLeft = unshiftedDpadX - dpadRadius * 2f
|
||||
val edgeGap = baseRadius * 0.05f
|
||||
val horizontalShift = min(faceRadius * 2f, (unshiftedDpadLeft - edgeGap).coerceAtLeast(0f))
|
||||
val gameLeft = leftInset + sideWidth - horizontalShift
|
||||
val gameRight = gameLeft + gameWidth
|
||||
val dpadX = unshiftedDpadX - horizontalShift
|
||||
val dpadY = h * 0.61f
|
||||
val dpadGap = radius
|
||||
val dpadOuterExtent = dpadRadius * 2f
|
||||
val dpadX = (unshiftedDpadX - horizontalShift +
|
||||
dpadHorizontalOffset.coerceIn(-1f, 1f) * faceRadius)
|
||||
.coerceIn(dpadOuterExtent, (w - dpadOuterExtent).coerceAtLeast(dpadOuterExtent))
|
||||
val dpadY = (h * (0.61f + dpadVerticalOffset.coerceIn(-1f, 1f) * 0.12f))
|
||||
.coerceIn(dpadOuterExtent, (h - dpadOuterExtent).coerceAtLeast(dpadOuterExtent))
|
||||
val dpadGap = dpadRadius
|
||||
val expandedRightWidth = (w - rightInset - gameRight).coerceAtLeast(1f)
|
||||
val faceX = gameRight + expandedRightWidth * 0.50f
|
||||
val faceY = h * 0.54f
|
||||
val faceGapX = min(radius * 1.70f, expandedRightWidth * 0.50f - radius)
|
||||
val faceGapY = radius * 1.70f
|
||||
val gameEdgeGap = radius * 0.25f
|
||||
val faceGapX = min(faceRadius * 1.70f, expandedRightWidth * 0.50f - faceRadius)
|
||||
val faceGapY = faceRadius * 1.70f
|
||||
val gameEdgeGap = faceRadius * 0.25f
|
||||
return VirtualGamepadGeometry(
|
||||
gameLeft = gameLeft,
|
||||
gameRight = gameRight,
|
||||
horizontalShift = horizontalShift,
|
||||
up = VirtualControl(dpadX, dpadY - dpadGap, radius),
|
||||
down = VirtualControl(dpadX, dpadY + dpadGap, radius),
|
||||
left = VirtualControl(dpadX - dpadGap, dpadY, radius),
|
||||
right = VirtualControl(dpadX + dpadGap, dpadY, radius),
|
||||
up = VirtualControl(dpadX, dpadY - dpadGap, dpadRadius),
|
||||
down = VirtualControl(dpadX, dpadY + dpadGap, dpadRadius),
|
||||
left = VirtualControl(dpadX - dpadGap, dpadY, dpadRadius),
|
||||
right = VirtualControl(dpadX + dpadGap, dpadY, dpadRadius),
|
||||
select = VirtualControl(
|
||||
leftInset + sideWidth - menuRadius - gameEdgeGap - horizontalShift,
|
||||
h * 0.89f,
|
||||
menuRadius,
|
||||
),
|
||||
faceY = VirtualControl(faceX, faceY - faceGapY, radius),
|
||||
faceB = VirtualControl(faceX + faceGapX, faceY, radius),
|
||||
faceA = VirtualControl(faceX, faceY + faceGapY, radius),
|
||||
faceX = VirtualControl(faceX - faceGapX, faceY, radius),
|
||||
faceY = VirtualControl(faceX, faceY - faceGapY, faceRadius),
|
||||
faceB = VirtualControl(faceX + faceGapX, faceY, faceRadius),
|
||||
faceA = VirtualControl(faceX, faceY + faceGapY, faceRadius),
|
||||
faceX = VirtualControl(faceX - faceGapX, faceY, faceRadius),
|
||||
start = VirtualControl(
|
||||
leftInset + sideWidth + gameWidth + menuRadius + gameEdgeGap - horizontalShift,
|
||||
h * 0.89f,
|
||||
|
||||
@@ -9,7 +9,9 @@ import android.graphics.RectF
|
||||
import android.view.HapticFeedbackConstants
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import org.neslite.app.input.DirectionMode
|
||||
import org.neslite.app.input.GameAction
|
||||
import org.neslite.app.input.VirtualDpadResolver
|
||||
import org.neslite.app.input.VirtualInputTracker
|
||||
import org.neslite.app.input.VirtualInputTransition
|
||||
import kotlin.math.hypot
|
||||
@@ -21,6 +23,7 @@ class VirtualGamepadView(
|
||||
private val onHorizontalShiftChanged: (Float) -> Unit = {},
|
||||
) : View(context) {
|
||||
private val tracker = VirtualInputTracker()
|
||||
private val dpadResolver = VirtualDpadResolver()
|
||||
private val resetPointers = mutableSetOf<Int>()
|
||||
private val resetRunnables = mutableMapOf<Int, Runnable>()
|
||||
private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.argb(145, 45, 52, 64) }
|
||||
@@ -43,6 +46,10 @@ class VirtualGamepadView(
|
||||
}
|
||||
private var safeLeft = 0f
|
||||
private var safeRight = 0f
|
||||
private var directionMode = DirectionMode.EIGHT_WAY
|
||||
private var dpadScale = 1.18f
|
||||
private var dpadHorizontalOffset = 0f
|
||||
private var dpadVerticalOffset = 0f
|
||||
|
||||
init {
|
||||
isFocusable = true
|
||||
@@ -77,6 +84,27 @@ class VirtualGamepadView(
|
||||
invalidate()
|
||||
}
|
||||
|
||||
internal fun configureDpad(
|
||||
mode: DirectionMode,
|
||||
scale: Float,
|
||||
horizontalOffset: Float,
|
||||
verticalOffset: Float,
|
||||
) {
|
||||
val normalizedScale = scale.coerceIn(0.85f, 1.50f)
|
||||
val normalizedHorizontal = horizontalOffset.coerceIn(-1f, 1f)
|
||||
val normalizedVertical = verticalOffset.coerceIn(-1f, 1f)
|
||||
if (directionMode == mode && dpadScale == normalizedScale &&
|
||||
dpadHorizontalOffset == normalizedHorizontal && dpadVerticalOffset == normalizedVertical
|
||||
) return
|
||||
clearInputs()
|
||||
directionMode = mode
|
||||
dpadScale = normalizedScale
|
||||
dpadHorizontalOffset = normalizedHorizontal
|
||||
dpadVerticalOffset = normalizedVertical
|
||||
onHorizontalShiftChanged(geometry().horizontalShift)
|
||||
invalidate()
|
||||
}
|
||||
|
||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> updatePointer(event, event.actionIndex)
|
||||
@@ -106,7 +134,7 @@ class VirtualGamepadView(
|
||||
val x = event.getX(index)
|
||||
val y = event.getY(index)
|
||||
if (inside(geometry().reset, x, y)) {
|
||||
emit(tracker.update(pointerId, null))
|
||||
emit(tracker.update(pointerId, emptySet()))
|
||||
beginResetHold(pointerId)
|
||||
return
|
||||
}
|
||||
@@ -114,7 +142,7 @@ class VirtualGamepadView(
|
||||
cancelPendingReset(pointerId, keepConsumed = true)
|
||||
return
|
||||
}
|
||||
emit(tracker.update(pointerId, hitTest(x, y)))
|
||||
emit(tracker.update(pointerId, hitTestActions(pointerId, x, y)))
|
||||
}
|
||||
|
||||
private fun releasePointer(pointerId: Int) {
|
||||
@@ -144,24 +172,35 @@ class VirtualGamepadView(
|
||||
|
||||
private fun emit(transitions: List<VirtualInputTransition>) {
|
||||
transitions.forEach { onTransition(it.pointerId, it.action, it.pressed) }
|
||||
if (transitions.any { it.pressed && it.action in DIRECTION_ACTIONS }) {
|
||||
performHapticFeedback(HapticFeedbackConstants.CLOCK_TICK)
|
||||
}
|
||||
if (transitions.isNotEmpty()) invalidate()
|
||||
}
|
||||
|
||||
internal fun hitTest(x: Float, y: Float): GameAction? {
|
||||
internal fun hitTestActions(pointerId: Int, x: Float, y: Float): Set<GameAction> {
|
||||
val geometry = geometry()
|
||||
val dpadCenterX = (geometry.left.x + geometry.right.x) / 2f
|
||||
val dpadCenterY = (geometry.up.y + geometry.down.y) / 2f
|
||||
val dpadTouchRadius = geometry.up.radius * 2f * DPAD_TOUCH_SCALE
|
||||
if (hypot(x - dpadCenterX, y - dpadCenterY) <= dpadTouchRadius) {
|
||||
return dpadResolver.resolve(
|
||||
dx = x - dpadCenterX,
|
||||
dy = y - dpadCenterY,
|
||||
outerRadius = dpadTouchRadius,
|
||||
mode = directionMode,
|
||||
previous = tracker.actionsFor(pointerId).filterTo(linkedSetOf()) { it in DIRECTION_ACTIONS },
|
||||
)
|
||||
}
|
||||
return when {
|
||||
inside(geometry.up, x, y) -> GameAction.UP
|
||||
inside(geometry.down, x, y) -> GameAction.DOWN
|
||||
inside(geometry.left, x, y) -> GameAction.LEFT
|
||||
inside(geometry.right, x, y) -> GameAction.RIGHT
|
||||
inside(geometry.select, x, y) -> GameAction.SELECT
|
||||
inside(geometry.faceY, x, y, 1.02f) -> GameAction.TURBO_B
|
||||
inside(geometry.faceB, x, y, 1.02f) -> GameAction.NES_B
|
||||
inside(geometry.faceA, x, y, 1.02f) -> GameAction.NES_A
|
||||
inside(geometry.faceX, x, y, 1.02f) -> GameAction.TURBO_A
|
||||
inside(geometry.start, x, y) -> GameAction.START
|
||||
inside(geometry.pause, x, y) -> GameAction.PAUSE_MENU
|
||||
else -> null
|
||||
inside(geometry.select, x, y) -> setOf(GameAction.SELECT)
|
||||
inside(geometry.faceY, x, y, 1.02f) -> setOf(GameAction.TURBO_B)
|
||||
inside(geometry.faceB, x, y, 1.02f) -> setOf(GameAction.NES_B)
|
||||
inside(geometry.faceA, x, y, 1.02f) -> setOf(GameAction.NES_A)
|
||||
inside(geometry.faceX, x, y, 1.02f) -> setOf(GameAction.TURBO_A)
|
||||
inside(geometry.start, x, y) -> setOf(GameAction.START)
|
||||
inside(geometry.pause, x, y) -> setOf(GameAction.PAUSE_MENU)
|
||||
else -> emptySet()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,9 +277,14 @@ class VirtualGamepadView(
|
||||
maxRadius = resources.displayMetrics.density * 42f,
|
||||
safeLeft = safeLeft,
|
||||
safeRight = safeRight,
|
||||
dpadScale = dpadScale,
|
||||
dpadHorizontalOffset = dpadHorizontalOffset,
|
||||
dpadVerticalOffset = dpadVerticalOffset,
|
||||
)
|
||||
|
||||
private companion object {
|
||||
const val RESET_HOLD_MS = 650L
|
||||
const val DPAD_TOUCH_SCALE = 1.30f
|
||||
val DIRECTION_ACTIONS = setOf(GameAction.UP, GameAction.DOWN, GameAction.LEFT, GameAction.RIGHT)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,21 @@ data class RoutedInputState(
|
||||
class InputRouter {
|
||||
private val sourcesByAction = GameAction.entries.associateWith { linkedSetOf<InputSource>() }
|
||||
private var fastForward = false
|
||||
private var eightDirectionEnabled = true
|
||||
private var lastDirectionAxis = DirectionAxis.HORIZONTAL
|
||||
|
||||
fun update(source: InputSource, action: GameAction, pressed: Boolean): RoutedInputState {
|
||||
val sources = sourcesByAction.getValue(action)
|
||||
val wasPressed = sources.isNotEmpty()
|
||||
if (pressed) sources += source else sources -= source
|
||||
val isPressed = sources.isNotEmpty()
|
||||
if (!wasPressed && isPressed) {
|
||||
when (action) {
|
||||
GameAction.UP, GameAction.DOWN -> lastDirectionAxis = DirectionAxis.VERTICAL
|
||||
GameAction.LEFT, GameAction.RIGHT -> lastDirectionAxis = DirectionAxis.HORIZONTAL
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
return snapshot(
|
||||
pauseMenuEdge = action == GameAction.PAUSE_MENU && !wasPressed && isPressed,
|
||||
newFastForward = if (action == GameAction.FAST_FORWARD && wasPressed != isPressed) isPressed else null,
|
||||
@@ -43,6 +52,11 @@ class InputRouter {
|
||||
|
||||
fun current(): RoutedInputState = snapshot()
|
||||
|
||||
fun setEightDirectionEnabled(enabled: Boolean): RoutedInputState {
|
||||
eightDirectionEnabled = enabled
|
||||
return snapshot()
|
||||
}
|
||||
|
||||
private fun snapshot(
|
||||
pauseMenuEdge: Boolean = false,
|
||||
newFastForward: Boolean? = null,
|
||||
@@ -53,10 +67,19 @@ class InputRouter {
|
||||
else -> null
|
||||
}
|
||||
fastForward = actualFastForward
|
||||
val up = pressed(GameAction.UP) && !pressed(GameAction.DOWN)
|
||||
val down = pressed(GameAction.DOWN) && !pressed(GameAction.UP)
|
||||
val left = pressed(GameAction.LEFT) && !pressed(GameAction.RIGHT)
|
||||
val right = pressed(GameAction.RIGHT) && !pressed(GameAction.LEFT)
|
||||
var up = pressed(GameAction.UP) && !pressed(GameAction.DOWN)
|
||||
var down = pressed(GameAction.DOWN) && !pressed(GameAction.UP)
|
||||
var left = pressed(GameAction.LEFT) && !pressed(GameAction.RIGHT)
|
||||
var right = pressed(GameAction.RIGHT) && !pressed(GameAction.LEFT)
|
||||
if (!eightDirectionEnabled && (up || down) && (left || right)) {
|
||||
if (lastDirectionAxis == DirectionAxis.HORIZONTAL) {
|
||||
up = false
|
||||
down = false
|
||||
} else {
|
||||
left = false
|
||||
right = false
|
||||
}
|
||||
}
|
||||
var mask = 0
|
||||
if (pressed(GameAction.NES_B)) mask = mask or (1 shl 0)
|
||||
if (pressed(GameAction.SELECT)) mask = mask or (1 shl 2)
|
||||
@@ -74,6 +97,8 @@ class InputRouter {
|
||||
|
||||
private fun pressed(action: GameAction): Boolean = sourcesByAction.getValue(action).isNotEmpty()
|
||||
private fun currentFastForward(): Boolean = pressed(GameAction.FAST_FORWARD)
|
||||
|
||||
private enum class DirectionAxis { HORIZONTAL, VERTICAL }
|
||||
}
|
||||
|
||||
data class AxisTransition(val source: InputSource, val pressed: Boolean)
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.neslite.app.input
|
||||
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.hypot
|
||||
|
||||
enum class DirectionMode { FOUR_WAY, EIGHT_WAY }
|
||||
|
||||
class VirtualDpadResolver(
|
||||
private val neutralRatio: Float = 0.15f,
|
||||
private val directionHoldBias: Float = 1.25f,
|
||||
) {
|
||||
init {
|
||||
require(neutralRatio in 0f..<1f)
|
||||
require(directionHoldBias >= 1f)
|
||||
}
|
||||
|
||||
fun resolve(
|
||||
dx: Float,
|
||||
dy: Float,
|
||||
outerRadius: Float,
|
||||
mode: DirectionMode,
|
||||
previous: Set<GameAction> = emptySet(),
|
||||
): Set<GameAction> {
|
||||
if (outerRadius <= 0f || hypot(dx, dy) > outerRadius) return emptySet()
|
||||
if (hypot(dx, dy) <= outerRadius * neutralRatio) return emptySet()
|
||||
|
||||
val horizontal = if (dx < 0f) GameAction.LEFT else GameAction.RIGHT
|
||||
val vertical = if (dy < 0f) GameAction.UP else GameAction.DOWN
|
||||
val heldHorizontal = previous.singleOrNull { it == GameAction.LEFT || it == GameAction.RIGHT }
|
||||
val heldVertical = previous.singleOrNull { it == GameAction.UP || it == GameAction.DOWN }
|
||||
val weightedX = abs(dx) * if (heldHorizontal == horizontal) directionHoldBias else 1f
|
||||
val weightedY = abs(dy) * if (heldVertical == vertical) directionHoldBias else 1f
|
||||
|
||||
if (mode == DirectionMode.FOUR_WAY) {
|
||||
return setOf(if (weightedX >= weightedY) horizontal else vertical)
|
||||
}
|
||||
|
||||
return when {
|
||||
weightedX > weightedY * EIGHT_WAY_CARDINAL_RATIO -> setOf(horizontal)
|
||||
weightedY > weightedX * EIGHT_WAY_CARDINAL_RATIO -> setOf(vertical)
|
||||
else -> linkedSetOf(horizontal, vertical)
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val EIGHT_WAY_CARDINAL_RATIO = 2.4142137f
|
||||
}
|
||||
}
|
||||
@@ -3,25 +3,31 @@ package org.neslite.app.input
|
||||
data class VirtualInputTransition(val pointerId: Int, val action: GameAction, val pressed: Boolean)
|
||||
|
||||
class VirtualInputTracker {
|
||||
private val active = mutableMapOf<Int, GameAction>()
|
||||
private val active = mutableMapOf<Int, Set<GameAction>>()
|
||||
|
||||
fun update(pointerId: Int, action: GameAction?): List<VirtualInputTransition> {
|
||||
val previous = active[pointerId]
|
||||
if (previous == action) return emptyList()
|
||||
val transitions = mutableListOf<VirtualInputTransition>()
|
||||
if (previous != null) transitions += VirtualInputTransition(pointerId, previous, false)
|
||||
if (action == null) active.remove(pointerId) else {
|
||||
active[pointerId] = action
|
||||
transitions += VirtualInputTransition(pointerId, action, true)
|
||||
fun update(pointerId: Int, action: GameAction?): List<VirtualInputTransition> =
|
||||
update(pointerId, action?.let(::setOf).orEmpty())
|
||||
|
||||
fun update(pointerId: Int, actions: Set<GameAction>): List<VirtualInputTransition> {
|
||||
val previous = active[pointerId].orEmpty()
|
||||
if (previous == actions) return emptyList()
|
||||
val transitions = buildList {
|
||||
(previous - actions).forEach { add(VirtualInputTransition(pointerId, it, false)) }
|
||||
(actions - previous).forEach { add(VirtualInputTransition(pointerId, it, true)) }
|
||||
}
|
||||
if (actions.isEmpty()) active.remove(pointerId) else active[pointerId] = actions.toSet()
|
||||
return transitions
|
||||
}
|
||||
|
||||
fun clear(): List<VirtualInputTransition> {
|
||||
val transitions = active.map { (pointerId, action) -> VirtualInputTransition(pointerId, action, false) }
|
||||
val transitions = active.flatMap { (pointerId, actions) ->
|
||||
actions.map { action -> VirtualInputTransition(pointerId, action, false) }
|
||||
}
|
||||
active.clear()
|
||||
return transitions
|
||||
}
|
||||
|
||||
fun activeActions(): Set<GameAction> = active.values.toSet()
|
||||
fun activeActions(): Set<GameAction> = active.values.flatten().toSet()
|
||||
|
||||
fun actionsFor(pointerId: Int): Set<GameAction> = active[pointerId].orEmpty()
|
||||
}
|
||||
|
||||
@@ -6,11 +6,26 @@ import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.floatPreferencesKey
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import kotlinx.coroutines.flow.first
|
||||
import org.neslite.app.input.DirectionMode
|
||||
|
||||
private val Context.appSettingsDataStore: DataStore<Preferences> by preferencesDataStore(name = "app_settings")
|
||||
|
||||
enum class VirtualDpadSize(val label: String, val scale: Float) {
|
||||
STANDARD("标准", 1.00f),
|
||||
LARGE("大", 1.18f),
|
||||
EXTRA_LARGE("特大", 1.35f),
|
||||
;
|
||||
|
||||
fun next(): VirtualDpadSize = entries[(ordinal + 1) % entries.size]
|
||||
|
||||
companion object {
|
||||
fun fromStored(value: Int): VirtualDpadSize = entries.getOrElse(value) { LARGE }
|
||||
}
|
||||
}
|
||||
|
||||
data class AppSettings(
|
||||
val integerScale: Boolean = false,
|
||||
val linearFilter: Boolean = false,
|
||||
@@ -19,6 +34,10 @@ data class AppSettings(
|
||||
val turboEnabled: Boolean = true,
|
||||
val fastForwardEnabled: Boolean = true,
|
||||
val showVirtualGamepadWithController: Boolean = true,
|
||||
val directionMode: DirectionMode = DirectionMode.EIGHT_WAY,
|
||||
val virtualDpadSize: VirtualDpadSize = VirtualDpadSize.LARGE,
|
||||
val virtualDpadHorizontalOffset: Float = 0f,
|
||||
val virtualDpadVerticalOffset: Float = 0f,
|
||||
val guideCompleted: Boolean = false,
|
||||
)
|
||||
|
||||
@@ -38,6 +57,12 @@ class AppSettingsStore(private val context: Context) {
|
||||
turboEnabled = values[TURBO] ?: true,
|
||||
fastForwardEnabled = values[FAST_FORWARD] ?: true,
|
||||
showVirtualGamepadWithController = values[SHOW_VIRTUAL_GAMEPAD_WITH_CONTROLLER] ?: true,
|
||||
directionMode = DirectionMode.entries.getOrElse(values[DIRECTION_MODE] ?: DirectionMode.EIGHT_WAY.ordinal) {
|
||||
DirectionMode.EIGHT_WAY
|
||||
},
|
||||
virtualDpadSize = VirtualDpadSize.fromStored(values[VIRTUAL_DPAD_SIZE] ?: VirtualDpadSize.LARGE.ordinal),
|
||||
virtualDpadHorizontalOffset = (values[VIRTUAL_DPAD_HORIZONTAL_OFFSET] ?: 0f).coerceIn(-1f, 1f),
|
||||
virtualDpadVerticalOffset = (values[VIRTUAL_DPAD_VERTICAL_OFFSET] ?: 0f).coerceIn(-1f, 1f),
|
||||
guideCompleted = values[GUIDE_COMPLETED] ?: false,
|
||||
)
|
||||
}
|
||||
@@ -51,6 +76,10 @@ class AppSettingsStore(private val context: Context) {
|
||||
values[TURBO] = settings.turboEnabled
|
||||
values[FAST_FORWARD] = settings.fastForwardEnabled
|
||||
values[SHOW_VIRTUAL_GAMEPAD_WITH_CONTROLLER] = settings.showVirtualGamepadWithController
|
||||
values[DIRECTION_MODE] = settings.directionMode.ordinal
|
||||
values[VIRTUAL_DPAD_SIZE] = settings.virtualDpadSize.ordinal
|
||||
values[VIRTUAL_DPAD_HORIZONTAL_OFFSET] = settings.virtualDpadHorizontalOffset.coerceIn(-1f, 1f)
|
||||
values[VIRTUAL_DPAD_VERTICAL_OFFSET] = settings.virtualDpadVerticalOffset.coerceIn(-1f, 1f)
|
||||
values[GUIDE_COMPLETED] = settings.guideCompleted
|
||||
}
|
||||
}
|
||||
@@ -64,6 +93,10 @@ class AppSettingsStore(private val context: Context) {
|
||||
private val FAST_FORWARD = booleanPreferencesKey("fast_forward_enabled")
|
||||
private val SHOW_VIRTUAL_GAMEPAD_WITH_CONTROLLER =
|
||||
booleanPreferencesKey("show_virtual_gamepad_with_controller")
|
||||
private val DIRECTION_MODE = intPreferencesKey("direction_mode")
|
||||
private val VIRTUAL_DPAD_SIZE = intPreferencesKey("virtual_dpad_size")
|
||||
private val VIRTUAL_DPAD_HORIZONTAL_OFFSET = floatPreferencesKey("virtual_dpad_horizontal_offset")
|
||||
private val VIRTUAL_DPAD_VERTICAL_OFFSET = floatPreferencesKey("virtual_dpad_vertical_offset")
|
||||
private val GUIDE_COMPLETED = booleanPreferencesKey("guide_completed")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ class BuildIdentityTest {
|
||||
@Test
|
||||
fun debugBuildUsesTheDocumentedIdentity() {
|
||||
assertEquals("org.neslite.app.debug", BuildConfig.APPLICATION_ID)
|
||||
assertEquals("0.9.10-debug", BuildConfig.VERSION_NAME)
|
||||
assertEquals(9010, BuildConfig.VERSION_CODE)
|
||||
assertEquals("0.9.11-debug", BuildConfig.VERSION_NAME)
|
||||
assertEquals(9011, BuildConfig.VERSION_CODE)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ class VirtualGamepadLayoutTest {
|
||||
val gameWidth = minOf(width, height * 4f / 3f)
|
||||
val sideWidth = (width - gameWidth) / 2f
|
||||
val previousPrimaryRadius = minOf(height * 0.095f, sideWidth * 0.16f, 126f)
|
||||
val geometry = computeVirtualGamepadGeometry(width, height, maxRadius = 126f)
|
||||
val geometry = computeVirtualGamepadGeometry(width, height, maxRadius = 126f, dpadScale = 1.18f)
|
||||
|
||||
assertEquals(previousPrimaryRadius * 1.50f, geometry.up.radius, 0.01f)
|
||||
assertEquals(previousPrimaryRadius * 1.50f * 1.18f, geometry.up.radius, 0.01f)
|
||||
assertEquals(previousPrimaryRadius * 1.50f, geometry.faceA.radius, 0.01f)
|
||||
assertEquals(previousPrimaryRadius * 0.60f * 1.30f, geometry.select.radius, 0.01f)
|
||||
assertEquals(previousPrimaryRadius * 0.60f * 1.30f, geometry.start.radius, 0.01f)
|
||||
@@ -33,6 +33,7 @@ class VirtualGamepadLayoutTest {
|
||||
height = height,
|
||||
maxRadius = 126f,
|
||||
safeLeft = safeLeft,
|
||||
dpadScale = 1.18f,
|
||||
)
|
||||
val gameWidth = minOf(width - safeLeft, height * 4f / 3f)
|
||||
val centeredGameLeft = safeLeft + (width - safeLeft - gameWidth) / 2f
|
||||
@@ -100,6 +101,28 @@ class VirtualGamepadLayoutTest {
|
||||
assertTrue(controls.all { it.y - it.radius >= 0f && it.y + it.radius <= height })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun extraLargeDpadPositionAdjustmentsCannotMoveItOffscreen() {
|
||||
val width = 2760f
|
||||
val height = 1256f
|
||||
val positions = listOf(-1f to -1f, -1f to 1f, 1f to -1f, 1f to 1f)
|
||||
|
||||
positions.forEach { (horizontal, vertical) ->
|
||||
val geometry = computeVirtualGamepadGeometry(
|
||||
width = width,
|
||||
height = height,
|
||||
maxRadius = 126f,
|
||||
dpadScale = 1.35f,
|
||||
dpadHorizontalOffset = horizontal,
|
||||
dpadVerticalOffset = vertical,
|
||||
)
|
||||
val dpadControls = listOf(geometry.up, geometry.down, geometry.left, geometry.right)
|
||||
|
||||
assertTrue(dpadControls.all { it.x - it.radius >= 0f && it.x + it.radius <= width })
|
||||
assertTrue(dpadControls.all { it.y - it.radius >= 0f && it.y + it.radius <= height })
|
||||
}
|
||||
}
|
||||
|
||||
private fun distance(first: VirtualControl, second: VirtualControl): Float =
|
||||
hypot(first.x - second.x, first.y - second.y)
|
||||
}
|
||||
|
||||
@@ -30,6 +30,21 @@ class InputRouterTest {
|
||||
assertTrue(recovered.joypadMask and (1 shl 6) != 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun directionModeSupportsJoystickDiagonalsAndFourWayAxisLocking() {
|
||||
val router = InputRouter()
|
||||
val horizontal = InputSource(12, InputSourceKind.AXIS, 0, 1)
|
||||
val vertical = InputSource(12, InputSourceKind.AXIS, 1, -1)
|
||||
router.update(horizontal, GameAction.RIGHT, true)
|
||||
val diagonal = router.update(vertical, GameAction.UP, true)
|
||||
|
||||
assertTrue(diagonal.joypadMask and (1 shl 7) != 0)
|
||||
assertTrue(diagonal.joypadMask and (1 shl 4) != 0)
|
||||
val fourWay = router.setEightDirectionEnabled(false)
|
||||
assertEquals(0, fourWay.joypadMask and (1 shl 7))
|
||||
assertTrue(fourWay.joypadMask and (1 shl 4) != 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun pauseAndFastCommandsOnlyChangeOnAggregateEdges() {
|
||||
val router = InputRouter()
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.neslite.app.input
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class VirtualDpadResolverTest {
|
||||
private val resolver = VirtualDpadResolver()
|
||||
|
||||
@Test
|
||||
fun fourWayChoosesOneDominantAxisAndHoldsTheCurrentDirectionNearBoundary() {
|
||||
assertEquals(
|
||||
setOf(GameAction.DOWN),
|
||||
resolver.resolve(0.95f, 1f, 2f, DirectionMode.FOUR_WAY),
|
||||
)
|
||||
assertEquals(
|
||||
setOf(GameAction.RIGHT),
|
||||
resolver.resolve(1f, 0.95f, 2f, DirectionMode.FOUR_WAY, setOf(GameAction.RIGHT)),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun eightWayEmitsTwoDirectionsOnlyInsideDiagonalSectors() {
|
||||
assertEquals(
|
||||
linkedSetOf(GameAction.RIGHT, GameAction.UP),
|
||||
resolver.resolve(1f, -1f, 2f, DirectionMode.EIGHT_WAY),
|
||||
)
|
||||
assertEquals(
|
||||
setOf(GameAction.RIGHT),
|
||||
resolver.resolve(1.5f, -0.2f, 2f, DirectionMode.EIGHT_WAY),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun centerAndOutsideTouchAreasAreNeutral() {
|
||||
assertEquals(emptySet<GameAction>(), resolver.resolve(0.1f, 0.1f, 2f, DirectionMode.EIGHT_WAY))
|
||||
assertEquals(emptySet<GameAction>(), resolver.resolve(3f, 0f, 2f, DirectionMode.EIGHT_WAY))
|
||||
}
|
||||
}
|
||||
@@ -48,4 +48,34 @@ class VirtualInputTrackerTest {
|
||||
assertTrue(router.update(touch, GameAction.NES_A, false).joypadMask and (1 shl 8) != 0)
|
||||
assertEquals(0, router.update(controller, GameAction.NES_A, false).joypadMask and (1 shl 8))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun onePointerCanHoldADiagonalAndReleaseOnlyOneDirection() {
|
||||
val tracker = VirtualInputTracker()
|
||||
tracker.update(4, linkedSetOf(GameAction.RIGHT, GameAction.UP))
|
||||
|
||||
assertEquals(setOf(GameAction.RIGHT, GameAction.UP), tracker.activeActions())
|
||||
assertEquals(
|
||||
listOf(VirtualInputTransition(4, GameAction.UP, false)),
|
||||
tracker.update(4, setOf(GameAction.RIGHT)),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun separatePointersCanHoldAAndBAtTheSameTime() {
|
||||
val tracker = VirtualInputTracker()
|
||||
val router = InputRouter()
|
||||
val transitions = tracker.update(1, GameAction.NES_A) + tracker.update(2, GameAction.NES_B)
|
||||
var state = router.current()
|
||||
transitions.forEach { transition ->
|
||||
state = router.update(
|
||||
InputSource(-1 - transition.pointerId, InputSourceKind.TOUCH, transition.action.ordinal),
|
||||
transition.action,
|
||||
transition.pressed,
|
||||
)
|
||||
}
|
||||
|
||||
assertTrue(state.joypadMask and (1 shl 8) != 0)
|
||||
assertTrue(state.joypadMask and (1 shl 0) != 0)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.neslite.app.settings
|
||||
|
||||
import org.neslite.app.input.DirectionMode
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
@@ -15,6 +16,10 @@ class AppSettingsTest {
|
||||
assertTrue(settings.turboEnabled)
|
||||
assertTrue(settings.fastForwardEnabled)
|
||||
assertTrue(settings.showVirtualGamepadWithController)
|
||||
assertEquals(DirectionMode.EIGHT_WAY, settings.directionMode)
|
||||
assertEquals(VirtualDpadSize.LARGE, settings.virtualDpadSize)
|
||||
assertEquals(0f, settings.virtualDpadHorizontalOffset)
|
||||
assertEquals(0f, settings.virtualDpadVerticalOffset)
|
||||
assertFalse(settings.guideCompleted)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user