From 9543f2f709209d589aa094f6ace5e6cc3a202efd Mon Sep 17 00:00:00 2001 From: Alexander Korolyov <49795502+alkorolyov@users.noreply.github.com> Date: Sun, 2 Aug 2020 11:38:12 +0200 Subject: [PATCH] Top bar selected civ refactor, increase performance for updates. (#2913) --- .../unciv/ui/worldscreen/WorldScreenTopBar.kt | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreenTopBar.kt b/core/src/com/unciv/ui/worldscreen/WorldScreenTopBar.kt index 68ed6b51a0..399ea303f3 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreenTopBar.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreenTopBar.kt @@ -3,10 +3,7 @@ package com.unciv.ui.worldscreen import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.Group -import com.badlogic.gdx.scenes.scene2d.ui.Image -import com.badlogic.gdx.scenes.scene2d.ui.Label -import com.badlogic.gdx.scenes.scene2d.ui.Table -import com.badlogic.gdx.scenes.scene2d.ui.TextButton +import com.badlogic.gdx.scenes.scene2d.ui.* import com.unciv.logic.civilization.CivilizationInfo import com.unciv.models.metadata.GameSpeed import com.unciv.models.ruleset.tile.ResourceType @@ -24,7 +21,8 @@ import kotlin.math.roundToInt class WorldScreenTopBar(val worldScreen: WorldScreen) : Table() { - private var selectedCivTable = Table() + private var selectedCivLabel = worldScreen.selectedCiv.civName.toLabel() + private var selectedCivIconHolder = Container() private val turnsLabel = "Turns: 0/400".toLabel() private val goldLabel = "Gold:".toLabel(colorFromRGB(225, 217, 71)) @@ -51,7 +49,7 @@ class WorldScreenTopBar(val worldScreen: WorldScreen) : Table() { pack() addActor(getMenuButton()) // needs to be after pack - addSelectedCivilizationTable() + addActor(getSelectedCivilizationTable()) addActor(getOverviewButton()) } @@ -144,17 +142,29 @@ class WorldScreenTopBar(val worldScreen: WorldScreen) : Table() { overviewButton.labelCell.pad(10f) overviewButton.pack() overviewButton.onClick { worldScreen.game.setScreen(EmpireOverviewScreen(worldScreen.selectedCiv)) } - overviewButton.center(this) + overviewButton.centerY(this) overviewButton.x = worldScreen.stage.width - overviewButton.width - 10 return overviewButton } - private fun addSelectedCivilizationTable() { + private fun getSelectedCivilizationTable(): Table { + val selectedCivTable = Table() selectedCivTable.centerY(this) selectedCivTable.left() selectedCivTable.x = getMenuButton().width + 20f - updateSelectedCivTabel() - addActor(selectedCivTable) + + selectedCivLabel.setFontSize(25) + selectedCivLabel.onClick { worldScreen.game.setScreen(EmpireOverviewScreen(worldScreen.selectedCiv)) } + + val nation = worldScreen.gameInfo.ruleSet.nations[worldScreen.selectedCiv.civName]!! + val selectedCivIcon = ImageGetter.getNationIndicator(nation, 35f) + selectedCivIconHolder.actor = selectedCivIcon + selectedCivIconHolder.onClick { worldScreen.game.setScreen(EmpireOverviewScreen(worldScreen.selectedCiv)) } + + selectedCivTable.add(selectedCivLabel).padRight(10f) + selectedCivTable.add(selectedCivIconHolder) + return selectedCivTable + } internal fun update(civInfo: CivilizationInfo) { @@ -199,17 +209,13 @@ class WorldScreenTopBar(val worldScreen: WorldScreen) : Table() { } private fun updateSelectedCivTabel() { - selectedCivTable.clear() + if (selectedCivLabel.text.toString() == worldScreen.selectedCiv.civName) return - val selectedCivLabel = worldScreen.selectedCiv.civName.toLabel() - selectedCivLabel.setFontSize(25) - selectedCivLabel.onClick { worldScreen.game.setScreen(EmpireOverviewScreen(worldScreen.selectedCiv)) } - selectedCivTable.add(selectedCivLabel).padRight(10f) + selectedCivLabel.setText(worldScreen.selectedCiv.civName) val nation = worldScreen.gameInfo.ruleSet.nations[worldScreen.selectedCiv.civName]!! - selectedCivTable.add(ImageGetter.getNationIndicator(nation, 35f).onClick { - worldScreen.game.setScreen(EmpireOverviewScreen(worldScreen.selectedCiv)) - }) + val selectedCivIcon = ImageGetter.getNationIndicator(nation, 35f) + selectedCivIconHolder.actor = selectedCivIcon } private fun getCultureText(civInfo: CivilizationInfo, nextTurnStats: Stats): String {