More performance

- Shoved TextureRegionDrawables into a hashmap for faster searching and more unified access (not everyone has to create a Drawable for the Region now)
- getLanguages() now does the minimum amount of work without saving intermediates

Unified ImageGetter.getTableBackground()  (helper, not performance)
This commit is contained in:
Yair Morgenstern 2019-10-31 12:59:19 +02:00
parent de30382536
commit 23178836cf
8 changed files with 59 additions and 38 deletions

View File

@ -42,7 +42,11 @@ class Translations : HashMap<String, TranslationEntry>(){
fun getLanguages(): List<String> {
val toReturn = mutableListOf<String>()
toReturn.addAll(values.flatMap { it.keys }.distinct())
for(value in values)
for(key in keys)
if(!toReturn.contains(key)) toReturn.add(key)
toReturn.remove("Japanese") // These were for tests but were never actually seriously translated
toReturn.remove("Thai")
return toReturn

View File

@ -404,24 +404,25 @@ class EmpireOverviewScreen : CameraStageBaseScreen(){
companion object {
fun getCivGroup(civ: CivilizationInfo, afterCivNameText:String,currentPlayer:CivilizationInfo): Table {
val civGroup = Table()
val civGroupBackground = ImageGetter.getDrawable("OtherIcons/civTableBackground")
var labelText = civ.civName.tr()+afterCivNameText
var labelColor=Color.WHITE
val backgroundColor:Color
if (civ.isDefeated()) {
civGroup.add(ImageGetter.getImage("OtherIcons/DisbandUnit")).size(30f)
civGroup.background = civGroupBackground.tint(Color.LIGHT_GRAY)
backgroundColor = Color.LIGHT_GRAY
labelColor = Color.BLACK
} else if (currentPlayer==civ || UnCivGame.Current.viewEntireMapForDebug || currentPlayer.knows(civ)) {
civGroup.add(ImageGetter.getNationIndicator(civ.nation, 30f))
civGroup.background = civGroupBackground.tint(civ.nation.getOuterColor())
backgroundColor = civ.nation.getOuterColor()
labelColor = civ.nation.getInnerColor()
} else {
civGroup.background = civGroupBackground.tint(Color.DARK_GRAY)
backgroundColor = Color.DARK_GRAY
labelText = "???"
}
civGroup.background = ImageGetter.getTableBackground(backgroundColor)
val label = labelText.toLabel(labelColor)
label.setAlignment(Align.center)

View File

@ -1,10 +1,15 @@
package com.unciv.ui.cityscreen
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.scenes.scene2d.ui.*
import com.badlogic.gdx.scenes.scene2d.ui.Table
import com.badlogic.gdx.scenes.scene2d.ui.TextButton
import com.badlogic.gdx.scenes.scene2d.ui.TextField
import com.badlogic.gdx.utils.Align
import com.unciv.models.gamebasics.tr
import com.unciv.ui.utils.*
import com.unciv.ui.utils.CameraStageBaseScreen
import com.unciv.ui.utils.ImageGetter
import com.unciv.ui.utils.onClick
import com.unciv.ui.utils.toLabel
import com.unciv.ui.worldscreen.optionstable.PopupTable
class CityScreenCityPickerTable(val cityScreen: CityScreen) : Table(){
@ -31,12 +36,12 @@ class CityScreenCityPickerTable(val cityScreen: CityScreen) : Table(){
}
if(city.isCapital()){
val starImage = Image(ImageGetter.getDrawable("OtherIcons/Star").tint(Color.LIGHT_GRAY))
val starImage = ImageGetter.getImage("OtherIcons/Star").apply { color= Color.LIGHT_GRAY }
cityNameTable.add(starImage).size(20f).padRight(5f)
}
if(city.isPuppet){
val starImage = Image(ImageGetter.getDrawable("OtherIcons/Puppet").tint(Color.LIGHT_GRAY))
val starImage = ImageGetter.getImage("OtherIcons/Puppet").apply { color= Color.LIGHT_GRAY }
cityNameTable.add(starImage).size(20f).padRight(5f)
}

View File

@ -17,7 +17,7 @@ class TechButton(techName:String, val techManager: TechManager, isWorldScreen: B
init {
touchable = Touchable.enabled
defaults().pad(10f)
background = ImageGetter.getDrawable("OtherIcons/civTableBackground")
background = ImageGetter.getTableBackground()
if (ImageGetter.techIconExists(techName))
add(ImageGetter.getTechIconGroup(techName, 60f))

View File

@ -47,8 +47,7 @@ class CityButton(val city: CityInfo, internal val tileGroup: WorldTileGroup, ski
if (tileGroup.tileInfo.airUnits.isEmpty()) return
val secondarycolor = city.civInfo.nation.getInnerColor()
val airUnitTable = Table().apply { defaults().pad(5f) }
airUnitTable.background = ImageGetter.getDrawable("OtherIcons/civTableBackground")
.tint(city.civInfo.nation.getOuterColor())
airUnitTable.background = ImageGetter.getTableBackground(city.civInfo.nation.getOuterColor())
val aircraftImage = ImageGetter.getImage("OtherIcons/Aircraft")
aircraftImage.color = secondarycolor
airUnitTable.add(aircraftImage).size(15f)
@ -93,8 +92,7 @@ class CityButton(val city: CityInfo, internal val tileGroup: WorldTileGroup, ski
val secondaryColor = city.civInfo.nation.getInnerColor()
val iconTable = Table()
iconTable.touchable=Touchable.enabled
iconTable.background = ImageGetter.getDrawable("OtherIcons/civTableBackground")
.tint(city.civInfo.nation.getOuterColor())
iconTable.background = ImageGetter.getTableBackground(city.civInfo.nation.getOuterColor())
if (city.resistanceCounter > 0) {
val resistanceImage = ImageGetter.getImage("StatIcons/Resistance")

View File

@ -24,6 +24,29 @@ object ImageGetter {
// So, we now use TexturePacker in the DesktopLauncher class to pack all the different images into single images,
// and the atlas is what tells us what was packed where.
var atlas = TextureAtlas("game.atlas")
// We then shove all the drawables into a hashmap, because the atlas specifically tells us
// that the search on it is inefficient
val textureRegionDrawables = HashMap<String,TextureRegionDrawable>()
init{
setTextureRegionDrawables()
}
fun setTextureRegionDrawables(){
textureRegionDrawables.clear()
for(region in atlas.regions){
val drawable =TextureRegionDrawable(region)
textureRegionDrawables[region.name] = drawable
}
}
fun refreshAltas() {
atlas.dispose() // To avoid OutOfMemory exceptions
atlas = TextureAtlas("game.atlas")
setTextureRegionDrawables()
}
fun getWhiteDot() = getImage(whiteDotLocation)
fun getDot(dotColor: Color) = getWhiteDot().apply { color = dotColor}
@ -32,29 +55,24 @@ object ImageGetter {
}
fun getImage(fileName: String): Image {
return Image(getTextureRegion(fileName))
return Image(getDrawable(fileName))
}
fun getDrawable(fileName: String): TextureRegionDrawable {
val drawable = TextureRegionDrawable(getTextureRegion(fileName))
drawable.minHeight = 0f
drawable.minWidth = 0f
return drawable
private fun getDrawable(fileName: String): TextureRegionDrawable {
if(textureRegionDrawables.containsKey(fileName)) return textureRegionDrawables[fileName]!!
else return textureRegionDrawables[whiteDotLocation]!!
}
private fun getTextureRegion(fileName: String): TextureRegion {
try {
val region = atlas.findRegion(fileName)
if(region==null)
throw Exception("Could not find $fileName")
return region
} catch (ex: Exception) {
return getTextureRegion(whiteDotLocation)
}
fun getTableBackground(tintColor: Color?=null): Drawable? {
val drawable = getDrawable("OtherIcons/civTableBackground")
drawable.minHeight=0f
drawable.minWidth=0f
if(tintColor==null) return drawable
return drawable.tint(tintColor)
}
fun imageExists(fileName:String) = atlas.findRegion(fileName)!=null
fun imageExists(fileName:String) = textureRegionDrawables.containsKey(fileName)
fun techIconExists(techName:String) = imageExists("TechIcons/$techName")
fun getStatIcon(statName: String): Image {
@ -153,10 +171,6 @@ object ImageGetter {
return getDrawable(whiteDotLocation).tint(color)
}
fun refreshAltas() {
atlas.dispose() // To avoid OutOfMemory exceptions
atlas = TextureAtlas("game.atlas")
}
fun getResourceImage(resourceName: String, size:Float): Actor {
val iconGroup = getImage("ResourceIcons/$resourceName").surroundWithCircle(size)

View File

@ -35,7 +35,7 @@ class NotificationsScroll(internal val worldScreen: WorldScreen) : ScrollPane(nu
listItem.add(ImageGetter.getCircle()
.apply { color=notification.color }).size(10f).pad(5f)
listItem.background(ImageGetter.getDrawable("OtherIcons/civTableBackground"))
listItem.background = ImageGetter.getTableBackground()
listItem.add(label).pad(5f).padRight(10f)
// using a large click area with no gap in between each message item.

View File

@ -260,8 +260,7 @@ class WorldScreen(val viewingCiv:CivilizationInfo) : CameraStageBaseScreen() {
if (viewingCiv.tech.currentTechnology() == null) {
val buttonPic = Table()
buttonPic.background = ImageGetter.getDrawable("OtherIcons/civTableBackground")
.tint(colorFromRGB(7, 46, 43))
buttonPic.background = ImageGetter.getTableBackground(colorFromRGB(7, 46, 43))
buttonPic.defaults().pad(20f)
buttonPic.add("{Pick a tech}!".toLabel(Color.WHITE,30))
techButtonHolder.add(buttonPic)