aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt33
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/renderables/RenderableUtils.kt6
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