mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-06 16:28:40 +07:00
Fix mod custom maps unavailable when no local ones exist (#4358)
This commit is contained in:
@ -21,6 +21,17 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() {
|
|||||||
private val savedMapOptionsTable = Table()
|
private val savedMapOptionsTable = Table()
|
||||||
lateinit var mapTypeSelectBox: TranslatedSelectBox
|
lateinit var mapTypeSelectBox: TranslatedSelectBox
|
||||||
|
|
||||||
|
private val mapFilesSequence = sequence<FileHandleWrapper> {
|
||||||
|
yieldAll(MapSaver.getMaps().asSequence().map { FileHandleWrapper(it) })
|
||||||
|
for (mod in Gdx.files.local("mods").list()) {
|
||||||
|
val mapsFolder = mod.child("maps")
|
||||||
|
if (mapsFolder.exists())
|
||||||
|
yieldAll(mapsFolder.list().asSequence().map { FileHandleWrapper(it) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val mapFileSelectBox = createMapFileSelectBox()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
defaults().pad(5f)
|
defaults().pad(5f)
|
||||||
add("Map Options".toLabel(fontSize = 24)).top().padBottom(20f).colspan(2).row()
|
add("Map Options".toLabel(fontSize = 24)).top().padBottom(20f).colspan(2).row()
|
||||||
@ -30,10 +41,9 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() {
|
|||||||
private fun addMapTypeSelection() {
|
private fun addMapTypeSelection() {
|
||||||
add("{Map Type}:".toLabel())
|
add("{Map Type}:".toLabel())
|
||||||
val mapTypes = arrayListOf("Generated")
|
val mapTypes = arrayListOf("Generated")
|
||||||
if (MapSaver.getMaps().isNotEmpty()) mapTypes.add(MapType.custom)
|
if (mapFilesSequence.any()) mapTypes.add(MapType.custom)
|
||||||
mapTypeSelectBox = TranslatedSelectBox(mapTypes, "Generated", CameraStageBaseScreen.skin)
|
mapTypeSelectBox = TranslatedSelectBox(mapTypes, "Generated", CameraStageBaseScreen.skin)
|
||||||
|
|
||||||
val mapFileSelectBox = getMapFileSelectBox()
|
|
||||||
savedMapOptionsTable.defaults().pad(5f)
|
savedMapOptionsTable.defaults().pad(5f)
|
||||||
savedMapOptionsTable.add("{Map file}:".toLabel()).left()
|
savedMapOptionsTable.add("{Map file}:".toLabel()).left()
|
||||||
// because SOME people gotta give the hugest names to their maps
|
// because SOME people gotta give the hugest names to their maps
|
||||||
@ -41,10 +51,10 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() {
|
|||||||
.right().row()
|
.right().row()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fun updateOnMapTypeChange() {
|
fun updateOnMapTypeChange() {
|
||||||
mapTypeSpecificTable.clear()
|
mapTypeSpecificTable.clear()
|
||||||
if (mapTypeSelectBox.selected.value == MapType.custom) {
|
if (mapTypeSelectBox.selected.value == MapType.custom) {
|
||||||
|
fillMapFileSelectBox()
|
||||||
mapParameters.type = MapType.custom
|
mapParameters.type = MapType.custom
|
||||||
mapParameters.name = mapFileSelectBox.selected.toString()
|
mapParameters.name = mapFileSelectBox.selected.toString()
|
||||||
mapTypeSpecificTable.add(savedMapOptionsTable)
|
mapTypeSpecificTable.add(savedMapOptionsTable)
|
||||||
@ -68,27 +78,8 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() {
|
|||||||
add(mapTypeSpecificTable).colspan(2).row()
|
add(mapTypeSpecificTable).colspan(2).row()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getMapFileSelectBox(): SelectBox<FileHandleWrapper> {
|
private fun createMapFileSelectBox(): SelectBox<FileHandleWrapper> {
|
||||||
val mapFileSelectBox = SelectBox<FileHandleWrapper>(CameraStageBaseScreen.skin)
|
val mapFileSelectBox = SelectBox<FileHandleWrapper>(CameraStageBaseScreen.skin)
|
||||||
val mapFiles = Array<FileHandleWrapper>()
|
|
||||||
for (mapFile in MapSaver.getMaps())
|
|
||||||
mapFiles.add(FileHandleWrapper(mapFile))
|
|
||||||
for (mod in Gdx.files.local("mods").list()) {
|
|
||||||
val mapsFolder = mod.child("maps")
|
|
||||||
if (mapsFolder.exists())
|
|
||||||
for (map in mapsFolder.list())
|
|
||||||
mapFiles.add(FileHandleWrapper(map))
|
|
||||||
}
|
|
||||||
mapFileSelectBox.items = mapFiles
|
|
||||||
val selectedItem = mapFiles.firstOrNull { it.fileHandle.name() == mapParameters.name }
|
|
||||||
if (selectedItem != null) {
|
|
||||||
mapFileSelectBox.selected = selectedItem
|
|
||||||
newGameScreen.gameSetupInfo.mapFile = mapFileSelectBox.selected.fileHandle
|
|
||||||
} else if (!mapFiles.isEmpty) {
|
|
||||||
mapFileSelectBox.selected = mapFiles.first()
|
|
||||||
newGameScreen.gameSetupInfo.mapFile = mapFileSelectBox.selected.fileHandle
|
|
||||||
}
|
|
||||||
|
|
||||||
mapFileSelectBox.onChange {
|
mapFileSelectBox.onChange {
|
||||||
val mapFile = mapFileSelectBox.selected.fileHandle
|
val mapFile = mapFileSelectBox.selected.fileHandle
|
||||||
val map: TileMap
|
val map: TileMap
|
||||||
@ -110,11 +101,26 @@ class MapOptionsTable(val newGameScreen: NewGameScreen): Table() {
|
|||||||
}
|
}
|
||||||
return mapFileSelectBox
|
return mapFileSelectBox
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun fillMapFileSelectBox() {
|
||||||
|
if (!mapFileSelectBox.items.isEmpty) return
|
||||||
|
val mapFiles = Array<FileHandleWrapper>()
|
||||||
|
mapFilesSequence.forEach { mapFiles.add(it) }
|
||||||
|
mapFileSelectBox.items = mapFiles
|
||||||
|
val selectedItem = mapFiles.firstOrNull { it.fileHandle.name() == mapParameters.name }
|
||||||
|
if (selectedItem != null) {
|
||||||
|
mapFileSelectBox.selected = selectedItem
|
||||||
|
newGameScreen.gameSetupInfo.mapFile = mapFileSelectBox.selected.fileHandle
|
||||||
|
} else if (!mapFiles.isEmpty) {
|
||||||
|
mapFileSelectBox.selected = mapFiles.first()
|
||||||
|
newGameScreen.gameSetupInfo.mapFile = mapFileSelectBox.selected.fileHandle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The SelectBox auto displays the text a object.toString(), which on the FileHandle itself includes the folder path.
|
// 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()
|
// So we wrap it in another object with a custom toString()
|
||||||
class FileHandleWrapper(val fileHandle: FileHandle) {
|
class FileHandleWrapper(val fileHandle: FileHandle) {
|
||||||
override fun toString(): String = fileHandle.name()
|
override fun toString(): String = fileHandle.name()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user