Skip to content

Commit

Permalink
Backend: Renderable Wrapped String Internal Alignement (#2372)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
  • Loading branch information
Thunderblade73 and hannibal002 authored Sep 21, 2024
1 parent fa8096a commit d3c4106
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/main/java/at/hannibal2/skyhanni/utils/renderables/Renderable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d3c4106

Please sign in to comment.