From a13570f109e8badc45a15729b7db5b4ea4ee2a31 Mon Sep 17 00:00:00 2001 From: drwhut Date: Tue, 7 Jan 2020 16:55:38 +0000 Subject: [PATCH] Added a growth progress bar to CityButton. (#1626) * Added temporary growth number to CityButton. * Added a growth bar to CityButton. * Made relevant changes based on comments in pull request #1626 * Added the unicode infinity symbol to reflect no growth. * New growth functions now return null instead of -1. * Adjusted the population group width to account for large numbers. * Changed the colour of the growth number. --- core/src/com/unciv/logic/city/CityInfo.kt | 31 ++++++++ .../src/com/unciv/ui/cityscreen/CityScreen.kt | 18 ++--- .../src/com/unciv/ui/tilegroups/CityButton.kt | 72 ++++++++++++++++++- core/src/com/unciv/ui/utils/Fonts.kt | 2 +- 4 files changed, 110 insertions(+), 13 deletions(-) diff --git a/core/src/com/unciv/logic/city/CityInfo.kt b/core/src/com/unciv/logic/city/CityInfo.kt index 7be3b0537f..45129dfd6a 100644 --- a/core/src/com/unciv/logic/city/CityInfo.kt +++ b/core/src/com/unciv/logic/city/CityInfo.kt @@ -19,6 +19,8 @@ import com.unciv.models.ruleset.tile.ResourceSupplyList import com.unciv.models.ruleset.tile.ResourceType import com.unciv.models.stats.Stats import com.unciv.ui.utils.withoutItem +import kotlin.math.ceil +import kotlin.math.floor import kotlin.math.min import kotlin.math.roundToInt @@ -192,6 +194,35 @@ class CityInfo { return 0 } + fun isGrowing(): Boolean { + return cityStats.currentCityStats.food > 0 && cityConstructions.currentConstruction != Constants.settler + } + + fun isStarving(): Boolean { + return cityStats.currentCityStats.food < 0 + } + + /** Take null to mean infinity. */ + fun getNumTurnsToNewPopulation(): Int? { + if (isGrowing()) { + var turnsToGrowth = ceil((population.getFoodToNextPopulation() - population.foodStored) + / cityStats.currentCityStats.food).toInt() + if (turnsToGrowth < 1) turnsToGrowth = 1 + return turnsToGrowth + } + + return null + } + + /** Take null to mean infinity. */ + fun getNumTurnsToStarvation(): Int? { + if (isStarving()) { + return floor(population.foodStored / -cityStats.currentCityStats.food).toInt() + 1 + } + + return null + } + fun getBuildingUniques(): Sequence = cityConstructions.getBuiltBuildings().flatMap { it.uniques.asSequence() } fun containsBuildingUnique(unique:String) = cityConstructions.getBuiltBuildings().any { it.uniques.contains(unique) } diff --git a/core/src/com/unciv/ui/cityscreen/CityScreen.kt b/core/src/com/unciv/ui/cityscreen/CityScreen.kt index 4cc2c7be05..df79fca66f 100644 --- a/core/src/com/unciv/ui/cityscreen/CityScreen.kt +++ b/core/src/com/unciv/ui/cityscreen/CityScreen.kt @@ -133,18 +133,14 @@ class CityScreen(internal val city: CityInfo) : CameraStageBaseScreen() { skin)).colspan(columns).row() val turnsToPopString : String - if (city.cityStats.currentCityStats.food > 0) { - if (city.cityConstructions.currentConstruction == Constants.settler) { - turnsToPopString = "Food converts to production".tr() - } else { - var turnsToPopulation = ceil((city.population.getFoodToNextPopulation()-city.population.foodStored) - / city.cityStats.currentCityStats.food).toInt() - if (turnsToPopulation < 1) turnsToPopulation = 1 - turnsToPopString = "[$turnsToPopulation] turns to new population".tr() - } - } else if (city.cityStats.currentCityStats.food < 0) { - val turnsToStarvation = floor(city.population.foodStored / -city.cityStats.currentCityStats.food).toInt() + 1 + if (city.isGrowing()) { + var turnsToGrowth = city.getNumTurnsToNewPopulation() + turnsToPopString = "[$turnsToGrowth] turns to new population".tr() + } else if (city.isStarving()) { + var turnsToStarvation = city.getNumTurnsToStarvation() turnsToPopString = "[$turnsToStarvation] turns to lose population".tr() + } else if (city.cityConstructions.currentConstruction == Constants.settler) { + turnsToPopString = "Food converts to production".tr() } else { turnsToPopString = "Stopped population growth".tr() } diff --git a/core/src/com/unciv/ui/tilegroups/CityButton.kt b/core/src/com/unciv/ui/tilegroups/CityButton.kt index 1726854767..9045d88559 100644 --- a/core/src/com/unciv/ui/tilegroups/CityButton.kt +++ b/core/src/com/unciv/ui/tilegroups/CityButton.kt @@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Interpolation import com.badlogic.gdx.scenes.scene2d.Group import com.badlogic.gdx.scenes.scene2d.Touchable import com.badlogic.gdx.scenes.scene2d.actions.Actions +import com.badlogic.gdx.scenes.scene2d.ui.Label import com.badlogic.gdx.scenes.scene2d.ui.Skin import com.badlogic.gdx.scenes.scene2d.ui.Table import com.badlogic.gdx.utils.Align @@ -124,7 +125,10 @@ class CityButton(val city: CityInfo, internal val tileGroup: WorldTileGroup, ski iconTable.add(connectionImage).size(20f).pad(2f).padLeft(5f) } - val cityButtonText = city.population.population.toString() + " | " + city.name + iconTable.add(getPopulationGroup(UncivGame.Current.viewEntireMapForDebug || belongsToViewingCiv())) + .padLeft(10f) + + val cityButtonText = city.name val label = cityButtonText.toLabel(secondaryColor) iconTable.add(label).pad(10f) // sufficient horizontal padding .fillY() // provide full-height clicking area @@ -155,6 +159,72 @@ class CityButton(val city: CityInfo, internal val tileGroup: WorldTileGroup, ski parent.addAction(floatAction) } + private fun getPopulationGroup(showGrowth: Boolean): Group { + val growthGreen = Color(0.0f, 0.5f, 0.0f, 1.0f) + + val group = Group() + + val populationLabel = city.population.population.toString().toLabel() + populationLabel.color = city.civInfo.nation.getInnerColor() + + group.addActor(populationLabel) + + val groupHeight = 25f + var groupWidth = populationLabel.width + if (showGrowth) groupWidth += 20f + group.setSize(groupWidth, groupHeight) + + if (showGrowth) { + var growthPercentage = city.population.foodStored / city.population.getFoodToNextPopulation().toFloat() + if (growthPercentage < 0) growthPercentage = 0.0f + + val growthBar = ImageGetter.getProgressBarVertical(2f, groupHeight, + if (city.isStarving()) 1.0f else growthPercentage, + if (city.isStarving()) Color.RED else growthGreen, Color.BLACK) + growthBar.x = populationLabel.width + 3 + growthBar.centerY(group) + + group.addActor(growthBar) + + val turnLabel : Label + if (city.isGrowing()) { + val turnsToGrowth = city.getNumTurnsToNewPopulation() + if (turnsToGrowth != null) { + if (turnsToGrowth < 100) { + turnLabel = turnsToGrowth.toString().toLabel() + } else { + turnLabel = "∞".toLabel() + } + } else { + turnLabel = "∞".toLabel() + } + } else if (city.isStarving()) { + val turnsToStarvation = city.getNumTurnsToStarvation() + if (turnsToStarvation != null) { + if (turnsToStarvation < 100) { + turnLabel = turnsToStarvation.toString().toLabel() + } else { + turnLabel = "∞".toLabel() + } + } else { + turnLabel = "∞".toLabel() + } + } else { + turnLabel = "∞".toLabel() + } + turnLabel.color = city.civInfo.nation.getInnerColor() + turnLabel.setFontSize(14) + turnLabel.pack() + + group.addActor(turnLabel) + turnLabel.x = growthBar.x + growthBar.width + 1 + } + + populationLabel.centerY(group) + + return group + } + private fun getConstructionGroup(cityConstructions: CityConstructions): Group { val group= Group() val groupHeight = 25f diff --git a/core/src/com/unciv/ui/utils/Fonts.kt b/core/src/com/unciv/ui/utils/Fonts.kt index 69cb0cef76..31a09efc66 100644 --- a/core/src/com/unciv/ui/utils/Fonts.kt +++ b/core/src/com/unciv/ui/utils/Fonts.kt @@ -24,7 +24,7 @@ class Fonts { "ÀÄĂÂĎÊĚÉÈÍÎŁĹĽÔÓÖƠŘŔŚŤƯŮÚÜÝŻŹäâąďêęěłĺľńňñôöơřŕśťưůýżźáèìíóûú" + "กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำิีึืฺุู฿เแโใไๅๆ็่้๊๋์ํ๎๏๐๑๒๓๔๕๖๗๘๙๚๛" + // Thai "1234567890" + - "‘?’'“!”(%)[#]{@}/&\\<-+÷×=>®©\$€£¥¢:;,.¡*|«»—" + "‘?’'“!”(%)[#]{@}/&\\<-+÷×=>®©\$€£¥¢:;,.¡*|«»—∞" val charSet = HashSet() charSet.addAll(defaultText.asIterable())