From 6a070358eba27eea7e7d793b3b569c023e049dd8 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Fri, 11 Feb 2022 10:30:37 +0200 Subject: [PATCH] Rendering performance improvements - removed nation icon from minimap for less texture switching, set isTransform on vertical progress bars to false, added a ton of debug-help functions --- core/src/com/unciv/ui/map/TileGroupMap.kt | 11 ++++---- .../src/com/unciv/ui/tilegroups/CityButton.kt | 2 +- core/src/com/unciv/ui/tilegroups/TileGroup.kt | 8 +++++- .../src/com/unciv/ui/utils/IconCircleGroup.kt | 3 ++ core/src/com/unciv/ui/utils/ImageGetter.kt | 1 + core/src/com/unciv/ui/worldscreen/Minimap.kt | 28 ++++++++++++------- 6 files changed, 36 insertions(+), 17 deletions(-) diff --git a/core/src/com/unciv/ui/map/TileGroupMap.kt b/core/src/com/unciv/ui/map/TileGroupMap.kt index ea3a8a77f3..ff1e7eda26 100644 --- a/core/src/com/unciv/ui/map/TileGroupMap.kt +++ b/core/src/com/unciv/ui/map/TileGroupMap.kt @@ -5,6 +5,7 @@ import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.scenes.scene2d.Group import com.unciv.logic.HexMath import com.unciv.logic.map.TileInfo +import com.unciv.ui.tilegroups.ActionlessGroup import com.unciv.ui.tilegroups.TileGroup import kotlin.math.max import kotlin.math.min @@ -78,13 +79,13 @@ class TileGroupMap( } } - val baseLayers = ArrayList() - val featureLayers = ArrayList() - val miscLayers = ArrayList() + val baseLayers = ArrayList() + val featureLayers = ArrayList() + val miscLayers = ArrayList() val unitLayers = ArrayList() - val unitImageLayers = ArrayList() + val unitImageLayers = ArrayList() val cityButtonLayers = ArrayList() - val circleCrosshairFogLayers = ArrayList() + val circleCrosshairFogLayers = ArrayList() // Apparently the sortedByDescending is kinda memory-intensive because it needs to sort ALL the tiles for (group in tileGroups.sortedByDescending { it.tileInfo.position.x + it.tileInfo.position.y }) { diff --git a/core/src/com/unciv/ui/tilegroups/CityButton.kt b/core/src/com/unciv/ui/tilegroups/CityButton.kt index d0434b985b..ded30567bd 100644 --- a/core/src/com/unciv/ui/tilegroups/CityButton.kt +++ b/core/src/com/unciv/ui/tilegroups/CityButton.kt @@ -182,7 +182,7 @@ class CityButton(val city: CityInfo, private val tileGroup: WorldTileGroup): Tab class IconTable: Table() { override fun draw(batch: Batch?, parentAlpha: Float) { super.draw(batch, parentAlpha) } } - val iconTable = IconTable() + val iconTable = IconTable().apply { isTransform = false } iconTable.touchable = Touchable.enabled iconTable.background = ImageGetter.getRoundedEdgeRectangle(city.civInfo.nation.getOuterColor()) diff --git a/core/src/com/unciv/ui/tilegroups/TileGroup.kt b/core/src/com/unciv/ui/tilegroups/TileGroup.kt index 5a50b75954..b3bdcee0c5 100644 --- a/core/src/com/unciv/ui/tilegroups/TileGroup.kt +++ b/core/src/com/unciv/ui/tilegroups/TileGroup.kt @@ -119,7 +119,13 @@ open class TileGroup(var tileInfo: TileInfo, val tileSetStrings:TileSetStrings, val unitLayerGroup = UnitLayerGroupClass().apply { isTransform = false; setSize(groupSize, groupSize);touchable = Touchable.disabled } val unitImageLayerGroup = UnitImageLayerGroupClass().apply { isTransform = false; setSize(groupSize, groupSize);touchable = Touchable.disabled } - val cityButtonLayerGroup = Group().apply { isTransform = false; setSize(groupSize, groupSize) + class CityButtonLayerGroupClass:Group() { + override fun draw(batch: Batch?, parentAlpha: Float) = super.draw(batch, parentAlpha) + override fun act(delta: Float) = super.act(delta) + override fun hit(x: Float, y: Float, touchable: Boolean) = super.hit(x, y, touchable) + } + + val cityButtonLayerGroup = CityButtonLayerGroupClass().apply { isTransform = false; setSize(groupSize, groupSize) touchable = Touchable.childrenOnly; setOrigin(Align.center) } val highlightCrosshairFogLayerGroup = ActionlessGroup().apply { isTransform = false; setSize(groupSize, groupSize) } diff --git a/core/src/com/unciv/ui/utils/IconCircleGroup.kt b/core/src/com/unciv/ui/utils/IconCircleGroup.kt index fd7d486c3b..1d3e1433fd 100644 --- a/core/src/com/unciv/ui/utils/IconCircleGroup.kt +++ b/core/src/com/unciv/ui/utils/IconCircleGroup.kt @@ -1,6 +1,7 @@ package com.unciv.ui.utils import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.g2d.Batch import com.badlogic.gdx.scenes.scene2d.Actor import com.badlogic.gdx.scenes.scene2d.Group import com.badlogic.gdx.utils.Align @@ -20,4 +21,6 @@ class IconCircleGroup(size: Float, val actor: Actor, resizeActor: Boolean = true actor.setOrigin(Align.center) addActor(actor) } + + override fun draw(batch: Batch?, parentAlpha: Float) = super.draw(batch, parentAlpha) } diff --git a/core/src/com/unciv/ui/utils/ImageGetter.kt b/core/src/com/unciv/ui/utils/ImageGetter.kt index fca4e788a5..d624a5a0cf 100644 --- a/core/src/com/unciv/ui/utils/ImageGetter.kt +++ b/core/src/com/unciv/ui/utils/ImageGetter.kt @@ -380,6 +380,7 @@ object ImageGetter { class VerticalProgressBar(width: Float, height: Float):Group() { init { setSize(width, height) + isTransform = false } fun addColor(color: Color, percentage: Float): VerticalProgressBar { diff --git a/core/src/com/unciv/ui/worldscreen/Minimap.kt b/core/src/com/unciv/ui/worldscreen/Minimap.kt index 2385115164..51baea47ee 100644 --- a/core/src/com/unciv/ui/worldscreen/Minimap.kt +++ b/core/src/com/unciv/ui/worldscreen/Minimap.kt @@ -17,6 +17,7 @@ import com.unciv.logic.civilization.CivilizationInfo import com.unciv.logic.map.MapShape import com.unciv.logic.map.MapSize import com.unciv.logic.map.TileInfo +import com.unciv.ui.tilegroups.ActionlessGroup import com.unciv.ui.utils.IconCircleGroup import com.unciv.ui.utils.ImageGetter import com.unciv.ui.utils.onClick @@ -24,11 +25,13 @@ import com.unciv.ui.utils.surroundWithCircle import kotlin.math.max import kotlin.math.min -class Minimap(val mapHolder: WorldMapHolder, minimapSize: Int) : Table(){ +class Minimap(val mapHolder: WorldMapHolder, minimapSize: Int) : Group(){ private val allTiles = Group() private val tileImages = HashMap() private val scrollPositionIndicators = ArrayList() + private val cityIconsGroup = ActionlessGroup() + init { isTransform = false // don't try to resize rotate etc - this table has a LOT of children so that's valuable render time! @@ -93,8 +96,10 @@ class Minimap(val mapHolder: WorldMapHolder, minimapSize: Int) : Table(){ allTiles.addActor(indicator) } - add(allTiles) - layout() + setSize(allTiles.width, allTiles.height) + addActor(allTiles) + cityIconsGroup.setSize(width, height) + addActor(cityIconsGroup) } /**### Transform and set coordinates for the scrollPositionIndicator. @@ -135,7 +140,7 @@ class Minimap(val mapHolder: WorldMapHolder, minimapSize: Int) : Table(){ private val cityIcons = HashMap() fun update(cloneCivilization: CivilizationInfo) { - for((tileInfo, hex) in tileImages) { + for ((tileInfo, hex) in tileImages) { hex.color = when { !(UncivGame.Current.viewEntireMapForDebug || cloneCivilization.exploredTiles.contains(tileInfo.position)) -> Color.DARK_GRAY tileInfo.isCityCenter() && !tileInfo.isWater -> tileInfo.getOwner()!!.nation.getInnerColor() @@ -146,16 +151,20 @@ class Minimap(val mapHolder: WorldMapHolder, minimapSize: Int) : Table(){ if (tileInfo.isCityCenter() && cloneCivilization.exploredTiles.contains(tileInfo.position) && (!cityIcons.containsKey(tileInfo) || cityIcons[tileInfo]!!.civInfo != tileInfo.getOwner())) { if (cityIcons.containsKey(tileInfo)) cityIcons[tileInfo]!!.image.remove() // city changed hands - remove old icon - val nationIcon= ImageGetter.getNationIndicator(tileInfo.getOwner()!!.nation,hex.width * 3) - nationIcon.setPosition(hex.x - nationIcon.width/3,hex.y - nationIcon.height/3) + val nation = tileInfo.getOwner()!!.nation + val nationIcon= ImageGetter.getCircle().apply { color = nation.getInnerColor() }.surroundWithCircle(hex.width, color = nation.getOuterColor()) + nationIcon.setPosition(hex.x, hex.y) nationIcon.onClick { mapHolder.setCenterPosition(tileInfo.position) } - allTiles.addActor(nationIcon) + cityIconsGroup.addActor(nationIcon) cityIcons[tileInfo] = CivAndImage(tileInfo.getOwner()!!, nationIcon) } } } + + // For debugging purposes + override fun draw(batch: Batch?, parentAlpha: Float) = super.draw(batch, parentAlpha) } class MinimapHolder(val mapHolder: WorldMapHolder): Table() { @@ -242,6 +251,7 @@ class MinimapHolder(val mapHolder: WorldMapHolder): Table() { init { rebuildIfSizeChanged() } + private fun rebuildIfSizeChanged() { val newMinimapSize = worldScreen.game.settings.minimapSize if (newMinimapSize == minimapSize) return @@ -295,9 +305,7 @@ class MinimapHolder(val mapHolder: WorldMapHolder): Table() { // For debugging purposes - override fun draw(batch: Batch?, parentAlpha: Float) { - super.draw(batch, parentAlpha) - } + override fun draw(batch: Batch?, parentAlpha: Float) = super.draw(batch, parentAlpha) } private class ClippingImage(drawable: Drawable) : Image(drawable) {