Added options to save game to clipboard and load game from clipboard - this will allow sharing and modification of games for those who want it

This commit is contained in:
Yair Morgenstern 2018-06-05 21:55:48 +03:00
parent c088c02b1e
commit e0f97e584d
2 changed files with 35 additions and 1 deletions

View File

@ -1,15 +1,21 @@
package com.unciv.ui
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.Label
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.utils.Json
import com.unciv.UnCivGame
import com.unciv.logic.GameInfo
import com.unciv.logic.GameSaver
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.pickerscreens.PickerScreen
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.disable
import com.unciv.ui.utils.enable
import com.unciv.ui.utils.setFontColor
import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.text.SimpleDateFormat
import java.util.*
@ -55,6 +61,25 @@ class LoadScreen : PickerScreen() {
saveTable.add(textButton).pad(5f).row()
}
val loadFromClipboardTable = Table()
val loadFromClipboardButton = TextButton("Load copied data",skin)
val errorLabel = Label("",skin).setFontColor(Color.RED)
loadFromClipboardButton.addClickListener {
try{
val clipbordContents = Toolkit.getDefaultToolkit().systemClipboard.getContents(null)
var clipbordContentsString = clipbordContents.getTransferData(DataFlavor.stringFlavor).toString()
val loadedGame = Json().fromJson(GameInfo::class.java, clipbordContentsString)
loadedGame.setTransients()
UnCivGame.Current.loadGame(loadedGame)
}catch (ex:Exception){
errorLabel.setText("Could not load game from clipboard!")
}
}
loadFromClipboardTable.add(loadFromClipboardButton).row()
loadFromClipboardTable.add(errorLabel)
topTable.add(loadFromClipboardTable)
rightSideButton.addClickListener {
UnCivGame.Current.loadGame(selectedSave)
}

View File

@ -4,12 +4,15 @@ import com.badlogic.gdx.scenes.scene2d.ui.Label
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.UnCivGame
import com.unciv.logic.GameSaver
import com.unciv.ui.cityscreen.addClickListener
import com.unciv.ui.pickerscreens.PickerScreen
import com.unciv.ui.utils.enable
import com.unciv.ui.utils.getRandom
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
class SaveScreen : PickerScreen() {
val textField = TextField("", skin)
@ -39,7 +42,13 @@ class SaveScreen : PickerScreen() {
textField.text = defaultSaveName
newSave.add(Label("Saved game name:",skin)).row()
newSave.add(textField).width(300f)
newSave.add(textField).width(300f).row()
val copyJsonButton = TextButton("Copy game info",skin)
val copiedString = StringSelection(Json().toJson(game.gameInfo))
copyJsonButton.addClickListener { Toolkit.getDefaultToolkit().systemClipboard.setContents(copiedString,copiedString) }
newSave.add(copyJsonButton)
topTable.add(newSave)
topTable.pack()