From 20fa1855ca22b45216e146b5d316ebf05879f777 Mon Sep 17 00:00:00 2001 From: shen Date: Sat, 12 Sep 2026 21:20:39 +0800 Subject: [PATCH] feat: refine immersive game controls --- README.md | 2 +- app/build.gradle.kts | 4 +- .../main/java/org/neslite/app/MainActivity.kt | 180 ++++++++++++------ .../org/neslite/app/VirtualGamepadLayout.kt | 16 +- .../org/neslite/app/VirtualGamepadView.kt | 166 ++++++++++++---- .../neslite/app/input/VirtualInputTracker.kt | 2 + .../java/org/neslite/app/BuildIdentityTest.kt | 4 +- .../neslite/app/VirtualGamepadLayoutTest.kt | 11 ++ .../app/input/VirtualInputTrackerTest.kt | 11 ++ docs/architecture.md | 8 +- docs/build.md | 4 +- docs/identity.md | 2 +- docs/known-issues.md | 4 +- docs/reports/m8-release-candidate.md | 4 +- docs/usage.md | 8 +- tools/package-rc.ps1 | 4 +- 16 files changed, 312 insertions(+), 118 deletions(-) diff --git a/README.md b/README.md index cfcff31..8391fac 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # NESLite NESLite 是一个面向 Android 的轻量 NES/FC 模拟器前端。它使用固定版本的 -FCEUmm libretro 内核。当前仓库已准备 0.9.5 候选基线的冻结样本、输入延迟指标、升级架构、原生对齐审计 +FCEUmm libretro 内核。当前仓库已准备 0.9.6 候选基线的冻结样本、输入延迟指标、升级架构、原生对齐审计 和候选交付脚本;荣耀 Magic8 与 X5 Lite 已由截图确认枚举,但手机未开放 ADB,且发布签名和可观察 玩法的合法 ROM 样本尚未提供,因此 M2~M8 的完整实机验收及 G8 仍待补齐。 diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 59fa980..fffa1e2 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -21,8 +21,8 @@ android { applicationId = "org.neslite.app" minSdk = 28 targetSdk = 36 - versionCode = 9005 - versionName = "0.9.5" + versionCode = 9006 + versionName = "0.9.6" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" ndk { diff --git a/app/src/main/java/org/neslite/app/MainActivity.kt b/app/src/main/java/org/neslite/app/MainActivity.kt index 9476541..5a2d1ea 100644 --- a/app/src/main/java/org/neslite/app/MainActivity.kt +++ b/app/src/main/java/org/neslite/app/MainActivity.kt @@ -11,6 +11,7 @@ import android.hardware.input.InputManager import android.graphics.Bitmap import android.graphics.Color import android.graphics.drawable.GradientDrawable +import android.graphics.drawable.StateListDrawable import android.net.Uri import android.os.Bundle import android.os.Handler @@ -209,7 +210,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { ActivityResultContracts.CreateDocument("application/vnd.neslite.save"), ) { uri -> if (uri == null) { - emulatorStatus.text = "已取消导出存档包" + showEmulatorStatus("已取消导出存档包") } else { beginSavePackageExport(uri) } @@ -217,7 +218,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { private val openSavePackage = registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri -> if (uri == null) { - emulatorStatus.text = "已取消导入存档包" + showEmulatorStatus("已取消导入存档包") } else { beginSavePackageImport(uri) } @@ -260,9 +261,10 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { foregroundPreparePending = activeSessionId != 0L setContentView(createContentView()) installWindowInsets() + setSystemBarsVisible(false) installUnifiedBackHandler() if (restoredSessionDiscarded) { - emulatorStatus.text = "上次进程中的会话已结束;已安全返回游戏列表,不会自动重载故障内容。" + showEmulatorStatus("上次进程中的会话已结束;已安全返回游戏列表,不会自动重载故障内容。") } recordSystemEvent("activity_created") loadAppSettings() @@ -314,7 +316,9 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { override fun onWindowFocusChanged(hasFocus: Boolean) { super.onWindowFocusChanged(hasFocus) - if (!hasFocus && ::stateText.isInitialized) { + if (hasFocus && ::rootShell.isInitialized) { + setSystemBarsVisible(currentOverlayPage != null) + } else if (!hasFocus && ::stateText.isInitialized) { pauseForExternalReason("window_focus_lost") inputDiagnostics.clearAll("focus_lost", SystemClock.uptimeMillis()) renderInputState() @@ -339,7 +343,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { BackDecision.RESUME_GAME -> sendControlCommand("run") BackDecision.PAUSE_GAME -> sendControlCommand("pause") BackDecision.SHOW_PAUSE_MENU -> showPauseMenu() - BackDecision.IGNORE_TRANSITION -> emulatorStatus.text = "正在完成 $lastSessionState 状态交接,请稍候…" + BackDecision.IGNORE_TRANSITION -> showEmulatorStatus("正在完成 $lastSessionState 状态交接,请稍候…") BackDecision.EXIT_ACTIVITY -> finish() } } @@ -375,6 +379,20 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { ViewCompat.requestApplyInsets(rootShell) } + private fun setSystemBarsVisible(visible: Boolean) { + val controller = WindowCompat.getInsetsController(window, window.decorView) + controller.systemBarsBehavior = + androidx.core.view.WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE + if (visible) controller.show(WindowInsetsCompat.Type.systemBars()) + else controller.hide(WindowInsetsCompat.Type.systemBars()) + } + + private fun showEmulatorStatus(message: CharSequence) { + if (!::emulatorStatus.isInitialized) return + emulatorStatus.text = message + emulatorStatus.visibility = View.VISIBLE + } + private fun setDiagnosticsExpanded(expanded: Boolean) { if (!::diagnosticsPanel.isInitialized) return if (expanded) { @@ -420,6 +438,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { target.visibility = View.VISIBLE overlayTitle.text = title currentOverlayPage = page + setSystemBarsVisible(true) overlayScreen.visibility = View.VISIBLE contentScroll.post { contentScroll.scrollTo(0, 0) } focusFirstButton(target) @@ -429,6 +448,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { clearControllerState() currentOverlayPage = null overlayScreen.visibility = View.GONE + setSystemBarsVisible(false) if (::libraryRailButton.isInitialized && lastSessionState != "Running") { libraryRailButton.requestFocus() } @@ -648,6 +668,24 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { ViewGroup.LayoutParams.MATCH_PARENT, ), ) + emulatorStatus = TextView(context).apply { + text = "未选择游戏\n请点左侧“列表”" + textSize = 12f + setTextColor(Color.WHITE) + gravity = Gravity.CENTER + maxWidth = dp(280) + maxLines = 4 + setPadding(dp(14), dp(8), dp(14), dp(8)) + background = roundedBackground(Color.argb(205, 0, 0, 0), dp(10).toFloat()) + } + addView( + emulatorStatus, + FrameLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT, + Gravity.CENTER, + ), + ) } addView( viewport, @@ -663,7 +701,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { context = context, onTransition = ::handleVirtualInput, onReset = { - if (activeSessionId == 0L) emulatorStatus.text = "请先从游戏列表选择游戏" + if (activeSessionId == 0L) showEmulatorStatus("请先从游戏列表选择游戏") else sendControlCommand("reset") }, ) @@ -686,34 +724,18 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { addView( LinearLayout(context).apply { orientation = LinearLayout.VERTICAL - background = roundedBackground(Color.argb(205, 34, 39, 49), dp(12).toFloat()) - setPadding(dp(6), dp(6), dp(6), dp(6)) - libraryRailButton = railButton("列表", ::openLibraryPage) + background = roundedBackground(Color.argb(120, 34, 39, 49), dp(10).toFloat()) + setPadding(dp(4), dp(4), dp(4), dp(4)) + libraryRailButton = railButton("≡\n列表", ::openLibraryPage) addView(libraryRailButton) - addView(railButton("导入", ::openImportPage)) - addView(railButton("设置", ::showSettings)) + addView(railButton("+\n导入", ::openImportPage)) + addView(railButton("⚙\n设置", ::showSettings)) }, - FrameLayout.LayoutParams(dp(78), ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.START or Gravity.TOP).apply { + FrameLayout.LayoutParams(dp(58), ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.START or Gravity.TOP).apply { leftMargin = dp(8) topMargin = dp(8) }, ) - - emulatorStatus = TextView(context).apply { - text = "未选择游戏" - textSize = 12f - setTextColor(Color.WHITE) - gravity = Gravity.CENTER - maxLines = 2 - setPadding(dp(10), dp(5), dp(10), dp(5)) - background = roundedBackground(Color.argb(190, 0, 0, 0), dp(10).toFloat()) - } - addView( - emulatorStatus, - FrameLayout.LayoutParams(dp(520), ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL or Gravity.BOTTOM).apply { - bottomMargin = dp(8) - }, - ) } addView( mainScreen, @@ -1100,7 +1122,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { private fun showSaveManager() { if (activeSessionId == 0L || activeGameHash.isBlank() || lastSessionState != "Paused") { - emulatorStatus.text = "请先加载游戏并暂停,再打开存档管理" + showEmulatorStatus("请先加载游戏并暂停,再打开存档管理") return } saveManagerPanel.removeAllViews() @@ -1543,7 +1565,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { } private fun importRom(uri: Uri) { - emulatorStatus.text = "正在流式导入…" + showEmulatorStatus("正在流式导入…") if (::importStatus.isInitialized) importStatus.text = "正在读取并校验文件…" libraryExecutor.execute { runCatching { RomImporter(contentResolver, filesDir, database.games()).import(uri) } @@ -1554,7 +1576,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { if (result.duplicate) "内容已存在,已复用原文件" else "导入完成", Toast.LENGTH_LONG, ).show() - emulatorStatus.text = "导入完成:${result.game.displayName}" + showEmulatorStatus("导入完成:${result.game.displayName}") importStatus.text = "导入完成:${result.game.displayName}" refreshLibrary() openLibraryPage() @@ -1563,7 +1585,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { .onFailure { error -> runOnUiThread { val message = "导入失败:${error.message ?: error.javaClass.simpleName}" - emulatorStatus.text = message + showEmulatorStatus(message) importStatus.text = message } } @@ -1575,7 +1597,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { val report = LibraryRecovery(filesDir, database.games()).reconcile() runOnUiThread { if (report.removedTemps + report.recoveredFiles + report.missingRows > 0) { - emulatorStatus.text = "恢复检查:临时 ${report.removedTemps},恢复 ${report.recoveredFiles},缺失 ${report.missingRows}" + showEmulatorStatus("恢复检查:临时 ${report.removedTemps},恢复 ${report.recoveredFiles},缺失 ${report.missingRows}") } refreshLibrary() } @@ -1649,7 +1671,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { private fun loadGame(game: GameEntity) { if (pendingSessionCommand != null) { - emulatorStatus.text = "正在完成会话状态交接,请稍候。" + showEmulatorStatus("正在完成会话状态交接,请稍候。") return } if (crashLoopGuard.isBlocked(game.sha256)) { @@ -1688,12 +1710,12 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { database.games().markPlayed(game.sha256, System.currentTimeMillis()) runOnUiThread { refreshLibrary() } } - emulatorStatus.text = if (activeSessionId == 0L) "命令队列已满" else "正在加载会话 $activeSessionId" + showEmulatorStatus(if (activeSessionId == 0L) "命令队列已满" else "正在加载会话 $activeSessionId") } private fun beginSavePackageExport(uri: Uri) { if (activeSessionId == 0L || lastSessionState != "Paused") { - emulatorStatus.text = "请先加载游戏并暂停,再导出存档包" + showEmulatorStatus("请先加载游戏并暂停,再导出存档包") return } val temporary = File(cacheDir, "save-export-$activeSessionId.nsp") @@ -1704,9 +1726,9 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { if (!NativeBridge.savePackageCommand(activeSessionId, "package_export", temporary.absolutePath)) { pendingSaveExportUri = null pendingSaveExportFile = null - emulatorStatus.text = "存档导出命令队列已满" + showEmulatorStatus("存档导出命令队列已满") } else { - emulatorStatus.text = "正在准备不含 ROM 的存档包" + showEmulatorStatus("正在准备不含 ROM 的存档包") } } @@ -1726,15 +1748,17 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { source.delete() File(source.absolutePath + ".bak").delete() runOnUiThread { - emulatorStatus.text = if (result.isSuccess) "存档包导出成功(不含 ROM)" else - "存档包导出失败:${result.exceptionOrNull()?.message ?: "无法写入目标"}" + showEmulatorStatus( + if (result.isSuccess) "存档包导出成功(不含 ROM)" else + "存档包导出失败:${result.exceptionOrNull()?.message ?: "无法写入目标"}", + ) } } } private fun beginSavePackageImport(uri: Uri) { if (activeSessionId == 0L || lastSessionState != "Paused") { - emulatorStatus.text = "请先加载对应游戏并暂停,再导入存档包" + showEmulatorStatus("请先加载对应游戏并暂停,再导入存档包") return } val target = File(cacheDir, "save-import-$activeSessionId.nsp") @@ -1762,8 +1786,10 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { } if (result.isFailure) target.delete() runOnUiThread { - emulatorStatus.text = if (result.isSuccess) "正在校验并导入存档包" else - "存档包导入失败:${result.exceptionOrNull()?.message ?: "无法读取来源"}" + showEmulatorStatus( + if (result.isSuccess) "正在校验并导入存档包" else + "存档包导入失败:${result.exceptionOrNull()?.message ?: "无法读取来源"}", + ) } } } @@ -1777,9 +1803,12 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { lastSessionState = nextState val completionId = status.optLong("completionId") val completion = status.optString("completion") - emulatorStatus.text = "状态:$nextState · $completion" + - (if (status.optBoolean("fastForward")) " · 2×" else "") + - status.optString("error").takeIf { it.isNotBlank() }?.let { "\n错误:$it;可留在暂停菜单重试或退出。" }.orEmpty() + val error = status.optString("error") + showEmulatorStatus( + "状态:$nextState · $completion" + + (if (status.optBoolean("fastForward")) " · 2×" else "") + + error.takeIf { it.isNotBlank() }?.let { "\n错误:$it;可留在暂停菜单重试或退出。" }.orEmpty(), + ) if (completionId != lastCompletionId) { lastCompletionId = completionId settleSessionTransition(nextState, completion) @@ -1804,6 +1833,9 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { refreshSaveManager() } } + if (nextState == "Running" && error.isBlank() && pendingSessionCommand == null) { + emulatorStatus.visibility = View.GONE + } recordRefreshRateSample(status) val candidateCount = status.optInt("recoveryCandidates") if (status.optBoolean("abnormalSession") && abnormalRecordedSession != activeSessionId && @@ -1847,7 +1879,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { "回调 ${audio.optLong("callbacks")},帧/回调 ${audio.optInt("maxCallbackFrames")},缓冲 ${audio.optInt("bufferLevel")}/${audio.optInt("bufferTarget")}/${audio.optInt("bufferCapacity")},欠载 ${audio.optLong("underrunFrames")},重建 ${audio.optLong("rebuilds")}\n" + "速度 ${String.format(Locale.ROOT, "%.3f", timing.optDouble("speedRatio"))}×,模拟帧 ${timing.optLong("frames")},帧工作 ${String.format(Locale.ROOT, "%.2f", timing.optDouble("averageFrameWorkMs"))}/${String.format(Locale.ROOT, "%.2f", timing.optDouble("maximumFrameWorkMs"))} ms\n" + "输入延迟 ${input.optLong("latencySamples")}/1000,P95 ${String.format(Locale.ROOT, "%.2f", input.optDouble("latencyP95Ms"))} ms(JNI→核心首次采样)" - }.onFailure { emulatorStatus.text = raw } + }.onFailure { showEmulatorStatus(raw) } } mainHandler.postDelayed(::pollSessionStatus, 250) } @@ -1938,28 +1970,32 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { if (activeSessionId == 0L) return false val transitionCommand = command in setOf("run", "pause", "stop") if (!sessionCommandAllowed(pendingSessionCommand, command, lastSessionState)) { - emulatorStatus.text = pendingSessionCommand?.let { "正在完成 $it 状态交接,已忽略重复操作。" } - ?: "当前状态 $lastSessionState 不接受命令:$command" + showEmulatorStatus( + pendingSessionCommand?.let { "正在完成 $it 状态交接,已忽略重复操作。" } + ?: "当前状态 $lastSessionState 不接受命令:$command", + ) return false } if (command == "run" && audioManager.requestAudioFocus(audioFocusRequest) != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { - emulatorStatus.text = "音频焦点未获准,游戏保持暂停" + showEmulatorStatus("音频焦点未获准,游戏保持暂停") return false } if (command == "pause" || command == "stop") clearControllerState() if (!NativeBridge.command(activeSessionId, command)) { - emulatorStatus.text = "命令队列已满:$command" + showEmulatorStatus("命令队列已满:$command") if (command == "run") audioManager.abandonAudioFocusRequest(audioFocusRequest) return false } if (transitionCommand) { pendingSessionCommand = command pendingPauseShowsMenu = command == "pause" - emulatorStatus.text = when (command) { - "pause" -> "正在安全暂停并请求存档…" - "run" -> "正在恢复游戏资源…" - else -> "正在安全保存并退出…" - } + showEmulatorStatus( + when (command) { + "pause" -> "正在安全暂停并请求存档…" + "run" -> "正在恢复游戏资源…" + else -> "正在安全保存并退出…" + }, + ) } when (command) { "run", "stop" -> { @@ -2198,8 +2234,23 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { private fun railButton(label: String, action: () -> Unit) = Button(this).apply { text = label isAllCaps = false - minHeight = dp(44) - setPadding(0, 0, 0, 0) + textSize = 11f + setTextColor(Color.WHITE) + minHeight = 0 + minimumHeight = 0 + minWidth = 0 + minimumWidth = 0 + gravity = Gravity.CENTER + setPadding(dp(2), dp(2), dp(2), dp(2)) + background = pressableRoundedBackground( + normalColor = Color.argb(130, 55, 63, 78), + pressedColor = Color.argb(235, 105, 83, 190), + radius = dp(8).toFloat(), + ) + layoutParams = LinearLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + dp(42), + ).apply { setMargins(0, dp(2), 0, dp(2)) } setOnClickListener { action() } } @@ -2208,6 +2259,19 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { cornerRadius = radius } + private fun pressableRoundedBackground(normalColor: Int, pressedColor: Int, radius: Float) = + StateListDrawable().apply { + addState( + intArrayOf(android.R.attr.state_pressed), + roundedBackground(pressedColor, radius), + ) + addState( + intArrayOf(android.R.attr.state_focused), + roundedBackground(pressedColor, radius), + ) + addState(intArrayOf(), roundedBackground(normalColor, radius)) + } + private fun dp(value: Int): Int = (value * resources.displayMetrics.density).toInt() private fun defaultExportName(): String { diff --git a/app/src/main/java/org/neslite/app/VirtualGamepadLayout.kt b/app/src/main/java/org/neslite/app/VirtualGamepadLayout.kt index dabb717..efa4c42 100644 --- a/app/src/main/java/org/neslite/app/VirtualGamepadLayout.kt +++ b/app/src/main/java/org/neslite/app/VirtualGamepadLayout.kt @@ -31,13 +31,15 @@ internal fun computeVirtualGamepadGeometry( val gameWidth = min(w, h * 4f / 3f) val sideWidth = ((w - gameWidth) / 2f).coerceAtLeast(1f) val radius = min(min(h * 0.095f, sideWidth * 0.16f), maxRadius) - val smallRadius = radius * 0.76f + val menuRadius = radius * 0.60f + val utilityRadius = radius * 0.58f val dpadX = sideWidth * 0.50f - val dpadY = h * 0.54f + val dpadY = h * 0.61f val dpadGap = radius * 1.42f val faceX = w - sideWidth * 0.50f val faceY = h * 0.54f - val faceGap = radius * 1.34f + val faceGap = radius * 1.85f + val gameEdgeGap = radius * 0.25f return VirtualGamepadGeometry( gameLeft = sideWidth, gameRight = w - sideWidth, @@ -45,13 +47,13 @@ internal fun computeVirtualGamepadGeometry( down = VirtualControl(dpadX, dpadY + dpadGap, radius), left = VirtualControl(dpadX - dpadGap, dpadY, radius), right = VirtualControl(dpadX + dpadGap, dpadY, radius), - select = VirtualControl(dpadX, h * 0.87f, smallRadius), + select = VirtualControl(sideWidth - menuRadius - gameEdgeGap, h * 0.89f, menuRadius), faceY = VirtualControl(faceX, faceY - faceGap, radius), faceB = VirtualControl(faceX + faceGap, faceY, radius), faceA = VirtualControl(faceX, faceY + faceGap, radius), faceX = VirtualControl(faceX - faceGap, faceY, radius), - start = VirtualControl(faceX, h * 0.87f, smallRadius), - pause = VirtualControl(w - sideWidth * 0.72f, h * 0.15f, smallRadius), - reset = VirtualControl(w - sideWidth * 0.28f, h * 0.15f, smallRadius), + 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), ) } diff --git a/app/src/main/java/org/neslite/app/VirtualGamepadView.kt b/app/src/main/java/org/neslite/app/VirtualGamepadView.kt index 6b8fa51..1eedeab 100644 --- a/app/src/main/java/org/neslite/app/VirtualGamepadView.kt +++ b/app/src/main/java/org/neslite/app/VirtualGamepadView.kt @@ -4,10 +4,14 @@ import android.content.Context import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint +import android.graphics.Path +import android.graphics.RectF +import android.view.HapticFeedbackConstants import android.view.MotionEvent import android.view.View import org.neslite.app.input.GameAction import org.neslite.app.input.VirtualInputTracker +import org.neslite.app.input.VirtualInputTransition import kotlin.math.hypot class VirtualGamepadView( @@ -17,9 +21,14 @@ class VirtualGamepadView( ) : View(context) { private val tracker = VirtualInputTracker() private val resetPointers = mutableSetOf() - private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.argb(175, 45, 52, 64) } + private val resetRunnables = mutableMapOf() + private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.argb(145, 45, 52, 64) } + private val turboFill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.argb(150, 65, 79, 99) } + private val aFill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.argb(180, 178, 57, 68) } + private val bFill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.argb(180, 183, 104, 45) } + private val pressedFill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.argb(235, 105, 83, 190) } private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { - color = Color.argb(210, 184, 190, 202) + color = Color.argb(185, 184, 190, 202) style = Paint.Style.STROKE strokeWidth = resources.displayMetrics.density * 1.5f } @@ -28,6 +37,9 @@ class VirtualGamepadView( textAlign = Paint.Align.CENTER typeface = android.graphics.Typeface.DEFAULT_BOLD } + private val secondaryTextPaint = Paint(textPaint).apply { + color = Color.argb(225, 235, 238, 244) + } init { isFocusable = true @@ -37,18 +49,16 @@ class VirtualGamepadView( override fun onDraw(canvas: Canvas) { super.onDraw(canvas) val geometry = geometry() - drawControl(canvas, geometry.up, "↑") - drawControl(canvas, geometry.down, "↓") - drawControl(canvas, geometry.left, "←") - drawControl(canvas, geometry.right, "→") - drawControl(canvas, geometry.select, "Select", compact = true) - drawControl(canvas, geometry.faceY, "Y") - drawControl(canvas, geometry.faceB, "B") - drawControl(canvas, geometry.faceA, "A") - drawControl(canvas, geometry.faceX, "X") - drawControl(canvas, geometry.start, "Start", compact = true) - drawControl(canvas, geometry.pause, "暂停", compact = true) - drawControl(canvas, geometry.reset, "重置", compact = true) + val pressed = tracker.activeActions() + drawDpad(canvas, geometry, pressed) + drawControl(canvas, geometry.select, "Select", compact = true, isPressed = GameAction.SELECT in pressed) + drawControl(canvas, geometry.faceY, "Y", basePaint = turboFill, secondary = "连发", isPressed = GameAction.TURBO_B in pressed) + drawControl(canvas, geometry.faceB, "B", basePaint = bFill, isPressed = GameAction.NES_B in pressed) + drawControl(canvas, geometry.faceA, "A", basePaint = aFill, isPressed = GameAction.NES_A in pressed) + drawControl(canvas, geometry.faceX, "X", basePaint = turboFill, secondary = "连发", isPressed = GameAction.TURBO_A in pressed) + drawControl(canvas, geometry.start, "Start", compact = true, isPressed = GameAction.START in pressed) + drawControl(canvas, geometry.pause, "暂停", compact = true, isPressed = GameAction.PAUSE_MENU in pressed) + drawControl(canvas, geometry.reset, "重置", compact = true, secondary = "长按", isPressed = resetPointers.isNotEmpty()) } override fun onTouchEvent(event: MotionEvent): Boolean { @@ -68,8 +78,11 @@ class VirtualGamepadView( } fun clearInputs() { - tracker.clear().forEach { onTransition(it.pointerId, it.action, it.pressed) } + resetRunnables.values.forEach(::removeCallbacks) + resetRunnables.clear() resetPointers.clear() + emit(tracker.clear()) + invalidate() } private fun updatePointer(event: MotionEvent, index: Int) { @@ -77,17 +90,45 @@ class VirtualGamepadView( val x = event.getX(index) val y = event.getY(index) if (inside(geometry().reset, x, y)) { - tracker.update(pointerId, null).forEach { onTransition(it.pointerId, it.action, it.pressed) } - if (resetPointers.add(pointerId)) onReset() + emit(tracker.update(pointerId, null)) + beginResetHold(pointerId) return } - if (pointerId in resetPointers) return - tracker.update(pointerId, hitTest(x, y)).forEach { onTransition(it.pointerId, it.action, it.pressed) } + if (pointerId in resetPointers) { + cancelPendingReset(pointerId, keepConsumed = true) + return + } + emit(tracker.update(pointerId, hitTest(x, y))) } private fun releasePointer(pointerId: Int) { - resetPointers.remove(pointerId) - tracker.update(pointerId, null).forEach { onTransition(it.pointerId, it.action, it.pressed) } + cancelPendingReset(pointerId, keepConsumed = false) + emit(tracker.update(pointerId, null)) + } + + private fun beginResetHold(pointerId: Int) { + if (!resetPointers.add(pointerId)) return + val runnable = Runnable { + if (resetRunnables.remove(pointerId) != null && pointerId in resetPointers) { + performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) + onReset() + invalidate() + } + } + resetRunnables[pointerId] = runnable + postDelayed(runnable, RESET_HOLD_MS) + invalidate() + } + + private fun cancelPendingReset(pointerId: Int, keepConsumed: Boolean) { + resetRunnables.remove(pointerId)?.let(::removeCallbacks) + if (!keepConsumed) resetPointers.remove(pointerId) + invalidate() + } + + private fun emit(transitions: List) { + transitions.forEach { onTransition(it.pointerId, it.action, it.pressed) } + if (transitions.isNotEmpty()) invalidate() } internal fun hitTest(x: Float, y: Float): GameAction? { @@ -98,29 +139,90 @@ class VirtualGamepadView( 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) -> GameAction.TURBO_B - inside(geometry.faceB, x, y) -> GameAction.NES_B - inside(geometry.faceA, x, y) -> GameAction.NES_A - inside(geometry.faceX, x, y) -> GameAction.TURBO_A + 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 } } - private fun drawControl(canvas: Canvas, control: VirtualControl, label: String, compact: Boolean = false) { - canvas.drawCircle(control.x, control.y, control.radius, fill) - canvas.drawCircle(control.x, control.y, control.radius, stroke) - textPaint.textSize = control.radius * if (compact) 0.42f else 0.62f - canvas.drawText(label, control.x, control.y - (textPaint.ascent() + textPaint.descent()) / 2f, textPaint) + private fun drawDpad(canvas: Canvas, geometry: VirtualGamepadGeometry, pressed: Set) { + val centerX = (geometry.left.x + geometry.right.x) / 2f + val centerY = (geometry.up.y + geometry.down.y) / 2f + val armHalfWidth = geometry.up.radius * 0.72f + val corner = geometry.up.radius * 0.24f + val horizontal = RectF( + geometry.left.x - geometry.left.radius, + centerY - armHalfWidth, + geometry.right.x + geometry.right.radius, + centerY + armHalfWidth, + ) + val vertical = RectF( + centerX - armHalfWidth, + geometry.up.y - geometry.up.radius, + centerX + armHalfWidth, + geometry.down.y + geometry.down.radius, + ) + val horizontalPath = Path().apply { addRoundRect(horizontal, corner, corner, Path.Direction.CW) } + val verticalPath = Path().apply { addRoundRect(vertical, corner, corner, Path.Direction.CW) } + horizontalPath.op(verticalPath, Path.Op.UNION) + canvas.drawPath(horizontalPath, fill) + canvas.drawPath(horizontalPath, stroke) + listOf( + Triple(geometry.up, "↑", GameAction.UP), + Triple(geometry.down, "↓", GameAction.DOWN), + Triple(geometry.left, "←", GameAction.LEFT), + Triple(geometry.right, "→", GameAction.RIGHT), + ).forEach { (control, label, action) -> + if (action in pressed) canvas.drawCircle(control.x, control.y, control.radius * 0.82f, pressedFill) + drawLabel(canvas, control, label) + } } - private fun inside(control: VirtualControl, x: Float, y: Float): Boolean = - hypot(x - control.x, y - control.y) <= control.radius * 1.14f + private fun drawControl( + canvas: Canvas, + control: VirtualControl, + label: String, + compact: Boolean = false, + basePaint: Paint = fill, + secondary: String? = null, + isPressed: Boolean = false, + ) { + canvas.drawCircle(control.x, control.y, control.radius, if (isPressed) pressedFill else basePaint) + canvas.drawCircle(control.x, control.y, control.radius, stroke) + drawLabel(canvas, control, label, compact, secondary) + } + + private fun drawLabel( + canvas: Canvas, + control: VirtualControl, + label: String, + compact: Boolean = false, + secondary: String? = null, + ) { + textPaint.textSize = control.radius * if (compact) 0.42f else 0.62f + val centerBaseline = control.y - (textPaint.ascent() + textPaint.descent()) / 2f + val labelBaseline = if (secondary == null) centerBaseline else centerBaseline - control.radius * 0.16f + canvas.drawText(label, control.x, labelBaseline, textPaint) + secondary?.let { + secondaryTextPaint.textSize = control.radius * 0.22f + canvas.drawText(it, control.x, control.y + control.radius * 0.48f, secondaryTextPaint) + } + } + + private fun inside(control: VirtualControl, x: Float, y: Float, hitScale: Float = 1.14f): Boolean = + hypot(x - control.x, y - control.y) <= control.radius * hitScale private fun geometry() = computeVirtualGamepadGeometry( width = width.toFloat(), height = height.toFloat(), maxRadius = resources.displayMetrics.density * 42f, ) + + private companion object { + const val RESET_HOLD_MS = 650L + } } diff --git a/app/src/main/java/org/neslite/app/input/VirtualInputTracker.kt b/app/src/main/java/org/neslite/app/input/VirtualInputTracker.kt index 4a7ffb2..fd13cba 100644 --- a/app/src/main/java/org/neslite/app/input/VirtualInputTracker.kt +++ b/app/src/main/java/org/neslite/app/input/VirtualInputTracker.kt @@ -22,4 +22,6 @@ class VirtualInputTracker { active.clear() return transitions } + + fun activeActions(): Set = active.values.toSet() } diff --git a/app/src/test/java/org/neslite/app/BuildIdentityTest.kt b/app/src/test/java/org/neslite/app/BuildIdentityTest.kt index 0965801..790975c 100644 --- a/app/src/test/java/org/neslite/app/BuildIdentityTest.kt +++ b/app/src/test/java/org/neslite/app/BuildIdentityTest.kt @@ -7,7 +7,7 @@ class BuildIdentityTest { @Test fun debugBuildUsesTheDocumentedIdentity() { assertEquals("org.neslite.app.debug", BuildConfig.APPLICATION_ID) - assertEquals("0.9.5-debug", BuildConfig.VERSION_NAME) - assertEquals(9005, BuildConfig.VERSION_CODE) + assertEquals("0.9.6-debug", BuildConfig.VERSION_NAME) + assertEquals(9006, BuildConfig.VERSION_CODE) } } diff --git a/app/src/test/java/org/neslite/app/VirtualGamepadLayoutTest.kt b/app/src/test/java/org/neslite/app/VirtualGamepadLayoutTest.kt index 8c05d25..b71e2a7 100644 --- a/app/src/test/java/org/neslite/app/VirtualGamepadLayoutTest.kt +++ b/app/src/test/java/org/neslite/app/VirtualGamepadLayoutTest.kt @@ -2,6 +2,7 @@ package org.neslite.app import org.junit.Assert.assertTrue import org.junit.Test +import kotlin.math.hypot class VirtualGamepadLayoutTest { @Test @@ -20,6 +21,13 @@ class VirtualGamepadLayoutTest { assertTrue(leftControls.all { it.x + it.radius <= geometry.gameLeft }) assertTrue(rightControls.all { it.x - it.radius >= geometry.gameRight }) + assertTrue(geometry.select.x > geometry.right.x) + assertTrue(geometry.start.x < geometry.faceX.x) + assertTrue(geometry.select.radius < geometry.up.radius) + assertTrue(geometry.start.radius < geometry.faceA.radius) + assertTrue(distance(geometry.select, geometry.right) > geometry.select.radius + geometry.right.radius) + assertTrue(distance(geometry.start, geometry.faceA) > geometry.start.radius + geometry.faceA.radius) + assertTrue(distance(geometry.faceY, geometry.faceB) > geometry.faceY.radius + geometry.faceB.radius) } @Test @@ -45,4 +53,7 @@ class VirtualGamepadLayoutTest { assertTrue(controls.all { it.x - it.radius >= 0f && it.x + it.radius <= width }) assertTrue(controls.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) } diff --git a/app/src/test/java/org/neslite/app/input/VirtualInputTrackerTest.kt b/app/src/test/java/org/neslite/app/input/VirtualInputTrackerTest.kt index 7b404d7..d6c6f70 100644 --- a/app/src/test/java/org/neslite/app/input/VirtualInputTrackerTest.kt +++ b/app/src/test/java/org/neslite/app/input/VirtualInputTrackerTest.kt @@ -5,6 +5,17 @@ import org.junit.Assert.assertTrue import org.junit.Test class VirtualInputTrackerTest { + @Test + fun exposesActiveActionsForPressedVisualFeedback() { + val tracker = VirtualInputTracker() + tracker.update(1, GameAction.UP) + tracker.update(2, GameAction.NES_A) + + assertEquals(setOf(GameAction.UP, GameAction.NES_A), tracker.activeActions()) + tracker.update(1, null) + assertEquals(setOf(GameAction.NES_A), tracker.activeActions()) + } + @Test fun slideReleasesOldActionBeforePressingNewOne() { val tracker = VirtualInputTracker() diff --git a/docs/architecture.md b/docs/architecture.md index d665c6c..337a613 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,4 +1,4 @@ -# NESLite 架构(0.9.5 候选基线) +# NESLite 架构(0.9.6 候选基线) ## 边界与线程 @@ -101,10 +101,10 @@ DataStore 保存,渲染线程只读取整数缩放和过滤快照,音频及 按 pointerId 维护触摸来源并送入与物理手柄相同的 `InputRouter`,因此多来源去重、相反方向和释放规则 一致;取消、滑出、失焦与暂停清空所有虚拟来源。首次使用状态也存于设置 DataStore,不接触用户存档。 -0.9.5 将游戏入口收敛为单一横屏主画面:中央 `AspectRatioFrameLayout` 只按 4:3 测量,左右剩余安全 +0.9.6 将游戏入口收敛为单一横屏主画面:中央 `AspectRatioFrameLayout` 只按 4:3 测量,左右剩余安全 区域绘制虚拟手柄;左侧固定入口打开游戏列表、导入和设置的全屏独立页面。X/Y 复用 M4 的 A/B 软件 -连发动作,重置为一次性触摸命令。页面打开前先等待运行会话进入暂停态,页面关闭后不自动恢复,避免 -文件选择或设置过程中游戏在后台继续。 +连发动作;重置要求持续按住 650 ms。游戏页沉浸式隐藏系统栏,管理页恢复系统栏;页面打开前先等待 +运行会话进入暂停态,页面关闭后不自动恢复,避免文件选择或设置过程中游戏在后台继续。 ## M7 系统交接与诊断 diff --git a/docs/build.md b/docs/build.md index a883dec..efd6106 100644 --- a/docs/build.md +++ b/docs/build.md @@ -1,6 +1,6 @@ # NESLite 可复现构建说明 -本说明对应 0.9.5 候选基线。命令使用系统默认 PowerShell 7.6.6(`pwsh`)从仓库根目录执行;不需要手工复制 `.so`、 +本说明对应 0.9.6 候选基线。命令使用系统默认 PowerShell 7.6.6(`pwsh`)从仓库根目录执行;不需要手工复制 `.so`、 FCEUmm 或 Oboe 文件。 ## 前置条件 @@ -50,7 +50,7 @@ pwsh -NoProfile -ExecutionPolicy Bypass -File .\tools\package-rc.ps1 脚本重跑 Stage 和 release lint,生成 `org.neslite.app` 的未签名 release APK、完整原生符号、应用及 三个固定第三方依赖的源码包、许可证/说明、静态原生审计、候选清单和 SHA-256。输出位于 -`.build/rc-0.9.5/`。静态审计逐个检查 APK 内 `.so` 的 ELF LOAD 段和 APK 16 KB 对齐,并拒绝非 +`.build/rc-0.9.6/`。静态审计逐个检查 APK 内 `.so` 的 ELF LOAD 段和 APK 16 KB 对齐,并拒绝非 arm64-v8a 库。 默认 APK 刻意保持未签名;发布私钥和口令不得传入仓库或写入脚本。获得受保护的发布密钥后,在隔离 diff --git a/docs/identity.md b/docs/identity.md index 080f164..2761899 100644 --- a/docs/identity.md +++ b/docs/identity.md @@ -6,7 +6,7 @@ | --- | --- | | 显示名称 | `NESLite` | | 正式 application ID/namespace | `org.neslite.app` | -| 当前候选版本 | `0.9.5`,`versionCode = 9005` | +| 当前候选版本 | `0.9.6`,`versionCode = 9006` | | 调试 application ID | `org.neslite.app.debug` | | 发布 ABI | `arm64-v8a` | | min/target SDK | 28/36 | diff --git a/docs/known-issues.md b/docs/known-issues.md index 89b1281..83f588b 100644 --- a/docs/known-issues.md +++ b/docs/known-issues.md @@ -1,6 +1,6 @@ # 已知问题与候选版限制 -本清单对应 0.9.5 候选源码。优先级含义:阻断表示 G8 不可通过;高表示发布前必须处理;中/低需有 +本清单对应 0.9.6 候选源码。优先级含义:阻断表示 G8 不可通过;高表示发布前必须处理;中/低需有 明确接受或修复结论。 | 编号 | 优先级 | 状态 | 问题与处理条件 | @@ -9,7 +9,7 @@ | RC-B02 | 阻断 | 开放 | 尚无满足动作/滚屏、声音和可观察电池进度要求的合法游戏样本;冻结的 12 个 CC0 最小样本只能用于加载冒烟。 | | RC-B03 | 阻断 | 开放 | H-LONG、H-INPUT、4 KB/16 KB 运行、API 28 冒烟及 H-UPGRADE 尚未在设备执行。 | | RC-B04 | 阻断 | 开放 | 尚未提供受保护的发布签名密钥及同 ID/同签名旧版数据集;当前 RC 静态审计包为未签名 APK。 | -| RC-M01 | 中 | 开放 | Debug lint 有 53 个非阻断警告,主要是动态界面文本国际化、依赖版本提示和 Android 16 固定横屏兼容提醒;无 lint 错误。 | +| RC-M01 | 中 | 开放 | Debug lint 有 46 个非阻断警告,主要是动态界面文本国际化、依赖版本提示和 Android 16 固定横屏兼容提醒;无 lint 错误。 | 不得通过删除失败 ROM、缩短规定轮数/时长、用 x86_64 替代 arm64,或用桌面原生测试替代 Android 实机运行来关闭阻断项。每项关闭时应在 M8 报告登记设备、包哈希、ROM 哈希、命令、原始日志和结论。 diff --git a/docs/reports/m8-release-candidate.md b/docs/reports/m8-release-candidate.md index ff4cea9..9f85bf5 100644 --- a/docs/reports/m8-release-candidate.md +++ b/docs/reports/m8-release-candidate.md @@ -1,7 +1,7 @@ # M8 0.9.4 发布候选记录 -> 此文件保留 0.9.4 的历史候选证据。0.9.5 改动了主界面、触屏输入路径和页面/会话交接,不能直接 -> 继承本记录作为最终 G8 结论;完成 Magic8 实机复核后需生成新的 0.9.5 候选记录。 +> 此文件保留 0.9.4 的历史候选证据。0.9.6 改动了主界面、触屏输入路径和页面/会话交接,不能直接 +> 继承本记录作为最终 G8 结论;完成 Magic8 实机复核后需生成新的 0.9.6 候选记录。 日期:2026-09-12 diff --git a/docs/usage.md b/docs/usage.md index 9ac343c..b2256f1 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -1,7 +1,8 @@ # NESLite 日常使用 -1. 应用会自动进入横屏。主画面中央是 4:3 游戏区,左侧是方向键与 Select,右侧是 A/B/X/Y、 - Start、暂停和重置;X/Y 分别是 A/B 连发。左上角的“列表”“导入”“设置”会打开独立页面。 +1. 应用会自动进入沉浸式横屏,中央 4:3 游戏区优先占满安全高度。左侧是十字方向键,右侧是 + A/B/X/Y;较小的 Select/Start 分别位于游戏画面底部两侧。X/Y 分别是 A/B 连发。左上角的 + “列表”“导入”“设置”会打开独立页面,并临时恢复系统栏。 2. 点左侧“导入”,选择你合法持有的 `.nes` 文件。应用按完整 SHA-256 建库;同名不同内容独立, 相同内容不会重复。可按名称/哈希搜索、收藏或从列表隐藏。隐藏不删除 ROM 和存档,重新导入相同 内容会恢复关联。 @@ -9,7 +10,8 @@ 屏幕按键。多点触控支持方向与动作同时按住,滑出、取消、失焦或暂停都会释放输入。 4. 点左侧“设置”可切换 4:3/整数缩放、最近邻/线性过滤、音量、静音、软件连发和 RB 快进,并进入 手柄按键设置、A/B 交换、死区调整及设备诊断。设置会在重启后恢复。 -5. 游戏中点右侧“暂停”或按手柄右摇杆进入暂停菜单。“存档管理”显示十个手动槽、三个自动恢复点、 +5. 游戏中点右侧“暂停”或按手柄右摇杆进入暂停菜单;“重置”需要持续按住约 650 ms,滑出会取消。 + “存档管理”显示十个手动槽、三个自动恢复点、 提交时间和缩略图。只有原生层确认安全落盘后才显示保存成功。 6. 存档包通过 Android 系统文件选择器导入导出,默认不含 ROM。导入前先加载对应游戏并暂停;哈希、 内核提交、配置、路径、大小或校验不匹配会被拒绝并保留现有有效存档。 diff --git a/tools/package-rc.ps1 b/tools/package-rc.ps1 index 4937f66..cdcdedf 100644 --- a/tools/package-rc.ps1 +++ b/tools/package-rc.ps1 @@ -14,8 +14,8 @@ $env:JAVA_HOME = if ($env:JAVA_HOME -and (Test-Path (Join-Path $env:JAVA_HOME 'b } $env:ANDROID_HOME = $sdkRoot $env:ANDROID_SDK_ROOT = $sdkRoot -$versionName = '0.9.5' -$versionCode = 9005 +$versionName = '0.9.6' +$versionCode = 9006 $gitStatus = @(& git -C $root status --porcelain) if ($LASTEXITCODE -ne 0) { throw 'Cannot inspect the Git worktree.' } if ($gitStatus.Count -ne 0) { throw 'RC packaging requires a clean committed worktree.' }