SIGNIFICANTLY decreased memory usage by only initializing overlay images when required

This commit is contained in:
yairm210 2024-09-17 17:37:23 +03:00
parent 7fd1a10a46
commit 98c49ecea3

View File

@ -2,6 +2,7 @@ package com.unciv.ui.components.tilegroups.layers
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.Actor
import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.unciv.Constants
import com.unciv.logic.civilization.Civilization
import com.unciv.models.ruleset.unique.LocalUniqueCache
@ -13,88 +14,126 @@ class TileLayerOverlay(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup,
override fun act(delta: Float) {}
override fun hit(x: Float, y: Float, touchable: Boolean): Actor? = null
private val highlight = ImageGetter.getImage(strings().highlight).setHexagonSize() // for blue and red circles/emphasis on the tile
private val crosshair = ImageGetter.getImage(strings().crosshair).setHexagonSize() // for when a unit is targeted
private val goodCityLocationIndicator = ImageGetter.getImage("OtherIcons/Cities").setHexagonSize(0.25f)
private val fog = ImageGetter.getImage(strings().crosshatchHexagon ).setHexagonSize()
private val unexplored = ImageGetter.getImage(strings().unexploredTile ).setHexagonSize()
private var highlight: Image? = null // for blue and red circles/emphasis on the tile
private var crosshair: Image? = null // for when a unit is targeted
private var goodCityLocationIndicator: Image? = null
private var fog: Image? = null
private var unexplored: Image? = null
init {
highlight.isVisible = false
crosshair.isVisible = false
goodCityLocationIndicator.isVisible = false
fog.isVisible = false
fog.color = Color.WHITE.cpy().apply { a = 0.2f }
if (ImageGetter.imageExists(strings().unexploredTile))
addActor(unexplored)
addActor(highlight)
addActor(fog)
addActor(crosshair)
addActor(goodCityLocationIndicator)
private fun getHighlight() = ImageGetter.getImage(strings().highlight).setHexagonSize() // for blue and red circles/emphasis on the tile
private fun getCrosshair() = ImageGetter.getImage(strings().crosshair).setHexagonSize() // for when a unit is targeted
private fun getGoodCityLocationIndicator() = ImageGetter.getImage("OtherIcons/Cities").setHexagonSize(0.25f)
private fun getFog() = ImageGetter.getImage(strings().crosshatchHexagon ).setHexagonSize().apply {
color = Color.WHITE.cpy().apply { a = 0.2f }
}
private fun getUnexplored() = ImageGetter.getImage(strings().unexploredTile ).setHexagonSize()
fun orderToFront() {
unexplored?.toFront()
highlight?.toFront()
fog?.toFront()
crosshair?.toFront()
goodCityLocationIndicator?.toFront()
}
fun showCrosshair(alpha: Float = 1f) {
crosshair.isVisible = true
crosshair.color.a = alpha
determineVisibility()
if (crosshair != null){
crosshair = getCrosshair()
addActor(crosshair)
determineVisibility()
}
crosshair!!.color.a = alpha
}
fun hideCrosshair() {
crosshair.isVisible = false
if (crosshair == null) return
crosshair?.remove()
crosshair = null
determineVisibility()
}
fun showHighlight(color: Color, alpha: Float = 0.3f) {
highlight.isVisible = true
highlight.color = color.cpy().apply { a = alpha }
determineVisibility()
}
fun showHighlight() {
highlight.isVisible = true
determineVisibility()
fun showHighlight(color: Color = Color.WHITE, alpha: Float = 0.3f) {
if (highlight == null) {
highlight = getHighlight()
addActor(highlight)
determineVisibility()
}
highlight!!.color = color.cpy().apply { a = alpha }
}
fun hideHighlight() {
highlight.isVisible = false
if (highlight == null) return
highlight?.remove()
highlight = null
determineVisibility()
}
fun showGoodCityLocationIndicator() {
goodCityLocationIndicator.isVisible = true
if (goodCityLocationIndicator != null) return
goodCityLocationIndicator = getGoodCityLocationIndicator()
addActor(goodCityLocationIndicator)
determineVisibility()
}
fun hideGoodCityLocationIndicator() {
goodCityLocationIndicator.isVisible = false
if (goodCityLocationIndicator == null) return
goodCityLocationIndicator?.remove()
goodCityLocationIndicator = null
determineVisibility()
}
fun reset() {
fog.isVisible = true
highlight.isVisible = false
crosshair.isVisible = false
goodCityLocationIndicator.isVisible = false
hideHighlight()
hideCrosshair()
hideGoodCityLocationIndicator()
determineVisibility()
}
override fun doUpdate(viewingCiv: Civilization?, localUniqueCache: LocalUniqueCache) {
val isViewable = viewingCiv == null || isViewable(viewingCiv)
fog.isVisible = !isViewable && !tileGroup.isForceVisible
setFog(isViewable)
if (viewingCiv == null) return
if (viewingCiv == null)
return
setUnexplored(viewingCiv)
unexplored.isVisible = !viewingCiv.hasExplored(tile())
if (tile().getShownImprovement(viewingCiv) == Constants.barbarianEncampment
&& tile().isExplored(viewingCiv))
showHighlight(Color.RED)
}
private fun setUnexplored(viewingCiv: Civilization) {
val unexploredShouldBeVisible = !viewingCiv.hasExplored(tile())
val unexploredIsVisible = unexplored != null
if (unexploredIsVisible && !unexploredShouldBeVisible) {
unexplored?.remove()
determineVisibility()
} else if (!unexploredIsVisible && unexploredShouldBeVisible
&& ImageGetter.imageExists(strings().unexploredTile)) {
unexplored = getUnexplored()
addActor(unexplored)
determineVisibility()
}
}
private fun setFog(isViewable: Boolean) {
val fogShouldBeVisible = !isViewable && !tileGroup.isForceVisible
val fogIsVisible = fog != null
if (fogIsVisible && !fogShouldBeVisible) {
fog?.remove()
fog = null
determineVisibility()
} else if (!fogIsVisible && fogShouldBeVisible) {
fog = getFog()
addActor(fog)
determineVisibility()
}
}
override fun determineVisibility() {
isVisible = fog.isVisible || highlight.isVisible || crosshair.isVisible || goodCityLocationIndicator.isVisible
isVisible = fog != null || unexplored != null || highlight != null || crosshair != null || goodCityLocationIndicator != null
orderToFront()
}
}