SnowyGUI/common/src/main/kotlin/fr/username404/snowygui/gui/Element.kt

51 lines
1.7 KiB
Kotlin

package fr.username404.snowygui.gui
import com.mojang.blaze3d.vertex.BufferBuilder
import com.mojang.blaze3d.vertex.PoseStack
import com.mojang.blaze3d.vertex.Tesselator
import com.mojang.blaze3d.vertex.VertexConsumer
import fr.username404.snowygui.Snowy
fun interface Renderable { fun render(poseStack: PoseStack?) }
abstract class Element(
var x: Double, var y: Double,
val width: Int, val height: Int
): Renderable {
companion object Rendering {
@JvmStatic protected val tessellator: Tesselator = Tesselator.getInstance()
@JvmStatic protected val buffer: BufferBuilder = tessellator.builder
}
fun display(stack: PoseStack? = null) {
if (!hidden) try {
render(stack)
} catch (t: Throwable) {
with(Snowy.logs) {
error("Caught an error when rendering an Element from SnowyGUI:")
catching(t)
}
}
}
var hidden: Boolean = false
}
abstract class ColoredElement(x: Double, y: Double, width: Int, height: Int, val color: Int, val opacity: Float) : Element(x, y, width, height) {
companion object {
const val TransparentColor: Int = -0x1
@JvmStatic protected fun VertexConsumer.colorIt(color: Int, opacity: Float = 1F): VertexConsumer {
with(hextoRGB(color)) {
return this@colorIt.color(get(0), get(1), get(2), opacity)
}
}
}
protected fun VertexConsumer.colorEnd() = colorIt(color, opacity).endVertex()
}
fun hextoRGB(hex: Int): MutableList<Float> {
val returnedColorList = mutableListOf<Float>()
for (i in 2 downTo 0) {
returnedColorList.add(
(hex shr (i * 8) and 255) / 255.0F
)
}
return returnedColorList
}