From a1474fb13cb74a74c5ce08896a2ec6aed409a2c7 Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Sun, 21 Apr 2024 14:31:02 +0200 Subject: [PATCH] Do not preselect custom map option and defer map file loading (#11485) --- .../newgamescreen/MapFileSelectTable.kt | 19 +++++++++++++------ .../screens/newgamescreen/MapOptionsTable.kt | 11 ++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/src/com/unciv/ui/screens/newgamescreen/MapFileSelectTable.kt b/core/src/com/unciv/ui/screens/newgamescreen/MapFileSelectTable.kt index 73ded176f6..bcf9e91e61 100644 --- a/core/src/com/unciv/ui/screens/newgamescreen/MapFileSelectTable.kt +++ b/core/src/com/unciv/ui/screens/newgamescreen/MapFileSelectTable.kt @@ -76,7 +76,10 @@ class MapFileSelectTable( private val collator = UncivGame.Current.settings.getCollatorFromLocale() + private var isActivated = false // Ensure we get activated only once + init { + // Take care that this class can be instantiated cheaply and does not do heavy lifting right away - it may not be displayed at all. add(Table().apply { defaults().pad(5f, 10f) // Must stay same as in MapParametersTable val mapCategoryLabel = "{Map Mod}:".toLabel() @@ -103,11 +106,8 @@ class MapFileSelectTable( .maxWidth(columnWidth - 20f) .colspan(2).center().row() - mapCategorySelectBox.onChange { onCategorySelectBoxChange() } - mapFileSelectBox.onChange { onFileSelectBoxChange() } useNationsFromMapButton.onActivation { onUseNationsFromMap() } - addMapWrappersAsync() } private fun getMapFilesSequence() = sequence { @@ -186,12 +186,19 @@ class MapFileSelectTable( private fun FileHandle.isRecentlyModified() = lastModified() > System.currentTimeMillis() - 900000 // 900s = quarter hour fun isNotEmpty() = firstMap != null - fun recentlySavedMapExists() = firstMap != null && firstMap!!.isRecentlyModified() fun activateCustomMaps() { - if (loadingIcon.isShowing()) return // Default map selection will be handled when background loading finishes + if (isActivated) { + if (loadingIcon.isShowing()) return // Default map selection will be handled when background loading finishes + onCategorySelectBoxChange() // Coming back to this after fully loading make sure selections are OK + } + // Code to only run once per NewGameScreen lifetime, after user switches from + // generated map to custom map, no matter how many times they repeat that + isActivated = true preselectedName = mapParameters.name - onFileSelectBoxChange() + mapCategorySelectBox.onChange { onCategorySelectBoxChange() } + mapFileSelectBox.onChange { onFileSelectBoxChange() } + addMapWrappersAsync() } private fun onCategorySelectBoxChange() { diff --git a/core/src/com/unciv/ui/screens/newgamescreen/MapOptionsTable.kt b/core/src/com/unciv/ui/screens/newgamescreen/MapOptionsTable.kt index 7cbb2f1186..ce04cee1c1 100644 --- a/core/src/com/unciv/ui/screens/newgamescreen/MapOptionsTable.kt +++ b/core/src/com/unciv/ui/screens/newgamescreen/MapOptionsTable.kt @@ -24,13 +24,7 @@ class MapOptionsTable(private val newGameScreen: NewGameScreen, isReset: Boolean val mapTypes = arrayListOf(MapGeneratedMainType.generated, MapGeneratedMainType.randomGenerated) if (savedMapOptionsTable.isNotEmpty()) mapTypes.add(MapGeneratedMainType.custom) - // Pre-select custom if any map saved within last 15 minutes - val chooseCustom = !isReset && ( - savedMapOptionsTable.recentlySavedMapExists() || - savedMapOptionsTable.isNotEmpty() && mapParameters.type == MapGeneratedMainType.custom && mapParameters.name.isNotEmpty() - ) - val mapTypeDefault = if (chooseCustom) MapGeneratedMainType.custom else MapGeneratedMainType.generated - mapTypeSelectBox = TranslatedSelectBox(mapTypes, mapTypeDefault, BaseScreen.skin) + mapTypeSelectBox = TranslatedSelectBox(mapTypes, MapGeneratedMainType.generated, BaseScreen.skin) fun updateOnMapTypeChange() { mapTypeSpecificTable.clear() @@ -46,7 +40,6 @@ class MapOptionsTable(private val newGameScreen: NewGameScreen, isReset: Boolean mapParameters.type = generatedMapOptionsTable.mapTypeSelectBox.selected.value mapTypeSpecificTable.add(generatedMapOptionsTable) newGameScreen.unlockTables() - } MapGeneratedMainType.randomGenerated -> { mapParameters.name = "" @@ -58,7 +51,7 @@ class MapOptionsTable(private val newGameScreen: NewGameScreen, isReset: Boolean newGameScreen.updateTables() } - // activate once, so when we had a file map before we'll have the right things set for another one + // activate once, so the MapGeneratedMainType.generated controls show updateOnMapTypeChange() mapTypeSelectBox.onChange { updateOnMapTypeChange() }