-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathUIShape.kt
103 lines (86 loc) · 3.63 KB
/
UIShape.kt
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
package gg.essential.elementa.components
import gg.essential.elementa.UIComponent
import gg.essential.elementa.dsl.toConstraint
import gg.essential.universal.UGraphics
import gg.essential.universal.UMatrixStack
import gg.essential.universal.render.URenderPipeline
import gg.essential.universal.shader.BlendState
import gg.essential.universal.vertex.UBufferBuilder
import org.lwjgl.opengl.GL11
import java.awt.Color
// feeling cute, might delete this class later
// (he did not delete the class later)
/**
* Component for drawing arbitrary shapes.
*/
@Deprecated("Currently only supports convex polygons. Use with care! Or better, create a dedicated component for your use case.")
open class UIShape @JvmOverloads constructor(color: Color = Color.WHITE) : UIComponent() {
private var vertices = mutableListOf<UIPoint>()
@Deprecated("Only supports GL_POLYGON on 1.17+, implemented as TRIANGEL_FAN.")
var drawMode = GL11.GL_POLYGON
init {
setColor(color.toConstraint())
}
fun addVertex(point: UIPoint) = apply {
this.parent.addChild(point)
vertices.add(point)
}
fun addVertices(vararg points: UIPoint) = apply {
parent.addChildren(*points)
vertices.addAll(points)
}
fun getVertices() = vertices
override fun draw(matrixStack: UMatrixStack) {
beforeDrawCompat(matrixStack)
val color = this.getColor()
if (color.alpha == 0) return super.draw(matrixStack)
if (URenderPipeline.isRequired) {
draw(matrixStack, color)
} else {
@Suppress("DEPRECATION")
drawLegacy(matrixStack, color)
}
super.draw(matrixStack)
}
private fun draw(matrixStack: UMatrixStack, color: Color) {
val bufferBuilder = UBufferBuilder.create(UGraphics.DrawMode.TRIANGLE_FAN, UGraphics.CommonVertexFormats.POSITION_COLOR)
vertices.forEach {
bufferBuilder
.pos(matrixStack, it.absoluteX.toDouble(), it.absoluteY.toDouble(), 0.0)
.color(color.red, color.green, color.blue, color.alpha)
.endVertex()
}
bufferBuilder.build()?.drawAndClose(PIPELINE)
}
@Deprecated("Stops working in 1.21.5, see UGraphics.Globals")
@Suppress("DEPRECATION")
private fun drawLegacy(matrixStack: UMatrixStack, color: Color) {
UGraphics.enableBlend()
UGraphics.disableTexture2D()
val red = color.red.toFloat() / 255f
val green = color.green.toFloat() / 255f
val blue = color.blue.toFloat() / 255f
val alpha = color.alpha.toFloat() / 255f
val worldRenderer = UGraphics.getFromTessellator()
UGraphics.tryBlendFuncSeparate(770, 771, 1, 0)
if (UGraphics.isCoreProfile()) {
worldRenderer.beginWithDefaultShader(UGraphics.DrawMode.TRIANGLE_FAN, UGraphics.CommonVertexFormats.POSITION_COLOR)
} else {
worldRenderer.begin(drawMode, UGraphics.CommonVertexFormats.POSITION_COLOR)
}
vertices.forEach {
worldRenderer
.pos(matrixStack, it.absoluteX.toDouble(), it.absoluteY.toDouble(), 0.0)
.color(red, green, blue, alpha)
.endVertex()
}
worldRenderer.drawDirect()
UGraphics.enableTexture2D()
UGraphics.disableBlend()
}
private companion object {
private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:shape", UGraphics.DrawMode.TRIANGLE_FAN, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
}.build()
}
}