diff options
author | Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> | 2024-09-22 00:30:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-22 00:30:36 +0200 |
commit | d3c4106dbd1d22b952992eb0c5705ff036a56c96 (patch) | |
tree | f2e726f64ced8c3a20424b5ab6c96c659a51596b /src/main/java | |
parent | fa8096a38e7364f7912361f81df4127ad7a231dd (diff) | |
download | skyhanni-d3c4106dbd1d22b952992eb0c5705ff036a56c96.tar.gz skyhanni-d3c4106dbd1d22b952992eb0c5705ff036a56c96.tar.bz2 skyhanni-d3c4106dbd1d22b952992eb0c5705ff036a56c96.zip |
Backend: Renderable Wrapped String Internal Alignement (#2372)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt | 33 | ||||
-rw-r--r-- | src/main/java/at/hannibal2/skyhanni/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 9a9cc2adc..d161cf2b0 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 9200b467a..64c1d8f86 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 |