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.
This commit is contained in:
drwhut 2020-01-07 16:55:38 +00:00 committed by Yair Morgenstern
parent 6420f36fb3
commit a13570f109
4 changed files with 110 additions and 13 deletions

View file

@ -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<String> = cityConstructions.getBuiltBuildings().flatMap { it.uniques.asSequence() }
fun containsBuildingUnique(unique:String) = cityConstructions.getBuiltBuildings().any { it.uniques.contains(unique) }

View file

@ -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()
}

View file

@ -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

View file

@ -24,7 +24,7 @@ class Fonts {
"ÀÄĂÂĎÊĚÉÈÍÎŁĹĽÔÓÖƠŘŔŚŤƯŮÚÜÝŻŹäâąďêęěłĺľńňñôöơřŕśťưůýżźáèìíóûú" +
"กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะัาำิีึืฺุู฿เแโใไๅๆ็่้๊๋์ํ๎๏๐๑๒๓๔๕๖๗๘๙๚๛" + // Thai
"1234567890" +
"?'“!”(%)[#]{@}/&\\<-+÷×=>®©\$€£¥¢:;,.¡*|«»—"
"?'“!”(%)[#]{@}/&\\<-+÷×=>®©\$€£¥¢:;,.¡*|«»—"
val charSet = HashSet<Char>()
charSet.addAll(defaultText.asIterable())