From f9e69fb4f0495df31f692da7ecd7bb48eb63727f Mon Sep 17 00:00:00 2001 From: Toxile Date: Sun, 22 Dec 2024 08:10:07 +0000 Subject: [PATCH] UI: various improvements mostly relating to centering and WorldScreenTopBar (#12673) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * (rebase) * centering pt. 1 * (rebase) * centering pt. 2 * world screen top bar fixes: – standardise icon-label order – add ad-hoc spacing to the turn count label – add clean and distinctive new style for 'per turn' stats * centering pt. 3 * –final --- .../com/unciv/ui/components/fonts/Fonts.kt | 16 ++++ .../src/com/unciv/ui/images/IconTextButton.kt | 12 ++- .../worldscreen/topbar/WorldScreenTopBar.kt | 25 +++-- .../topbar/WorldScreenTopBarResources.kt | 2 +- .../topbar/WorldScreenTopBarStats.kt | 95 ++++++++++++------- 5 files changed, 102 insertions(+), 48 deletions(-) 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