diff --git a/android/src/com/unciv/app/AndroidLauncher.kt b/android/src/com/unciv/app/AndroidLauncher.kt index 91093acfdc..aa4331ee35 100644 --- a/android/src/com/unciv/app/AndroidLauncher.kt +++ b/android/src/com/unciv/app/AndroidLauncher.kt @@ -20,9 +20,11 @@ class AndroidLauncher : AndroidApplication() { } val config = AndroidApplicationConfiguration().apply { useImmersiveMode = true } - val game = UncivGame(BuildConfig.VERSION_NAME, - CrashReportSenderAndroid(this)) - {this.finish()} + val game = UncivGame ( + version = BuildConfig.VERSION_NAME, + crashReportSender = CrashReportSenderAndroid(this), + exitEvent = this::finish + ) initialize(game, config) } diff --git a/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt b/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt index 2d82d2ad85..6254930726 100644 --- a/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt +++ b/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt @@ -86,7 +86,7 @@ open class CameraStageBaseScreen : Screen { fun onBackButtonClicked(action:()->Unit): InputListener { val listener = object : InputListener(){ override fun keyDown(event: InputEvent?, keycode: Int): Boolean { - if(keycode == Input.Keys.BACK){ + if(keycode == Input.Keys.BACK || keycode == Input.Keys.ESCAPE){ action() return true } diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt index a68c4d6183..2f55d4d2aa 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt @@ -132,7 +132,7 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() { shouldUpdate = true } - backButtonListener = onBackButtonClicked { exitGamePrompt() } + backButtonListener = onBackButtonClicked { backButtonAndESCHandler() } addKeyboardListener() // for map panning by W,S,A,D @@ -565,26 +565,31 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() { displayTutorial(Tutorial.Embarking) } - private fun exitGamePrompt() { + private fun backButtonAndESCHandler() { + + // Since Popups including the Main Menu and the Options screen have no own back button + // listener and no trivial way to set one, back/esc with one of them open ends up here. + // Also, the reaction of other popups like 'disband this unit' to back/esc feels nicer this way. + // After removeListener just in case this is slow (enumerating all stage actors) + if (hasOpenPopups()) { + closeAllPopups() + return + } // don't show a dialog, if it can't exit the game - if (game.exitEvent == null) + if (game.exitEvent == null) { return - - // remove current listener for the "BACK" button to avoid showing the dialog twice - stage.removeListener( backButtonListener ) + } val promptWindow = Popup(this) promptWindow.addGoodSizedLabel("Do you want to exit the game?".tr()) promptWindow.row() - promptWindow.addButton("Yes"){game.exitEvent?.invoke()} + promptWindow.addButton("Yes") { game.exitEvent?.invoke() } promptWindow.addButton("No") { - // restore the listener back - stage.addListener(backButtonListener) promptWindow.close() } // show the dialog - promptWindow.open() + promptWindow.open (true) // true = always on top } }