Fix images not being able to have their size set (#6887)

* Fix images not being able to have their size set

* Small corrections

`> 0f` to include `-0f`
This commit is contained in:
Timo T 2022-05-21 20:43:30 +02:00 committed by GitHub
parent 3e95e3f152
commit a128ea0d59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 2 deletions

View File

@ -162,11 +162,11 @@ object ImageGetter {
// loading screen and Tutorial.WorldScreen quite a bit. More anisotropy barely helps. // loading screen and Tutorial.WorldScreen quite a bit. More anisotropy barely helps.
val texture = Texture("ExtraImages/$fileName") val texture = Texture("ExtraImages/$fileName")
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear) texture.setFilter(TextureFilter.Linear, TextureFilter.Linear)
return Image(TextureRegion(texture)) return ImageWithCustomSize(TextureRegion(texture))
} }
fun getImage(fileName: String?): Image { fun getImage(fileName: String?): Image {
return Image(getDrawable(fileName)) return ImageWithCustomSize(getDrawable(fileName))
} }
fun getDrawable(fileName: String?): TextureRegionDrawable { fun getDrawable(fileName: String?): TextureRegionDrawable {

View File

@ -0,0 +1,38 @@
package com.unciv.ui.images
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.badlogic.gdx.scenes.scene2d.utils.Drawable
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable
/**
* Either [Image] or [com.badlogic.gdx.scenes.scene2d.utils.Drawable] is badly written, as the [Image.drawable] always has its texture width/height
* as min width/height, and [Image.getPrefWidth]/Height is always equal to its [Image.drawable]'s minWidth/Height.
*
* This results in an [Image.getPrefWidth]/Height call to completely ignore the width/height that was set by [setSize]. To fix this, this class provides a
* custom implementation of [getPrefWidth] and [getPrefHeight].
*/
class ImageWithCustomSize(drawable: Drawable) : Image(drawable) {
constructor(region: TextureRegion) : this(TextureRegionDrawable(region))
override fun getPrefWidth(): Float {
return if (width > 0f) {
width
} else if (drawable != null) {
drawable.minWidth
} else {
0f
}
}
override fun getPrefHeight(): Float {
return if (height > 0f) {
height
} else if (drawable != null) {
drawable.minHeight
} else {
0f
}
}
}