Skip to content

Commit 5d38504

Browse files
committed
feat(shale): add minecraft renderer
1 parent 48e5bac commit 5d38504

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package moe.nea.shale.render.minecraft
2+
3+
import io.github.notenoughupdates.moulconfig.common.RenderContext
4+
import moe.nea.shale.layout.Area
5+
import moe.nea.shale.layout.MeasuredText
6+
import moe.nea.shale.layout.Position
7+
import moe.nea.shale.layout.Size
8+
import moe.nea.shale.render.GraphicsContext
9+
import java.awt.Color
10+
11+
class MinecraftGraphicsContext(
12+
val renderContext: RenderContext,
13+
) : GraphicsContext {
14+
override fun rect(area: Area, color: Color) {
15+
renderContext.drawColoredRect(area.left.toFloat(), area.top.toFloat(), area.right.toFloat(), area.bottom.toFloat(), color.rgb)
16+
}
17+
18+
override fun text(position: Position, color: Color, measuredText: MeasuredText) {
19+
measuredText as MinecraftWrappedText
20+
var offset = 0
21+
for (line in measuredText.lines) {
22+
renderContext.drawString(font, line, position.x, position.y + offset, color.rgb, false)
23+
offset += font.height
24+
}
25+
}
26+
27+
val font = renderContext.minecraft.defaultFontRenderer
28+
29+
override fun measureWrappedText(text: String, width: Int): MeasuredText {
30+
val lines = font.splitText(text, width)
31+
val size = Size(
32+
lines.maxOfOrNull { font.getStringWidth(it) } ?: 0,
33+
font.height * lines.size
34+
)
35+
return MinecraftWrappedText(lines, size)
36+
}
37+
38+
override fun measureUnwrappedText(text: String): Int {
39+
return font.getStringWidth(text)
40+
}
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package moe.nea.shale.render.minecraft
2+
3+
import moe.nea.shale.layout.MeasuredText
4+
import moe.nea.shale.layout.Size
5+
6+
data class MinecraftWrappedText(
7+
val lines: List<String>,
8+
override val size: Size
9+
) : MeasuredText

0 commit comments

Comments
 (0)