From d3c4106dbd1d22b952992eb0c5705ff036a56c96 Mon Sep 17 00:00:00 2001 From: Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> Date: Sun, 22 Sep 2024 00:30:36 +0200 Subject: [PATCH] Backend: Renderable Wrapped String Internal Alignement (#2372) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../skyhanni/utils/renderables/Renderable.kt | 33 ++++++++++++------- .../utils/renderables/RenderableUtils.kt | 6 ++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt index 9a9cc2adc9d2..d161cf2b0eeb 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -438,23 +438,27 @@ interface Renderable { color: Color = Color.WHITE, horizontalAlign: HorizontalAlignment = HorizontalAlignment.LEFT, verticalAlign: VerticalAlignment = VerticalAlignment.CENTER, + internalAlign: HorizontalAlignment = HorizontalAlignment.LEFT, ) = object : Renderable { - val list by lazy { - Minecraft.getMinecraft().fontRendererObj.listFormattedStringToWidth( + val fontRenderer by lazy { Minecraft.getMinecraft().fontRendererObj } + + val map by lazy { + fontRenderer.listFormattedStringToWidth( text, (width / scale).toInt(), - ) + ).associateWith { fontRenderer.getStringWidth(it) } } - override val width by lazy { - if (list.size == 1) { - (Minecraft.getMinecraft().fontRendererObj.getStringWidth(text) * scale).toInt() + 1 - } else { - width - } + override val width by lazy { (rawWidth * scale).toInt() + 1 } + + val rawWidth by lazy { + if (map.size == 1) + map.entries.first().value + else + map.maxOf { it.value } } - override val height by lazy { list.size * ((9 * scale).toInt() + 1) } + override val height by lazy { map.size * ((9 * scale).toInt() + 1) } override val horizontalAlign = horizontalAlign override val verticalAlign = verticalAlign @@ -464,8 +468,13 @@ interface Renderable { val fontRenderer = Minecraft.getMinecraft().fontRendererObj GlStateManager.translate(1.0, 1.0, 0.0) GlStateManager.scale(scale, scale, 1.0) - list.forEachIndexed { index, text -> - fontRenderer.drawStringWithShadow(text, 0f, index * 10.0f, color.rgb) + map.entries.forEachIndexed { index, (text, size) -> + fontRenderer.drawStringWithShadow( + text, + RenderableUtils.calculateAlignmentXOffset(size, rawWidth, internalAlign).toFloat(), + index * 10.0f, + color.rgb, + ) } GlStateManager.scale(inverseScale, inverseScale, 1.0) GlStateManager.translate(-1.0, -1.0, 0.0) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt index 9200b467a20e..64c1d8f86b6a 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt @@ -39,6 +39,12 @@ internal object RenderableUtils { } ?: listOf(yPadding)) } + fun calculateAlignmentXOffset(width: Int, xSpace: Int, alignment: HorizontalAlignment) = when (alignment) { + HorizontalAlignment.CENTER -> (xSpace - width) / 2 + HorizontalAlignment.RIGHT -> xSpace - width + else -> 0 + } + private fun calculateAlignmentXOffset(renderable: Renderable, xSpace: Int) = when (renderable.horizontalAlign) { HorizontalAlignment.LEFT -> 0 HorizontalAlignment.CENTER -> (xSpace - renderable.width) / 2