From a2a38e48caffe30911ffd31bd217460b8da6c99d Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Thu, 26 Mar 2020 15:45:59 +0200 Subject: [PATCH] Simplified MapParametersTable sliders More onChange{ } changes --- .../ui/mapeditor/TileEditorOptionsTable.kt | 7 +- .../ui/newgamescreen/MapParametersTable.kt | 150 +++++------------- .../unciv/ui/utils/CameraStageBaseScreen.kt | 3 +- .../mainmenu/WorldScreenOptionsPopup.kt | 93 +++++------ 4 files changed, 84 insertions(+), 169 deletions(-) diff --git a/core/src/com/unciv/ui/mapeditor/TileEditorOptionsTable.kt b/core/src/com/unciv/ui/mapeditor/TileEditorOptionsTable.kt index 2c716462..99cc22dd 100644 --- a/core/src/com/unciv/ui/mapeditor/TileEditorOptionsTable.kt +++ b/core/src/com/unciv/ui/mapeditor/TileEditorOptionsTable.kt @@ -182,10 +182,9 @@ class TileEditorOptionsTable(val mapEditorScreen: MapEditorScreen): Table(Camera val terrain = resource.terrainsCanBeFoundOn.first() val terrainObject = ruleset.terrains[terrain]!! if (terrainObject.type == TerrainType.TerrainFeature) { - tileInfo.baseTerrain = when { - terrainObject.occursOn == null -> terrainObject.occursOn!!.first() - else -> "Grassland" - } + tileInfo.baseTerrain = + if (terrainObject.occursOn == null) terrainObject.occursOn!!.first() + else "Grassland" tileInfo.terrainFeature = terrain } else tileInfo.baseTerrain = terrain diff --git a/core/src/com/unciv/ui/newgamescreen/MapParametersTable.kt b/core/src/com/unciv/ui/newgamescreen/MapParametersTable.kt index 294ad765..f7c5637c 100644 --- a/core/src/com/unciv/ui/newgamescreen/MapParametersTable.kt +++ b/core/src/com/unciv/ui/newgamescreen/MapParametersTable.kt @@ -1,17 +1,16 @@ package com.unciv.ui.newgamescreen -import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.ui.CheckBox import com.badlogic.gdx.scenes.scene2d.ui.Slider import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.scenes.scene2d.ui.TextButton -import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener import com.unciv.logic.map.MapParameters import com.unciv.logic.map.MapShape import com.unciv.logic.map.MapSize import com.unciv.logic.map.MapType import com.unciv.models.translations.tr import com.unciv.ui.utils.CameraStageBaseScreen +import com.unciv.ui.utils.onChange import com.unciv.ui.utils.onClick import com.unciv.ui.utils.toLabel @@ -45,11 +44,9 @@ class MapParametersTable(val mapParameters: MapParameters, val isEmptyMapAllowed ) val mapShapeSelectBox = TranslatedSelectBox(mapShapes, mapParameters.shape, skin) - mapShapeSelectBox.addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { + mapShapeSelectBox.onChange { mapParameters.shape = mapShapeSelectBox.selected.value } - }) add ("{Map shape}:".toLabel()).left() add(mapShapeSelectBox).fillX().row() @@ -68,15 +65,13 @@ class MapParametersTable(val mapParameters: MapParameters, val isEmptyMapAllowed mapTypeSelectBox = TranslatedSelectBox(mapTypes, mapParameters.type, skin) - mapTypeSelectBox.addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { + mapTypeSelectBox.onChange { mapParameters.type = mapTypeSelectBox.selected.value // If the map won't be generated, these options are irrelevant and are hidden noRuinsCheckbox.isVisible = mapParameters.type != MapType.empty noNaturalWondersCheckbox.isVisible = mapParameters.type != MapType.empty } - }) add("{Map generation type}:".toLabel()).left() add(mapTypeSelectBox).fillX().row() @@ -90,11 +85,9 @@ class MapParametersTable(val mapParameters: MapParameters, val isEmptyMapAllowed skin ) - worldSizeSelectBox.addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { + worldSizeSelectBox.onChange { mapParameters.size = MapSize.valueOf(worldSizeSelectBox.selected.value) } - }) add("{World size}:".toLabel()).left() add(worldSizeSelectBox).fillX().row() @@ -103,28 +96,23 @@ class MapParametersTable(val mapParameters: MapParameters, val isEmptyMapAllowed private fun addNoRuinsCheckbox() { noRuinsCheckbox = CheckBox("No ancient ruins".tr(), skin) noRuinsCheckbox.isChecked = mapParameters.noRuins - noRuinsCheckbox.addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.noRuins = noRuinsCheckbox.isChecked - } - }) + noRuinsCheckbox.onChange { mapParameters.noRuins = noRuinsCheckbox.isChecked } add(noRuinsCheckbox).colspan(2).row() } private fun addNoNaturalWondersCheckbox() { noNaturalWondersCheckbox = CheckBox("No Natural Wonders".tr(), skin) noNaturalWondersCheckbox.isChecked = mapParameters.noNaturalWonders - noNaturalWondersCheckbox.addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.noNaturalWonders = noNaturalWondersCheckbox.isChecked - } - }) + noNaturalWondersCheckbox.onChange { + mapParameters.noNaturalWonders = noNaturalWondersCheckbox.isChecked + } add(noNaturalWondersCheckbox).colspan(2).row() } private fun addAdvancedSettings() { val button = TextButton("Show advanced settings".tr(), skin) - val advancedSettingsTable = Table().apply {isVisible = false; defaults().pad(5f)} + val advancedSettingsTable = Table() + .apply {isVisible = false; defaults().pad(5f)} add(button).colspan(2).row() val advancedSettingsCell = add(Table()).colspan(2) @@ -142,111 +130,47 @@ class MapParametersTable(val mapParameters: MapParameters, val isEmptyMapAllowed } } + val sliders = HashMapFloat>() - val elevationExponentSlider = Slider(0.5f,1f,0.01f, false, skin).apply { - addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.elevationExponent = this@apply.value - } - }) + fun addSlider(text:String, getValue:()->Float, min:Float, max:Float, onChange: (value:Float)->Unit): Slider { + val slider = Slider(min, max, (max-min)/20,false,skin) + slider.value = getValue() + slider.onChange { onChange(slider.value) } + advancedSettingsTable.add(text.toLabel()).left() + advancedSettingsTable.add(slider).fillX().row() + sliders.put(slider, getValue) + return slider } - elevationExponentSlider.value = mapParameters.elevationExponent - advancedSettingsTable.add("Map Height".toLabel()).left() - advancedSettingsTable.add(elevationExponentSlider).fillX().row() + addSlider("Map Height", {mapParameters.elevationExponent}, 0.5f,1f) + {mapParameters.elevationExponent=it} - val tempExtremeSlider = Slider(0.4f,0.8f,0.01f, false, skin).apply { - addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.temperatureExtremeness = this@apply.value - } - }) - } - tempExtremeSlider.value = mapParameters.temperatureExtremeness - advancedSettingsTable.add("Temperature extremeness".toLabel()).left() - advancedSettingsTable.add(tempExtremeSlider).fillX().row() + addSlider("Temperature extremeness", {mapParameters.temperatureExtremeness}, 0.4f,0.8f) + { mapParameters.temperatureExtremeness = it} + addSlider("Resource richness", {mapParameters.resourceRichness},0f,0.5f) + { mapParameters.resourceRichness=it } - val resourceRichnessSlider = Slider(0f,0.5f,0.01f, false, skin).apply { - addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.resourceRichness = this@apply.value - } - }) - } - resourceRichnessSlider.value = mapParameters.resourceRichness - advancedSettingsTable.add("Resource richness".toLabel()).left() - advancedSettingsTable.add(resourceRichnessSlider).fillX().row() + addSlider("Vegetation richness", {mapParameters.vegetationRichness}, 0f, 1f) + { mapParameters.vegetationRichness=it } - val vegetationRichnessSlider = Slider(0f,1f,0.01f, false, skin).apply { - addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.vegetationRichness = this@apply.value - } - }) - } - vegetationRichnessSlider.value = mapParameters.vegetationRichness - advancedSettingsTable.add("Vegetation richness".toLabel()).left() - advancedSettingsTable.add(vegetationRichnessSlider).fillX().row() + addSlider("Rare features richness", {mapParameters.rareFeaturesRichness}, 0f, 0.5f) + { mapParameters.rareFeaturesRichness = it } - val rareFeaturesRichnessSlider = Slider(0f,0.5f,0.01f, false, skin).apply { - addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.rareFeaturesRichness = this@apply.value - } - }) - } - rareFeaturesRichnessSlider.value = mapParameters.rareFeaturesRichness - advancedSettingsTable.add("Rare features richness".toLabel()).left() - advancedSettingsTable.add(rareFeaturesRichnessSlider).fillX().row() + addSlider("Max Coast extension", {mapParameters.maxCoastExtension.toFloat()}, 0f, 5f) + { mapParameters.maxCoastExtension =it.toInt() }.apply { stepSize=1f } + addSlider("Biome areas extension", {mapParameters.tilesPerBiomeArea.toFloat()}, 1f, 15f) + { mapParameters.tilesPerBiomeArea = it.toInt() }.apply { stepSize=1f } - val maxCoastExtensionSlider = Slider(0f,5f,1f, false, skin).apply { - addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.maxCoastExtension = this@apply.value.toInt() - } - }) - } - maxCoastExtensionSlider.value = mapParameters.maxCoastExtension.toFloat() - advancedSettingsTable.add("Max Coast extension".toLabel()).left() - advancedSettingsTable.add(maxCoastExtensionSlider).fillX().row() - - - val tilesPerBiomeAreaSlider = Slider(1f,15f,1f, false, skin).apply { - addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.tilesPerBiomeArea = this@apply.value.toInt() - } - }) - } - tilesPerBiomeAreaSlider.value = mapParameters.tilesPerBiomeArea.toFloat() - advancedSettingsTable.add("Biome areas extension".toLabel()).left() - advancedSettingsTable.add(tilesPerBiomeAreaSlider).fillX().row() - - - val waterThresholdSlider = Slider(-0.1f,0.1f,0.01f, false, skin).apply { - addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - mapParameters.waterThreshold = this@apply.value - } - }) - } - waterThresholdSlider.value = mapParameters.waterThreshold - advancedSettingsTable.add("Water level".toLabel()).left() - advancedSettingsTable.add(waterThresholdSlider).fillX().row() + addSlider("Water level", {mapParameters.waterThreshold}, -0.1f, 0.1f) + { mapParameters.waterThreshold = it } val resetToDefaultButton = TextButton("Reset to default".tr(), skin) resetToDefaultButton.onClick { mapParameters.resetAdvancedSettings() - elevationExponentSlider.value = mapParameters.elevationExponent - tempExtremeSlider.value = mapParameters.temperatureExtremeness - resourceRichnessSlider.value = mapParameters.resourceRichness - vegetationRichnessSlider.value = mapParameters.vegetationRichness - rareFeaturesRichnessSlider.value = mapParameters.rareFeaturesRichness - maxCoastExtensionSlider.value = mapParameters.maxCoastExtension.toFloat() - tilesPerBiomeAreaSlider.value = mapParameters.tilesPerBiomeArea.toFloat() - waterThresholdSlider.value = mapParameters.waterThreshold + for(entry in sliders) + entry.key.value = entry.value() } advancedSettingsTable.add(resetToDefaultButton).colspan(2).row() } diff --git a/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt b/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt index c8576193..cd14460d 100644 --- a/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt +++ b/core/src/com/unciv/ui/utils/CameraStageBaseScreen.kt @@ -143,12 +143,13 @@ fun Actor.onClick(function: () -> Unit): Actor { return this } -fun Actor.onChange(function: () -> Unit){ +fun Actor.onChange(function: () -> Unit): Actor { this.addListener(object : ChangeListener() { override fun changed(event: ChangeEvent?, actor: Actor?) { function() } }) + return this } fun Actor.surroundWithCircle(size:Float,resizeActor:Boolean=true): IconCircleGroup { diff --git a/core/src/com/unciv/ui/worldscreen/mainmenu/WorldScreenOptionsPopup.kt b/core/src/com/unciv/ui/worldscreen/mainmenu/WorldScreenOptionsPopup.kt index dd03e6e5..a67d71d0 100644 --- a/core/src/com/unciv/ui/worldscreen/mainmenu/WorldScreenOptionsPopup.kt +++ b/core/src/com/unciv/ui/worldscreen/mainmenu/WorldScreenOptionsPopup.kt @@ -21,7 +21,7 @@ class Language(val language:String, val percentComplete:Int){ } } -class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){ +class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen) { var selectedLanguage: String = "English" init { @@ -41,7 +41,7 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){ innerTable.add("Show worked tiles".toLabel()) addButton(innerTable, if (settings.showWorkedTiles) "Yes" else "No") { - settings.showWorkedTiles= !settings.showWorkedTiles + settings.showWorkedTiles = !settings.showWorkedTiles update() } @@ -187,16 +187,16 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){ val soundEffectsVolumeSlider = Slider(0f, 1.0f, 0.1f, false, skin) soundEffectsVolumeSlider.value = UncivGame.Current.settings.soundEffectsVolume soundEffectsVolumeSlider.onChange { - UncivGame.Current.settings.soundEffectsVolume = soundEffectsVolumeSlider.value - UncivGame.Current.settings.save() - Sounds.play(UncivSound.Click) - } + UncivGame.Current.settings.soundEffectsVolume = soundEffectsVolumeSlider.value + UncivGame.Current.settings.save() + Sounds.play(UncivSound.Click) + } innerTable.add(soundEffectsVolumeSlider).pad(10f).row() } private fun addMusicVolumeSlider(innerTable: Table) { - val musicLocation =Gdx.files.local(UncivGame.Current.musicLocation) - if(musicLocation.exists()) { + val musicLocation = Gdx.files.local(UncivGame.Current.musicLocation) + if (musicLocation.exists()) { innerTable.add("Music volume".tr()) val musicVolumeSlider = Slider(0f, 1.0f, 0.1f, false, skin) @@ -212,16 +212,15 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){ music?.volume = 0.4f * musicVolumeSlider.value } innerTable.add(musicVolumeSlider).pad(10f).row() - } - else{ - val downloadMusicButton = TextButton("Download music".tr(),CameraStageBaseScreen.skin) + } else { + val downloadMusicButton = TextButton("Download music".tr(), CameraStageBaseScreen.skin) innerTable.add(downloadMusicButton).colspan(2).row() val errorTable = Table() innerTable.add(errorTable).colspan(2).row() downloadMusicButton.onClick { // So the whole game doesn't get stuck while downloading the file - thread(name="Music") { + thread(name = "Music") { try { downloadMusicButton.disable() errorTable.clear() @@ -244,7 +243,7 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){ val resolutionSelectBox = SelectBox(skin) val resolutionArray = Array() - resolutionArray.addAll("750x500","900x600", "1050x700", "1200x800", "1500x1000") + resolutionArray.addAll("750x500", "900x600", "1050x700", "1200x800", "1500x1000") resolutionSelectBox.items = resolutionArray resolutionSelectBox.selected = UncivGame.Current.settings.resolution innerTable.add(resolutionSelectBox).minWidth(240f).pad(10f).row() @@ -265,20 +264,18 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){ val tileSetArray = Array() val tileSets = ImageGetter.atlas.regions.filter { it.name.startsWith("TileSets") } .map { it.name.split("/")[1] }.distinct() - for(tileset in tileSets) tileSetArray.add(tileset) + for (tileset in tileSets) tileSetArray.add(tileset) tileSetSelectBox.items = tileSetArray tileSetSelectBox.selected = UncivGame.Current.settings.tileSet innerTable.add(tileSetSelectBox).minWidth(240f).pad(10f).row() - tileSetSelectBox.addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - UncivGame.Current.settings.tileSet = tileSetSelectBox.selected - UncivGame.Current.settings.save() - UncivGame.Current.worldScreen = WorldScreen(worldScreen.viewingCiv) - UncivGame.Current.setWorldScreen() - WorldScreenOptionsPopup(UncivGame.Current.worldScreen).open() - } - }) + tileSetSelectBox.onChange { + UncivGame.Current.settings.tileSet = tileSetSelectBox.selected + UncivGame.Current.settings.save() + UncivGame.Current.worldScreen = WorldScreen(worldScreen.viewingCiv) + UncivGame.Current.setWorldScreen() + WorldScreenOptionsPopup(UncivGame.Current.worldScreen).open() + } } private fun addAutosaveTurnsSelectBox(innerTable: Table) { @@ -286,19 +283,17 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){ val autosaveTurnsSelectBox = SelectBox(skin) val autosaveTurnsArray = Array() - autosaveTurnsArray.addAll(1,2,5,10) + autosaveTurnsArray.addAll(1, 2, 5, 10) autosaveTurnsSelectBox.items = autosaveTurnsArray autosaveTurnsSelectBox.selected = UncivGame.Current.settings.turnsBetweenAutosaves innerTable.add(autosaveTurnsSelectBox).pad(10f).row() - autosaveTurnsSelectBox.addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - UncivGame.Current.settings.turnsBetweenAutosaves= autosaveTurnsSelectBox.selected - UncivGame.Current.settings.save() - update() - } - }) + autosaveTurnsSelectBox.onChange { + UncivGame.Current.settings.turnsBetweenAutosaves = autosaveTurnsSelectBox.selected + UncivGame.Current.settings.save() + update() + } } private fun addMultiplayerTurnCheckerDelayBox(innerTable: Table) { @@ -312,42 +307,38 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){ innerTable.add(checkDelaySelectBox).pad(10f).row() - checkDelaySelectBox.addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - UncivGame.Current.settings.multiplayerTurnCheckerDelayInMinutes = checkDelaySelectBox.selected - UncivGame.Current.settings.save() - update() - } - }) + checkDelaySelectBox.onChange { + UncivGame.Current.settings.multiplayerTurnCheckerDelayInMinutes = checkDelaySelectBox.selected + UncivGame.Current.settings.save() + update() + } } private fun addLanguageSelectBox(innerTable: Table) { val languageSelectBox = SelectBox(skin) val languageArray = Array() UncivGame.Current.translations.percentCompleteOfLanguages - .map { Language(it.key, if(it.key=="English") 100 else it.value) } + .map { Language(it.key, if (it.key == "English") 100 else it.value) } .sortedByDescending { it.percentComplete } .forEach { languageArray.add(it) } - if(languageArray.size==0) return + if (languageArray.size == 0) return + innerTable.add("Language".toLabel()) languageSelectBox.items = languageArray val matchingLanguage = languageArray.firstOrNull { it.language == UncivGame.Current.settings.language } languageSelectBox.selected = if (matchingLanguage != null) matchingLanguage else languageArray.first() innerTable.add(languageSelectBox).minWidth(240f).pad(10f).row() - languageSelectBox.addListener(object : ChangeListener() { - override fun changed(event: ChangeEvent?, actor: Actor?) { - // Sometimes the "changed" is triggered even when we didn't choose something that isn't the - selectedLanguage = languageSelectBox.selected.language - - if(selectedLanguage!=UncivGame.Current.settings.language ) - selectLanguage() - } - }) + languageSelectBox.onChange { + // Sometimes the "changed" is triggered even when we didn't choose something that isn't the + selectedLanguage = languageSelectBox.selected.language + if (selectedLanguage != UncivGame.Current.settings.language) + selectLanguage() + } } - fun selectLanguage(){ + fun selectLanguage() { UncivGame.Current.settings.language = selectedLanguage UncivGame.Current.settings.save() @@ -357,4 +348,4 @@ class WorldScreenOptionsPopup(val worldScreen:WorldScreen) : Popup(worldScreen){ UncivGame.Current.setWorldScreen() WorldScreenOptionsPopup(UncivGame.Current.worldScreen).open() } -} +} \ No newline at end of file