diff --git a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt index 4408a68344..988a0feb6a 100644 --- a/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt +++ b/core/src/com/unciv/ui/mapeditor/MapDownloadPopup.kt @@ -33,6 +33,7 @@ class MapDownloadPopup(loadMapScreen: SaveAndLoadMapScreen): Popup(loadMapScreen val listener = TextField.TextFieldListener{ textField: TextField, _: Char -> updateList(textField.text) } filter.setTextFieldListener(listener) header.add(filter).row() + keyboardFocus = filter header.addSeparator().row() pack() } @@ -56,14 +57,18 @@ class MapDownloadPopup(loadMapScreen: SaveAndLoadMapScreen): Popup(loadMapScreen } scrollableMapTable.add(downloadMapButton).row() } - contentTable.add(ScrollPane(scrollableMapTable)).height(screen.stage.height * 2 / 3).row() - pack() - close() - // the list is loaded and ready to be shown - removeActor(loadingLabel) + val scrollPane = ScrollPane(scrollableMapTable) + contentTable.add(scrollPane).height(screen.stage.height * 2 / 3).row() + // the list is loaded and ready to be shown - remove "Loading..." + innerTable.removeActor(loadingLabel) // create the header with a filter tool createHeader() - open() + pack() + center(screen.stage) + // Due to some jokers spamming very long names the content would end up way on the right + // scrollPane.scrollPercentX = 0.5f does _not_ work for this! That fun doesn't refer to the center. + // This will bounce in with visual effect, if undesired call updateVisualScroll() + scrollPane.scrollX = scrollPane.maxX / 2 } } catch (ex: Exception) { Gdx.app.postRunnable { addGoodSizedLabel("Could not get list of maps!").row() } diff --git a/core/src/com/unciv/ui/mapeditor/SaveAndLoadMapScreen.kt b/core/src/com/unciv/ui/mapeditor/SaveAndLoadMapScreen.kt index fcd2492259..3bea52f341 100644 --- a/core/src/com/unciv/ui/mapeditor/SaveAndLoadMapScreen.kt +++ b/core/src/com/unciv/ui/mapeditor/SaveAndLoadMapScreen.kt @@ -7,6 +7,7 @@ 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.badlogic.gdx.Input import com.unciv.logic.MapSaver import com.unciv.logic.map.MapType import com.unciv.logic.map.TileMap @@ -19,16 +20,17 @@ import com.unciv.ui.utils.AutoScrollPane as ScrollPane class SaveAndLoadMapScreen(mapToSave: TileMap?, save:Boolean = false, previousScreen: CameraStageBaseScreen) : PickerScreen(disableScroll = true) { - var chosenMap: FileHandle? = null + private var chosenMap: FileHandle? = null val deleteButton = "Delete map".toTextButton() val mapsTable = Table().apply { defaults().pad(10f) } - val mapNameTextField = TextField("", skin).apply { maxLength = 100 } + private val mapNameTextField = TextField("", skin).apply { maxLength = 100 } init { + val rightSideButtonAction: ()->Unit if (save) { rightSideButton.enable() rightSideButton.setText("Save map".tr()) - rightSideButton.onClick { + rightSideButtonAction = { mapToSave!!.mapParameters.name = mapNameTextField.text mapToSave.mapParameters.type = MapType.custom thread(name = "SaveMap") { @@ -52,7 +54,7 @@ class SaveAndLoadMapScreen(mapToSave: TileMap?, save:Boolean = false, previousSc } } else { rightSideButton.setText("Load map".tr()) - rightSideButton.onClick { + rightSideButtonAction = { thread { Gdx.app.postRunnable { val popup = Popup(this) @@ -68,6 +70,8 @@ class SaveAndLoadMapScreen(mapToSave: TileMap?, save:Boolean = false, previousSc } } } + rightSideButton.onClick(rightSideButtonAction) + keyPressDispatcher['\r'] = rightSideButtonAction topTable.add(ScrollPane(mapsTable)).maxWidth(stage.width / 2) @@ -75,13 +79,18 @@ class SaveAndLoadMapScreen(mapToSave: TileMap?, save:Boolean = false, previousSc if (save) { mapNameTextField.textFieldFilter = TextField.TextFieldFilter { _, char -> char != '\\' && char != '/' } - mapNameTextField.text = "My new map" + mapNameTextField.text = if (mapToSave == null || mapToSave.mapParameters.name.isEmpty()) "My new map" + else mapToSave.mapParameters.name rightSideTable.add(mapNameTextField).width(300f).pad(10f) + stage.keyboardFocus = mapNameTextField + mapNameTextField.selectAll() } else { val downloadMapButton = "Download map".toTextButton() - downloadMapButton.onClick { + val downloadAction = { MapDownloadPopup(this).open() } + downloadMapButton.onClick(downloadAction) + keyPressDispatcher['\u0004'] = downloadAction // Ctrl-D rightSideTable.add(downloadMapButton).row() } @@ -89,16 +98,18 @@ class SaveAndLoadMapScreen(mapToSave: TileMap?, save:Boolean = false, previousSc if (save) { val copyMapAsTextButton = "Copy to clipboard".toTextButton() - copyMapAsTextButton.onClick { + val copyMapAsTextAction = { val json = Json().toJson(mapToSave) val base64Gzip = Gzip.zip(json) Gdx.app.clipboard.contents = base64Gzip } + copyMapAsTextButton.onClick (copyMapAsTextAction) + keyPressDispatcher['\u0003'] = copyMapAsTextAction // Ctrl-C 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 { + val loadFromClipboardAction = { try { val clipboardContentsString = Gdx.app.clipboard.contents.trim() val decoded = Gzip.unzip(clipboardContentsString) @@ -108,16 +119,20 @@ class SaveAndLoadMapScreen(mapToSave: TileMap?, save:Boolean = false, previousSc couldNotLoadMapLabel.isVisible = true } } + loadFromClipboardButton.onClick(loadFromClipboardAction) + keyPressDispatcher['\u0016'] = loadFromClipboardAction // Ctrl-V rightSideTable.add(loadFromClipboardButton).row() rightSideTable.add(couldNotLoadMapLabel).row() } - deleteButton.onClick { + val deleteAction = { YesNoPopup("Are you sure you want to delete this map?", { chosenMap!!.delete() game.setScreen(SaveAndLoadMapScreen(mapToSave, save, previousScreen)) }, this).open() } + deleteButton.onClick(deleteAction) + keyPressDispatcher['\u007f'] = deleteAction // Input.Keys.DEL but ascii has precedence rightSideTable.add(deleteButton).row() topTable.add(rightSideTable) @@ -132,22 +147,23 @@ class SaveAndLoadMapScreen(mapToSave: TileMap?, save:Boolean = false, previousSc 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 { + val existingMapButton = TextButton(map.name(), skin) + existingMapButton.onClick { for (cell in mapsTable.cells) cell.actor.color = Color.WHITE - loadMapButton.color = Color.BLUE + existingMapButton.color = Color.BLUE rightSideButton.enable() chosenMap = map mapNameTextField.text = map.name() + mapNameTextField.setSelection(Int.MAX_VALUE,Int.MAX_VALUE) // sets caret to end of text + deleteButton.enable() deleteButton.color = Color.RED } - mapsTable.add(loadMapButton).row() + mapsTable.add(existingMapButton).row() } }