From 15bca9cc1c3854fbe4a2a3579fa51eb9fe396179 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Fri, 14 Aug 2020 14:50:22 +0300 Subject: [PATCH] Can now load scenarios from inside mods, so they aren't sent as 2 separate files! --- .../unciv/ui/newgamescreen/MapOptionsTable.kt | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt b/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt index 442d773b16..61156a0e5a 100644 --- a/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt +++ b/core/src/com/unciv/ui/newgamescreen/MapOptionsTable.kt @@ -1,5 +1,6 @@ package com.unciv.ui.newgamescreen +import com.badlogic.gdx.Gdx import com.badlogic.gdx.files.FileHandle import com.badlogic.gdx.scenes.scene2d.ui.SelectBox import com.badlogic.gdx.scenes.scene2d.ui.Table @@ -41,13 +42,20 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() { selectedScenarioSaveGame = savedGame } + fun getScenarioFiles(): Sequence { + val localSaveScenarios = GameSaver.getSaves().filter { it.name().toLowerCase().endsWith("scenario") } + val modScenarios = Gdx.files.local("mods").list().asSequence() + .filter { it.child("scenarios").exists() }.flatMap { it.child("scenarios").list().asSequence() } + return localSaveScenarios + modScenarios + } + private fun addMapTypeSelection() { add("{Map Type}:".toLabel()) val mapTypes = arrayListOf("Generated") if (MapSaver.getMaps().isNotEmpty()) mapTypes.add(MapType.custom) if (MapSaver.getScenarios().isNotEmpty() && UncivGame.Current.settings.extendedMapEditor) mapTypes.add(MapType.scenarioMap) - if (GameSaver.getSaves().any { it.name().toLowerCase().endsWith("scenario") }) + if (getScenarioFiles().any()) mapTypes.add(MapType.scenario) mapTypeSelectBox = TranslatedSelectBox(mapTypes, "Generated", CameraStageBaseScreen.skin) @@ -66,16 +74,20 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() { scenarioMapOptionsTable.add(scenarioMapSelectBox).maxWidth(newGameScreen.stage.width / 2) .right().row() + // The SelectBox auto displays the text a object.toString(), which on the FileHandle itself includes the folder path. + // So we wrap it in another object with a custom toString() + class FileHandleWrapper(val fileHandle: FileHandle){ + override fun toString() = fileHandle.name() + } - val scenarioSelectBox = SelectBox(CameraStageBaseScreen.skin) - for (savedGame in GameSaver.getSaves()) { - if (savedGame.name().toLowerCase().endsWith("scenario")) - scenarioSelectBox.items.add(savedGame) + val scenarioSelectBox = SelectBox(CameraStageBaseScreen.skin) + for (savedGame in getScenarioFiles()) { + scenarioSelectBox.items.add(FileHandleWrapper(savedGame)) } scenarioSelectBox.items = scenarioSelectBox.items // it doesn't register them until you do this. scenarioSelectBox.selected = scenarioSelectBox.items.first() // needs to be after the item change, so it doesn't activate before we choose the Scenario maptype - scenarioSelectBox.onChange { selectSavedGameAsScenario(scenarioSelectBox.selected) } + scenarioSelectBox.onChange { selectSavedGameAsScenario(scenarioSelectBox.selected.fileHandle) } scenarioOptionsTable.add("{Scenario file}:".toLabel()).left() scenarioOptionsTable.add(scenarioSelectBox) @@ -100,7 +112,7 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() { newGameScreen.lockTables() newGameScreen.updateTables() } else if(mapTypeSelectBox.selected.value == MapType.scenario){ - selectSavedGameAsScenario(scenarioSelectBox.selected) + selectSavedGameAsScenario(scenarioSelectBox.selected.fileHandle) mapTypeSpecificTable.add(scenarioOptionsTable) newGameScreen.lockTables() } else { // generated map