Merged save map functionality into load map screen - now map saving is more streamlined!

This commit is contained in:
Yair Morgenstern
2021-02-28 23:56:09 +02:00
parent 29360938c6
commit a390df8b36
5 changed files with 167 additions and 168 deletions

View File

@ -6,7 +6,6 @@ import com.badlogic.gdx.scenes.scene2d.Touchable
import com.badlogic.gdx.scenes.scene2d.actions.Actions
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.utils.SerializationException
import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver
import com.unciv.logic.GameStarter
@ -158,7 +157,7 @@ class MainMenuScreen: CameraStageBaseScreen() {
add(newMapButton).row()
val loadMapButton = screen.getTableBlock("Load map", "OtherIcons/Load") {
val loadMapScreen = LoadMapScreen(null)
val loadMapScreen = SaveAndLoadMapScreen(null)
loadMapScreen.closeButton.isVisible = true
loadMapScreen.closeButton.onClick {
screen.game.setScreen(MainMenuScreen())

View File

@ -1,110 +0,0 @@
package com.unciv.ui.mapeditor
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.unciv.MainMenuScreen
import com.unciv.UncivGame
import com.unciv.logic.MapSaver
import com.unciv.logic.map.TileMap
import com.unciv.models.translations.tr
import com.unciv.ui.pickerscreens.PickerScreen
import com.unciv.ui.saves.Gzip
import com.unciv.ui.utils.*
import kotlin.concurrent.thread
import com.unciv.ui.utils.AutoScrollPane as ScrollPane
class LoadMapScreen(previousMap: TileMap?) : PickerScreen() {
var chosenMap: FileHandle? = null
val deleteButton = "Delete map".toTextButton()
val mapsTable = Table().apply { defaults().pad(10f) }
init {
rightSideButton.setText("Load map".tr())
rightSideButton.onClick {
thread {
Gdx.app.postRunnable {
val popup = Popup(this)
popup.addGoodSizedLabel("Loading...")
popup.open()
}
val map = MapSaver.loadMap(chosenMap!!)
Gdx.app.postRunnable {
Gdx.input.inputProcessor = null // This is to stop ANRs happening here, until the map editor screen sets up.
UncivGame.Current.setScreen(MapEditorScreen(map))
dispose()
}
}
}
topTable.add(ScrollPane(mapsTable)).height(stage.height * 2 / 3)
.maxWidth(stage.width / 2)
val rightSideTable = Table().apply { defaults().pad(10f) }
val downloadMapButton = "Download map".toTextButton()
downloadMapButton.onClick {
MapDownloadPopup(this).open()
}
rightSideTable.add(downloadMapButton).row()
rightSideTable.addSeparator()
val loadFromClipboardButton = "Load copied data".toTextButton()
val couldNotLoadMapLabel = "Could not load map!".toLabel(Color.RED).apply { isVisible = false }
loadFromClipboardButton.onClick {
try {
val clipboardContentsString = Gdx.app.clipboard.contents.trim()
val decoded = Gzip.unzip(clipboardContentsString)
val loadedMap = MapSaver.mapFromJson(decoded)
UncivGame.Current.setScreen(MapEditorScreen(loadedMap))
} catch (ex: Exception) {
couldNotLoadMapLabel.isVisible = true
}
}
rightSideTable.add(loadFromClipboardButton).row()
rightSideTable.add(couldNotLoadMapLabel).row()
deleteButton.onClick {
YesNoPopup("Are you sure you want to delete this map?", {
chosenMap!!.delete()
UncivGame.Current.setScreen(LoadMapScreen(previousMap))
}, this).open()
}
rightSideTable.add(deleteButton).row()
topTable.add(rightSideTable)
setDefaultCloseAction(MainMenuScreen())
update()
}
fun update() {
chosenMap = null
deleteButton.disable()
deleteButton.color = Color.RED
deleteButton.setText("Delete map".tr())
rightSideButton.setText("Load map".tr())
mapsTable.clear()
for (map in MapSaver.getMaps()) {
val loadMapButton = TextButton(map.name(), skin)
loadMapButton.onClick {
for (cell in mapsTable.cells) cell.actor.color = Color.WHITE
loadMapButton.color = Color.BLUE
rightSideButton.enable()
chosenMap = map
deleteButton.enable()
deleteButton.color = Color.RED
}
mapsTable.add(loadMapButton).row()
}
}
}

View File

@ -12,7 +12,7 @@ import com.unciv.ui.worldscreen.mainmenu.DropBox
import kotlin.concurrent.thread
import com.unciv.ui.utils.AutoScrollPane as ScrollPane
class MapDownloadPopup(loadMapScreen: LoadMapScreen): Popup(loadMapScreen) {
class MapDownloadPopup(loadMapScreen: SaveAndLoadMapScreen): Popup(loadMapScreen) {
private val contentTable = Table()
private val header = Table()
private val listOfMaps = mutableListOf<TextButton>()

View File

@ -1,34 +1,19 @@
package com.unciv.ui.mapeditor
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.scenes.scene2d.ui.TextField
import com.badlogic.gdx.utils.Json
import com.unciv.Constants
import com.unciv.MainMenuScreen
import com.unciv.UncivGame
import com.unciv.logic.MapSaver
import com.unciv.logic.map.MapType
import com.unciv.logic.map.RoadStatus
import com.unciv.logic.map.TileMap
import com.unciv.models.metadata.Player
import com.unciv.ui.saves.Gzip
import com.unciv.ui.utils.*
import com.unciv.ui.worldscreen.mainmenu.DropBox
import kotlin.concurrent.thread
import com.unciv.ui.utils.Popup
import com.unciv.ui.utils.onClick
import com.unciv.ui.utils.toTextButton
class MapEditorMenuPopup(var mapEditorScreen: MapEditorScreen): Popup(mapEditorScreen){
private val mapNameEditor: TextField = TextField(mapEditorScreen.mapName, skin)
init {
mapNameEditor.textFieldFilter = TextField.TextFieldFilter { _, char -> char != '\\' && char != '/' }
add(mapNameEditor).fillX().row()
mapNameEditor.selectAll()
mapNameEditor.maxLength = 240 // A few under max for most filesystems
mapEditorScreen.stage.keyboardFocus = mapNameEditor
addNewMapButton()
addSaveMapButton()
addCopyMapAsTextButton()
addLoadMapButton()
addExitMapEditorButton()
addCloseOptionsButton()
@ -45,50 +30,15 @@ class MapEditorMenuPopup(var mapEditorScreen: MapEditorScreen): Popup(mapEditorS
private fun Popup.addSaveMapButton() {
val saveMapButton = "Save map".toTextButton()
saveMapButton.onClick {
mapEditorScreen.tileMap.mapParameters.name = mapEditorScreen.mapName
mapEditorScreen.tileMap.mapParameters.type = MapType.custom
thread(name = "SaveMap") {
try {
MapSaver.saveMap(mapEditorScreen.mapName, mapEditorScreen.tileMap)
close()
Gdx.app.postRunnable {
ToastPopup("Map saved", mapEditorScreen) // todo - add this text to translations
}
} catch (ex: Exception) {
ex.printStackTrace()
Gdx.app.postRunnable {
val cantLoadGamePopup = Popup(mapEditorScreen)
cantLoadGamePopup.addGoodSizedLabel("It looks like your map can't be saved!").row()
cantLoadGamePopup.addCloseButton()
cantLoadGamePopup.open(force = true)
}
}
}
mapEditorScreen.game.setScreen(SaveAndLoadMapScreen(mapEditorScreen.tileMap, true))
}
saveMapButton.isEnabled = mapNameEditor.text.isNotEmpty()
add(saveMapButton).row()
mapNameEditor.addListener {
mapEditorScreen.mapName = mapNameEditor.text
saveMapButton.isEnabled = mapNameEditor.text.isNotEmpty()
true
}
}
private fun Popup.addCopyMapAsTextButton() {
val copyMapAsTextButton = "Copy to clipboard".toTextButton()
copyMapAsTextButton.onClick {
val json = Json().toJson(mapEditorScreen.tileMap)
val base64Gzip = Gzip.zip(json)
Gdx.app.clipboard.contents = base64Gzip
}
add(copyMapAsTextButton).row()
}
private fun Popup.addLoadMapButton() {
val loadMapButton = "Load map".toTextButton()
loadMapButton.onClick {
UncivGame.Current.setScreen(LoadMapScreen(mapEditorScreen.tileMap))
UncivGame.Current.setScreen(SaveAndLoadMapScreen(mapEditorScreen.tileMap))
}
add(loadMapButton).row()
}

View File

@ -0,0 +1,160 @@
package com.unciv.ui.mapeditor
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.scenes.scene2d.ui.TextField
import com.badlogic.gdx.utils.Json
import com.unciv.MainMenuScreen
import com.unciv.UncivGame
import com.unciv.logic.MapSaver
import com.unciv.logic.map.MapType
import com.unciv.logic.map.TileMap
import com.unciv.models.translations.tr
import com.unciv.ui.pickerscreens.PickerScreen
import com.unciv.ui.saves.Gzip
import com.unciv.ui.utils.*
import kotlin.concurrent.thread
import com.unciv.ui.utils.AutoScrollPane as ScrollPane
class SaveAndLoadMapScreen(mapToSave: TileMap?, save:Boolean = false) : PickerScreen() {
var chosenMap: FileHandle? = null
val deleteButton = "Delete map".toTextButton()
val mapsTable = Table().apply { defaults().pad(10f) }
val mapNameTextField = TextField("", skin).apply { maxLength = 100 }
init {
if(save) {
rightSideButton.setText("Save map".tr())
rightSideButton.onClick {
mapToSave!!.mapParameters.name = mapNameTextField.text
mapToSave.mapParameters.type = MapType.custom
thread(name = "SaveMap") {
try {
MapSaver.saveMap(mapNameTextField.text, mapToSave)
Gdx.app.postRunnable {
Gdx.input.inputProcessor = null // This is to stop ANRs happening here, until the map editor screen sets up.
UncivGame.Current.setScreen(MapEditorScreen(mapToSave))
dispose()
}
} catch (ex: Exception) {
ex.printStackTrace()
Gdx.app.postRunnable {
val cantLoadGamePopup = Popup(this)
cantLoadGamePopup.addGoodSizedLabel("It looks like your map can't be saved!").row()
cantLoadGamePopup.addCloseButton()
cantLoadGamePopup.open(force = true)
}
}
}
}
}
else {
rightSideButton.setText("Load map".tr())
rightSideButton.onClick {
thread {
Gdx.app.postRunnable {
val popup = Popup(this)
popup.addGoodSizedLabel("Loading...")
popup.open()
}
val map = MapSaver.loadMap(chosenMap!!)
Gdx.app.postRunnable {
Gdx.input.inputProcessor = null // This is to stop ANRs happening here, until the map editor screen sets up.
UncivGame.Current.setScreen(MapEditorScreen(map))
dispose()
}
}
}
}
topTable.add(ScrollPane(mapsTable)).height(stage.height * 2 / 3)
.maxWidth(stage.width / 2)
val rightSideTable = Table().apply { defaults().pad(10f) }
if(save) {
mapNameTextField.textFieldFilter = TextField.TextFieldFilter { _, char -> char != '\\' && char != '/' }
mapNameTextField.text = "My new map"
rightSideTable.add(mapNameTextField).width(300f).pad(10f)
}
else {
val downloadMapButton = "Download map".toTextButton()
downloadMapButton.onClick {
MapDownloadPopup(this).open()
}
rightSideTable.add(downloadMapButton).row()
}
rightSideTable.addSeparator()
if(save) {
val copyMapAsTextButton = "Copy to clipboard".toTextButton()
copyMapAsTextButton.onClick {
val json = Json().toJson(mapToSave)
val base64Gzip = Gzip.zip(json)
Gdx.app.clipboard.contents = base64Gzip
}
rightSideTable.add(copyMapAsTextButton).row()
}
else {
val loadFromClipboardButton = "Load copied data".toTextButton()
val couldNotLoadMapLabel = "Could not load map!".toLabel(Color.RED).apply { isVisible = false }
loadFromClipboardButton.onClick {
try {
val clipboardContentsString = Gdx.app.clipboard.contents.trim()
val decoded = Gzip.unzip(clipboardContentsString)
val loadedMap = MapSaver.mapFromJson(decoded)
UncivGame.Current.setScreen(MapEditorScreen(loadedMap))
} catch (ex: Exception) {
couldNotLoadMapLabel.isVisible = true
}
}
rightSideTable.add(loadFromClipboardButton).row()
rightSideTable.add(couldNotLoadMapLabel).row()
}
deleteButton.onClick {
YesNoPopup("Are you sure you want to delete this map?", {
chosenMap!!.delete()
UncivGame.Current.setScreen(SaveAndLoadMapScreen(mapToSave))
}, this).open()
}
rightSideTable.add(deleteButton).row()
topTable.add(rightSideTable)
setDefaultCloseAction(MainMenuScreen())
update()
}
fun update() {
chosenMap = null
deleteButton.disable()
deleteButton.color = Color.RED
deleteButton.setText("Delete map".tr())
// rightSideButton.setText("Load map".tr())
mapsTable.clear()
for (map in MapSaver.getMaps()) {
val loadMapButton = TextButton(map.name(), skin)
loadMapButton.onClick {
for (cell in mapsTable.cells) cell.actor.color = Color.WHITE
loadMapButton.color = Color.BLUE
rightSideButton.enable()
chosenMap = map
mapNameTextField.text = map.name()
deleteButton.enable()
deleteButton.color = Color.RED
}
mapsTable.add(loadMapButton).row()
}
}
}