From ad5879d319b236ddec744978dfaebebf91135209 Mon Sep 17 00:00:00 2001 From: Thunderblade73 <85900443+Thunderblade73@users.noreply.github.com> Date: Wed, 3 Apr 2024 21:17:57 +0200 Subject: Backend: Renderable advanced Strings (#888) --- .../skyhanni/utils/renderables/Renderable.kt | 56 ++++++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'src/main') 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 8a57ded54..30b595186 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt @@ -292,19 +292,26 @@ interface Renderable { fun string( text: String, + scale: Double = 1.0, color: Color = Color.WHITE, horizontalAlign: HorizontalAlignment = HorizontalAlignment.LEFT, verticalAlign: VerticalAlignment = VerticalAlignment.TOP, ) = object : Renderable { - override val width: Int - get() = Minecraft.getMinecraft().fontRendererObj.getStringWidth(text) - override val height = 10 + override val width by lazy { (Minecraft.getMinecraft().fontRendererObj.getStringWidth(text) * scale).toInt() + 1 } + override val height = (9 * scale).toInt() + 1 override val horizontalAlign = horizontalAlign override val verticalAlign = verticalAlign + val inverseScale = 1 / scale + override fun render(posX: Int, posY: Int) { - Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow(text, 1f, 1f, color.rgb) + val fontRenderer = Minecraft.getMinecraft().fontRendererObj + GlStateManager.translate(1.0, 1.0, 0.0) + GlStateManager.scale(scale, scale, 1.0) + fontRenderer.drawStringWithShadow(text, 0f, 0f, color.rgb) + GlStateManager.scale(inverseScale, inverseScale, 1.0) + GlStateManager.translate(-1.0, -1.0, 0.0) } } @@ -318,6 +325,47 @@ interface Renderable { } } + fun wrappedString( + text: String, + width: Int, + scale: Double = 1.0, + color: Color = Color.WHITE, + horizontalAlign: HorizontalAlignment = HorizontalAlignment.LEFT, + verticalAlign: VerticalAlignment = VerticalAlignment.TOP, + ) = object : Renderable { + + val list by lazy { + Minecraft.getMinecraft().fontRendererObj.listFormattedStringToWidth( + text, (width / scale).toInt() + ) + } + + override val width by lazy { + if (list.size == 1) { + (Minecraft.getMinecraft().fontRendererObj.getStringWidth(text) / scale).toInt() + 1 + } else { + (width / scale).toInt() + 1 + } + } + + override val height by lazy { list.size * ((9 * scale).toInt() + 1) } + override val horizontalAlign = horizontalAlign + override val verticalAlign = verticalAlign + + val inverseScale = 1 / scale + + override fun render(posX: Int, posY: Int) { + 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) + } + GlStateManager.scale(inverseScale, inverseScale, 1.0) + GlStateManager.translate(-1.0, -1.0, 0.0) + } + } + fun progressBar( percent: Double, startColor: Color = Color(255, 0, 0), -- cgit