From 2dfeb4a14269ee1577c4c5e10535e9019676d358 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Thu, 5 Dec 2024 20:37:59 +0200 Subject: [PATCH] perf(mem): Only create terrainOverlay images when needed --- .../tilegroups/layers/TileLayerMisc.kt | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerMisc.kt b/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerMisc.kt index 3e706da574..8be1edd463 100644 --- a/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerMisc.kt +++ b/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerMisc.kt @@ -50,12 +50,7 @@ private class MapArrow(val targetTile: Tile, val arrowType: MapArrowType, val st class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, size) { // For different unit views, we want to effectively "ignore" the terrain and color it by special view - private val terrainOverlay = ImageGetter.getImage(strings().hexagon).setHexagonSize() - - init { - terrainOverlay.isVisible = false - addActor(terrainOverlay) - } + private var terrainOverlay: Image? = ImageGetter.getImage(strings().hexagon).setHexagonSize() override fun act(delta: Float) {} override fun hit(x: Float, y: Float, touchable: Boolean): Actor? { @@ -337,21 +332,23 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si } /** Activates a colored semitransparent overlay. [color] is cloned, brightened by 0.3f and an alpha of 0.4f applied. */ - fun overlayTerrain(color: Color) { - terrainOverlay.color = color.brighten(0.3f).apply { a = 0.4f } - terrainOverlay.isVisible = true - determineVisibility() - } + fun overlayTerrain(color: Color) = overlayTerrainInner(color.brighten(0.3f).apply { a = 0.4f }) /** Activates a colored semitransparent overlay. [color] is cloned and [alpha] applied. No brightening unlike the overload without explicit alpha! */ - fun overlayTerrain(color: Color, alpha: Float) { - terrainOverlay.color = color.cpy().apply { a = alpha } - terrainOverlay.isVisible = true + fun overlayTerrain(color: Color, alpha: Float) = overlayTerrainInner(color.cpy().apply { a = alpha }) + + private fun overlayTerrainInner(color: Color) { + if (terrainOverlay == null){ + terrainOverlay = ImageGetter.getImage(strings().hexagon).setHexagonSize() + addActor(terrainOverlay) + } + terrainOverlay?.color = color determineVisibility() } fun hideTerrainOverlay() { - terrainOverlay.isVisible = false + terrainOverlay?.remove() + terrainOverlay = null determineVisibility() } @@ -401,7 +398,7 @@ class TileLayerMisc(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, si || hexOutlineIcon != null || arrows.isNotEmpty() || startingLocationIcons.isNotEmpty() - || terrainOverlay.isVisible + || terrainOverlay != null } fun reset(localUniqueCache: LocalUniqueCache) {