Moved the map editor into the main menu as well

This commit is contained in:
Yair Morgenstern
2020-04-21 00:07:05 +03:00
parent bb3a84fbe7
commit 661ac08e5c
4 changed files with 128 additions and 126 deletions

View File

@ -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)
}
}

View File

@ -5,22 +5,14 @@ import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input import com.badlogic.gdx.Input
import com.badlogic.gdx.audio.Music 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.actions.Actions
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.utils.Align import com.badlogic.gdx.utils.Align
import com.unciv.logic.GameInfo import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver 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.metadata.GameSettings
import com.unciv.models.ruleset.RulesetCache import com.unciv.models.ruleset.RulesetCache
import com.unciv.models.translations.Translations import com.unciv.models.translations.Translations
import com.unciv.ui.LanguagePickerScreen 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.utils.*
import com.unciv.ui.worldscreen.WorldScreen import com.unciv.ui.worldscreen.WorldScreen
import java.util.* 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)
}
}

View File

@ -4,7 +4,6 @@ import com.unciv.ui.utils.AutoScrollPane as ScrollPane
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.* import com.badlogic.gdx.scenes.scene2d.ui.*
import com.unciv.UncivGame
import com.unciv.logic.GameInfo import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver import com.unciv.logic.GameSaver
import com.unciv.logic.IdChecker import com.unciv.logic.IdChecker
@ -15,7 +14,7 @@ import com.unciv.ui.worldscreen.mainmenu.OnlineMultiplayer
import java.util.* import java.util.*
import kotlin.concurrent.thread import kotlin.concurrent.thread
class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() { class MultiplayerScreen(previousScreen: CameraStageBaseScreen) : PickerScreen() {
private lateinit var selectedGame: GameInfo private lateinit var selectedGame: GameInfo
private lateinit var selectedGameName: String private lateinit var selectedGameName: String
@ -36,7 +35,7 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() {
private val refreshButton = TextButton(refreshText, skin) private val refreshButton = TextButton(refreshText, skin)
init { init {
setDefaultCloseAction() setDefaultCloseAction(previousScreen)
//Help Button Setup //Help Button Setup
val tab = Table() val tab = Table()
@ -77,7 +76,7 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() {
//rightTable Setup //rightTable Setup
copyUserIdButton.onClick { copyUserIdButton.onClick {
Gdx.app.clipboard.contents = uncivGame.settings.userId Gdx.app.clipboard.contents = game.settings.userId
ResponsePopup("UserID copied to clipboard".tr(), this) ResponsePopup("UserID copied to clipboard".tr(), this)
} }
rightSideTable.add(copyUserIdButton).pad(10f).padBottom(30f).row() rightSideTable.add(copyUserIdButton).pad(10f).padBottom(30f).row()
@ -89,14 +88,14 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() {
rightSideTable.add(copyGameIdButton).pad(10f).row() rightSideTable.add(copyGameIdButton).pad(10f).row()
editButton.onClick { 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 //game must be unselected in case the game gets deleted inside the EditScreen
unselectGame() unselectGame()
} }
rightSideTable.add(editButton).pad(10f).row() rightSideTable.add(editButton).pad(10f).row()
addGameButton.onClick { addGameButton.onClick {
uncivGame.setScreen(AddMultiplayerGameScreen(this)) game.setScreen(AddMultiplayerGameScreen(this))
} }
rightSideTable.add(addGameButton).pad(10f).padBottom(30f).row() 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 //the game will be downloaded opon joining it anyway
private fun joinMultiplaerGame(){ private fun joinMultiplaerGame(){
try { try {
uncivGame.loadGame(selectedGame) game.loadGame(selectedGame)
} catch (ex: Exception) { } catch (ex: Exception) {
val errorPopup = Popup(this) val errorPopup = Popup(this)
errorPopup.addGoodSizedLabel("Could not download game!".tr()) errorPopup.addGoodSizedLabel("Could not download game!".tr())
@ -179,7 +178,7 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() {
fun reloadGameListUI(){ fun reloadGameListUI(){
val leftSubTable = Table() val leftSubTable = Table()
val gameSaver = GameSaver val gameSaver = GameSaver
var savedGames : List<String>? val savedGames : List<String>?
try { try {
savedGames = gameSaver.getSaves(true) 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 //Add games to list so saves don't have to be loaded as Files so often
if (!gameIsAlreadySavedAsMultiplayer(game.gameId)) if (!gameIsAlreadySavedAsMultiplayer(game.gameId))
multiplayerGameList.put(game.gameId, gameSaveName) multiplayerGameList[game.gameId] = gameSaveName
if (isUsersTurn(game)) { if (isUsersTurn(game)) {
gameTable.add(ImageGetter.getNationIndicator(game.currentPlayerCiv.nation, 45f)) 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 //Adds a Button to add the currently running game to multiplayerGameList
private fun addCurrentGameButton(){ private fun addCurrentGameButton(){
val currentlyRunningGame = uncivGame.gameInfo val currentlyRunningGame = game.gameInfo
if (!currentlyRunningGame.gameParameters.isOnlineMultiplayer || gameIsAlreadySavedAsMultiplayer(currentlyRunningGame.gameId)) if (!currentlyRunningGame.gameParameters.isOnlineMultiplayer || gameIsAlreadySavedAsMultiplayer(currentlyRunningGame.gameId))
return return
@ -300,8 +299,8 @@ class MultiplayerScreen(val uncivGame: UncivGame) : PickerScreen() {
} }
//check if its the users turn //check if its the users turn
private fun isUsersTurn(game: GameInfo) : Boolean{ private fun isUsersTurn(gameInfo: GameInfo) : Boolean{
return (game.currentPlayerCiv.playerId == uncivGame.settings.userId) return (gameInfo.currentPlayerCiv.playerId == game.settings.userId)
} }
fun removeFromList(gameId: String){ fun removeFromList(gameId: String){
@ -328,7 +327,7 @@ class EditMultiplayerGameInfoScreen(game: GameInfo, gameName: String, backScreen
askPopup.addButton("Yes"){ askPopup.addButton("Yes"){
try { try {
GameSaver.deleteSave(gameName, true) GameSaver.deleteSave(gameName, true)
backScreen.uncivGame.setScreen(backScreen) backScreen.game.setScreen(backScreen)
backScreen.reloadGameListUI() backScreen.reloadGameListUI()
}catch (ex: Exception) { }catch (ex: Exception) {
askPopup.close() askPopup.close()
@ -346,7 +345,7 @@ class EditMultiplayerGameInfoScreen(game: GameInfo, gameName: String, backScreen
//CloseButton Setup //CloseButton Setup
closeButton.setText("Back".tr()) closeButton.setText("Back".tr())
closeButton.onClick { closeButton.onClick {
backScreen.uncivGame.setScreen(backScreen) backScreen.game.setScreen(backScreen)
} }
//RightSideButton Setup //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 //using addMultiplayerGame will download the game from Dropbox so the descriptionLabel displays the right things
backScreen.addMultiplayerGame(game.gameId, textField.text) backScreen.addMultiplayerGame(game.gameId, textField.text)
GameSaver.deleteSave(gameName, true) GameSaver.deleteSave(gameName, true)
backScreen.uncivGame.setScreen(backScreen) backScreen.game.setScreen(backScreen)
backScreen.reloadGameListUI() backScreen.reloadGameListUI()
}catch (ex: Exception) { }catch (ex: Exception) {
val errorPopup = Popup(this) val errorPopup = Popup(this)
@ -393,7 +392,7 @@ class AddMultiplayerGameScreen(backScreen: MultiplayerScreen) : PickerScreen(){
//CloseButton Setup //CloseButton Setup
closeButton.setText("Back".tr()) closeButton.setText("Back".tr())
closeButton.onClick { closeButton.onClick {
backScreen.uncivGame.setScreen(backScreen) backScreen.game.setScreen(backScreen)
} }
//RightSideButton Setup //RightSideButton Setup
@ -408,7 +407,7 @@ class AddMultiplayerGameScreen(backScreen: MultiplayerScreen) : PickerScreen(){
} }
backScreen.addMultiplayerGame(gameIDTextField.text.trim(), gameNameTextField.text.trim()) backScreen.addMultiplayerGame(gameIDTextField.text.trim(), gameNameTextField.text.trim())
backScreen.uncivGame.setScreen(backScreen) backScreen.game.setScreen(backScreen)
} }
} }
} }

View File

@ -2,6 +2,7 @@ package com.unciv.ui.worldscreen.mainmenu
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.unciv.Constants import com.unciv.Constants
import com.unciv.MenuScreen
import com.unciv.UncivGame import com.unciv.UncivGame
import com.unciv.models.translations.tr import com.unciv.models.translations.tr
import com.unciv.ui.CivilopediaScreen import com.unciv.ui.CivilopediaScreen
@ -22,8 +23,8 @@ class WorldScreenMenuPopup(val worldScreen: WorldScreen) : Popup(worldScreen) {
init { init {
val width = 200f val width = 200f
val height = 30f val height = 30f
addSquareButton("Map editor".tr()){ addSquareButton("Main menu".tr()){
openMapEditorPopup() worldScreen.game.setScreen(MenuScreen())
close() close()
}.size(width,height) }.size(width,height)
addSeparator() addSeparator()
@ -34,29 +35,12 @@ class WorldScreenMenuPopup(val worldScreen: WorldScreen) : Popup(worldScreen) {
}.size(width,height) }.size(width,height)
addSeparator() addSeparator()
addSquareButton("Load game".tr()){
worldScreen.game.setScreen(LoadGameScreen(worldScreen))
close()
}.size(width,height)
addSeparator()
addSquareButton("Save game".tr()){ addSquareButton("Save game".tr()){
worldScreen.game.setScreen(SaveGameScreen()) worldScreen.game.setScreen(SaveGameScreen())
close() close()
}.size(width,height) }.size(width,height)
addSeparator() 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()){ addSquareButton("Victory status".tr()){
worldScreen.game.setScreen(VictoryScreen(worldScreen)) worldScreen.game.setScreen(VictoryScreen(worldScreen))
close() 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)
}
} }