From a390df8b360a0ab4ffb0973e7041621cb376aefe Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sun, 28 Feb 2021 23:56:09 +0200 Subject: [PATCH] Merged save map functionality into load map screen - now map saving is more streamlined! --- core/src/com/unciv/MainMenuScreen.kt | 3 +- .../com/unciv/ui/mapeditor/LoadMapScreen.kt | 110 ------------ .../unciv/ui/mapeditor/MapDownloadPopup.kt | 2 +- .../unciv/ui/mapeditor/MapEditorMenuPopup.kt | 60 +------ .../ui/mapeditor/SaveAndLoadMapScreen.kt | 160 ++++++++++++++++++ 5 files changed, 167 insertions(+), 168 deletions(-) delete mode 100644 core/src/com/unciv/ui/mapeditor/LoadMapScreen.kt create mode 100644 core/src/com/unciv/ui/mapeditor/SaveAndLoadMapScreen.kt diff --git a/core/src/com/unciv/MainMenuScreen.kt b/core/src/com/unciv/MainMenuScreen.kt index 8bae55c111..3072509c5b 100644 --- a/core/src/com/unciv/MainMenuScreen.kt +++ b/core/src/com/unciv/MainMenuScreen.kt @@ -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()) diff --git a/core/src/com/unciv/ui/mapeditor/LoadMapScreen.kt b/core/src/com/unciv/ui/mapeditor/LoadMapScreen.kt deleted file mode 100644 index f01442637b..0000000000 --- a/core/src/com/unciv/ui/mapeditor/LoadMapScreen.kt +++ /dev/null @@ -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() - } - } - -} - - diff --git a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt index ce7bcbbaa7..3b9beba4e6 100644 --- a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt +++ b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt @@ -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() diff --git a/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt b/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt index 1dce1b6f0b..adbd2b7ec9 100644 --- a/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt +++ b/core/src/com/unciv/ui/mapeditor/MapEditorMenuPopup.kt @@ -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() } diff --git a/core/src/com/unciv/ui/mapeditor/SaveAndLoadMapScreen.kt b/core/src/com/unciv/ui/mapeditor/SaveAndLoadMapScreen.kt new file mode 100644 index 0000000000..8311f6faff --- /dev/null +++ b/core/src/com/unciv/ui/mapeditor/SaveAndLoadMapScreen.kt @@ -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() + } + } + +} + +