diff --git a/core/src/com/unciv/ui/components/fonts/Fonts.kt b/core/src/com/unciv/ui/components/fonts/Fonts.kt index 149eb8e235..c32b7fd3bc 100644 --- a/core/src/com/unciv/ui/components/fonts/Fonts.kt +++ b/core/src/com/unciv/ui/components/fonts/Fonts.kt @@ -58,6 +58,22 @@ object Fonts { .sortedWith(compareBy(UncivGame.Current.settings.getCollatorFromLocale()) { it.localName }) } + /** + * Helper for v-centering the text of Icon – Label -type components: + * + * Normal vertical centering uses the entire font height. In reality, + * it is customary to align the centre from the baseline to the ascent + * with the centre of the other element. This function estimates the + * correct amount to shift the text element. + */ + fun getDescenderHeight(fontSize: Int): Float { + val ratio = fontImplementation.getMetrics().run { + descent / height } + // For whatever reason, undershooting the adjustment slightly + // causes rounding to work better + return ratio * fontSize.toFloat() + 2.25f + } + /** * Turn a TextureRegion into a Pixmap. * diff --git a/core/src/com/unciv/ui/images/IconTextButton.kt b/core/src/com/unciv/ui/images/IconTextButton.kt index 5340c0f857..9068f77a42 100644 --- a/core/src/com/unciv/ui/images/IconTextButton.kt +++ b/core/src/com/unciv/ui/images/IconTextButton.kt @@ -7,8 +7,11 @@ import com.badlogic.gdx.scenes.scene2d.ui.Cell import com.badlogic.gdx.scenes.scene2d.ui.Label import com.badlogic.gdx.utils.Align import com.unciv.Constants +import com.unciv.ui.components.fonts.Fonts import com.unciv.ui.components.extensions.toLabel import com.unciv.ui.screens.basescreen.BaseScreen +import kotlin.math.floor +import kotlin.math.ceil /** * Translate a [String] and make a [Button] widget from it, with control over font size, font colour, an optional icon, and custom formatting. @@ -32,10 +35,15 @@ open class IconTextButton( val size = fontSize.toFloat() icon.setSize(size, size) icon.setOrigin(Align.center) - add(icon).size(size).padRight(size / 3) + add(icon).size(size).padRight(size / 3.0f) } else { - add() + add().padRight(fontSize / 2f) } /** Table cell instance containing the [label]. */ val labelCell: Cell