New "Load map" screen, with load from clipboard and delete map options

This commit is contained in:
Yair Morgenstern
2019-08-25 23:26:50 +03:00
parent 17f41a549e
commit 3a04fe75eb
7 changed files with 135 additions and 102 deletions

View File

@ -21,8 +21,8 @@ android {
applicationId "com.unciv.app" applicationId "com.unciv.app"
minSdkVersion 14 minSdkVersion 14
targetSdkVersion 28 targetSdkVersion 28
versionCode 288 versionCode 289
versionName "2.19.7" versionName "2.19.8"
} }
// Had to add this crap for Travis to build, it wanted to sign the app // Had to add this crap for Travis to build, it wanted to sign the app

View File

@ -63,8 +63,14 @@ class GameSaver {
saveGame(gameInfoClone, "Autosave") saveGame(gameInfoClone, "Autosave")
// keep auto-saves for the last 10 turns for debugging purposes // keep auto-saves for the last 10 turns for debugging purposes
// eg turn 238 is saved as "Autosave-8" val newAutosaveFilename = saveFilesFolder + File.separator + "Autosave-${gameInfo.currentPlayer}-${gameInfoClone.turns}"
getSave("Autosave").copyTo(Gdx.files.local(saveFilesFolder + File.separator + "Autosave-${gameInfoClone.turns%10}")) getSave("Autosave").copyTo(Gdx.files.local(newAutosaveFilename))
val autosaves = getSaves().filter { it.startsWith("Autosave") }
while(autosaves.size>10){
val saveToDelete = autosaves.minBy { getSave(it).lastModified() }!!
deleteSave(saveToDelete)
}
// do this on main thread // do this on main thread
Gdx.app.postRunnable { Gdx.app.postRunnable {

View File

@ -17,6 +17,7 @@ class MapSaver(){
val unzippedJson = Gzip.unzip(gzippedString) val unzippedJson = Gzip.unzip(gzippedString)
return json().fromJson(TileMap::class.java, unzippedJson) return json().fromJson(TileMap::class.java, unzippedJson)
} }
fun deleteMap(mapName: String) = getMap(mapName).delete()
fun getMaps() = Gdx.files.local(mapsFolder).list().map { it.name() } fun getMaps() = Gdx.files.local(mapsFolder).list().map { it.name() }

View File

@ -1,6 +1,7 @@
package com.unciv.ui.mapeditor package com.unciv.ui.mapeditor
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane
import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.scenes.scene2d.ui.TextField import com.badlogic.gdx.scenes.scene2d.ui.TextField
@ -11,6 +12,7 @@ import com.unciv.logic.MapSaver
import com.unciv.logic.map.RoadStatus import com.unciv.logic.map.RoadStatus
import com.unciv.models.gamebasics.tr import com.unciv.models.gamebasics.tr
import com.unciv.ui.saves.Gzip import com.unciv.ui.saves.Gzip
import com.unciv.ui.saves.LoadMapScreen
import com.unciv.ui.utils.CameraStageBaseScreen import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.onClick import com.unciv.ui.utils.onClick
import com.unciv.ui.worldscreen.optionstable.DropBox import com.unciv.ui.worldscreen.optionstable.DropBox
@ -56,7 +58,9 @@ class MapEditorOptionsTable(mapEditorScreen: MapEditorScreen): PopupTable(mapEdi
add(copyMapAsTextButton).row() add(copyMapAsTextButton).row()
val loadMapButton = TextButton("Load".tr(), skin) val loadMapButton = TextButton("Load".tr(), skin)
loadMapButton.onClick { MapScreenLoadTable(mapEditorScreen); remove() } loadMapButton.onClick {
UnCivGame.Current.screen = LoadMapScreen(mapEditorScreen.tileMap)
}
add(loadMapButton).row() add(loadMapButton).row()
val uploadMapButton = TextButton("Upload".tr(), skin) val uploadMapButton = TextButton("Upload".tr(), skin)
@ -105,7 +109,7 @@ class MapDownloadTable(mapEditorScreen: MapEditorScreen):PopupTable(mapEditorScr
val folderList: DropBox.FolderList val folderList: DropBox.FolderList
try { try {
folderList = DropBox().getFolderList("/Maps") folderList = DropBox().getFolderList("/Maps")
val scrollableMapTable = Table() val scrollableMapTable = Table().apply { defaults().pad(10f) }
for (downloadableMap in folderList.entries) { for (downloadableMap in folderList.entries) {
val downloadMapButton = TextButton(downloadableMap.name, CameraStageBaseScreen.skin) val downloadMapButton = TextButton(downloadableMap.name, CameraStageBaseScreen.skin)
downloadMapButton.onClick { downloadMapButton.onClick {
@ -117,7 +121,7 @@ class MapDownloadTable(mapEditorScreen: MapEditorScreen):PopupTable(mapEditorScr
} }
scrollableMapTable.add(downloadMapButton).row() scrollableMapTable.add(downloadMapButton).row()
} }
add(scrollableMapTable).height(mapEditorScreen.stage.height * 2 / 3).row() add(ScrollPane(scrollableMapTable)).height(mapEditorScreen.stage.height * 2 / 3).row()
} catch (ex: Exception) { } catch (ex: Exception) {
addGoodSizedLabel("Could not get list of maps!").row() addGoodSizedLabel("Could not get list of maps!").row()
} }

View File

@ -1,44 +0,0 @@
package com.unciv.ui.mapeditor
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.utils.Array
import com.unciv.UnCivGame
import com.unciv.logic.MapSaver
import com.unciv.models.gamebasics.tr
import com.unciv.ui.saves.Gzip
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.onClick
import com.unciv.ui.worldscreen.optionstable.PopupTable
class MapScreenLoadTable(mapEditorScreen: MapEditorScreen): PopupTable(mapEditorScreen){
init{
val mapFileSelectBox = SelectBox<String>(CameraStageBaseScreen.skin)
val mapNames = Array<String>()
for (mapName in MapSaver().getMaps()) mapNames.add(mapName)
mapFileSelectBox.items = mapNames
add(mapFileSelectBox).row()
val loadMapButton = TextButton("Load".tr(), CameraStageBaseScreen.skin)
loadMapButton.onClick {
UnCivGame.Current.screen = MapEditorScreen(mapFileSelectBox.selected)
}
add(loadMapButton).row()
val loadFromClipboardButton = TextButton("Load copied data".tr(), CameraStageBaseScreen.skin)
loadFromClipboardButton .onClick {
val clipboardContentsString = Gdx.app.clipboard.contents.trim()
val decoded = Gzip.unzip(clipboardContentsString)
val loadedMap = MapSaver().mapFromJson(decoded)
UnCivGame.Current.screen = MapEditorScreen(loadedMap)
}
add(loadFromClipboardButton).row()
val closeOptionsButton = TextButton("Close".tr(), CameraStageBaseScreen.skin)
closeOptionsButton.onClick { remove() }
add(closeOptionsButton).row()
open()
}
}

View File

@ -22,62 +22,16 @@ class LoadGameScreen : PickerScreen() {
lateinit var selectedSave:String lateinit var selectedSave:String
val copySavedGameToClipboardButton = TextButton("Copy saved game to clipboard",skin) val copySavedGameToClipboardButton = TextButton("Copy saved game to clipboard",skin)
val saveTable = Table() val saveTable = Table()
val deleteSaveButton = TextButton("Delete save".tr(), skin)
init { init {
setDefaultCloseAction() setDefaultCloseAction()
val deleteSaveButton = TextButton("Delete save".tr(), skin)
deleteSaveButton .onClick {
GameSaver().deleteSave(selectedSave)
UnCivGame.Current.screen = LoadGameScreen()
}
deleteSaveButton.disable()
rightSideButton.setText("Load game".tr()) rightSideButton.setText("Load game".tr())
updateLoadableGames(deleteSaveButton,false) updateLoadableGames(false)
topTable.add(ScrollPane(saveTable)).height(stage.height*2/3) topTable.add(ScrollPane(saveTable)).height(stage.height*2/3)
val rightSideTable = Table() val rightSideTable = getRightSideTable()
val errorLabel = "".toLabel().setFontColor(Color.RED)
val loadFromClipboardButton = TextButton("Load copied data".tr(),skin)
loadFromClipboardButton.onClick {
try{
val clipboardContentsString = Gdx.app.clipboard.contents.trim()
val decoded = Gzip.unzip(clipboardContentsString)
val loadedGame = GameSaver().json().fromJson(GameInfo::class.java, decoded)
loadedGame.setTransients()
UnCivGame.Current.loadGame(loadedGame)
}catch (ex:Exception){
errorLabel.setText("Could not load game from clipboard!".tr())
ex.printStackTrace()
}
}
rightSideTable.add(loadFromClipboardButton).row()
rightSideTable.add(errorLabel).row()
rightSideTable.add(deleteSaveButton).row()
copySavedGameToClipboardButton.disable()
copySavedGameToClipboardButton.onClick {
val gameText = GameSaver().getSave(selectedSave).readString()
val gzippedGameText = Gzip.zip(gameText)
Gdx.app.clipboard.contents = gzippedGameText
}
rightSideTable.add(copySavedGameToClipboardButton).row()
val showAutosavesCheckbox = CheckBox("Show autosaves".tr(), skin)
showAutosavesCheckbox.isChecked = false
showAutosavesCheckbox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
updateLoadableGames(deleteSaveButton,showAutosavesCheckbox.isChecked)
}
})
rightSideTable.add(showAutosavesCheckbox).row()
topTable.add(rightSideTable) topTable.add(rightSideTable)
@ -98,7 +52,55 @@ class LoadGameScreen : PickerScreen() {
} }
private fun updateLoadableGames(deleteSaveButton: TextButton, showAutosaves:Boolean) { private fun getRightSideTable(): Table {
val rightSideTable = Table()
val errorLabel = "".toLabel().setFontColor(Color.RED)
val loadFromClipboardButton = TextButton("Load copied data".tr(), skin)
loadFromClipboardButton.onClick {
try {
val clipboardContentsString = Gdx.app.clipboard.contents.trim()
val decoded = Gzip.unzip(clipboardContentsString)
val loadedGame = GameSaver().json().fromJson(GameInfo::class.java, decoded)
loadedGame.setTransients()
UnCivGame.Current.loadGame(loadedGame)
} catch (ex: Exception) {
errorLabel.setText("Could not load game from clipboard!".tr())
ex.printStackTrace()
}
}
rightSideTable.add(loadFromClipboardButton).row()
rightSideTable.add(errorLabel).row()
deleteSaveButton.onClick {
GameSaver().deleteSave(selectedSave)
UnCivGame.Current.screen = LoadGameScreen()
}
deleteSaveButton.disable()
rightSideTable.add(deleteSaveButton).row()
copySavedGameToClipboardButton.disable()
copySavedGameToClipboardButton.onClick {
val gameText = GameSaver().getSave(selectedSave).readString()
val gzippedGameText = Gzip.zip(gameText)
Gdx.app.clipboard.contents = gzippedGameText
}
rightSideTable.add(copySavedGameToClipboardButton).row()
val showAutosavesCheckbox = CheckBox("Show autosaves".tr(), skin)
showAutosavesCheckbox.isChecked = false
showAutosavesCheckbox.addListener(object : ChangeListener() {
override fun changed(event: ChangeEvent?, actor: Actor?) {
updateLoadableGames(showAutosavesCheckbox.isChecked)
}
})
rightSideTable.add(showAutosavesCheckbox).row()
return rightSideTable
}
private fun updateLoadableGames(showAutosaves:Boolean) {
saveTable.clear() saveTable.clear()
for (save in GameSaver().getSaves().sortedByDescending { GameSaver().getSave(it).lastModified() }) { for (save in GameSaver().getSaves().sortedByDescending { GameSaver().getSave(it).lastModified() }) {
if(save.startsWith("Autosave") && !showAutosaves) continue if(save.startsWith("Autosave") && !showAutosaves) continue
@ -128,4 +130,5 @@ class LoadGameScreen : PickerScreen() {
} }
} }
} }

View File

@ -0,0 +1,63 @@
package com.unciv.ui.saves
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.unciv.UnCivGame
import com.unciv.logic.MapSaver
import com.unciv.logic.map.TileMap
import com.unciv.models.gamebasics.tr
import com.unciv.ui.mapeditor.MapEditorScreen
import com.unciv.ui.pickerscreens.PickerScreen
import com.unciv.ui.utils.disable
import com.unciv.ui.utils.enable
import com.unciv.ui.utils.onClick
class LoadMapScreen(previousMap: TileMap) : PickerScreen(){
var chosenMap = ""
val deleteMapButton = TextButton("Delete map",skin)
init {
rightSideButton.setText("Load map")
rightSideButton.onClick {
UnCivGame.Current.screen = MapEditorScreen(chosenMap)
dispose()
}
val mapsTable = Table().apply { defaults().pad(10f) }
for (map in MapSaver().getMaps()) {
val loadMapButton = TextButton(map, skin)
loadMapButton.onClick {
rightSideButton.enable()
chosenMap = map
deleteMapButton.enable()
deleteMapButton.color = Color.RED
}
mapsTable.add(loadMapButton).row()
}
topTable.add(ScrollPane(mapsTable)).height(stage.height * 2 / 3)
val rightSideTable = Table().apply { defaults().pad(10f) }
val loadFromClipboardButton = TextButton("Load copied data".tr(), skin)
loadFromClipboardButton .onClick {
val clipboardContentsString = Gdx.app.clipboard.contents.trim()
val decoded = Gzip.unzip(clipboardContentsString)
val loadedMap = MapSaver().mapFromJson(decoded)
UnCivGame.Current.screen = MapEditorScreen(loadedMap)
}
rightSideTable.add(loadFromClipboardButton).row()
deleteMapButton.onClick {
MapSaver().deleteMap(chosenMap)
UnCivGame.Current.screen = LoadMapScreen(previousMap)
}
deleteMapButton.disable()
deleteMapButton.color = Color.RED
rightSideTable.add(deleteMapButton).row()
topTable.add(rightSideTable)
closeButton.onClick { UnCivGame.Current.screen = MapEditorScreen(previousMap) }
}
}