fix: allow virtual button swaps without a controller in 1.0.2
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package org.neslite.app
|
||||
|
||||
import android.os.SystemClock
|
||||
import androidx.test.core.app.ActivityScenario
|
||||
import androidx.test.espresso.Espresso.onView
|
||||
import androidx.test.espresso.NoMatchingViewException
|
||||
import androidx.test.espresso.action.ViewActions.click
|
||||
import androidx.test.espresso.action.ViewActions.scrollTo
|
||||
import androidx.test.espresso.assertion.ViewAssertions.matches
|
||||
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
|
||||
import androidx.test.espresso.matcher.ViewMatchers.isEnabled
|
||||
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
|
||||
import androidx.test.espresso.matcher.ViewMatchers.withText
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import org.junit.Assume.assumeTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.neslite.app.diagnostics.DeviceDiagnostics
|
||||
import org.neslite.app.settings.AppSettingsStore
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ButtonSwapSettingsTest {
|
||||
@Test
|
||||
fun noControllerCanSwapPersistReopenAndResetScreenButtons() {
|
||||
assumeTrue("Disconnect external controllers for this test", DeviceDiagnostics.controllerDeviceIds().isEmpty())
|
||||
val store = AppSettingsStore(InstrumentationRegistry.getInstrumentation().targetContext)
|
||||
val previous = runBlocking { store.load() }
|
||||
runBlocking { store.save(previous.copy(guideCompleted = true, virtualSwapAB = false, virtualSwapXY = false)) }
|
||||
try {
|
||||
ActivityScenario.launch(MainActivity::class.java).use { scenario ->
|
||||
openSwaps()
|
||||
awaitButton("交换 X/Y 按键:关")
|
||||
awaitButton("交换 A/B 按键:关")
|
||||
onView(withText("交换 X/Y 按键:关")).perform(click())
|
||||
onView(withText("交换 A/B 按键:关")).perform(click())
|
||||
awaitSaved(store, swapAB = true, swapXY = true)
|
||||
onView(withText("交换 A/B 按键:开")).perform(click())
|
||||
awaitSaved(store, swapAB = false, swapXY = true)
|
||||
|
||||
scenario.recreate()
|
||||
openSwaps()
|
||||
awaitButton("交换 X/Y 按键:开")
|
||||
awaitButton("交换 A/B 按键:关")
|
||||
onView(withText("恢复屏幕按键默认顺序")).perform(scrollTo(), click())
|
||||
awaitSaved(store, swapAB = false, swapXY = false)
|
||||
awaitButton("交换 X/Y 按键:关")
|
||||
awaitButton("交换 A/B 按键:关")
|
||||
}
|
||||
} finally {
|
||||
runBlocking { store.save(previous) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun openSwaps() {
|
||||
onView(withContentDescription("设置")).perform(click())
|
||||
onView(withText("交换 X/Y、A/B 按键与手动映射")).perform(scrollTo(), click())
|
||||
}
|
||||
|
||||
private fun awaitButton(label: String) {
|
||||
val deadline = SystemClock.uptimeMillis() + 5000L
|
||||
while (true) {
|
||||
try {
|
||||
onView(withText(label)).check(matches(isDisplayed())).check(matches(isEnabled()))
|
||||
return
|
||||
} catch (failure: NoMatchingViewException) {
|
||||
if (SystemClock.uptimeMillis() >= deadline) throw failure
|
||||
} catch (failure: AssertionError) {
|
||||
if (SystemClock.uptimeMillis() >= deadline) throw failure
|
||||
}
|
||||
SystemClock.sleep(50L)
|
||||
}
|
||||
}
|
||||
|
||||
private fun awaitSaved(store: AppSettingsStore, swapAB: Boolean, swapXY: Boolean) = runBlocking {
|
||||
withTimeout(5000L) {
|
||||
while (true) {
|
||||
val settings = store.load()
|
||||
if (settings.virtualSwapAB == swapAB && settings.virtualSwapXY == swapXY) break
|
||||
delay(50L)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1330,6 +1330,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
}
|
||||
applyAppSettings()
|
||||
renderSettings()
|
||||
renderControllerSettings()
|
||||
if (!loaded.guideCompleted) showFirstUseGuide()
|
||||
}
|
||||
}
|
||||
@@ -1366,19 +1367,24 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
// Do not overwrite saved screen controls with a temporary default during reconnect.
|
||||
if (deviceId !in readyControllerProfileIds) return
|
||||
val profile = controllerProfiles[deviceId] ?: return
|
||||
virtualButtonSwapsOverride = profile.swapAB to profile.swapXY
|
||||
val updated = appSettings.copy(virtualSwapAB = profile.swapAB, virtualSwapXY = profile.swapXY)
|
||||
updateVirtualButtonSwaps(profile.swapAB, profile.swapXY)
|
||||
}
|
||||
|
||||
private fun updateVirtualButtonSwaps(swapAB: Boolean, swapXY: Boolean) {
|
||||
virtualButtonSwapsOverride = swapAB to swapXY
|
||||
val updated = appSettings.copy(virtualSwapAB = swapAB, virtualSwapXY = swapXY)
|
||||
val changed = updated != appSettings
|
||||
appSettings = updated
|
||||
if (::virtualGamepad.isInitialized) {
|
||||
virtualGamepad.configureButtonSwaps(profile.swapAB, profile.swapXY)
|
||||
virtualGamepad.configureButtonSwaps(swapAB, swapXY)
|
||||
}
|
||||
if (changed && appSettingsLoaded) persistAppSettings()
|
||||
}
|
||||
|
||||
private fun persistAppSettings() {
|
||||
val snapshot = appSettings
|
||||
controllerScope.launch { appSettingsStore.save(snapshot) }
|
||||
// Start preference writes in UI order, including rapid independent swap toggles.
|
||||
controllerScope.launch(Dispatchers.Main.immediate) { appSettingsStore.save(snapshot) }
|
||||
}
|
||||
|
||||
private fun showFirstUseGuide() {
|
||||
@@ -1503,7 +1509,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
manualMappingIndex = null
|
||||
bindingCaptureWaitForNeutral = false
|
||||
controllerSettingsMessage = "可逐项设置全部按键,也可以点击任意动作单独修改。"
|
||||
showOverlayPage(OverlayPage.CONTROLLER_SETTINGS, "手柄按键设置")
|
||||
showOverlayPage(OverlayPage.CONTROLLER_SETTINGS, "屏幕与手柄按键设置")
|
||||
renderControllerSettings()
|
||||
}
|
||||
|
||||
@@ -1523,15 +1529,64 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
controllerReturnToSettings = false
|
||||
}
|
||||
|
||||
private fun renderButtonSwapSettings(profile: ControllerProfile?) {
|
||||
controllerSettingsPanel.addView(section("交换 X/Y、A/B 按键"))
|
||||
controllerSettingsPanel.addView(body(
|
||||
if (profile == null) {
|
||||
"调整屏幕虚拟按键的位置:点击后立即生效并自动保存,重启后保留。按钮文字、颜色和触控区域一起换位。"
|
||||
} else {
|
||||
"默认顺序:A → NES A,B → NES B,X → A 连发,Y → B 连发。\n" +
|
||||
"两组可独立交换;已有自定义映射时,交换对应的普通或连发动作绑定。点击保存后,当前手柄和屏幕按键同步交换。"
|
||||
},
|
||||
))
|
||||
val swapXY = profile?.swapXY ?: appSettings.virtualSwapXY
|
||||
val swapAB = profile?.swapAB ?: appSettings.virtualSwapAB
|
||||
controllerSettingsPanel.addView(Button(this).apply {
|
||||
text = if (swapXY) "交换 X/Y 按键:开" else "交换 X/Y 按键:关"
|
||||
isEnabled = profile == null || (pendingBindingAction == null && manualMappingIndex == null)
|
||||
setOnClickListener {
|
||||
if (profile == null) {
|
||||
updateVirtualButtonSwaps(appSettings.virtualSwapAB, !appSettings.virtualSwapXY)
|
||||
} else {
|
||||
draftProfile = profile.toggleXY()
|
||||
controllerSettingsMessage = "已交换 A/B 连发按键(默认 X/Y);保存后生效。"
|
||||
}
|
||||
renderControllerSettings()
|
||||
}
|
||||
})
|
||||
controllerSettingsPanel.addView(Button(this).apply {
|
||||
text = if (swapAB) "交换 A/B 按键:开" else "交换 A/B 按键:关"
|
||||
isEnabled = profile == null || (pendingBindingAction == null && manualMappingIndex == null)
|
||||
setOnClickListener {
|
||||
if (profile == null) {
|
||||
updateVirtualButtonSwaps(!appSettings.virtualSwapAB, appSettings.virtualSwapXY)
|
||||
} else {
|
||||
draftProfile = profile.toggleAB()
|
||||
controllerSettingsMessage = "已交换 NES A/B;保存后生效。"
|
||||
}
|
||||
renderControllerSettings()
|
||||
}
|
||||
})
|
||||
if (profile == null) {
|
||||
controllerSettingsPanel.addView(Button(this).apply {
|
||||
text = "恢复屏幕按键默认顺序"
|
||||
setOnClickListener {
|
||||
updateVirtualButtonSwaps(swapAB = false, swapXY = false)
|
||||
renderControllerSettings()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private fun renderControllerSettings() {
|
||||
if (!::controllerSettingsPanel.isInitialized || controllerSettingsPanel.visibility != View.VISIBLE) return
|
||||
controllerSettingsPanel.removeAllViews()
|
||||
controllerSettingsPanel.addView(section("手柄映射与死区"))
|
||||
val deviceId = selectedControllerId
|
||||
val controller = controllers.firstOrNull { it.deviceId == deviceId }
|
||||
val profile = draftProfile ?: deviceId?.let(controllerProfiles::get)
|
||||
val profile = if (controller == null) null else draftProfile ?: deviceId?.let(controllerProfiles::get)
|
||||
renderButtonSwapSettings(profile)
|
||||
if (controller == null || profile == null) {
|
||||
controllerSettingsPanel.addView(body("当前没有已连接的手柄;设置入口保持可达,连接后即可编辑。"))
|
||||
controllerSettingsPanel.addView(body("未连接手柄也可以交换上方的屏幕按键。手柄按键映射和摇杆设置需连接手柄后使用。"))
|
||||
controllerSettingsPanel.addView(Button(this).apply {
|
||||
text = "关闭"
|
||||
setOnClickListener { closeControllerSettings() }
|
||||
@@ -1539,6 +1594,7 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
focusFirstButton(controllerSettingsPanel)
|
||||
return
|
||||
}
|
||||
controllerSettingsPanel.addView(section("手柄映射与死区"))
|
||||
controllerSettingsPanel.addView(
|
||||
body("${controller.name}\n档案:${profile.origin} · ${profile.fingerprint.take(12)};不使用临时 deviceId 持久化"),
|
||||
)
|
||||
@@ -1590,29 +1646,6 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
})
|
||||
})
|
||||
}
|
||||
controllerSettingsPanel.addView(section("交换 X/Y、A/B 按键"))
|
||||
controllerSettingsPanel.addView(body(
|
||||
"默认顺序:A → NES A,B → NES B,X → A 连发,Y → B 连发。\n" +
|
||||
"两组可独立交换;已有自定义映射时,交换对应的普通或连发动作绑定。点击保存后,当前手柄和屏幕按键同步交换;屏幕按键的文字与触控位置一起调整。",
|
||||
))
|
||||
controllerSettingsPanel.addView(Button(this).apply {
|
||||
text = if (profile.swapXY) "交换 X/Y 按键:开" else "交换 X/Y 按键:关"
|
||||
isEnabled = pendingBindingAction == null && manualMappingIndex == null
|
||||
setOnClickListener {
|
||||
draftProfile = profile.toggleXY()
|
||||
controllerSettingsMessage = "已交换 A/B 连发按键(默认 X/Y);保存后生效。"
|
||||
renderControllerSettings()
|
||||
}
|
||||
})
|
||||
controllerSettingsPanel.addView(Button(this).apply {
|
||||
text = if (profile.swapAB) "交换 A/B 按键:开" else "交换 A/B 按键:关"
|
||||
isEnabled = pendingBindingAction == null && manualMappingIndex == null
|
||||
setOnClickListener {
|
||||
draftProfile = profile.toggleAB()
|
||||
controllerSettingsMessage = "已交换 NES A/B;保存后生效。"
|
||||
renderControllerSettings()
|
||||
}
|
||||
})
|
||||
controllerSettingsPanel.addView(body("轴按下阈值:${formatThreshold(profile.pressThreshold)}"))
|
||||
controllerSettingsPanel.addView(SeekBar(this).apply {
|
||||
min = 20
|
||||
@@ -1958,6 +1991,8 @@ class MainActivity : ComponentActivity(), InputManager.InputDeviceListener {
|
||||
selectedControllerId = controllers.firstOrNull()?.deviceId
|
||||
draftProfile = null
|
||||
pendingBindingAction = null
|
||||
manualMappingIndex = null
|
||||
bindingCaptureWaitForNeutral = false
|
||||
}
|
||||
syncVirtualButtonSwaps()
|
||||
renderControllerSettings()
|
||||
|
||||
@@ -7,7 +7,7 @@ class BuildIdentityTest {
|
||||
@Test
|
||||
fun debugBuildUsesTheDocumentedIdentity() {
|
||||
assertEquals("org.neslite.app.debug", BuildConfig.APPLICATION_ID)
|
||||
assertEquals("1.0.1-debug", BuildConfig.VERSION_NAME)
|
||||
assertEquals(1000001, BuildConfig.VERSION_CODE)
|
||||
assertEquals("1.0.2-debug", BuildConfig.VERSION_NAME)
|
||||
assertEquals(1000002, BuildConfig.VERSION_CODE)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user