Empire Overview Tweaks to better utilize new TabbedPager (#6434)

This commit is contained in:
SomeTroglodyte 2022-03-26 21:16:51 +01:00 committed by GitHub
parent 2c2ffd02d1
commit 892fc1f936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 45 deletions

View File

@ -19,12 +19,6 @@ import com.unciv.ui.utils.*
import com.unciv.ui.utils.UncivTooltip.Companion.addTooltip
import kotlin.math.roundToInt
private fun String.isStat() = Stat.values().any { it.name == this }
private fun CityInfo.getStat(stat: Stat) =
if (stat == Stat.Happiness)
cityStats.happinessList.values.sum().roundToInt()
else cityStats.currentCityStats[stat].roundToInt()
class CityOverviewTab(
viewingPlayer: CivilizationInfo,
overviewScreen: EmpireOverviewScreen,
@ -60,6 +54,17 @@ class CityOverviewTab(
.apply { color = Color.BLACK }
.surroundWithCircle(iconSize, color = Color.LIGHT_GRAY)
.apply { addTooltip("Current construction", 18f, tipAlign = Align.center) }
// Readability helpers
private fun String.isStat() = Stat.values().any { it.name == this }
private fun CityInfo.getStat(stat: Stat) =
if (stat == Stat.Happiness)
cityStats.happinessList.values.sum().roundToInt()
else cityStats.currentCityStats[stat].roundToInt()
private fun Int.toCenteredLabel(): Label =
this.toLabel().apply { setAlignment(Align.center) }
}
private val columnsNames = arrayListOf("Population", "Food", "Gold", "Science", "Production", "Culture", "Happiness")
@ -85,6 +90,7 @@ class CityOverviewTab(
updateTotal()
update()
top()
add(cityInfoTableDetails).row()
addSeparator(Color.GRAY).pad(paddingVert, 0f)
add(cityInfoTableTotal)
@ -234,8 +240,4 @@ class CityOverviewTab(
cityInfoTableTotal.add(viewingPlayer.cities.count { it.isWeLoveTheKingDayActive() }.toCenteredLabel())
cityInfoTableTotal.pack()
}
private fun Int.toCenteredLabel(): Label =
this.toLabel().apply { setAlignment(Align.center) }
}

View File

@ -1,5 +1,6 @@
package com.unciv.ui.overviewscreen
import com.badlogic.gdx.utils.Align
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.ui.utils.KeyCharAndCode
import com.unciv.ui.overviewscreen.EmpireOverviewTab.EmpireOverviewTabPersistableData
@ -18,34 +19,35 @@ private fun Boolean.toState(): EmpireOverviewTabState = if (this) EmpireOverview
enum class EmpireOverviewCategories(
val iconName: String,
val shortcutKey: KeyCharAndCode,
val scrollAlign: Int,
val factory: FactoryType,
val stateTester: StateTesterType
) {
Cities("OtherIcons/Cities", 'C',
Cities("OtherIcons/Cities", 'C', Align.topLeft,
fun (viewingPlayer: CivilizationInfo, overviewScreen: EmpireOverviewScreen, persistedData: EmpireOverviewTabPersistableData?)
= CityOverviewTab(viewingPlayer, overviewScreen, persistedData),
fun (viewingPlayer: CivilizationInfo) = viewingPlayer.cities.isEmpty().toState()),
Stats("StatIcons/Gold", 'S',
Stats("StatIcons/Gold", 'S', Align.top,
fun (viewingPlayer: CivilizationInfo, overviewScreen: EmpireOverviewScreen, _: EmpireOverviewTabPersistableData?)
= StatsOverviewTab(viewingPlayer, overviewScreen),
fun (_: CivilizationInfo) = EmpireOverviewTabState.Normal),
Trades("StatIcons/Acquire", 'T',
Trades("StatIcons/Acquire", 'T', Align.top,
fun (viewingPlayer: CivilizationInfo, overviewScreen: EmpireOverviewScreen, _: EmpireOverviewTabPersistableData?)
= TradesOverviewTab(viewingPlayer, overviewScreen),
fun (viewingPlayer: CivilizationInfo) = viewingPlayer.diplomacy.values.all { it.trades.isEmpty() }.toState()),
Units("OtherIcons/Shield", 'U',
Units("OtherIcons/Shield", 'U', Align.topLeft,
fun (viewingPlayer: CivilizationInfo, overviewScreen: EmpireOverviewScreen, persistedData: EmpireOverviewTabPersistableData?)
= UnitOverviewTab(viewingPlayer, overviewScreen, persistedData),
fun (viewingPlayer: CivilizationInfo) = viewingPlayer.getCivUnits().none().toState()),
Diplomacy("OtherIcons/DiplomacyW", 'D',
Diplomacy("OtherIcons/DiplomacyW", 'D', Align.top,
fun (viewingPlayer: CivilizationInfo, overviewScreen: EmpireOverviewScreen, persistedData: EmpireOverviewTabPersistableData?)
= DiplomacyOverviewTab(viewingPlayer, overviewScreen, persistedData),
fun (viewingPlayer: CivilizationInfo) = viewingPlayer.diplomacy.isEmpty().toState()),
Resources("StatIcons/Happiness", 'R',
Resources("StatIcons/Happiness", 'R', Align.topLeft,
fun (viewingPlayer: CivilizationInfo, overviewScreen: EmpireOverviewScreen, _: EmpireOverviewTabPersistableData?)
= ResourcesOverviewTab(viewingPlayer, overviewScreen),
fun (viewingPlayer: CivilizationInfo) = viewingPlayer.detailedCivResources.isEmpty().toState()),
Religion("StatIcons/Faith", 'F',
Religion("StatIcons/Faith", 'F', Align.top,
fun (viewingPlayer: CivilizationInfo, overviewScreen: EmpireOverviewScreen, persistedData: EmpireOverviewTabPersistableData?)
= ReligionOverviewTab(viewingPlayer, overviewScreen, persistedData),
fun (viewingPlayer: CivilizationInfo) = when {
@ -53,13 +55,12 @@ enum class EmpireOverviewCategories(
viewingPlayer.gameInfo.religions.isEmpty() -> EmpireOverviewTabState.Disabled
else -> EmpireOverviewTabState.Normal
}),
Wonders("OtherIcons/Wonders", 'W',
Wonders("OtherIcons/Wonders", 'W', Align.top,
fun (viewingPlayer: CivilizationInfo, overviewScreen: EmpireOverviewScreen, _: EmpireOverviewTabPersistableData?)
= WonderOverviewTab(viewingPlayer, overviewScreen),
fun (viewingPlayer: CivilizationInfo) = (viewingPlayer.naturalWonders.isEmpty() && viewingPlayer.cities.isEmpty()).toState()),
;
constructor(iconName: String, shortcutChar: Char, factory: FactoryType, stateTester: StateTesterType = { _ -> EmpireOverviewTabState.Normal })
: this(iconName, KeyCharAndCode(shortcutChar), factory, stateTester)
constructor(iconName: String, shortcutChar: Char, scrollAlign: Int, factory: FactoryType, stateTester: StateTesterType = { _ -> EmpireOverviewTabState.Normal })
: this(iconName, KeyCharAndCode(shortcutChar), scrollAlign, factory, stateTester)
}

View File

@ -70,6 +70,7 @@ class EmpireOverviewScreen(
icon, iconSize,
disabled = tabState != EmpireOverviewTabState.Normal,
shortcutKey = category.shortcutKey,
scrollAlign = category.scrollAlign,
fixedContent = pageObject.getFixedContent(),
onDeactivation = { _, _, scrollY -> pageObject.deactivated(scrollY) }
) {
@ -96,4 +97,8 @@ class EmpireOverviewScreen(
}
}
fun resizePage(tab: EmpireOverviewTab) {
val category = (pageObjects.entries.find { it.value == tab } ?: return).key
tabbedPager.replacePage(category.name, tab, tab.getFixedContent())
}
}

View File

@ -38,18 +38,18 @@ class UnitOverviewTab(
private val supplyTableWidth = (overviewScreen.stage.width * 0.25f).coerceAtLeast(240f)
private val unitListTable = Table() // could be `this` instead, extra nesting helps readability a little
private val unitHeaderTable = Table()
private val fixedContent = Table()
override fun getFixedContent(): WidgetGroup {
return Table().apply {
add(getUnitSupplyTable()).align(Align.top).padBottom(10f).row()
add(unitHeaderTable.updateUnitHeaderTable())
equalizeColumns(unitListTable, unitHeaderTable)
}
return fixedContent
}
init {
fixedContent.add(getUnitSupplyTable()).align(Align.top).padBottom(10f).row()
fixedContent.add(unitHeaderTable.updateUnitHeaderTable())
top()
add(unitListTable.updateUnitListTable())
equalizeColumns(unitListTable, unitHeaderTable)
}
// Here overloads are simpler than a generic:
@ -89,7 +89,10 @@ class UnitOverviewTab(
icon = icon,
startsOutOpened = deficit > 0,
defaultPad = 0f,
expanderWidth = supplyTableWidth
expanderWidth = supplyTableWidth,
onChange = {
overviewScreen.resizePage(this)
}
) {
it.defaults().pad(5f).fill(false)
it.background = ImageGetter.getBackground(ImageGetter.getBlue().darken(0.6f))

View File

@ -2,6 +2,7 @@ package com.unciv.ui.overviewscreen
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup
import com.badlogic.gdx.utils.Align
import com.unciv.Constants
import com.unciv.UncivGame
@ -79,8 +80,30 @@ class WonderOverviewTab(
private val wonders: Array<WonderInfo> = collectInfo()
private val fixedContent = Table()
override fun getFixedContent(): WidgetGroup {
return fixedContent
}
init {
fixedContent.apply {
defaults().pad(10f).align(Align.center)
add()
add("Name".toLabel())
add("Status".toLabel())
add("Location".toLabel())
add().minWidth(30f)
row()
}
defaults().pad(10f).align(Align.center)
(1..5).forEach { _ -> add() } // dummies so equalizeColumns can work because the first grid cell is colspan(5)
row()
top()
createGrid()
equalizeColumns(fixedContent, this)
}
private fun shouldBeDisplayed(wonder: Building, wonderEra: Int) = when {
@ -114,8 +137,7 @@ class WonderOverviewTab(
val wonderEraMap: Map<String, Era> =
ruleSet.buildings.values.asSequence()
.filter { it.isWonder }
.map { it.name to (ruleSet.eras[ruleSet.technologies[it.requiredTech]?.era()] ?: viewingPlayer.getEra()) }
.toMap()
.associate { it.name to (ruleSet.eras[ruleSet.technologies[it.requiredTech]?.era()] ?: viewingPlayer.getEra()) }
// Maps all World Wonders by their position in sort order to their name
val allWonderMap: Map<Int, String> =
@ -123,8 +145,7 @@ class WonderOverviewTab(
.filter { it.isWonder }
.sortedWith(compareBy<Building> { wonderEraMap[it.name]!!.eraNumber }.thenBy(collator, { it.name.tr() }))
.withIndex()
.map { it.index to it.value.name }
.toMap()
.associate { it.index to it.value.name }
val wonderCount = allWonderMap.size
// Inverse of the above
@ -134,16 +155,14 @@ class WonderOverviewTab(
val allNaturalsMap: Map<String, TileInfo> =
gameInfo.tileMap.values.asSequence()
.filter { it.isNaturalWonder() }
.map { it.naturalWonder!! to it }
.toMap()
.associateBy { it.naturalWonder!! }
val naturalsCount = allNaturalsMap.size
// Natural Wonders sort order index to name
val naturalsIndexMap: Map<Int, String> = allNaturalsMap.keys
.sortedWith(compareBy(collator, { it.tr() }))
.sortedWith(compareBy(collator) { it.tr() })
.withIndex()
.map { it.index to it.value }
.toMap()
.associate { it.index to it.value }
// Pre-populate result with "Unknown" entries
val wonders = Array(wonderCount + naturalsCount) { index ->
@ -197,14 +216,6 @@ class WonderOverviewTab(
}
fun createGrid() {
defaults().pad(10f).align(Align.center)
add()
add("Name".toLabel())
add("Status".toLabel())
add("Location".toLabel())
add().minWidth(30f)
row()
//addSeparator()
var lastGroup = ""
for (wonder in wonders) {