Skip to content

Commit

Permalink
Fix for the "Back button can't end Unciv (#1513)" (#1661)
Browse files Browse the repository at this point in the history
* Callback to exit the game by 'Back' button

* Prompt dialog for the game exit

* Additional strings for translations are generated

* Do not show exit prompt dialog twice
  • Loading branch information
JackRainy authored and yairm210 committed Jan 12, 2020
1 parent ca59dc4 commit 8a327fa
Show file tree
Hide file tree
Showing 25 changed files with 77 additions and 10 deletions.
2 changes: 2 additions & 0 deletions android/assets/jsons/translationsByLanguage/Czech.properties
Expand Up @@ -790,6 +790,8 @@ Victory status = Cesta k vítězství
Social policies = Sociální politika
Community = Komunita
Close = Zavřít
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
2 changes: 2 additions & 0 deletions android/assets/jsons/translationsByLanguage/Dutch.properties
Expand Up @@ -1222,6 +1222,8 @@ Social policies = Sociaal beleid
# Requires translation!
Community =
Close = Sluiten
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
Expand Up @@ -1385,6 +1385,8 @@ Social policies =
Community =
# Requires translation!
Close =
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
2 changes: 2 additions & 0 deletions android/assets/jsons/translationsByLanguage/French.properties
Expand Up @@ -775,6 +775,8 @@ Victory status = Conditions de victoire
Social policies = Doctrines
Community = Communauté
Close = Fermer
# Requires translation!
Do you want to exit the game? =

# City screen

Expand Down
2 changes: 2 additions & 0 deletions android/assets/jsons/translationsByLanguage/German.properties
Expand Up @@ -793,6 +793,8 @@ Victory status = Siegesstatus
Social policies = Sozialpolitiken
Community = Gemeinschaft
Close = Schließen
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
Expand Up @@ -791,6 +791,8 @@ Victory status = Status kemenangan
Social policies = Kebijakan sosial
Community = Komunitas
Close = Tutup
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
Expand Up @@ -762,6 +762,8 @@ Victory status = Condizioni di vittoria
Social policies = Politiche sociali
Community = Community
Close = Chiudi
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
2 changes: 2 additions & 0 deletions android/assets/jsons/translationsByLanguage/Korean.properties
Expand Up @@ -812,6 +812,8 @@ Victory status = 승리 조건 확인
Social policies = 사회 정책
Community = 커뮤니티
Close = 닫기
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
2 changes: 2 additions & 0 deletions android/assets/jsons/translationsByLanguage/Malay.properties
Expand Up @@ -1274,6 +1274,8 @@ Social policies =
Community =
# Requires translation!
Close =
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
2 changes: 2 additions & 0 deletions android/assets/jsons/translationsByLanguage/Polish.properties
Expand Up @@ -775,6 +775,8 @@ Victory status = Status zwycięstwa
Social policies = Ustroje społeczne
Community = Społeczność
Close = Zamknij
# Requires translation!
Do you want to exit the game? =

# City screen

Expand Down
Expand Up @@ -833,6 +833,8 @@ Victory status = Status da vitória
Social policies = Políticas sociais
Community = Comunidade
Close = Fechar
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
Expand Up @@ -1104,6 +1104,8 @@ Social policies = Politici sociale
# Requires translation!
Community =
Close = Închide
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
Expand Up @@ -775,6 +775,7 @@ Victory status = Статус победы
Social policies = Общественные институты
Community = Сообщество
Close = Закрыть
Do you want to exit the game? = Вы хотите выйти из игры?
# City screen
Expand Down
Expand Up @@ -791,6 +791,8 @@ Victory status = 胜利进度
Social policies = 社会政策
Community = 开发者社区
Close = 关闭
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
Expand Up @@ -779,6 +779,8 @@ Victory status = Estado de victoria
Social policies = Políticas sociales
Community = Comunidad
Close = Cerrar
# Requires translation!
Do you want to exit the game? =

# City screen

Expand Down
Expand Up @@ -791,6 +791,8 @@ Victory status = 勝利進度
Social policies = 社會政策
Community = 社群
Close = 關閉
# Requires translation!
Do you want to exit the game? =
# City screen
Expand Down
Expand Up @@ -775,6 +775,7 @@ Victory status = Статус перемоги
Social policies = Соціальні інститути
Community = Громада
Close = Закрити
Do you want to exit the game? = Ви бажаєте вийти з гри?

# City screen

Expand Down
Expand Up @@ -762,6 +762,7 @@ Victory status =
Social policies =
Community =
Close =
Do you want to exit the game? =
# City screen
Expand Down
4 changes: 3 additions & 1 deletion android/src/com/unciv/app/AndroidLauncher.kt
Expand Up @@ -9,7 +9,9 @@ class AndroidLauncher : AndroidApplication() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val config = AndroidApplicationConfiguration().apply { useImmersiveMode = true }
val game = UncivGame(BuildConfig.VERSION_NAME, CrashReportSenderAndroid(this))
val game = UncivGame(BuildConfig.VERSION_NAME,
CrashReportSenderAndroid(this))
{this.finish()}
initialize(game, config)
}
}
7 changes: 5 additions & 2 deletions core/src/com/unciv/UncivGame.kt
Expand Up @@ -24,8 +24,12 @@ import kotlin.concurrent.thread

class UncivGame(
val version: String,
private val crashReportSender: CrashReportSender? = null
private val crashReportSender: CrashReportSender? = null,
val exitEvent: (()->Unit)? = null
) : Game() {
// we need this secondary constructor because Java code for iOS can't handle Kotlin lambda parameters
constructor(version: String) : this(version, null)

lateinit var gameInfo: GameInfo
lateinit var settings : GameSettings
lateinit var crashController: CrashController
Expand All @@ -39,7 +43,6 @@ class UncivGame(

var rewriteTranslationFiles = false


lateinit var worldScreen: WorldScreen

var music : Music? =null
Expand Down
9 changes: 6 additions & 3 deletions core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt
Expand Up @@ -106,16 +106,19 @@ open class CameraStageBaseScreen : Screen {
internal var batch: Batch = SpriteBatch()
}

fun onBackButtonClicked(action:()->Unit){
stage.addListener(object : InputListener(){
/** It returns the assigned [InputListener] */
fun onBackButtonClicked(action:()->Unit): InputListener {
var listener = object : InputListener(){
override fun keyDown(event: InputEvent?, keycode: Int): Boolean {
if(keycode == Input.Keys.BACK){
action()
return true
}
return false
}
})
}
stage.addListener( listener )
return listener
}

}
Expand Down
26 changes: 26 additions & 0 deletions core/src/com/unciv/ui/worldscreen/WorldScreen.kt
Expand Up @@ -3,6 +3,7 @@ package com.unciv.ui.worldscreen
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.math.Vector2
import com.badlogic.gdx.scenes.scene2d.InputListener
import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.actions.Actions
import com.badlogic.gdx.scenes.scene2d.ui.Button
Expand Down Expand Up @@ -58,6 +59,8 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
private val notificationsScroll: NotificationsScroll
var shouldUpdate=false

private var backButtonListener : InputListener

init {
topBar.setPosition(0f, stage.height - topBar.height)
topBar.width = stage.width
Expand Down Expand Up @@ -126,6 +129,8 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
shouldUpdate = true
}

backButtonListener = onBackButtonClicked { exitGamePrompt() }

// don't run update() directly, because the UncivGame.worldScreen should be set so that the city buttons and tile groups
// know what the viewing civ is.
shouldUpdate = true
Expand Down Expand Up @@ -502,5 +507,26 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
displayTutorial(Tutorial.Embarking)
}

private fun exitGamePrompt() {

// don't show a dialog, if it can't exit the game
if (game.exitEvent == null)
return

// remove current listener for the "BACK" button to avoid showing the dialog twice
stage.removeListener( backButtonListener )

var promptWindow = PopupTable(this)
promptWindow.addGoodSizedLabel("Do you want to exit the game?".tr())
promptWindow.row()
promptWindow.addButton("Yes"){game.exitEvent?.invoke()}
promptWindow.addButton("No") {
// restore the listener back
stage.addListener(backButtonListener)
promptWindow.close()
}
// show the dialog
promptWindow.open()
}
}

4 changes: 2 additions & 2 deletions desktop/src/com/unciv/app/desktop/DesktopLauncher.kt
Expand Up @@ -12,6 +12,7 @@ import com.unciv.UncivGame
import com.unciv.models.translations.tr
import java.io.File
import kotlin.concurrent.thread
import kotlin.system.exitProcess


internal object DesktopLauncher {
Expand All @@ -26,8 +27,7 @@ internal object DesktopLauncher {
config.title = "Unciv"
config.useHDPI = true

val game = UncivGame("Desktop")

val game = UncivGame("Desktop", null){exitProcess(0)}

if(!RaspberryPiDetector.isRaspberryPi()) // No discord RPC for Raspberry Pi, see https://github.com/yairm210/Unciv/issues/1624
tryActivateDiscord(game)
Expand Down
2 changes: 1 addition & 1 deletion ios/src/com/unciv/app/IOSLauncher.java
Expand Up @@ -10,7 +10,7 @@ class IOSLauncher extends IOSApplication.Delegate {
@Override
protected IOSApplication createApplication() {
IOSApplicationConfiguration config = new IOSApplicationConfiguration();
return new IOSApplication(new com.unciv.UncivGame("IOS", null), config);
return new IOSApplication(new com.unciv.UncivGame("IOS"), config);
}

public static void main(String[] argv) {
Expand Down
2 changes: 1 addition & 1 deletion tests/src/com/unciv/testing/BasicTests.kt
Expand Up @@ -35,7 +35,7 @@ class BasicTests {

@Test
fun gameIsNotRunWithDebugModes() {
val game = UncivGame("", null)
val game = UncivGame("", null, null)
Assert.assertTrue("This test will only pass if the game is not run with debug modes",
!game.superchargedForDebug
&& !game.viewEntireMapForDebug
Expand Down

0 comments on commit 8a327fa

Please sign in to comment.