diff --git a/README.md b/README.md index 25a9e84..cfcff31 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ # NESLite NESLite 是一个面向 Android 的轻量 NES/FC 模拟器前端。它使用固定版本的 -FCEUmm libretro 内核。当前仓库已准备 0.9.4 RC 的冻结样本、输入延迟指标、升级架构、原生对齐审计 -和候选交付脚本;由于目标手机、发布签名和可观察玩法的合法 ROM 样本尚未提供,M2~M8 的实机 -验收及 G8 仍待补齐。 +FCEUmm libretro 内核。当前仓库已准备 0.9.5 候选基线的冻结样本、输入延迟指标、升级架构、原生对齐审计 +和候选交付脚本;荣耀 Magic8 与 X5 Lite 已由截图确认枚举,但手机未开放 ADB,且发布签名和可观察 +玩法的合法 ROM 样本尚未提供,因此 M2~M8 的完整实机验收及 G8 仍待补齐。 + +主界面固定为横屏游戏布局:中央是 4:3 游戏画面,左右为虚拟按键,左上角提供游戏列表、导入和 +设置入口;管理功能均在独立页面中完成,不再与游戏画面纵向堆叠。 ## 快速构建 diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 839016c..59fa980 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 = 9004 - versionName = "0.9.4" + versionCode = 9005 + versionName = "0.9.5" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" ndk { diff --git a/app/src/main/java/org/neslite/app/AspectRatioFrameLayout.kt b/app/src/main/java/org/neslite/app/AspectRatioFrameLayout.kt new file mode 100644 index 0000000..ca0d016 --- /dev/null +++ b/app/src/main/java/org/neslite/app/AspectRatioFrameLayout.kt @@ -0,0 +1,31 @@ +package org.neslite.app + +import android.content.Context +import android.view.View +import android.widget.FrameLayout +import kotlin.math.min + +internal data class ViewportSize(val width: Int, val height: Int) + +internal fun fitFourByThree(maxWidth: Int, maxHeight: Int): ViewportSize { + if (maxWidth <= 0 || maxHeight <= 0) return ViewportSize(0, 0) + val integerUnit = min(maxWidth / 4, maxHeight / 3) + return ViewportSize(width = integerUnit * 4, height = integerUnit * 3) +} + +/** Keeps the emulator viewport at the NES presentation ratio without stretching it. */ +class AspectRatioFrameLayout(context: Context) : FrameLayout(context) { + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val maxWidth = View.MeasureSpec.getSize(widthMeasureSpec) + val maxHeight = View.MeasureSpec.getSize(heightMeasureSpec) + if (maxWidth == 0 || maxHeight == 0) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec) + return + } + val (width, height) = fitFourByThree(maxWidth, maxHeight) + super.onMeasure( + View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), + View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY), + ) + } +} diff --git a/app/src/main/java/org/neslite/app/MainActivity.kt b/app/src/main/java/org/neslite/app/MainActivity.kt index 15bc0a1..9476541 100644 --- a/app/src/main/java/org/neslite/app/MainActivity.kt +++ b/app/src/main/java/org/neslite/app/MainActivity.kt @@ -9,6 +9,8 @@ import android.media.AudioManager import android.hardware.display.DisplayManager import android.hardware.input.InputManager import android.graphics.Bitmap +import android.graphics.Color +import android.graphics.drawable.GradientDrawable import android.net.Uri import android.os.Bundle import android.os.Handler @@ -16,12 +18,14 @@ import android.os.Looper import android.os.SystemClock import android.text.method.ScrollingMovementMethod import android.view.InputDevice +import android.view.Gravity import android.view.KeyEvent import android.view.MotionEvent import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.EditText +import android.widget.FrameLayout import android.widget.ImageView import android.widget.LinearLayout import android.widget.ScrollView @@ -116,11 +120,18 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { private lateinit var saveManagerPanel: LinearLayout private lateinit var virtualGamepad: VirtualGamepadView private lateinit var searchInput: EditText + private lateinit var rootShell: FrameLayout + private lateinit var mainScreen: FrameLayout + private lateinit var overlayScreen: FrameLayout + private lateinit var overlayTitle: TextView private lateinit var contentScroll: ScrollView private lateinit var contentRoot: LinearLayout + private lateinit var libraryPanel: LinearLayout + private lateinit var importPanel: LinearLayout + private lateinit var importStatus: TextView private lateinit var diagnosticsPanel: LinearLayout - private lateinit var diagnosticsToggle: Button - private lateinit var virtualGamepadToggle: Button + private lateinit var virtualControlsOverlay: FrameLayout + private lateinit var libraryRailButton: Button private lateinit var audioManager: AudioManager private lateinit var audioFocusRequest: AudioFocusRequest private var activeSessionId = 0L @@ -160,6 +171,18 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { private var abnormalRecordedSession = 0L private var restoredSessionDiscarded = false private var stopPreservesCrashGuard = false + private var currentOverlayPage: OverlayPage? = null + private var diagnosticsReturnToSettings = false + + private enum class OverlayPage { + LIBRARY, + IMPORT, + SETTINGS, + CONTROLLER_SETTINGS, + SAVE_MANAGER, + PAUSE_MENU, + DIAGNOSTICS, + } private val audioFocusListener = AudioManager.OnAudioFocusChangeListener { focusChange -> if (focusChange != AudioManager.AUDIOFOCUS_GAIN) { @@ -307,16 +330,12 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { private fun handleUnifiedBack() { when (SystemUiPolicy.backDecision(currentUiLayer())) { BackDecision.CLOSE_CONTROLLER_SETTINGS -> closeControllerSettings() - BackDecision.CLOSE_SETTINGS -> { - settingsPanel.visibility = View.GONE - if (activeSessionId != 0L && lastSessionState == "Paused") showPauseMenu() - else searchInput.requestFocus() - } + BackDecision.CLOSE_SETTINGS -> closeSettingsPage() BackDecision.CLOSE_SAVE_MANAGER -> { - saveManagerPanel.visibility = View.GONE showPauseMenu() } BackDecision.CLOSE_DIAGNOSTICS -> setDiagnosticsExpanded(false) + BackDecision.CLOSE_AUXILIARY_PAGE -> closeOverlayPage() BackDecision.RESUME_GAME -> sendControlCommand("run") BackDecision.PAUSE_GAME -> sendControlCommand("pause") BackDecision.SHOW_PAUSE_MENU -> showPauseMenu() @@ -327,18 +346,19 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { private fun currentUiLayer(): UiLayer = when { pendingSessionCommand != null || lastSessionState in setOf("Loading", "Pausing", "Stopping") -> UiLayer.TRANSITION - ::controllerSettingsPanel.isInitialized && controllerSettingsPanel.visibility == View.VISIBLE -> UiLayer.CONTROLLER_SETTINGS - ::settingsPanel.isInitialized && settingsPanel.visibility == View.VISIBLE -> UiLayer.SETTINGS - ::saveManagerPanel.isInitialized && saveManagerPanel.visibility == View.VISIBLE -> UiLayer.SAVE_MANAGER - ::diagnosticsPanel.isInitialized && diagnosticsPanel.visibility == View.VISIBLE -> UiLayer.DIAGNOSTICS - ::pauseMenu.isInitialized && pauseMenu.visibility == View.VISIBLE -> UiLayer.PAUSE_MENU + currentOverlayPage == OverlayPage.CONTROLLER_SETTINGS -> UiLayer.CONTROLLER_SETTINGS + currentOverlayPage == OverlayPage.SETTINGS -> UiLayer.SETTINGS + currentOverlayPage == OverlayPage.SAVE_MANAGER -> UiLayer.SAVE_MANAGER + currentOverlayPage == OverlayPage.DIAGNOSTICS -> UiLayer.DIAGNOSTICS + currentOverlayPage == OverlayPage.PAUSE_MENU -> UiLayer.PAUSE_MENU + currentOverlayPage in setOf(OverlayPage.LIBRARY, OverlayPage.IMPORT) -> UiLayer.AUXILIARY_PAGE lastSessionState == "Running" -> UiLayer.GAME_RUNNING activeSessionId != 0L && lastSessionState in setOf("Paused", "Error") -> UiLayer.GAME_PAUSED else -> UiLayer.LIBRARY } private fun installWindowInsets() { - ViewCompat.setOnApplyWindowInsetsListener(contentScroll) { view, windowInsets -> + ViewCompat.setOnApplyWindowInsetsListener(rootShell) { view, windowInsets -> fun safe(type: Int): SafeInsets { val value = windowInsets.getInsets(type) return SafeInsets(value.left, value.top, value.right, value.bottom) @@ -352,42 +372,91 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { recordSystemEvent("safe_insets_${merged.left}_${merged.top}_${merged.right}_${merged.bottom}") windowInsets } - ViewCompat.requestApplyInsets(contentScroll) + ViewCompat.requestApplyInsets(rootShell) } private fun setDiagnosticsExpanded(expanded: Boolean) { if (!::diagnosticsPanel.isInitialized) return - diagnosticsPanel.visibility = if (expanded) View.VISIBLE else View.GONE - diagnosticsToggle.text = if (expanded) { - "收起设备诊断与手柄测试" - } else { - "展开设备诊断与手柄测试" - } if (expanded) { + diagnosticsReturnToSettings = currentOverlayPage == OverlayPage.SETTINGS + showOverlayPage(OverlayPage.DIAGNOSTICS, "设备诊断与手柄测试") refreshControllers() } else { - diagnosticsToggle.requestFocus() + if (diagnosticsReturnToSettings) showSettingsPage() else closeOverlayPage() + diagnosticsReturnToSettings = false } } private fun updateVirtualGamepadVisibility() { - if (!::virtualGamepad.isInitialized || !::virtualGamepadToggle.isInitialized) return + if (!::virtualGamepad.isInitialized || !::virtualControlsOverlay.isInitialized) return val hasExternalController = controllers.isNotEmpty() val shouldShow = shouldShowVirtualGamepad( hasExternalController = hasExternalController, showWithController = appSettings.showVirtualGamepadWithController, ) - if (!shouldShow && virtualGamepad.visibility == View.VISIBLE) virtualGamepad.clearInputs() - virtualGamepad.visibility = if (shouldShow) View.VISIBLE else View.GONE - virtualGamepadToggle.visibility = if (hasExternalController) View.VISIBLE else View.GONE - if (hasExternalController) { - val controllerName = controllers.first().name - virtualGamepadToggle.text = if (shouldShow) { - "$controllerName 已连接 · 隐藏屏幕按键" - } else { - "$controllerName 已连接 · 显示屏幕按键" - } + if (!shouldShow && virtualControlsOverlay.visibility == View.VISIBLE) virtualGamepad.clearInputs() + virtualControlsOverlay.visibility = if (shouldShow) View.VISIBLE else View.GONE + } + + private fun showOverlayPage(page: OverlayPage, title: String) { + listOf( + libraryPanel, + importPanel, + settingsPanel, + controllerSettingsPanel, + saveManagerPanel, + pauseMenu, + diagnosticsPanel, + ).forEach { it.visibility = View.GONE } + val target = when (page) { + OverlayPage.LIBRARY -> libraryPanel + OverlayPage.IMPORT -> importPanel + OverlayPage.SETTINGS -> settingsPanel + OverlayPage.CONTROLLER_SETTINGS -> controllerSettingsPanel + OverlayPage.SAVE_MANAGER -> saveManagerPanel + OverlayPage.PAUSE_MENU -> pauseMenu + OverlayPage.DIAGNOSTICS -> diagnosticsPanel } + target.visibility = View.VISIBLE + overlayTitle.text = title + currentOverlayPage = page + overlayScreen.visibility = View.VISIBLE + contentScroll.post { contentScroll.scrollTo(0, 0) } + focusFirstButton(target) + } + + private fun closeOverlayPage() { + clearControllerState() + currentOverlayPage = null + overlayScreen.visibility = View.GONE + if (::libraryRailButton.isInitialized && lastSessionState != "Running") { + libraryRailButton.requestFocus() + } + } + + private fun openLibraryPage() { + if (lastSessionState == "Running" && !pauseForExternalReason("open_library")) return + showOverlayPage(OverlayPage.LIBRARY, "游戏列表") + refreshLibrary() + } + + private fun openImportPage() { + if (lastSessionState == "Running" && !pauseForExternalReason("open_import")) return + showOverlayPage(OverlayPage.IMPORT, "导入游戏") + } + + private fun showSettingsPage() { + showOverlayPage(OverlayPage.SETTINGS, "设置") + renderSettings() + } + + private fun closeSettingsPage() { + if (settingsReturnToPause && activeSessionId != 0L && lastSessionState == "Paused") { + showPauseMenu() + } else { + closeOverlayPage() + } + settingsReturnToPause = false } private fun prepareForegroundResources() { @@ -559,176 +628,242 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { return false } - private fun createContentView(): ScrollView = DiagnosticScrollView( + private fun createContentView(): View = InputFrameLayout( context = this, keyHandler = ::handleKeyEvent, motionHandler = ::handleMotionEvent, ).apply { - contentScroll = this - isFillViewport = true - clipToPadding = true - contentRoot = LinearLayout(context).apply { - orientation = LinearLayout.VERTICAL - setPadding(dp(20), dp(16), dp(20), dp(24)) - addView(title(getString(R.string.home_title, BuildConfig.VERSION_NAME))) - addView(body(getString(R.string.home_instructions))) - addView(section("游戏库")) - addView(Button(context).apply { - text = "导入 NES ROM" - setOnClickListener { openRom.launch(arrayOf("application/octet-stream", "*/*")) } - }) - searchInput = EditText(context).apply { - hint = "搜索游戏名称或哈希" - isSingleLine = true - doAfterTextChanged { refreshLibrary() } - } - addView(searchInput) - gameList = LinearLayout(context).apply { orientation = LinearLayout.VERTICAL } - addView(gameList) + rootShell = this + setBackgroundColor(Color.rgb(16, 19, 24)) - addView(section("游戏画面")) - emulatorSurface = EmulatorSurface(context, ::recordSystemEvent) - addView( - emulatorSurface, - LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp(240)), - ) - virtualGamepadToggle = Button(context).apply { - visibility = View.GONE - setOnClickListener { - appSettings = appSettings.copy( - showVirtualGamepadWithController = !appSettings.showVirtualGamepadWithController, - ) - updateVirtualGamepadVisibility() - persistAppSettings() - } - } - addView(virtualGamepadToggle) - virtualGamepad = VirtualGamepadView(context, ::handleVirtualInput) - addView( - virtualGamepad, - LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp(120)), - ) - emulatorStatus = body("状态:Idle") - addView(emulatorStatus) - - addView(section("游戏控制")) - addView(LinearLayout(context).apply { - orientation = LinearLayout.HORIZONTAL - listOf( - "运行" to "run", - "暂停" to "pause", - "重置" to "reset", - "退出" to "stop", - ).forEach { (label, command) -> - addView(Button(context).apply { - text = label - setOnClickListener { sendControlCommand(command) } - }) - } - }) - addView(LinearLayout(context).apply { - orientation = LinearLayout.HORIZONTAL - addView(Button(context).apply { - text = "静音:关" - setOnClickListener { - appSettings = appSettings.copy(muted = !appSettings.muted) - muted = appSettings.muted - text = if (appSettings.muted) "静音:开" else "静音:关" - sendControlCommand(if (appSettings.muted) "mute:on" else "mute:off") - persistAppSettings() - } - }) - addView(Button(context).apply { - text = "按住 2× 快进" - setOnClickListener { } - setOnTouchListener { view, event -> - when (event.actionMasked) { - MotionEvent.ACTION_DOWN -> if (appSettings.fastForwardEnabled) sendControlCommand("fast:on") - MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { - sendControlCommand("fast:off") - if (event.actionMasked == MotionEvent.ACTION_UP) view.performClick() - } - } - true - } - }) - }) - addView(Button(context).apply { - text = "显示、音频与输入设置" - setOnClickListener { showSettings() } - }) - - pauseMenu = createPauseMenu() - addView(pauseMenu) - saveManagerPanel = LinearLayout(context).apply { - orientation = LinearLayout.VERTICAL - visibility = View.GONE - } - addView(saveManagerPanel) - settingsPanel = LinearLayout(context).apply { - orientation = LinearLayout.VERTICAL - visibility = View.GONE - } - addView(settingsPanel) - controllerSettingsPanel = LinearLayout(context).apply { - orientation = LinearLayout.VERTICAL - visibility = View.GONE - } - addView(controllerSettingsPanel) - - diagnosticsToggle = Button(context).apply { - text = "展开设备诊断与手柄测试" - setOnClickListener { setDiagnosticsExpanded(diagnosticsPanel.visibility != View.VISIBLE) } - } - addView( - diagnosticsToggle, - LinearLayout.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT, - ).apply { topMargin = dp(24) }, - ) - diagnosticsPanel = LinearLayout(context).apply { - orientation = LinearLayout.VERTICAL - visibility = View.GONE - addView(section("设备诊断与手柄测试")) - addView(section(getString(R.string.section_native))) - addView(body(nativeProbe.render())) - addView(section(getString(R.string.section_device))) - deviceText = body(this@MainActivity.baseline.render()) - addView(deviceText) - addView(section(getString(R.string.section_controllers))) - controllerText = body("") - addView(controllerText) - addView(Button(context).apply { - text = "手柄映射与设置" - setOnClickListener { showControllerSettings(returnToPause = false) } - }) - addView(section(getString(R.string.section_input_state))) - stateText = body("") - addView(stateText) - addView(section(getString(R.string.section_events))) - eventText = body("").apply { - movementMethod = ScrollingMovementMethod() - setTextIsSelectable(true) - } - addView(eventText) - addView(section("运行指标")) - audioStatus = body("音频:等待 ROM;重新获得焦点后需手动继续") - addView(audioStatus) + mainScreen = FrameLayout(context).apply { + setBackgroundColor(Color.rgb(16, 19, 24)) + val viewport = AspectRatioFrameLayout(context).apply { + setBackgroundColor(Color.BLACK) + emulatorSurface = EmulatorSurface(context, ::recordSystemEvent) addView( - Button(context).apply { - text = getString(R.string.export_diagnostics) - setOnClickListener { createDocument.launch(defaultExportName()) } - }, - LinearLayout.LayoutParams( + emulatorSurface, + FrameLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, - ViewGroup.LayoutParams.WRAP_CONTENT, - ).apply { topMargin = dp(16) }, + ViewGroup.LayoutParams.MATCH_PARENT, + ), ) } - addView(diagnosticsPanel) + addView( + viewport, + FrameLayout.LayoutParams( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT, + Gravity.CENTER, + ), + ) + + virtualControlsOverlay = FrameLayout(context).apply { + virtualGamepad = VirtualGamepadView( + context = context, + onTransition = ::handleVirtualInput, + onReset = { + if (activeSessionId == 0L) emulatorStatus.text = "请先从游戏列表选择游戏" + else sendControlCommand("reset") + }, + ) + addView( + virtualGamepad, + FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT, + ), + ) + } + addView( + virtualControlsOverlay, + FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT, + ), + ) + + 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) + addView(libraryRailButton) + addView(railButton("导入", ::openImportPage)) + addView(railButton("设置", ::showSettings)) + }, + FrameLayout.LayoutParams(dp(78), 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(contentRoot) + addView( + mainScreen, + FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT, + ), + ) + + overlayScreen = FrameLayout(context).apply { + visibility = View.GONE + setBackgroundColor(Color.WHITE) + addView( + LinearLayout(context).apply { + orientation = LinearLayout.VERTICAL + addView( + LinearLayout(context).apply { + orientation = LinearLayout.HORIZONTAL + gravity = Gravity.CENTER_VERTICAL + setPadding(dp(12), dp(8), dp(12), dp(8)) + addView(Button(context).apply { + text = "返回" + setOnClickListener { handleUnifiedBack() } + }) + overlayTitle = TextView(context).apply { + textSize = 22f + setTextColor(Color.rgb(35, 38, 44)) + setPadding(dp(14), 0, 0, 0) + } + addView( + overlayTitle, + LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f), + ) + }, + LinearLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT, + ), + ) + contentScroll = ScrollView(context).apply { + isFillViewport = true + clipToPadding = true + contentRoot = LinearLayout(context).apply { + orientation = LinearLayout.VERTICAL + setPadding(dp(20), dp(8), dp(20), dp(24)) + + libraryPanel = LinearLayout(context).apply { + orientation = LinearLayout.VERTICAL + visibility = View.GONE + addView(body("选择游戏后将返回主画面并自动开始运行。")) + searchInput = EditText(context).apply { + hint = "搜索游戏名称或哈希" + isSingleLine = true + doAfterTextChanged { refreshLibrary() } + } + addView(searchInput) + gameList = LinearLayout(context).apply { orientation = LinearLayout.VERTICAL } + addView(gameList) + } + addView(libraryPanel) + + importPanel = LinearLayout(context).apply { + orientation = LinearLayout.VERTICAL + visibility = View.GONE + addView(body("选择你合法持有的 .nes 文件。应用会复制文件并按 SHA-256 建立游戏条目。")) + addView(Button(context).apply { + text = "选择 NES ROM 文件" + setOnClickListener { openRom.launch(arrayOf("application/octet-stream", "*/*")) } + }) + importStatus = body("尚未选择文件") + addView(importStatus) + } + addView(importPanel) + + settingsPanel = LinearLayout(context).apply { + orientation = LinearLayout.VERTICAL + visibility = View.GONE + } + addView(settingsPanel) + controllerSettingsPanel = LinearLayout(context).apply { + orientation = LinearLayout.VERTICAL + visibility = View.GONE + } + addView(controllerSettingsPanel) + saveManagerPanel = LinearLayout(context).apply { + orientation = LinearLayout.VERTICAL + visibility = View.GONE + } + addView(saveManagerPanel) + pauseMenu = createPauseMenu() + addView(pauseMenu) + + diagnosticsPanel = LinearLayout(context).apply { + orientation = LinearLayout.VERTICAL + visibility = View.GONE + addView(section(getString(R.string.section_native))) + addView(body(nativeProbe.render())) + addView(section(getString(R.string.section_device))) + deviceText = body(this@MainActivity.baseline.render()) + addView(deviceText) + addView(section(getString(R.string.section_controllers))) + controllerText = body("") + addView(controllerText) + addView(Button(context).apply { + text = "手柄映射与设置" + setOnClickListener { showControllerSettings(returnToPause = false, returnToSettings = true) } + }) + addView(section(getString(R.string.section_input_state))) + stateText = body("") + addView(stateText) + addView(section(getString(R.string.section_events))) + eventText = body("").apply { + movementMethod = ScrollingMovementMethod() + setTextIsSelectable(true) + } + addView(eventText) + addView(section("运行指标")) + audioStatus = body("音频:等待 ROM;重新获得焦点后需手动继续") + addView(audioStatus) + addView(Button(context).apply { + text = getString(R.string.export_diagnostics) + setOnClickListener { createDocument.launch(defaultExportName()) } + }) + } + addView(diagnosticsPanel) + } + addView(contentRoot) + } + addView( + contentScroll, + LinearLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + 0, + 1f, + ), + ) + }, + FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT, + ), + ) + } + addView( + overlayScreen, + FrameLayout.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT, + ), + ) } private fun createPauseMenu() = LinearLayout(this).apply { @@ -796,26 +931,21 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { } private fun showPauseMenu() { - setDiagnosticsExpanded(false) - closeControllerSettings(showPreviousMenu = false) - if (::settingsPanel.isInitialized) settingsPanel.visibility = View.GONE - if (::saveManagerPanel.isInitialized) saveManagerPanel.visibility = View.GONE - pauseMenu.visibility = View.VISIBLE - (pauseMenu.getChildAt(1) as? Button)?.requestFocus() + settingsReturnToPause = false + controllerReturnToSettings = false + showOverlayPage(OverlayPage.PAUSE_MENU, "暂停") } private fun hidePauseMenu() { if (::pauseMenu.isInitialized) pauseMenu.visibility = View.GONE + if (::overlayScreen.isInitialized && currentOverlayPage == OverlayPage.PAUSE_MENU) closeOverlayPage() } private fun showSettings() { - if (lastSessionState == "Running" && !sendControlCommand("pause")) return - setDiagnosticsExpanded(false) - hidePauseMenu() - if (::saveManagerPanel.isInitialized) saveManagerPanel.visibility = View.GONE - closeControllerSettings(showPreviousMenu = false) - settingsPanel.visibility = View.VISIBLE - renderSettings() + val openedFromPause = currentOverlayPage == OverlayPage.PAUSE_MENU + if (lastSessionState == "Running" && !pauseForExternalReason("open_settings")) return + settingsReturnToPause = openedFromPause + showSettingsPage() } private fun renderSettings() { @@ -902,16 +1032,16 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { settingsPanel.addView(Button(this).apply { text = "手柄映射、A/B 交换与死区" setOnClickListener { - settingsPanel.visibility = View.GONE showControllerSettings(returnToPause = false, returnToSettings = true) } }) + settingsPanel.addView(Button(this).apply { + text = "设备诊断与手柄测试" + setOnClickListener { setDiagnosticsExpanded(true) } + }) settingsPanel.addView(Button(this).apply { text = "关闭设置" - setOnClickListener { - settingsPanel.visibility = View.GONE - if (lastSessionState == "Paused") showPauseMenu() - } + setOnClickListener { closeSettingsPage() } }) focusFirstButton(settingsPanel) } @@ -951,7 +1081,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { .setMessage( "1. 点“导入 NES ROM”选择你合法持有的游戏文件。\n\n" + "2. 可连接手柄并在设置中测试/调整映射;没有手柄也能使用屏幕虚拟键。\n\n" + - "3. 选择游戏后点运行。屏幕“暂停”或右摇杆按下可打开菜单。\n\n" + + "3. 从游戏列表选择游戏后会自动开始。屏幕“暂停”或右摇杆按下可打开菜单。\n\n" + "4. 暂停菜单可保存、读档、导入导出(包内不含 ROM)和退出。", ) .setPositiveButton("开始使用") { _, _ -> @@ -973,12 +1103,9 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { emulatorStatus.text = "请先加载游戏并暂停,再打开存档管理" return } - hidePauseMenu() - settingsPanel.visibility = View.GONE - closeControllerSettings(showPreviousMenu = false) - saveManagerPanel.visibility = View.VISIBLE saveManagerPanel.removeAllViews() saveManagerPanel.addView(body("正在读取已校验槽位…")) + showOverlayPage(OverlayPage.SAVE_MANAGER, "存档管理") refreshSaveManager() } @@ -1015,10 +1142,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { }) saveManagerPanel.addView(Button(this).apply { text = "返回暂停菜单" - setOnClickListener { - saveManagerPanel.visibility = View.GONE - showPauseMenu() - } + setOnClickListener { showPauseMenu() } }) focusFirstButton(saveManagerPanel) } @@ -1056,31 +1180,30 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { } private fun showControllerSettings(returnToPause: Boolean, returnToSettings: Boolean = false) { - val shouldReturnToPause = returnToPause || lastSessionState == "Running" - if (lastSessionState == "Running" && !sendControlCommand("pause")) return + val shouldReturnToPause = returnToPause || settingsReturnToPause || + currentOverlayPage == OverlayPage.PAUSE_MENU + if (lastSessionState == "Running" && !pauseForExternalReason("open_controller_settings")) return settingsReturnToPause = shouldReturnToPause - controllerReturnToSettings = returnToSettings - setDiagnosticsExpanded(false) - hidePauseMenu() - if (::settingsPanel.isInitialized) settingsPanel.visibility = View.GONE - if (::saveManagerPanel.isInitialized) saveManagerPanel.visibility = View.GONE - controllerSettingsPanel.visibility = View.VISIBLE + controllerReturnToSettings = returnToSettings || currentOverlayPage == OverlayPage.SETTINGS || + currentOverlayPage == OverlayPage.DIAGNOSTICS draftProfile = selectedControllerId?.let(controllerProfiles::get) pendingBindingAction = null controllerSettingsMessage = "选择动作后按一个手柄键或推动一个轴。" + showOverlayPage(OverlayPage.CONTROLLER_SETTINGS, "手柄按键设置") renderControllerSettings() } private fun closeControllerSettings(showPreviousMenu: Boolean = true) { + val returnToSettings = showPreviousMenu && controllerReturnToSettings && ::settingsPanel.isInitialized + val settingsShouldReturnToPause = settingsReturnToPause pendingBindingAction = null draftProfile = null - if (::controllerSettingsPanel.isInitialized) controllerSettingsPanel.visibility = View.GONE clearControllerState() - if (showPreviousMenu && controllerReturnToSettings && ::settingsPanel.isInitialized) { - settingsPanel.visibility = View.VISIBLE - renderSettings() - } else if (showPreviousMenu && settingsReturnToPause && ::pauseMenu.isInitialized) showPauseMenu() - settingsReturnToPause = false + if (returnToSettings) showSettingsPage() + else if (showPreviousMenu && settingsReturnToPause && ::pauseMenu.isInitialized) showPauseMenu() + else if (showPreviousMenu) closeOverlayPage() + else controllerSettingsPanel.visibility = View.GONE + settingsReturnToPause = returnToSettings && settingsShouldReturnToPause controllerReturnToSettings = false } @@ -1421,6 +1544,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { private fun importRom(uri: Uri) { emulatorStatus.text = "正在流式导入…" + if (::importStatus.isInitialized) importStatus.text = "正在读取并校验文件…" libraryExecutor.execute { runCatching { RomImporter(contentResolver, filesDir, database.games()).import(uri) } .onSuccess { result -> @@ -1431,12 +1555,16 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { Toast.LENGTH_LONG, ).show() emulatorStatus.text = "导入完成:${result.game.displayName}" + importStatus.text = "导入完成:${result.game.displayName}" refreshLibrary() + openLibraryPage() } } .onFailure { error -> runOnUiThread { - emulatorStatus.text = "导入失败:${error.message ?: error.javaClass.simpleName}" + val message = "导入失败:${error.message ?: error.javaClass.simpleName}" + emulatorStatus.text = message + importStatus.text = message } } } @@ -1552,6 +1680,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { pendingSessionCommand = "load" lastSessionState = "Loading" recordSystemEvent("rom_load_requested", JSONObject().put("romHash", game.sha256)) + closeOverlayPage() } else { activeGameHash = "" } @@ -1729,6 +1858,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { if (!settled) return pendingSessionCommand = null when (pending) { + "load" -> if (nextState == "Paused") sendControlCommand("run") "pause" -> if (pendingPauseShowsMenu && nextState == "Paused") showPauseMenu() "pause_external" -> Unit "stop" -> if (nextState == "Idle") { @@ -1837,6 +1967,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { if (::settingsPanel.isInitialized) settingsPanel.visibility = View.GONE if (::saveManagerPanel.isInitialized) saveManagerPanel.visibility = View.GONE closeControllerSettings(showPreviousMenu = false) + if (::overlayScreen.isInitialized && overlayScreen.visibility == View.VISIBLE) closeOverlayPage() } } if (command == "pause" || command == "stop") { @@ -1862,7 +1993,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { } private fun gameplayInputActive(): Boolean = lastSessionState == "Running" && - pauseMenu.visibility != View.VISIBLE && controllerSettingsPanel.visibility != View.VISIBLE + overlayScreen.visibility != View.VISIBLE private fun clearControllerState() { if (::virtualGamepad.isInitialized) virtualGamepad.clearInputs() @@ -2064,6 +2195,19 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { setTextIsSelectable(true) } + private fun railButton(label: String, action: () -> Unit) = Button(this).apply { + text = label + isAllCaps = false + minHeight = dp(44) + setPadding(0, 0, 0, 0) + setOnClickListener { action() } + } + + private fun roundedBackground(color: Int, radius: Float) = GradientDrawable().apply { + setColor(color) + cornerRadius = radius + } + private fun dp(value: Int): Int = (value * resources.displayMetrics.density).toInt() private fun defaultExportName(): String { @@ -2085,11 +2229,11 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener { } } - private class DiagnosticScrollView( + private class InputFrameLayout( context: Context, private val keyHandler: (KeyEvent) -> Boolean, private val motionHandler: (MotionEvent) -> Boolean, - ) : ScrollView(context) { + ) : FrameLayout(context) { override fun dispatchKeyEvent(event: KeyEvent): Boolean = keyHandler(event) || super.dispatchKeyEvent(event) diff --git a/app/src/main/java/org/neslite/app/VirtualGamepadLayout.kt b/app/src/main/java/org/neslite/app/VirtualGamepadLayout.kt new file mode 100644 index 0000000..dabb717 --- /dev/null +++ b/app/src/main/java/org/neslite/app/VirtualGamepadLayout.kt @@ -0,0 +1,57 @@ +package org.neslite.app + +import kotlin.math.min + +internal data class VirtualControl(val x: Float, val y: Float, val radius: Float) + +internal data class VirtualGamepadGeometry( + val gameLeft: Float, + val gameRight: Float, + val up: VirtualControl, + val down: VirtualControl, + val left: VirtualControl, + val right: VirtualControl, + val select: VirtualControl, + val faceY: VirtualControl, + val faceB: VirtualControl, + val faceA: VirtualControl, + val faceX: VirtualControl, + val start: VirtualControl, + val pause: VirtualControl, + val reset: VirtualControl, +) + +internal fun computeVirtualGamepadGeometry( + width: Float, + height: Float, + maxRadius: Float, +): 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 radius = min(min(h * 0.095f, sideWidth * 0.16f), maxRadius) + val smallRadius = radius * 0.76f + val dpadX = sideWidth * 0.50f + val dpadY = h * 0.54f + val dpadGap = radius * 1.42f + val faceX = w - sideWidth * 0.50f + val faceY = h * 0.54f + val faceGap = radius * 1.34f + return VirtualGamepadGeometry( + gameLeft = sideWidth, + gameRight = w - sideWidth, + 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(dpadX, h * 0.87f, smallRadius), + 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), + ) +} diff --git a/app/src/main/java/org/neslite/app/VirtualGamepadView.kt b/app/src/main/java/org/neslite/app/VirtualGamepadView.kt index 882220d..6b8fa51 100644 --- a/app/src/main/java/org/neslite/app/VirtualGamepadView.kt +++ b/app/src/main/java/org/neslite/app/VirtualGamepadView.kt @@ -8,43 +8,47 @@ import android.view.MotionEvent import android.view.View import org.neslite.app.input.GameAction import org.neslite.app.input.VirtualInputTracker +import kotlin.math.hypot class VirtualGamepadView( context: Context, private val onTransition: (pointerId: Int, action: GameAction, pressed: Boolean) -> Unit, + private val onReset: () -> Unit, ) : View(context) { private val tracker = VirtualInputTracker() - private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.argb(110, 45, 55, 70) } + private val resetPointers = mutableSetOf() + private val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.argb(175, 45, 52, 64) } + private val stroke = Paint(Paint.ANTI_ALIAS_FLAG).apply { + color = Color.argb(210, 184, 190, 202) + style = Paint.Style.STROKE + strokeWidth = resources.displayMetrics.density * 1.5f + } private val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = Color.WHITE textAlign = Paint.Align.CENTER - textSize = 34f + typeface = android.graphics.Typeface.DEFAULT_BOLD } init { isFocusable = true - contentDescription = "屏幕虚拟手柄:方向、A、B、Start、Select 和暂停" + contentDescription = "屏幕虚拟手柄:方向、A、B、X、Y、Start、Select、暂停和重置" } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) - val w = width.toFloat() - val h = height.toFloat() - fun circle(x: Float, y: Float, radius: Float, label: String) { - canvas.drawCircle(x, y, radius, fill) - canvas.drawText(label, x, y + textPaint.textSize * 0.35f, textPaint) - } - circle(w * .21f, h * .30f, h * .16f, "↑") - circle(w * .21f, h * .82f, h * .16f, "↓") - circle(w * .08f, h * .56f, h * .16f, "←") - circle(w * .34f, h * .56f, h * .16f, "→") - circle(w * .75f, h * .68f, h * .18f, "B") - circle(w * .91f, h * .42f, h * .18f, "A") - textPaint.textSize = 24f - circle(w * .45f, h * .82f, h * .11f, "Select") - circle(w * .58f, h * .82f, h * .11f, "Start") - circle(w * .52f, h * .23f, h * .11f, "暂停") - textPaint.textSize = 34f + 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) } override fun onTouchEvent(event: MotionEvent): Boolean { @@ -52,7 +56,7 @@ class VirtualGamepadView( MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> updatePointer(event, event.actionIndex) MotionEvent.ACTION_MOVE -> repeat(event.pointerCount) { updatePointer(event, it) } MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> releasePointer(event.getPointerId(event.actionIndex)) - MotionEvent.ACTION_CANCEL -> tracker.clear().forEach { onTransition(it.pointerId, it.action, it.pressed) } + MotionEvent.ACTION_CANCEL -> clearInputs() } if (event.actionMasked == MotionEvent.ACTION_UP) performClick() return true @@ -65,36 +69,58 @@ class VirtualGamepadView( fun clearInputs() { tracker.clear().forEach { onTransition(it.pointerId, it.action, it.pressed) } + resetPointers.clear() } private fun updatePointer(event: MotionEvent, index: Int) { val pointerId = event.getPointerId(index) - tracker.update(pointerId, hitTest(event.getX(index), event.getY(index))) - .forEach { onTransition(it.pointerId, it.action, it.pressed) } + 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() + return + } + if (pointerId in resetPointers) return + tracker.update(pointerId, hitTest(x, y)).forEach { onTransition(it.pointerId, it.action, it.pressed) } } private fun releasePointer(pointerId: Int) { + resetPointers.remove(pointerId) tracker.update(pointerId, null).forEach { onTransition(it.pointerId, it.action, it.pressed) } } internal fun hitTest(x: Float, y: Float): GameAction? { - if (width <= 0 || height <= 0) return null - val nx = x / width - val ny = y / height - if (nx >= .42f && nx <= .59f && ny < .38f) return GameAction.PAUSE_MENU - if (nx < .42f) { - val dx = nx - .21f - val dy = ny - .56f - if (kotlin.math.abs(dx) < .25f && kotlin.math.abs(dy) < .42f) { - return if (kotlin.math.abs(dx) > kotlin.math.abs(dy)) { - if (dx < 0f) GameAction.LEFT else GameAction.RIGHT - } else if (dy < 0f) GameAction.UP else GameAction.DOWN - } + val geometry = geometry() + 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) -> 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.start, x, y) -> GameAction.START + inside(geometry.pause, x, y) -> GameAction.PAUSE_MENU + else -> null } - if (ny > .68f && nx >= .37f && nx <= .51f) return GameAction.SELECT - if (ny > .68f && nx >= .51f && nx <= .65f) return GameAction.START - if (nx > .82f && ny < .64f) return GameAction.NES_A - if (nx >= .62f && nx <= .85f && ny > .48f) return GameAction.NES_B - return 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 inside(control: VirtualControl, x: Float, y: Float): Boolean = + hypot(x - control.x, y - control.y) <= control.radius * 1.14f + + private fun geometry() = computeVirtualGamepadGeometry( + width = width.toFloat(), + height = height.toFloat(), + maxRadius = resources.displayMetrics.density * 42f, + ) } diff --git a/app/src/main/java/org/neslite/app/settings/AppSettingsStore.kt b/app/src/main/java/org/neslite/app/settings/AppSettingsStore.kt index 8c940df..2697d4d 100644 --- a/app/src/main/java/org/neslite/app/settings/AppSettingsStore.kt +++ b/app/src/main/java/org/neslite/app/settings/AppSettingsStore.kt @@ -18,7 +18,7 @@ data class AppSettings( val muted: Boolean = false, val turboEnabled: Boolean = true, val fastForwardEnabled: Boolean = true, - val showVirtualGamepadWithController: Boolean = false, + val showVirtualGamepadWithController: Boolean = true, val guideCompleted: Boolean = false, ) @@ -37,7 +37,7 @@ class AppSettingsStore(private val context: Context) { muted = values[MUTED] ?: false, turboEnabled = values[TURBO] ?: true, fastForwardEnabled = values[FAST_FORWARD] ?: true, - showVirtualGamepadWithController = values[SHOW_VIRTUAL_GAMEPAD_WITH_CONTROLLER] ?: false, + showVirtualGamepadWithController = values[SHOW_VIRTUAL_GAMEPAD_WITH_CONTROLLER] ?: true, guideCompleted = values[GUIDE_COMPLETED] ?: false, ) } diff --git a/app/src/main/java/org/neslite/app/system/SystemUiPolicy.kt b/app/src/main/java/org/neslite/app/system/SystemUiPolicy.kt index ca2e22f..5a70a00 100644 --- a/app/src/main/java/org/neslite/app/system/SystemUiPolicy.kt +++ b/app/src/main/java/org/neslite/app/system/SystemUiPolicy.kt @@ -5,6 +5,7 @@ enum class UiLayer { SETTINGS, SAVE_MANAGER, DIAGNOSTICS, + AUXILIARY_PAGE, PAUSE_MENU, GAME_RUNNING, GAME_PAUSED, @@ -17,6 +18,7 @@ enum class BackDecision { CLOSE_SETTINGS, CLOSE_SAVE_MANAGER, CLOSE_DIAGNOSTICS, + CLOSE_AUXILIARY_PAGE, RESUME_GAME, PAUSE_GAME, SHOW_PAUSE_MENU, @@ -30,6 +32,7 @@ object SystemUiPolicy { UiLayer.SETTINGS -> BackDecision.CLOSE_SETTINGS UiLayer.SAVE_MANAGER -> BackDecision.CLOSE_SAVE_MANAGER UiLayer.DIAGNOSTICS -> BackDecision.CLOSE_DIAGNOSTICS + UiLayer.AUXILIARY_PAGE -> BackDecision.CLOSE_AUXILIARY_PAGE UiLayer.PAUSE_MENU -> BackDecision.RESUME_GAME UiLayer.GAME_RUNNING -> BackDecision.PAUSE_GAME UiLayer.GAME_PAUSED -> BackDecision.SHOW_PAUSE_MENU diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4ae4eb5..52e8355 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,7 +1,5 @@ NESLite - NESLite %1$s - 导入你合法持有的 NES ROM,使用手柄或屏幕按键游玩。暂停后可以保存、恢复或安全退出。 JNI 自检 手机与显示 已连接控制器 diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 2f74a3c..1fb7a58 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,7 +1,9 @@ diff --git a/app/src/test/java/org/neslite/app/AspectRatioFrameLayoutTest.kt b/app/src/test/java/org/neslite/app/AspectRatioFrameLayoutTest.kt new file mode 100644 index 0000000..8b0d95c --- /dev/null +++ b/app/src/test/java/org/neslite/app/AspectRatioFrameLayoutTest.kt @@ -0,0 +1,21 @@ +package org.neslite.app + +import org.junit.Assert.assertEquals +import org.junit.Test + +class AspectRatioFrameLayoutTest { + @Test + fun magic8LandscapeViewportUsesAllAvailableHeightWithoutStretching() { + assertEquals(ViewportSize(width = 1672, height = 1254), fitFourByThree(2760, 1256)) + } + + @Test + fun narrowBoundsConstrainTheViewportByWidth() { + assertEquals(ViewportSize(width = 1200, height = 900), fitFourByThree(1200, 1600)) + } + + @Test + fun emptyBoundsProduceAnEmptyViewport() { + assertEquals(ViewportSize(width = 0, height = 0), fitFourByThree(0, 1256)) + } +} diff --git a/app/src/test/java/org/neslite/app/BuildIdentityTest.kt b/app/src/test/java/org/neslite/app/BuildIdentityTest.kt index 28615ec..0965801 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.4-debug", BuildConfig.VERSION_NAME) - assertEquals(9004, BuildConfig.VERSION_CODE) + assertEquals("0.9.5-debug", BuildConfig.VERSION_NAME) + assertEquals(9005, 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 new file mode 100644 index 0000000..8c05d25 --- /dev/null +++ b/app/src/test/java/org/neslite/app/VirtualGamepadLayoutTest.kt @@ -0,0 +1,48 @@ +package org.neslite.app + +import org.junit.Assert.assertTrue +import org.junit.Test + +class VirtualGamepadLayoutTest { + @Test + fun magic8ControlsStayOutsideTheFourByThreeViewport() { + val geometry = computeVirtualGamepadGeometry(width = 2760f, height = 1256f, maxRadius = 126f) + val leftControls = listOf(geometry.up, geometry.down, geometry.left, geometry.right, geometry.select) + val rightControls = listOf( + geometry.faceY, + geometry.faceB, + geometry.faceA, + geometry.faceX, + geometry.start, + geometry.pause, + geometry.reset, + ) + + assertTrue(leftControls.all { it.x + it.radius <= geometry.gameLeft }) + assertTrue(rightControls.all { it.x - it.radius >= geometry.gameRight }) + } + + @Test + fun controlsRemainInsideThePhysicalDisplay() { + val width = 2760f + val height = 1256f + val geometry = computeVirtualGamepadGeometry(width, height, maxRadius = 126f) + val controls = listOf( + geometry.up, + geometry.down, + geometry.left, + geometry.right, + geometry.select, + geometry.faceY, + geometry.faceB, + geometry.faceA, + geometry.faceX, + geometry.start, + geometry.pause, + geometry.reset, + ) + + 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 }) + } +} diff --git a/app/src/test/java/org/neslite/app/settings/AppSettingsTest.kt b/app/src/test/java/org/neslite/app/settings/AppSettingsTest.kt index e1f885c..3c1008c 100644 --- a/app/src/test/java/org/neslite/app/settings/AppSettingsTest.kt +++ b/app/src/test/java/org/neslite/app/settings/AppSettingsTest.kt @@ -14,12 +14,12 @@ class AppSettingsTest { assertEquals(1f, settings.volume) assertTrue(settings.turboEnabled) assertTrue(settings.fastForwardEnabled) - assertFalse(settings.showVirtualGamepadWithController) + assertTrue(settings.showVirtualGamepadWithController) assertFalse(settings.guideCompleted) } @Test - fun virtualGamepadStaysAvailableWithoutAControllerAndDefaultsHiddenWithOne() { + fun virtualGamepadStaysAvailableWithoutAControllerAndHonorsTheControllerPreference() { assertTrue(shouldShowVirtualGamepad(hasExternalController = false, showWithController = false)) assertFalse(shouldShowVirtualGamepad(hasExternalController = true, showWithController = false)) assertTrue(shouldShowVirtualGamepad(hasExternalController = true, showWithController = true)) diff --git a/app/src/test/java/org/neslite/app/system/SystemUiPolicyTest.kt b/app/src/test/java/org/neslite/app/system/SystemUiPolicyTest.kt index 34b8f80..3cf7ccc 100644 --- a/app/src/test/java/org/neslite/app/system/SystemUiPolicyTest.kt +++ b/app/src/test/java/org/neslite/app/system/SystemUiPolicyTest.kt @@ -21,6 +21,7 @@ class SystemUiPolicyTest { SystemUiPolicy.backDecision(UiLayer.CONTROLLER_SETTINGS), ) assertEquals(BackDecision.CLOSE_DIAGNOSTICS, SystemUiPolicy.backDecision(UiLayer.DIAGNOSTICS)) + assertEquals(BackDecision.CLOSE_AUXILIARY_PAGE, SystemUiPolicy.backDecision(UiLayer.AUXILIARY_PAGE)) } @Test diff --git a/docs/architecture.md b/docs/architecture.md index c288ff9..d665c6c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -1,4 +1,4 @@ -# NESLite 架构(0.9.4 RC 基线) +# NESLite 架构(0.9.5 候选基线) ## 边界与线程 @@ -101,6 +101,11 @@ DataStore 保存,渲染线程只读取整数缩放和过滤快照,音频及 按 pointerId 维护触摸来源并送入与物理手柄相同的 `InputRouter`,因此多来源去重、相反方向和释放规则 一致;取消、滑出、失焦与暂停清空所有虚拟来源。首次使用状态也存于设置 DataStore,不接触用户存档。 +0.9.5 将游戏入口收敛为单一横屏主画面:中央 `AspectRatioFrameLayout` 只按 4:3 测量,左右剩余安全 +区域绘制虚拟手柄;左侧固定入口打开游戏列表、导入和设置的全屏独立页面。X/Y 复用 M4 的 A/B 软件 +连发动作,重置为一次性触摸命令。页面打开前先等待运行会话进入暂停态,页面关闭后不自动恢复,避免 +文件选择或设置过程中游戏在后台继续。 + ## M7 系统交接与诊断 AndroidX 返回分发先把当前页面和会话状态归一成单一决策:设置/存档页回上级,暂停菜单返回会在 diff --git a/docs/build.md b/docs/build.md index fa1ae46..a883dec 100644 --- a/docs/build.md +++ b/docs/build.md @@ -1,6 +1,6 @@ # NESLite 可复现构建说明 -本说明对应 0.9.4 RC 基线。命令使用系统默认 PowerShell 7.6.6(`pwsh`)从仓库根目录执行;不需要手工复制 `.so`、 +本说明对应 0.9.5 候选基线。命令使用系统默认 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.4/`。静态审计逐个检查 APK 内 `.so` 的 ELF LOAD 段和 APK 16 KB 对齐,并拒绝非 +`.build/rc-0.9.5/`。静态审计逐个检查 APK 内 `.so` 的 ELF LOAD 段和 APK 16 KB 对齐,并拒绝非 arm64-v8a 库。 默认 APK 刻意保持未签名;发布私钥和口令不得传入仓库或写入脚本。获得受保护的发布密钥后,在隔离 diff --git a/docs/identity.md b/docs/identity.md index 05fb884..080f164 100644 --- a/docs/identity.md +++ b/docs/identity.md @@ -6,7 +6,7 @@ | --- | --- | | 显示名称 | `NESLite` | | 正式 application ID/namespace | `org.neslite.app` | -| 当前候选版本 | `0.9.4`,`versionCode = 9004` | +| 当前候选版本 | `0.9.5`,`versionCode = 9005` | | 调试 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 19a5f5a..89b1281 100644 --- a/docs/known-issues.md +++ b/docs/known-issues.md @@ -1,6 +1,6 @@ # 已知问题与候选版限制 -本清单对应 0.9.4 RC 源码。优先级含义:阻断表示 G8 不可通过;高表示发布前必须处理;中/低需有 +本清单对应 0.9.5 候选源码。优先级含义:阻断表示 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 | 中 | 开放 | Release lint 有 55 个非阻断警告,主要是动态界面文本国际化、依赖版本提示和 Android 16 固定横屏兼容提醒;无 lint 错误。 | +| RC-M01 | 中 | 开放 | Debug lint 有 53 个非阻断警告,主要是动态界面文本国际化、依赖版本提示和 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 b2fde40..ff4cea9 100644 --- a/docs/reports/m8-release-candidate.md +++ b/docs/reports/m8-release-candidate.md @@ -1,5 +1,8 @@ # M8 0.9.4 发布候选记录 +> 此文件保留 0.9.4 的历史候选证据。0.9.5 改动了主界面、触屏输入路径和页面/会话交接,不能直接 +> 继承本记录作为最终 G8 结论;完成 Magic8 实机复核后需生成新的 0.9.5 候选记录。 + 日期:2026-09-12 候选范围冻结为 M7 已实现功能加 M8 验收/交付能力,不再新增产品功能。源码冻结点为包含本报告的 diff --git a/docs/usage.md b/docs/usage.md index a527f6c..9ac343c 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -1,23 +1,25 @@ # NESLite 日常使用 -1. 点“从文件导入”选择你合法持有的 `.nes` 文件。应用按完整 SHA-256 建库;同名不同内容独立, +1. 应用会自动进入横屏。主画面中央是 4:3 游戏区,左侧是方向键与 Select,右侧是 A/B/X/Y、 + Start、暂停和重置;X/Y 分别是 A/B 连发。左上角的“列表”“导入”“设置”会打开独立页面。 +2. 点左侧“导入”,选择你合法持有的 `.nes` 文件。应用按完整 SHA-256 建库;同名不同内容独立, 相同内容不会重复。可按名称/哈希搜索、收藏或从列表隐藏。隐藏不删除 ROM 和存档,重新导入相同 内容会恢复关联。 -2. 点游戏名称加载,状态为 Paused 后点“运行”。可使用标准 Android 手柄;没有手柄时使用画面下方的 - 多点虚拟方向、A/B、Start/Select 和暂停键。滑出或系统取消会释放对应按键。 -3. “显示、音频与输入设置”可切换 4:3/整数缩放、最近邻/线性过滤、音量、静音、软件连发和 RB - 快进,并进入手柄映射、A/B 交换和死区设置。设置会在重启后恢复。 -4. 游戏中点虚拟“暂停”或按手柄右摇杆进入暂停菜单。“存档管理”显示十个手动槽、三个自动恢复点、 +3. 点左侧“列表”,再点游戏条目;加载完成后会自动开始。连接外接手柄后仍可按设置选择是否显示 + 屏幕按键。多点触控支持方向与动作同时按住,滑出、取消、失焦或暂停都会释放输入。 +4. 点左侧“设置”可切换 4:3/整数缩放、最近邻/线性过滤、音量、静音、软件连发和 RB 快进,并进入 + 手柄按键设置、A/B 交换、死区调整及设备诊断。设置会在重启后恢复。 +5. 游戏中点右侧“暂停”或按手柄右摇杆进入暂停菜单。“存档管理”显示十个手动槽、三个自动恢复点、 提交时间和缩略图。只有原生层确认安全落盘后才显示保存成功。 -5. 存档包通过 Android 系统文件选择器导入导出,默认不含 ROM。导入前先加载对应游戏并暂停;哈希、 +6. 存档包通过 Android 系统文件选择器导入导出,默认不含 ROM。导入前先加载对应游戏并暂停;哈希、 内核提交、配置、路径、大小或校验不匹配会被拒绝并保留现有有效存档。 -6. 用“退出游戏”正常结束。上次进程异常终止时,再次加载相同游戏只列出校验有效的自动恢复点, +7. 用“退出游戏”正常结束。上次进程异常终止时,再次加载相同游戏只列出校验有效的自动恢复点, 不会自动运行或覆盖当前进度;同一游戏和恢复点连续异常两次后会返回列表并阻止重复启动,只有 明确点“手动重试”才再次加载。 -7. 系统返回手势与手柄 B 使用相同规则:运行中先安全暂停,暂停菜单中继续,设置和存档页回上级, - 游戏列表中退出应用。加载、暂停或退出交接尚未确认时,重复操作会被忽略。 -8. 切到后台、锁屏、来电/通知失去音频焦点时,应用会清输入、静音并请求暂停保存;回到前台后保持 +8. 系统返回手势与手柄 B 使用相同规则:运行中先安全暂停;各独立页面返回主画面;设置、按键设置、 + 诊断和存档页按原路径逐级返回。加载、暂停或退出交接尚未确认时,重复操作会被忽略。 +9. 切到后台、锁屏、来电/通知失去音频焦点时,应用会清输入、静音并请求暂停保存;回到前台后保持 暂停,由你手动继续。诊断导出会附带限额系统日志和各显示模式的短时运行样本,不包含 ROM。 -当前尚未连接目标手机,真机手势、挖孔布局、刷新率、生命周期、文件提供器和完整触控流程仍需在 -L2/G6/G7 验收中确认。 +荣耀 Magic8 截图已确认 Android 16、arm64-v8a、120 Hz 显示和 GameSir X5 Lite 枚举正常;新版三栏 +布局、实际按键事件、系统手势、文件提供器、生命周期和完整游戏流程仍需在手机上复核。 diff --git a/tools/package-rc.ps1 b/tools/package-rc.ps1 index dd90c2f..4937f66 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.4' -$versionCode = 9004 +$versionName = '0.9.5' +$versionCode = 9005 $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.' }