diff --git a/core/src/com/unciv/MenuScreen.kt b/core/src/com/unciv/MenuScreen.kt new file mode 100644 index 0000000000..b848a725ea --- /dev/null +++ b/core/src/com/unciv/MenuScreen.kt @@ -0,0 +1,109 @@ +package com.unciv + +import com.badlogic.gdx.scenes.scene2d.Touchable +import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane +import com.badlogic.gdx.scenes.scene2d.ui.Table +import com.badlogic.gdx.utils.Align +import com.unciv.logic.GameSaver +import com.unciv.logic.GameStarter +import com.unciv.logic.map.MapParameters +import com.unciv.models.metadata.GameParameters +import com.unciv.models.translations.tr +import com.unciv.ui.MultiplayerScreen +import com.unciv.ui.mapeditor.LoadMapScreen +import com.unciv.ui.mapeditor.NewMapScreen +import com.unciv.ui.newgamescreen.NewGameScreen +import com.unciv.ui.saves.LoadGameScreen +import com.unciv.ui.utils.* + +class MenuScreen: CameraStageBaseScreen() { + val autosave = "Autosave" + + private fun getTableBlock(text: String, function: () -> Unit): Table { + val table = Table() + table.background = ImageGetter.getBackground(colorFromRGB(11, 135, 133)) + table.add(text.toLabel().setFontSize(30).apply { setAlignment(Align.center) }).pad(40f).width(200f) + table.touchable= Touchable.enabled + table.onClick(function) + table.pack() + return table + } + + init { + val table = Table().apply { defaults().pad(10f) } + val autosaveGame = GameSaver.getSave(autosave, false) + if (autosaveGame.exists()) { + val resumeTable = getTableBlock("Resume") { autoLoadGame() } + table.add(resumeTable).row() + } + + val quickstartTable = getTableBlock("Quickstart") { startNewGame() } + table.add(quickstartTable).row() + + val newGameButton = getTableBlock("Start new game") { game.setScreen(NewGameScreen(this)) } + table.add(newGameButton).row() + + if (GameSaver.getSaves(false).any()) { + val loadGameTable = getTableBlock("Load game") { game.setScreen(LoadGameScreen(this)) } + table.add(loadGameTable).row() + } + + val multiplayerTable = getTableBlock("Multiplayer") { game.setScreen(MultiplayerScreen(this)) } + table.add(multiplayerTable).row() + + val mapEditorScreenTable = getTableBlock("Map editor") { openMapEditorPopup() } + table.add(mapEditorScreenTable) + + + table.pack() + val scroll = ScrollPane(table) + scroll.setSize(table.width, stage.height * 0.8f) + scroll.center(stage) + scroll.setOverscroll(false, false) + stage.addActor(scroll) + } + + + /** Shows the [Popup] with the map editor initialization options */ + private fun openMapEditorPopup() { + + val mapEditorPopup = Popup(this) + + mapEditorPopup.addGoodSizedLabel("Map editor".tr()).row() + + // Create a new map + mapEditorPopup.addButton("New map") { + game.setScreen(NewMapScreen()) + mapEditorPopup.close() + } + + // Load the map + mapEditorPopup.addButton("Load map") { + val loadMapScreen = LoadMapScreen(null) + loadMapScreen.closeButton.isVisible = true + loadMapScreen.closeButton.onClick { + game.setWorldScreen() + loadMapScreen.dispose() + } + game.setScreen(loadMapScreen) + mapEditorPopup.close() + } + + mapEditorPopup.addCloseButton() + mapEditorPopup.open(force = true) + } + + fun autoLoadGame() { + try { + game.loadGame(autosave) + } catch (ex: Exception) { // silent fail if we can't read the autosave + ResponsePopup("Cannot resume game!", this) + } + } + + fun startNewGame() { + val newGame = GameStarter.startNewGame(GameParameters().apply { difficulty = "Chieftain" }, MapParameters()) + game.loadGame(newGame) + } + +} \ No newline at end of file diff --git a/core/src/com/unciv/UncivGame.kt b/core/src/com/unciv/UncivGame.kt index ae6ba0d2c4..8878aa1698 100644 --- a/core/src/com/unciv/UncivGame.kt +++ b/core/src/com/unciv/UncivGame.kt @@ -5,22 +5,14 @@ import com.badlogic.gdx.Game import com.badlogic.gdx.Gdx import com.badlogic.gdx.Input import com.badlogic.gdx.audio.Music -import com.badlogic.gdx.graphics.Color -import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.actions.Actions -import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align import com.unciv.logic.GameInfo import com.unciv.logic.GameSaver -import com.unciv.logic.GameStarter -import com.unciv.logic.map.MapParameters -import com.unciv.models.metadata.GameParameters import com.unciv.models.metadata.GameSettings import com.unciv.models.ruleset.RulesetCache import com.unciv.models.translations.Translations import com.unciv.ui.LanguagePickerScreen -import com.unciv.ui.newgamescreen.NewGameScreen -import com.unciv.ui.saves.LoadGameScreen import com.unciv.ui.utils.* import com.unciv.ui.worldscreen.WorldScreen import java.util.* @@ -197,57 +189,3 @@ class LoadingScreen:CameraStageBaseScreen() { } -class MenuScreen:CameraStageBaseScreen() { - val autosave = "Autosave" - - fun getTableBlock(text:String): Table { - val table = Table() - table.background = ImageGetter.getBackground(colorFromRGB(11, 135, 133)) - table.add(text.toLabel().setFontSize(30).apply { setAlignment(Align.center) }).pad(40f).width(200f) - table.touchable=Touchable.enabled - table.pack() - return table - } - - init { - val table = Table().apply { defaults().pad(10f) } - val autosaveGame = GameSaver.getSave(autosave, false) - if (autosaveGame.exists()) { - val resumeTable = getTableBlock("Resume") - resumeTable.onClick { autoLoadGame() } - table.add(resumeTable).row() - } - - val quickstartTable = getTableBlock("Quickstart") - quickstartTable.onClick { startNewGame() } - table.add(quickstartTable).row() - - val newGameButton = getTableBlock("Start new game") - newGameButton.onClick { UncivGame.Current.setScreen(NewGameScreen(this)) } - table.add(newGameButton).row() - - if (GameSaver.getSaves(false).any()) { - val loadGameTable = getTableBlock("Load game") - loadGameTable.onClick { UncivGame.Current.setScreen(LoadGameScreen(this)) } - table.add(loadGameTable).row() - } - - table.pack() - table.center(stage) - stage.addActor(table) - } - - fun autoLoadGame() { - try { - game.loadGame(autosave) - } catch (ex: Exception) { // silent fail if we can't read the autosave - ResponsePopup("Cannot resume game!", this) - } - } - - fun startNewGame() { - val newGame = GameStarter.startNewGame(GameParameters().apply { difficulty = "Chieftain" }, MapParameters()) - game.loadGame(newGame) - } - -} \ No newline at end of file diff --git a/core/src/com/unciv/ui/MultiplayerScreen.kt b/core/src/com/unciv/ui/MultiplayerScreen.kt index a3a0023f2a..526b28bd70 100644 --- a/core/src/com/unciv/ui/MultiplayerScreen.kt +++ b/core/src/com/unciv/ui/MultiplayerScreen.kt @@ -4,7 +4,6 @@ import com.unciv.ui.utils.AutoScrollPane as ScrollPane import com.badlogic.gdx.Gdx import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.ui.* -import com.unciv.UncivGame import com.unciv.logic.GameInfo import com.unciv.logic.GameSaver import com.unciv.logic.IdChecker @@ -15,7 +14,7 @@ import com.unciv.ui.worldscreen.mainmenu.OnlineMultiplayer import java.util.* import kotlin.concurrent.thread -class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { +class MultiplayerScreen(previousScreen: CameraStageBaseScreen) : PickerScreen() { private lateinit var selectedGame: GameInfo private lateinit var selectedGameName: String @@ -36,7 +35,7 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { private val refreshButton = TextButton(refreshText, skin) init { - setDefaultCloseAction() + setDefaultCloseAction(previousScreen) //Help Button Setup val tab = Table() @@ -77,7 +76,7 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { //rightTable Setup copyUserIdButton.onClick { - Gdx.app.clipboard.contents = uncivGame.settings.userId + Gdx.app.clipboard.contents = game.settings.userId ResponsePopup("UserID copied to clipboard".tr(), this) } rightSideTable.add(copyUserIdButton).pad(10f).padBottom(30f).row() @@ -89,14 +88,14 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { rightSideTable.add(copyGameIdButton).pad(10f).row() editButton.onClick { - uncivGame.setScreen(EditMultiplayerGameInfoScreen(selectedGame, selectedGameName, this)) + game.setScreen(EditMultiplayerGameInfoScreen(selectedGame, selectedGameName, this)) //game must be unselected in case the game gets deleted inside the EditScreen unselectGame() } rightSideTable.add(editButton).pad(10f).row() addGameButton.onClick { - uncivGame.setScreen(AddMultiplayerGameScreen(this)) + game.setScreen(AddMultiplayerGameScreen(this)) } rightSideTable.add(addGameButton).pad(10f).padBottom(30f).row() @@ -161,7 +160,7 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { //the game will be downloaded opon joining it anyway private fun joinMultiplaerGame(){ try { - uncivGame.loadGame(selectedGame) + game.loadGame(selectedGame) } catch (ex: Exception) { val errorPopup = Popup(this) errorPopup.addGoodSizedLabel("Could not download game!".tr()) @@ -179,7 +178,7 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { fun reloadGameListUI(){ val leftSubTable = Table() val gameSaver = GameSaver - var savedGames : List? + val savedGames : List? try { savedGames = gameSaver.getSaves(true) @@ -199,7 +198,7 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { //Add games to list so saves don't have to be loaded as Files so often if (!gameIsAlreadySavedAsMultiplayer(game.gameId)) - multiplayerGameList.put(game.gameId, gameSaveName) + multiplayerGameList[game.gameId] = gameSaveName if (isUsersTurn(game)) { gameTable.add(ImageGetter.getNationIndicator(game.currentPlayerCiv.nation, 45f)) @@ -267,7 +266,7 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { //Adds a Button to add the currently running game to multiplayerGameList private fun addCurrentGameButton(){ - val currentlyRunningGame = uncivGame.gameInfo + val currentlyRunningGame = game.gameInfo if (!currentlyRunningGame.gameParameters.isOnlineMultiplayer || gameIsAlreadySavedAsMultiplayer(currentlyRunningGame.gameId)) return @@ -300,8 +299,8 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { } //check if its the users turn - private fun isUsersTurn(game: GameInfo) : Boolean{ - return (game.currentPlayerCiv.playerId == uncivGame.settings.userId) + private fun isUsersTurn(gameInfo: GameInfo) : Boolean{ + return (gameInfo.currentPlayerCiv.playerId == game.settings.userId) } fun removeFromList(gameId: String){ @@ -328,7 +327,7 @@ class EditMultiplayerGameInfoScreen(game: GameInfo, gameName: String, backScreen askPopup.addButton("Yes"){ try { GameSaver.deleteSave(gameName, true) - backScreen.uncivGame.setScreen(backScreen) + backScreen.game.setScreen(backScreen) backScreen.reloadGameListUI() }catch (ex: Exception) { askPopup.close() @@ -346,7 +345,7 @@ class EditMultiplayerGameInfoScreen(game: GameInfo, gameName: String, backScreen //CloseButton Setup closeButton.setText("Back".tr()) closeButton.onClick { - backScreen.uncivGame.setScreen(backScreen) + backScreen.game.setScreen(backScreen) } //RightSideButton Setup @@ -359,7 +358,7 @@ class EditMultiplayerGameInfoScreen(game: GameInfo, gameName: String, backScreen //using addMultiplayerGame will download the game from Dropbox so the descriptionLabel displays the right things backScreen.addMultiplayerGame(game.gameId, textField.text) GameSaver.deleteSave(gameName, true) - backScreen.uncivGame.setScreen(backScreen) + backScreen.game.setScreen(backScreen) backScreen.reloadGameListUI() }catch (ex: Exception) { val errorPopup = Popup(this) @@ -393,7 +392,7 @@ class AddMultiplayerGameScreen(backScreen: MultiplayerScreen) : PickerScreen(){ //CloseButton Setup closeButton.setText("Back".tr()) closeButton.onClick { - backScreen.uncivGame.setScreen(backScreen) + backScreen.game.setScreen(backScreen) } //RightSideButton Setup @@ -408,7 +407,7 @@ class AddMultiplayerGameScreen(backScreen: MultiplayerScreen) : PickerScreen(){ } backScreen.addMultiplayerGame(gameIDTextField.text.trim(), gameNameTextField.text.trim()) - backScreen.uncivGame.setScreen(backScreen) + backScreen.game.setScreen(backScreen) } } } diff --git a/core/src/com/unciv/ui/worldscreen/mainmenu/WorldScreenMenuPopup.kt b/core/src/com/unciv/ui/worldscreen/mainmenu/WorldScreenMenuPopup.kt index af4d486724..108312ff9c 100644 --- a/core/src/com/unciv/ui/worldscreen/mainmenu/WorldScreenMenuPopup.kt +++ b/core/src/com/unciv/ui/worldscreen/mainmenu/WorldScreenMenuPopup.kt @@ -2,6 +2,7 @@ package com.unciv.ui.worldscreen.mainmenu import com.badlogic.gdx.Gdx import com.unciv.Constants +import com.unciv.MenuScreen import com.unciv.UncivGame import com.unciv.models.translations.tr import com.unciv.ui.CivilopediaScreen @@ -22,8 +23,8 @@ class WorldScreenMenuPopup(val worldScreen: WorldScreen) : Popup(worldScreen) { init { val width = 200f val height = 30f - addSquareButton("Map editor".tr()){ - openMapEditorPopup() + addSquareButton("Main menu".tr()){ + worldScreen.game.setScreen(MenuScreen()) close() }.size(width,height) addSeparator() @@ -34,29 +35,12 @@ class WorldScreenMenuPopup(val worldScreen: WorldScreen) : Popup(worldScreen) { }.size(width,height) addSeparator() - addSquareButton("Load game".tr()){ - worldScreen.game.setScreen(LoadGameScreen(worldScreen)) - close() - }.size(width,height) - addSeparator() - addSquareButton("Save game".tr()){ worldScreen.game.setScreen(SaveGameScreen()) close() }.size(width,height) addSeparator() - addSquareButton("Start new game".tr()){ - worldScreen.game.setScreen(NewGameScreen(worldScreen, worldScreen.gameInfo)) - }.size(width,height) - addSeparator() - - addSquareButton("Multiplayer".tr()){ - worldScreen.game.setScreen(MultiplayerScreen(worldScreen.game)) - close() - }.size(width,height) - addSeparator() - addSquareButton("Victory status".tr()){ worldScreen.game.setScreen(VictoryScreen(worldScreen)) close() @@ -81,34 +65,6 @@ class WorldScreenMenuPopup(val worldScreen: WorldScreen) : Popup(worldScreen) { } - /** Shows the [Popup] with the map editor initialization options */ - private fun openMapEditorPopup() { - - close() - val mapEditorPopup = Popup(screen) - - mapEditorPopup.addGoodSizedLabel("Map editor".tr()).row() - - // Create a new map - mapEditorPopup.addButton("New map") { - worldScreen.game.setScreen(NewMapScreen()) - mapEditorPopup.close() - } - - // Load the map - mapEditorPopup.addButton("Load map") { - val loadMapScreen = LoadMapScreen(null) - loadMapScreen.closeButton.isVisible = true - loadMapScreen.closeButton.onClick { - worldScreen.game.setWorldScreen() - loadMapScreen.dispose() } - worldScreen.game.setScreen(loadMapScreen) - mapEditorPopup.close() - } - - mapEditorPopup.addCloseButton() - mapEditorPopup.open(force = true) - } }