feat: shift gameplay layout and spread face buttons
This commit is contained in:
@@ -21,8 +21,8 @@ android {
|
||||
applicationId = "org.neslite.app"
|
||||
minSdk = 28
|
||||
targetSdk = 36
|
||||
versionCode = 9009
|
||||
versionName = "0.9.9"
|
||||
versionCode = 9010
|
||||
versionName = "0.9.10"
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
|
||||
ndk {
|
||||
|
||||
@@ -113,6 +113,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
private lateinit var database: GameDatabase
|
||||
private lateinit var gameList: LinearLayout
|
||||
private lateinit var emulatorSurface: EmulatorSurface
|
||||
private lateinit var gameViewport: AspectRatioFrameLayout
|
||||
private lateinit var emulatorStatus: TextView
|
||||
private lateinit var audioStatus: TextView
|
||||
private lateinit var pauseMenu: LinearLayout
|
||||
@@ -381,6 +382,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
view.setPadding(0, 0, 0, 0)
|
||||
layoutInsets.gameplay.run {
|
||||
mainScreen.setPadding(left, top, right, bottom)
|
||||
virtualGamepad.setSafeHorizontalInsets(left, right)
|
||||
}
|
||||
layoutInsets.overlay.run {
|
||||
overlayScreen.setPadding(left, top, right, bottom)
|
||||
@@ -690,7 +692,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
|
||||
mainScreen = FrameLayout(context).apply {
|
||||
setBackgroundColor(Color.rgb(16, 19, 24))
|
||||
val viewport = AspectRatioFrameLayout(context).apply {
|
||||
gameViewport = AspectRatioFrameLayout(context).apply {
|
||||
setBackgroundColor(Color.BLACK)
|
||||
emulatorSurface = EmulatorSurface(context, ::recordSystemEvent)
|
||||
addView(
|
||||
@@ -720,7 +722,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
)
|
||||
}
|
||||
addView(
|
||||
viewport,
|
||||
gameViewport,
|
||||
FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT,
|
||||
@@ -736,6 +738,9 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
if (activeSessionId == 0L) showEmulatorStatus("请先从游戏列表选择游戏")
|
||||
else sendControlCommand("reset")
|
||||
},
|
||||
onHorizontalShiftChanged = { horizontalShift ->
|
||||
gameViewport.translationX = -horizontalShift
|
||||
},
|
||||
)
|
||||
addView(
|
||||
virtualGamepad,
|
||||
@@ -745,14 +750,6 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
),
|
||||
)
|
||||
}
|
||||
addView(
|
||||
virtualControlsOverlay,
|
||||
FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
),
|
||||
)
|
||||
|
||||
}
|
||||
addView(
|
||||
mainScreen,
|
||||
@@ -761,6 +758,13 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
),
|
||||
)
|
||||
addView(
|
||||
virtualControlsOverlay,
|
||||
FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
),
|
||||
)
|
||||
addView(
|
||||
LinearLayout(context).apply {
|
||||
orientation = LinearLayout.HORIZONTAL
|
||||
|
||||
@@ -7,6 +7,7 @@ internal data class VirtualControl(val x: Float, val y: Float, val radius: Float
|
||||
internal data class VirtualGamepadGeometry(
|
||||
val gameLeft: Float,
|
||||
val gameRight: Float,
|
||||
val horizontalShift: Float,
|
||||
val up: VirtualControl,
|
||||
val down: VirtualControl,
|
||||
val left: VirtualControl,
|
||||
@@ -25,37 +26,58 @@ internal fun computeVirtualGamepadGeometry(
|
||||
width: Float,
|
||||
height: Float,
|
||||
maxRadius: Float,
|
||||
safeLeft: Float = 0f,
|
||||
safeRight: Float = 0f,
|
||||
): VirtualGamepadGeometry {
|
||||
val w = width.coerceAtLeast(1f)
|
||||
val h = height.coerceAtLeast(1f)
|
||||
val gameWidth = min(w, h * 4f / 3f)
|
||||
val sideWidth = ((w - gameWidth) / 2f).coerceAtLeast(1f)
|
||||
val leftInset = safeLeft.coerceIn(0f, w)
|
||||
val rightInset = safeRight.coerceIn(0f, w - leftInset)
|
||||
val usableWidth = (w - leftInset - rightInset).coerceAtLeast(1f)
|
||||
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 menuRadius = baseRadius * 0.60f * 1.30f
|
||||
val utilityRadius = baseRadius * 0.58f * 1.30f
|
||||
val dpadX = sideWidth * 0.50f
|
||||
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 gameLeft = leftInset + sideWidth - horizontalShift
|
||||
val gameRight = gameLeft + gameWidth
|
||||
val dpadX = unshiftedDpadX - horizontalShift
|
||||
val dpadY = h * 0.61f
|
||||
val dpadGap = radius
|
||||
val faceX = w - sideWidth * 0.50f
|
||||
val expandedRightWidth = (w - rightInset - gameRight).coerceAtLeast(1f)
|
||||
val faceX = gameRight + expandedRightWidth * 0.50f
|
||||
val faceY = h * 0.54f
|
||||
val faceGapX = radius * 1.06f
|
||||
val faceGapX = min(radius * 1.70f, expandedRightWidth * 0.50f - radius)
|
||||
val faceGapY = radius * 1.70f
|
||||
val gameEdgeGap = radius * 0.25f
|
||||
return VirtualGamepadGeometry(
|
||||
gameLeft = sideWidth,
|
||||
gameRight = w - sideWidth,
|
||||
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),
|
||||
select = VirtualControl(sideWidth - menuRadius - gameEdgeGap, h * 0.89f, menuRadius),
|
||||
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),
|
||||
start = VirtualControl(w - sideWidth + menuRadius + gameEdgeGap, h * 0.89f, menuRadius),
|
||||
pause = VirtualControl(w - sideWidth * 0.72f, h * 0.13f, utilityRadius),
|
||||
reset = VirtualControl(w - sideWidth * 0.28f, h * 0.13f, utilityRadius),
|
||||
start = VirtualControl(
|
||||
leftInset + sideWidth + gameWidth + menuRadius + gameEdgeGap - horizontalShift,
|
||||
h * 0.89f,
|
||||
menuRadius,
|
||||
),
|
||||
pause = VirtualControl(w - rightInset - sideWidth * 0.72f - horizontalShift, h * 0.13f, utilityRadius),
|
||||
reset = VirtualControl(w - rightInset - sideWidth * 0.28f - horizontalShift, h * 0.13f, utilityRadius),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ class VirtualGamepadView(
|
||||
context: Context,
|
||||
private val onTransition: (pointerId: Int, action: GameAction, pressed: Boolean) -> Unit,
|
||||
private val onReset: () -> Unit,
|
||||
private val onHorizontalShiftChanged: (Float) -> Unit = {},
|
||||
) : View(context) {
|
||||
private val tracker = VirtualInputTracker()
|
||||
private val resetPointers = mutableSetOf<Int>()
|
||||
@@ -40,6 +41,8 @@ class VirtualGamepadView(
|
||||
private val secondaryTextPaint = Paint(textPaint).apply {
|
||||
color = Color.argb(225, 235, 238, 244)
|
||||
}
|
||||
private var safeLeft = 0f
|
||||
private var safeRight = 0f
|
||||
|
||||
init {
|
||||
isFocusable = true
|
||||
@@ -61,6 +64,19 @@ class VirtualGamepadView(
|
||||
drawControl(canvas, geometry.reset, "重置", compact = true, secondary = "长按", isPressed = resetPointers.isNotEmpty())
|
||||
}
|
||||
|
||||
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
||||
super.onSizeChanged(w, h, oldw, oldh)
|
||||
onHorizontalShiftChanged(geometry().horizontalShift)
|
||||
}
|
||||
|
||||
internal fun setSafeHorizontalInsets(left: Int, right: Int) {
|
||||
if (safeLeft == left.toFloat() && safeRight == right.toFloat()) return
|
||||
safeLeft = left.toFloat()
|
||||
safeRight = right.toFloat()
|
||||
onHorizontalShiftChanged(geometry().horizontalShift)
|
||||
invalidate()
|
||||
}
|
||||
|
||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> updatePointer(event, event.actionIndex)
|
||||
@@ -220,6 +236,8 @@ class VirtualGamepadView(
|
||||
width = width.toFloat(),
|
||||
height = height.toFloat(),
|
||||
maxRadius = resources.displayMetrics.density * 42f,
|
||||
safeLeft = safeLeft,
|
||||
safeRight = safeRight,
|
||||
)
|
||||
|
||||
private companion object {
|
||||
|
||||
@@ -7,7 +7,7 @@ class BuildIdentityTest {
|
||||
@Test
|
||||
fun debugBuildUsesTheDocumentedIdentity() {
|
||||
assertEquals("org.neslite.app.debug", BuildConfig.APPLICATION_ID)
|
||||
assertEquals("0.9.9-debug", BuildConfig.VERSION_NAME)
|
||||
assertEquals(9009, BuildConfig.VERSION_CODE)
|
||||
assertEquals("0.9.10-debug", BuildConfig.VERSION_NAME)
|
||||
assertEquals(9010, BuildConfig.VERSION_CODE)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,26 @@ class VirtualGamepadLayoutTest {
|
||||
assertEquals(previousPrimaryRadius * 0.58f * 1.30f, geometry.reset.radius, 0.01f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun magic8InsetAllowsAFullButtonDiameterShiftAndExpandsFaceSpacing() {
|
||||
val width = 2760f
|
||||
val height = 1256f
|
||||
val safeLeft = 238f
|
||||
val geometry = computeVirtualGamepadGeometry(
|
||||
width = width,
|
||||
height = height,
|
||||
maxRadius = 126f,
|
||||
safeLeft = safeLeft,
|
||||
)
|
||||
val gameWidth = minOf(width - safeLeft, height * 4f / 3f)
|
||||
val centeredGameLeft = safeLeft + (width - safeLeft - gameWidth) / 2f
|
||||
|
||||
assertEquals(geometry.faceA.radius * 2f, geometry.horizontalShift, 0.01f)
|
||||
assertEquals(centeredGameLeft - geometry.horizontalShift, geometry.gameLeft, 0.01f)
|
||||
assertTrue(geometry.left.x - geometry.left.radius > 0f)
|
||||
assertTrue(geometry.faceB.x - geometry.faceY.x > geometry.faceB.radius * 1.6f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun magic8ControlsStayOutsideTheFourByThreeViewport() {
|
||||
val geometry = computeVirtualGamepadGeometry(width = 2760f, height = 1256f, maxRadius = 126f)
|
||||
|
||||
Reference in New Issue
Block a user