All UI changes in threads are now run in a postRunnable - should hopefully get rid of IndexOutOfBoundsExceptions

This commit is contained in:
Yair Morgenstern 2020-05-18 23:08:19 +03:00
parent 507c677d08
commit 7330daff48
10 changed files with 55 additions and 42 deletions

View File

@ -65,7 +65,6 @@ class MainMenuScreen: CameraStageBaseScreen() {
Actions.fadeIn(0.3f) Actions.fadeIn(0.3f)
)) ))
} }
} }

View File

@ -143,14 +143,16 @@ class MultiplayerScreen(previousScreen: CameraStageBaseScreen) : PickerScreen()
else else
GameSaver.saveGame(game, gameName, true) GameSaver.saveGame(game, gameName, true)
reloadGameListUI() Gdx.app.postRunnable { reloadGameListUI() }
} catch (ex: Exception) { } catch (ex: Exception) {
Gdx.app.postRunnable {
val errorPopup = Popup(this) val errorPopup = Popup(this)
errorPopup.addGoodSizedLabel("Could not download game!".tr()) errorPopup.addGoodSizedLabel("Could not download game!".tr())
errorPopup.row() errorPopup.row()
errorPopup.addCloseButton() errorPopup.addCloseButton()
errorPopup.open() errorPopup.open()
} }
}
addGameButton.setText(addGameText) addGameButton.setText(addGameText)
addGameButton.enable() addGameButton.enable()
} }

View File

@ -67,7 +67,7 @@ class MapDownloadPopup(loadMapScreen: LoadMapScreen): Popup(loadMapScreen) {
open() open()
} }
} catch (ex: Exception) { } catch (ex: Exception) {
addGoodSizedLabel("Could not get list of maps!").row() Gdx.app.postRunnable { addGoodSizedLabel("Could not get list of maps!").row() }
} }
} }

View File

@ -54,6 +54,7 @@ class NewMapScreen : PickerScreen() {
} }
} catch (exception: Exception) { } catch (exception: Exception) {
Gdx.app.postRunnable {
rightButtonSetEnabled(true) rightButtonSetEnabled(true)
val cantMakeThatMapPopup = Popup(this) val cantMakeThatMapPopup = Popup(this)
cantMakeThatMapPopup.addGoodSizedLabel("It looks like we can't make a map with the parameters you requested!".tr()) cantMakeThatMapPopup.addGoodSizedLabel("It looks like we can't make a map with the parameters you requested!".tr())
@ -63,6 +64,7 @@ class NewMapScreen : PickerScreen() {
Gdx.input.inputProcessor = stage Gdx.input.inputProcessor = stage
} }
} }
}
} }
} }

View File

@ -98,12 +98,14 @@ class NewGameScreen(previousScreen:CameraStageBaseScreen, _gameSetupInfo: GameSe
//Save gameId to clipboard because you have to do it anyway. //Save gameId to clipboard because you have to do it anyway.
Gdx.app.clipboard.contents = newGame!!.gameId Gdx.app.clipboard.contents = newGame!!.gameId
//Popup to notify the User that the gameID got copied to the clipboard //Popup to notify the User that the gameID got copied to the clipboard
ResponsePopup("gameID copied to clipboard".tr(), UncivGame.Current.worldScreen, 2500) Gdx.app.postRunnable { ResponsePopup("gameID copied to clipboard".tr(), UncivGame.Current.worldScreen, 2500) }
} catch (ex: Exception) { } catch (ex: Exception) {
Gdx.app.postRunnable {
val cantUploadNewGamePopup = Popup(this) val cantUploadNewGamePopup = Popup(this)
cantUploadNewGamePopup.addGoodSizedLabel("Could not upload game!") cantUploadNewGamePopup.addGoodSizedLabel("Could not upload game!")
cantUploadNewGamePopup.addCloseButton() cantUploadNewGamePopup.addCloseButton()
cantUploadNewGamePopup.open() cantUploadNewGamePopup.open()
}
newGame = null newGame = null
} }
} }

View File

@ -57,7 +57,7 @@ class SaveGameScreen : PickerScreen() {
rightSideButton.setText("Saving...".tr()) rightSideButton.setText("Saving...".tr())
thread(name="SaveGame") { thread(name="SaveGame") {
GameSaver.saveGame(UncivGame.Current.gameInfo, textField.text) GameSaver.saveGame(UncivGame.Current.gameInfo, textField.text)
UncivGame.Current.setWorldScreen() Gdx.app.postRunnable { UncivGame.Current.setWorldScreen() }
} }
} }
rightSideButton.enable() rightSideButton.enable()

View File

@ -461,17 +461,19 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
try { try {
OnlineMultiplayer().tryUploadGame(gameInfoClone) OnlineMultiplayer().tryUploadGame(gameInfoClone)
} catch (ex: Exception) { } catch (ex: Exception) {
Gdx.app.postRunnable { // Since we're changing the UI, that should be done on the main thread
val cantUploadNewGamePopup = Popup(this) val cantUploadNewGamePopup = Popup(this)
cantUploadNewGamePopup.addGoodSizedLabel("Could not upload game!").row() cantUploadNewGamePopup.addGoodSizedLabel("Could not upload game!").row()
cantUploadNewGamePopup.addCloseButton() cantUploadNewGamePopup.addCloseButton()
cantUploadNewGamePopup.open() cantUploadNewGamePopup.open()
}
isPlayersTurn = true // Since we couldn't push the new game clone, then it's like we never clicked the "next turn" button isPlayersTurn = true // Since we couldn't push the new game clone, then it's like we never clicked the "next turn" button
shouldUpdate = true shouldUpdate = true
return@thread return@thread
} }
} }
} catch (ex: Exception) { } catch (ex: Exception) {
game.crashController.crashOccurred() Gdx.app.postRunnable { game.crashController.crashOccurred() }
throw ex throw ex
} }

View File

@ -220,17 +220,21 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen)
innerTable.add(errorTable).colspan(2).row() innerTable.add(errorTable).colspan(2).row()
downloadMusicButton.onClick { downloadMusicButton.onClick {
// So the whole game doesn't get stuck while downloading the file
thread(name = "Music") {
try {
downloadMusicButton.disable() downloadMusicButton.disable()
errorTable.clear() errorTable.clear()
errorTable.add("Downloading...".toLabel()) errorTable.add("Downloading...".toLabel())
// So the whole game doesn't get stuck while downloading the file
thread(name = "Music") {
try {
val file = DropBox.downloadFile("/Music/thatched-villagers.mp3") val file = DropBox.downloadFile("/Music/thatched-villagers.mp3")
musicLocation.write(file, false) musicLocation.write(file, false)
Gdx.app.postRunnable {
rebuildInnerTable() rebuildInnerTable()
worldScreen.game.startMusic() worldScreen.game.startMusic()
}
} catch (ex: Exception) { } catch (ex: Exception) {
Gdx.app.postRunnable {
errorTable.clear() errorTable.clear()
errorTable.add("Could not download music!".toLabel(Color.RED)) errorTable.add("Could not download music!".toLabel(Color.RED))
} }
@ -238,6 +242,7 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen)
} }
} }
} }
}
private fun addResolutionSelectBox() { private fun addResolutionSelectBox() {
innerTable.add("Resolution".toLabel()) innerTable.add("Resolution".toLabel())

View File

@ -66,7 +66,9 @@ class UnitContextMenu(val tileMapHolder: WorldMapHolder, val selectedUnit: MapUn
val tileToMoveTo: TileInfo val tileToMoveTo: TileInfo
try { try {
tileToMoveTo = selectedUnit.movement.getTileToMoveToThisTurn(targetTile) tileToMoveTo = selectedUnit.movement.getTileToMoveToThisTurn(targetTile)
}catch (ex:Exception){ return@thread } // can't move here } catch (ex: Exception) {
return@thread
} // can't move here
Gdx.app.postRunnable { Gdx.app.postRunnable {
try { try {
@ -86,8 +88,7 @@ class UnitContextMenu(val tileMapHolder: WorldMapHolder, val selectedUnit: MapUn
tileMapHolder.worldScreen.shouldUpdate = true tileMapHolder.worldScreen.shouldUpdate = true
tileMapHolder.unitActionOverlay?.remove() tileMapHolder.unitActionOverlay?.remove()
} catch (e: Exception) { } catch (e: Exception) {}
}
} }
} }