From a128ea0d598023ab509d4f5a8b5c77bac5e13921 Mon Sep 17 00:00:00 2001 From: Timo T Date: Sat, 21 May 2022 20:43:30 +0200 Subject: [PATCH] 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` --- core/src/com/unciv/ui/images/ImageGetter.kt | 4 +- .../unciv/ui/images/ImageWithCustomSize.kt | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 core/src/com/unciv/ui/images/ImageWithCustomSize.kt diff --git a/core/src/com/unciv/ui/images/ImageGetter.kt b/core/src/com/unciv/ui/images/ImageGetter.kt index c33fed9b48..8bcfb3f90a 100644 --- a/core/src/com/unciv/ui/images/ImageGetter.kt +++ b/core/src/com/unciv/ui/images/ImageGetter.kt @@ -162,11 +162,11 @@ object ImageGetter { // loading screen and Tutorial.WorldScreen quite a bit. More anisotropy barely helps. val texture = Texture("ExtraImages/$fileName") texture.setFilter(TextureFilter.Linear, TextureFilter.Linear) - return Image(TextureRegion(texture)) + return ImageWithCustomSize(TextureRegion(texture)) } fun getImage(fileName: String?): Image { - return Image(getDrawable(fileName)) + return ImageWithCustomSize(getDrawable(fileName)) } fun getDrawable(fileName: String?): TextureRegionDrawable { diff --git a/core/src/com/unciv/ui/images/ImageWithCustomSize.kt b/core/src/com/unciv/ui/images/ImageWithCustomSize.kt new file mode 100644 index 0000000000..df6d0778c7 --- /dev/null +++ b/core/src/com/unciv/ui/images/ImageWithCustomSize.kt @@ -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 + } + } +}