aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/render/FacingThePlayerContext.kt
blob: dc4593941904764893fe4a6c2d8c00aff1c62eb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package moe.nea.firmament.util.render

import org.joml.Matrix4f
import util.render.CustomRenderLayers
import net.minecraft.client.gui.Font
import net.minecraft.client.renderer.LightTexture
import net.minecraft.client.renderer.RenderType
import com.mojang.blaze3d.vertex.VertexConsumer
import net.minecraft.network.chat.Component
import net.minecraft.resources.ResourceLocation
import net.minecraft.core.BlockPos
import moe.nea.firmament.util.FirmFormatters
import moe.nea.firmament.util.MC
import moe.nea.firmament.util.assertTrueOr

@RenderContextDSL
class FacingThePlayerContext(val worldContext: RenderInWorldContext) {
    val matrixStack by worldContext::matrixStack
    fun waypoint(position: BlockPos, label: Component) {
        text(
            label,
            Component.literal("§e${FirmFormatters.formatDistance(MC.player?.position?.distanceTo(position.center) ?: 42069.0)}")
        )
    }

    fun text(
		vararg texts: Component,
		verticalAlign: RenderInWorldContext.VerticalAlign = RenderInWorldContext.VerticalAlign.CENTER,
		background: Int = 0x70808080,
    ) {
        assertTrueOr(texts.isNotEmpty()) { return@text }
        for ((index, text) in texts.withIndex()) {
            worldContext.matrixStack.pushPose()
            val width = MC.font.width(text)
            worldContext.matrixStack.translate(-width / 2F, verticalAlign.align(index, texts.size), 0F)
            val vertexConsumer: VertexConsumer =
                worldContext.vertexConsumers.getBuffer(RenderType.textBackgroundSeeThrough())
            val matrix4f = worldContext.matrixStack.last().pose()
            vertexConsumer.addVertex(matrix4f, -1.0f, -1.0f, 0.0f).setColor(background)
                .setLight(LightTexture.FULL_BLOCK)
            vertexConsumer.addVertex(matrix4f, -1.0f, MC.font.lineHeight.toFloat(), 0.0f).setColor(background)
                .setLight(LightTexture.FULL_BLOCK)
            vertexConsumer.addVertex(matrix4f, width.toFloat(), MC.font.lineHeight.toFloat(), 0.0f)
                .setColor(background)
                .setLight(LightTexture.FULL_BLOCK)
            vertexConsumer.addVertex(matrix4f, width.toFloat(), -1.0f, 0.0f).setColor(background)
                .setLight(LightTexture.FULL_BLOCK)
            worldContext.matrixStack.translate(0F, 0F, 0.01F)

            MC.font.drawInBatch(
                text,
                0F,
                0F,
                -1,
                false,
                worldContext.matrixStack.last().pose(),
                worldContext.vertexConsumers,
                Font.DisplayMode.SEE_THROUGH,
                0,
                LightTexture.FULL_BRIGHT
            )
            worldContext.matrixStack.popPose()
        }
    }


    fun texture(
		texture: ResourceLocation, width: Int, height: Int,
		u1: Float, v1: Float,
		u2: Float, v2: Float,
    ) {
		val buf = worldContext.vertexConsumers.getBuffer(CustomRenderLayers.GUI_TEXTURED_NO_DEPTH_TRIS.apply(texture)) // TODO: this is strictly an incorrect render layer
        val hw = width / 2F
        val hh = height / 2F
        val matrix4f: Matrix4f = worldContext.matrixStack.last().pose()
        buf.addVertex(matrix4f, -hw, -hh, 0F)
            .setColor(-1)
            .setUv(u1, v1)
        buf.addVertex(matrix4f, -hw, +hh, 0F)
            .setColor(-1)
            .setUv(u1, v2)
        buf.addVertex(matrix4f, +hw, +hh, 0F)
            .setColor(-1)
            .setUv(u2, v2)
        buf.addVertex(matrix4f, +hw, -hh, 0F)
            .setColor(-1)
            .setUv(u2, v1)
	    worldContext.vertexConsumers.endBatch()
    }

}