Reuse code for spreading out tiles onto tile layer between Minimap and ReplayMap by factoring it out into a new MinimapTileUtil

This commit is contained in:
WhoIsJohannes 2023-03-09 13:38:06 +01:00
parent 93ef877deb
commit d4cddb4312
4 changed files with 38 additions and 48 deletions

View File

@ -4,6 +4,7 @@ import com.badlogic.gdx.graphics.g2d.Batch
import com.badlogic.gdx.scenes.scene2d.Group
import com.unciv.UncivGame
import com.unciv.logic.map.TileMap
import com.unciv.ui.screens.worldscreen.minimap.MinimapTileUtil
import com.unciv.ui.screens.worldscreen.minimap.MinimapTile
import kotlin.math.max
import kotlin.math.min
@ -18,32 +19,9 @@ class ReplayMap(val tileMap: TileMap) : Group() {
// render time!
isTransform = false
var topX = 0f
var topY = 0f
var bottomX = 0f
var bottomY = 0f
val tileSize = calcTileSize(22)
minimapTiles = createReplayMap(tileSize)
for (image in minimapTiles.map { it.image }) {
tileLayer.addActor(image)
// keeps track of the current top/bottom/left/rightmost tiles to size and position the
// minimap correctly
topX = max(topX, image.x + tileSize)
topY = max(topY, image.y + tileSize)
bottomX = min(bottomX, image.x)
bottomY = min(bottomY, image.y)
}
for (group in tileLayer.children) {
group.moveBy(-bottomX, -bottomY)
}
// there are tiles "below the zero",
// so we zero out the starting position of the whole board so they will be displayed as well
tileLayer.setSize(topX - bottomX, topY - bottomY)
MinimapTileUtil.spreadOutMinimapTiles(tileLayer, minimapTiles, tileSize)
setSize(tileLayer.width, tileLayer.height)
addActor(tileLayer)
}

View File

@ -27,31 +27,9 @@ class Minimap(val mapHolder: WorldMapHolder, minimapSize: Int) : Group() {
// don't try to resize rotate etc - this table has a LOT of children so that's valuable render time!
isTransform = false
var topX = 0f
var topY = 0f
var bottomX = 0f
var bottomY = 0f
val tileSize = calcTileSize(minimapSize)
minimapTiles = createMinimapTiles(tileSize)
for (image in minimapTiles.map { it.image }) {
tileLayer.addActor(image)
// keeps track of the current top/bottom/left/rightmost tiles to size and position the minimap correctly
topX = max(topX, image.x + tileSize)
topY = max(topY, image.y + tileSize)
bottomX = min(bottomX, image.x)
bottomY = min(bottomY, image.y)
}
for (group in tileLayer.children) {
group.moveBy(-bottomX, -bottomY)
}
// there are tiles "below the zero",
// so we zero out the starting position of the whole board so they will be displayed as well
tileLayer.setSize(topX - bottomX, topY - bottomY)
MinimapTileUtil.spreadOutMinimapTiles(tileLayer, minimapTiles, tileSize)
scrollPositionIndicators = createScrollPositionIndicators()
scrollPositionIndicators.forEach(tileLayer::addActor)

View File

@ -16,7 +16,7 @@ import com.unciv.utils.DebugUtils
import kotlin.math.PI
import kotlin.math.atan
internal class MinimapTile(val tile: Tile, tileSize: Float, val onClick: () -> Unit) {
class MinimapTile(val tile: Tile, tileSize: Float, val onClick: () -> Unit) {
val image: Image = ImageGetter.getImage("OtherIcons/Hexagon")
private var cityCircleImage: IconCircleGroup? = null
var owningCiv: Civilization? = null

View File

@ -0,0 +1,34 @@
package com.unciv.ui.screens.worldscreen.minimap
import com.badlogic.gdx.scenes.scene2d.Group
import kotlin.math.max
import kotlin.math.min
object MinimapTileUtil {
fun spreadOutMinimapTiles(tileLayer: Group, tiles: List<MinimapTile>, tileSize: Float) {
var topX = 0f
var topY = 0f
var bottomX = 0f
var bottomY = 0f
for (image in tiles.map { it.image }) {
tileLayer.addActor(image)
// keeps track of the current top/bottom/left/rightmost tiles to size and position the
// minimap correctly
topX = max(topX, image.x + tileSize)
topY = max(topY, image.y + tileSize)
bottomX = min(bottomX, image.x)
bottomY = min(bottomY, image.y)
}
for (group in tileLayer.children) {
group.moveBy(-bottomX, -bottomY)
}
// there are tiles "below the zero",
// so we zero out the starting position of the whole board so they will be displayed as well
tileLayer.setSize(topX - bottomX, topY - bottomY)
}
}