summaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/render/ScreenRenderUtils.kt
blob: 959d0a868096d525902e2e4421d61d3d9d2d2e39 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package moe.nea.ultranotifier.util.render

import gg.essential.universal.UGraphics
import gg.essential.universal.UMatrixStack
import juuxel.libninepatch.NinePatch
import juuxel.libninepatch.TextureRenderer
import moe.nea.ultranotifier.util.minecrat.MC
import net.minecraft.util.Identifier
import java.awt.Color
//#if MC > 1.16
import net.minecraft.client.gui.DrawContext
import net.minecraft.client.util.math.MatrixStack

//#endif

object ScreenRenderUtils {
	//#if MC > 1.16
	@JvmStatic
	fun umatrix(
		matrixStack: MatrixStack
	) = UMatrixStack(matrixStack)

	//#endif
	//#if MC >= 1.20
	@JvmStatic
	fun umatrix(
		context: DrawContext
	) = UMatrixStack(context.matrices)
	//#endif

	@JvmStatic
	fun umatrix() = UMatrixStack()

	fun fillRect(
		matrixStack: UMatrixStack,
		left: Double, top: Double,
		right: Double, bottom: Double,
		color: Color,
	) {
		val buffer = UGraphics.getFromTessellator()
		buffer.beginWithDefaultShader(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR)
		buffer.pos(matrixStack, left, top, 0.0).color(color).endVertex()
		buffer.pos(matrixStack, left, bottom, 0.0).color(color).endVertex()
		buffer.pos(matrixStack, right, bottom, 0.0).color(color).endVertex()
		buffer.pos(matrixStack, right, top, 0.0).color(color).endVertex()
		buffer.drawDirect()
	}

	fun renderTexture(
		identifier: Identifier,
		matrixStack: UMatrixStack,
		left: Double, top: Double,
		right: Double, bottom: Double,
	) {
		UGraphics.bindTexture(0, identifier)
		val graphics = UGraphics.getFromTessellator()
		graphics.beginWithDefaultShader(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_TEXTURE)
		graphics.pos(matrixStack, left, top, 0.0).tex(0.0, 0.0).endVertex()
		graphics.pos(matrixStack, left, bottom, 0.0).tex(0.0, 1.0).endVertex()
		graphics.pos(matrixStack, right, bottom, 0.0).tex(1.0, 1.0).endVertex()
		graphics.pos(matrixStack, right, top, 0.0).tex(1.0, 0.0).endVertex()
		graphics.drawDirect()
	}

	fun renderNineSlice(
		ninePatch: NinePatch<Identifier>,
		matrixStack: UMatrixStack,
		left: Double, top: Double,
		right: Double, bottom: Double,
	) {
		class Saver : TextureRenderer<Identifier> {
			override fun draw(
				texture: Identifier?,
				x: Int,
				y: Int,
				width: Int,
				height: Int,
				u1: Float,
				v1: Float,
				u2: Float,
				v2: Float
			) {
				this.texture = texture
			}

			var texture: Identifier? = null
		}

		val saver = Saver()
		ninePatch.draw(saver, 1, 1)
		UGraphics.bindTexture(0, saver.texture!!)
		val graphics = UGraphics.getFromTessellator()
		graphics.beginWithDefaultShader(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_TEXTURE)
		ninePatch.draw(object : TextureRenderer<Identifier> {
			override fun draw(
				texture: Identifier,
				x: Int,
				y: Int,
				width: Int,
				height: Int,
				u1: Float,
				v1: Float,
				u2: Float,
				v2: Float
			) {
				val x1 = left + x.toDouble()
				val y1 = top + y.toDouble()
				val x2 = x1 + width
				val y2 = y1 + height
				graphics.pos(matrixStack, x1, y1, 0.0)
					.tex(u1.toDouble(), v1.toDouble())
					.endVertex()
				graphics.pos(matrixStack, x1, y2, 0.0)
					.tex(u1.toDouble(), v2.toDouble())
					.endVertex()
				graphics.pos(matrixStack, x2, y2, 0.0)
					.tex(u2.toDouble(), v2.toDouble())
					.endVertex()
				graphics.pos(matrixStack, x2, y1, 0.0)
					.tex(u2.toDouble(), v1.toDouble())
					.endVertex()
			}
		}, (right - left).toInt(), (bottom - top).toInt())
		graphics.drawDirect()
	}

	fun getTextWidth(text: String): Int {
		return MC.font.getWidth(text)
	}

	fun renderText(matrixStack: UMatrixStack, x: Double, y: Double, text: String) {
		UGraphics.drawString(matrixStack, text, x.toFloat(), y.toFloat(), -1, false)
	}

}