From 892fc1f936848366dccbc462e4b474a702d8f50f Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Sat, 26 Mar 2022 21:16:51 +0100 Subject: [PATCH] Empire Overview Tweaks to better utilize new TabbedPager (#6434) --- .../ui/overviewscreen/CityOverviewTable.kt | 22 ++++----- .../EmpireOverviewCategories.kt | 23 +++++----- .../ui/overviewscreen/EmpireOverviewScreen.kt | 5 +++ .../ui/overviewscreen/UnitOverviewTable.kt | 17 ++++--- .../ui/overviewscreen/WonderOverviewTable.kt | 45 ++++++++++++------- 5 files changed, 67 insertions(+), 45 deletions(-) diff --git a/core/src/com/unciv/ui/overviewscreen/CityOverviewTable.kt b/core/src/com/unciv/ui/overviewscreen/CityOverviewTable.kt index d27d6ef526..4f1eb17f1f 100644 --- a/core/src/com/unciv/ui/overviewscreen/CityOverviewTable.kt +++ b/core/src/com/unciv/ui/overviewscreen/CityOverviewTable.kt @@ -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) } - } diff --git a/core/src/com/unciv/ui/overviewscreen/EmpireOverviewCategories.kt b/core/src/com/unciv/ui/overviewscreen/EmpireOverviewCategories.kt index 10efc58acf..28fe2bb5bf 100644 --- a/core/src/com/unciv/ui/overviewscreen/EmpireOverviewCategories.kt +++ b/core/src/com/unciv/ui/overviewscreen/EmpireOverviewCategories.kt @@ -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) } - diff --git a/core/src/com/unciv/ui/overviewscreen/EmpireOverviewScreen.kt b/core/src/com/unciv/ui/overviewscreen/EmpireOverviewScreen.kt index fdd292d515..05339a9bd2 100644 --- a/core/src/com/unciv/ui/overviewscreen/EmpireOverviewScreen.kt +++ b/core/src/com/unciv/ui/overviewscreen/EmpireOverviewScreen.kt @@ -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()) + } } diff --git a/core/src/com/unciv/ui/overviewscreen/UnitOverviewTable.kt b/core/src/com/unciv/ui/overviewscreen/UnitOverviewTable.kt index 99e4ef5310..21f83c7b9b 100644 --- a/core/src/com/unciv/ui/overviewscreen/UnitOverviewTable.kt +++ b/core/src/com/unciv/ui/overviewscreen/UnitOverviewTable.kt @@ -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)) diff --git a/core/src/com/unciv/ui/overviewscreen/WonderOverviewTable.kt b/core/src/com/unciv/ui/overviewscreen/WonderOverviewTable.kt index 385963a4ba..0525074e4b 100644 --- a/core/src/com/unciv/ui/overviewscreen/WonderOverviewTable.kt +++ b/core/src/com/unciv/ui/overviewscreen/WonderOverviewTable.kt @@ -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 = 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 = 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 = @@ -123,8 +145,7 @@ class WonderOverviewTab( .filter { it.isWonder } .sortedWith(compareBy { 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 = 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 = 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) {