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