From e0f97e584d576cbc0dd34146ed91e678952db090 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Tue, 5 Jun 2018 21:55:48 +0300 Subject: [PATCH] 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 --- core/src/com/unciv/ui/LoadScreen.kt | 25 +++++++++++++++++++++++++ core/src/com/unciv/ui/SaveScreen.kt | 11 ++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/core/src/com/unciv/ui/LoadScreen.kt b/core/src/com/unciv/ui/LoadScreen.kt index 2e1285bc9b..2049c02714 100644 --- a/core/src/com/unciv/ui/LoadScreen.kt +++ b/core/src/com/unciv/ui/LoadScreen.kt @@ -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) } diff --git a/core/src/com/unciv/ui/SaveScreen.kt b/core/src/com/unciv/ui/SaveScreen.kt index 95a45accaa..2b623f42cb 100644 --- a/core/src/com/unciv/ui/SaveScreen.kt +++ b/core/src/com/unciv/ui/SaveScreen.kt @@ -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()