Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/Elementa.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ public final class gg/essential/elementa/ElementaVersion : java/lang/Enum {
public static final field Companion Lgg/essential/elementa/ElementaVersion$Companion;
public static final field V0 Lgg/essential/elementa/ElementaVersion;
public static final field V1 Lgg/essential/elementa/ElementaVersion;
public static final field V10 Lgg/essential/elementa/ElementaVersion;
public static final field V2 Lgg/essential/elementa/ElementaVersion;
public static final field V3 Lgg/essential/elementa/ElementaVersion;
public static final field V4 Lgg/essential/elementa/ElementaVersion;
public static final field V5 Lgg/essential/elementa/ElementaVersion;
public static final field V6 Lgg/essential/elementa/ElementaVersion;
public static final field V7 Lgg/essential/elementa/ElementaVersion;
public static final field V8 Lgg/essential/elementa/ElementaVersion;
public static final field V9 Lgg/essential/elementa/ElementaVersion;
public final fun enableFor (Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public static fun valueOf (Ljava/lang/String;)Lgg/essential/elementa/ElementaVersion;
public static fun values ()[Lgg/essential/elementa/ElementaVersion;
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
kotlin = "1.5.10"
kotlinx-coroutines = "1.5.2"
jetbrains-annotations = "23.0.0"
universalcraft = "389"
universalcraft = "406"
commonmark = "0.17.1"
dom4j = "2.1.1"

Expand Down
27 changes: 27 additions & 0 deletions src/main/kotlin/gg/essential/elementa/ElementaVersion.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package gg.essential.elementa

import gg.essential.elementa.components.UIText
import gg.essential.elementa.components.UIWrappedText
import gg.essential.elementa.components.UpdateFunc
import gg.essential.elementa.components.Window
import gg.essential.elementa.constraints.SuperConstraint
import gg.essential.elementa.constraints.animation.AnimationComponent
import gg.essential.elementa.effects.Effect
import gg.essential.universal.render.URenderPipeline
import gg.essential.universal.shader.BlendState

/**
* Sometimes it is necessary or desirable to introduce breaking behavioral changes to Elementa. In order to maintain
Expand Down Expand Up @@ -170,8 +174,23 @@ enum class ElementaVersion {
* The new constraint tracking will only invalidate constraints which were evaluated, and the [UpdateFunc]s
* are tracked intelligently at registration, such that no more full tree traversals should be necessary.
*/
@Deprecated(DEPRECATION_MESSAGE)
V8,

/**
* All Minecraft versions now use [URenderPipeline] instead of modifying global GL state.
* Additionally, [UIText] and [UIWrappedText] no longer enable (and forget to disable) blending.
*/
@Deprecated(DEPRECATION_MESSAGE)
V9,

/**
* All components now use [BlendState.ALPHA] instead of [BlendState.NORMAL] and variants.
* This fixes the alpha channel of the render result, allowing it to be correctly composited with other textures.
* See [UniversalCraft#105](https://github.com/EssentialGG/UniversalCraft/pull/105) for more details.
*/
V10,

;

/**
Expand Down Expand Up @@ -218,8 +237,16 @@ Be sure to read through all the changes between your current version and your ne
internal val v6 = V6
@Suppress("DEPRECATION")
internal val v7 = V7
@Suppress("DEPRECATION")
internal val v8 = V8
@Suppress("DEPRECATION")
internal val v9 = V9
internal val v10 = V10

internal val atLeastV9Active: Boolean
get() = active >= v9
internal val atLeastV10Active: Boolean
get() = active >= v10

@PublishedApi
internal var active: ElementaVersion = v0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.essential.elementa.components

import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.state.BasicState
import gg.essential.elementa.state.MappedState
import gg.essential.elementa.state.State
Expand Down Expand Up @@ -127,7 +128,7 @@ open class GradientComponent constructor(
endColor: Color,
direction: GradientDirection
) {
if (!URenderPipeline.isRequired) {
if (!URenderPipeline.isRequired && !ElementaVersion.atLeastV9Active) {
@Suppress("DEPRECATION")
return drawGradientBlockLegacy(matrixStack, x1, y1, x2, y2, startColor, endColor, direction)
}
Expand All @@ -138,7 +139,7 @@ open class GradientComponent constructor(
bufferBuilder.pos(matrixStack, x1, y1, 0.0).color(colors.topLeft).endVertex()
bufferBuilder.pos(matrixStack, x1, y2, 0.0).color(colors.bottomLeft).endVertex()
bufferBuilder.pos(matrixStack, x2, y2, 0.0).color(colors.bottomRight).endVertex()
bufferBuilder.build()?.drawAndClose(PIPELINE)
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE)
}

@Deprecated("Stops working in 1.21.5")
Expand Down Expand Up @@ -168,7 +169,12 @@ open class GradientComponent constructor(
}

private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:gradient_block", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
@Suppress("DEPRECATION")
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
}.build()

private val PIPELINE2 = URenderPipeline.builderWithDefaultShader("elementa:gradient_block", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
blendState = BlendState.ALPHA
}.build()
}
}
15 changes: 13 additions & 2 deletions src/main/kotlin/gg/essential/elementa/components/UIBlock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ open class UIBlock(colorConstraint: ColorConstraint = Color.WHITE.toConstraint()
drawBlock(UMatrixStack(), color, x1, y1, x2, y2)

fun drawBlock(matrixStack: UMatrixStack, color: Color, x1: Double, y1: Double, x2: Double, y2: Double) {
if (!URenderPipeline.isRequired) {
if (!URenderPipeline.isRequired && !ElementaVersion.atLeastV9Active) {
@Suppress("DEPRECATION")
return drawBlockLegacy(matrixStack, color, x1, y1, x2, y2)
}

val bufferBuilder = UBufferBuilder.create(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR)
drawBlock(bufferBuilder, matrixStack, color, x1, y1, x2, y2)
bufferBuilder.build()?.drawAndClose(PIPELINE)
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE)
}

@Deprecated("Stops working in 1.21.5, see UGraphics.Globals")
Expand Down Expand Up @@ -133,6 +133,7 @@ open class UIBlock(colorConstraint: ColorConstraint = Color.WHITE.toConstraint()
}

private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:block", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
@Suppress("DEPRECATION")
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
// At some point MC started enabling its depth test during font rendering but all GUI code is
// essentially flat and has depth tests disabled. This can cause stuff rendered in the background of the
Expand All @@ -141,5 +142,15 @@ open class UIBlock(colorConstraint: ColorConstraint = Color.WHITE.toConstraint()
// To work around this, we'll write depth buffer unconditionally in the area where we draw the block.
depthTest = URenderPipeline.DepthTest.Always
}.build()

private val PIPELINE2 = URenderPipeline.builderWithDefaultShader("elementa:block", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
blendState = BlendState.ALPHA
// At some point MC started enabling its depth test during font rendering but all GUI code is
// essentially flat and has depth tests disabled. This can cause stuff rendered in the background of the
// GUI to interfere with text rendered in the foreground because none of the blocks rendered in between
// will actually write to the depth buffer.
// To work around this, we'll write depth buffer unconditionally in the area where we draw the block.
depthTest = URenderPipeline.DepthTest.Always
}.build()
}
}
17 changes: 15 additions & 2 deletions src/main/kotlin/gg/essential/elementa/components/UICircle.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.essential.elementa.components

import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
import gg.essential.elementa.dsl.toConstraint
import gg.essential.elementa.dsl.pixels
Expand Down Expand Up @@ -77,10 +78,22 @@ class UICircle @JvmOverloads constructor(radius: Float = 0f, color: Color = Colo
readElementaShaderSource("rect", "vsh"),
readElementaShaderSource("circle", "fsh"),
).apply {
@Suppress("DEPRECATION")
blendState = BlendState.NORMAL
depthTest = URenderPipeline.DepthTest.Always // see UIBlock.PIPELINE
}.build()

private val PIPELINE2 = URenderPipeline.builderWithLegacyShader(
"elementa:circle",
UGraphics.DrawMode.QUADS,
UGraphics.CommonVertexFormats.POSITION_COLOR,
readElementaShaderSource("rect", "vsh"),
readElementaShaderSource("circle", "fsh"),
).apply {
blendState = BlendState.ALPHA
depthTest = URenderPipeline.DepthTest.Always // see UIBlock.PIPELINE
}.build()

fun initShaders() {
if (URenderPipeline.isRequired) return
if (::shader.isInitialized)
Expand All @@ -104,7 +117,7 @@ class UICircle @JvmOverloads constructor(radius: Float = 0f, color: Color = Colo
drawCircle(UMatrixStack(), centerX, centerY, radius, color)

fun drawCircle(matrixStack: UMatrixStack, centerX: Float, centerY: Float, radius: Float, color: Color) {
if (!URenderPipeline.isRequired) {
if (!URenderPipeline.isRequired && !ElementaVersion.atLeastV9Active) {
@Suppress("DEPRECATION")
return drawCircleLegacy(matrixStack, centerX, centerY, radius, color)
}
Expand All @@ -119,7 +132,7 @@ class UICircle @JvmOverloads constructor(radius: Float = 0f, color: Color = Colo
(centerX + radius).toDouble(),
(centerY + radius).toDouble()
)
bufferBuilder.build()?.drawAndClose(PIPELINE) {
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE) {
uniform("u_Radius", radius)
uniform("u_CenterPos", centerX, centerY)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.essential.elementa.components

import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
import gg.essential.elementa.dsl.pixels
import gg.essential.elementa.utils.readElementaShaderSource
Expand Down Expand Up @@ -48,10 +49,22 @@ open class UIRoundedRectangle(radius: Float) : UIComponent() {
readElementaShaderSource("rect", "vsh"),
readElementaShaderSource("rounded_rect", "fsh"),
).apply {
@Suppress("DEPRECATION")
blendState = BlendState.NORMAL
depthTest = URenderPipeline.DepthTest.Always // see UIBlock.PIPELINE
}.build()

private val PIPELINE2 = URenderPipeline.builderWithLegacyShader(
"elementa:rounded_rectangle",
UGraphics.DrawMode.QUADS,
UGraphics.CommonVertexFormats.POSITION_COLOR,
readElementaShaderSource("rect", "vsh"),
readElementaShaderSource("rounded_rect", "fsh"),
).apply {
blendState = BlendState.ALPHA
depthTest = URenderPipeline.DepthTest.Always // see UIBlock.PIPELINE
}.build()

fun initShaders() {
if (URenderPipeline.isRequired) return
if (::shader.isInitialized)
Expand All @@ -78,14 +91,14 @@ open class UIRoundedRectangle(radius: Float) : UIComponent() {
* Draws a rounded rectangle
*/
fun drawRoundedRectangle(matrixStack: UMatrixStack, left: Float, top: Float, right: Float, bottom: Float, radius: Float, color: Color) {
if (!URenderPipeline.isRequired) {
if (!URenderPipeline.isRequired && !ElementaVersion.atLeastV9Active) {
@Suppress("DEPRECATION")
return drawRoundedRectangleLegacy(matrixStack, left, top, right, bottom, radius, color)
}

val bufferBuilder = UBufferBuilder.create(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR)
UIBlock.drawBlock(bufferBuilder, matrixStack, color, left.toDouble(), top.toDouble(), right.toDouble(), bottom.toDouble())
bufferBuilder.build()?.drawAndClose(PIPELINE) {
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE) {
uniform("u_Radius", radius)
uniform("u_InnerRect", left + radius, top + radius, right - radius, bottom - radius)
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/gg/essential/elementa/components/UIShape.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.essential.elementa.components

import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
import gg.essential.elementa.dsl.toConstraint
import gg.essential.universal.UGraphics
Expand Down Expand Up @@ -44,7 +45,7 @@ open class UIShape @JvmOverloads constructor(color: Color = Color.WHITE) : UICom
val color = this.getColor()
if (color.alpha == 0) return super.draw(matrixStack)

if (URenderPipeline.isRequired) {
if (URenderPipeline.isRequired || ElementaVersion.atLeastV9Active) {
draw(matrixStack, color)
} else {
@Suppress("DEPRECATION")
Expand All @@ -62,7 +63,7 @@ open class UIShape @JvmOverloads constructor(color: Color = Color.WHITE) : UICom
.color(color.red, color.green, color.blue, color.alpha)
.endVertex()
}
bufferBuilder.build()?.drawAndClose(PIPELINE)
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE)
}

@Deprecated("Stops working in 1.21.5, see UGraphics.Globals")
Expand Down Expand Up @@ -97,7 +98,11 @@ open class UIShape @JvmOverloads constructor(color: Color = Color.WHITE) : UICom

private companion object {
private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:shape", UGraphics.DrawMode.TRIANGLE_FAN, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
@Suppress("DEPRECATION")
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
}.build()
private val PIPELINE2 = URenderPipeline.builderWithDefaultShader("elementa:shape", UGraphics.DrawMode.TRIANGLE_FAN, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
blendState = BlendState.ALPHA
}.build()
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/gg/essential/elementa/components/UIText.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.essential.elementa.components

import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
import gg.essential.elementa.UIConstraints
import gg.essential.elementa.constraints.CenterConstraint
Expand Down Expand Up @@ -118,7 +119,7 @@ constructor(
return super.draw(matrixStack)
}

if (!URenderPipeline.isRequired) {
if (!URenderPipeline.isRequired && !ElementaVersion.atLeastV9Active) {
@Suppress("DEPRECATION")
UGraphics.enableBlend()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.essential.elementa.components

import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
import gg.essential.elementa.UIConstraints
import gg.essential.elementa.constraints.CenterConstraint
Expand Down Expand Up @@ -152,7 +153,7 @@ open class UIWrappedText @JvmOverloads constructor(
return super.draw(matrixStack)
}

if (!URenderPipeline.isRequired) {
if (!URenderPipeline.isRequired && !ElementaVersion.atLeastV9Active) {
@Suppress("DEPRECATION")
UGraphics.enableBlend()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class Inspector @JvmOverloads constructor(
UGraphics.DrawMode.QUADS,
UGraphics.CommonVertexFormats.POSITION_COLOR,
).apply {
blendState = BlendState.NORMAL
blendState = BlendState.ALPHA
depthTest = URenderPipeline.DepthTest.Less
}.build()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.essential.elementa.font

import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
import gg.essential.elementa.constraints.ConstraintType
import gg.essential.elementa.constraints.resolution.ConstraintVisitor
Expand Down Expand Up @@ -148,10 +149,10 @@ class BasicFontRenderer(
y: Float,
originalPointSize: Float
) {
if (URenderPipeline.isRequired) {
if (URenderPipeline.isRequired || ElementaVersion.atLeastV9Active) {
val bufferBuilder = UBufferBuilder.create(UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_TEXTURE_COLOR)
drawStringNow(bufferBuilder, matrixStack, string, color, x, y, originalPointSize)
bufferBuilder.build()?.drawAndClose(PIPELINE) {
bufferBuilder.build()?.drawAndClose(if (ElementaVersion.atLeastV10Active) PIPELINE2 else PIPELINE) {
texture(0, regularFont.getTexture().dynamicGlId)
}
} else {
Expand Down Expand Up @@ -274,7 +275,11 @@ class BasicFontRenderer(

private companion object {
private val PIPELINE = URenderPipeline.builderWithDefaultShader("elementa:basic_font", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
@Suppress("DEPRECATION")
blendState = BlendState.NORMAL.copy(srcAlpha = BlendState.Param.ONE, dstAlpha = BlendState.Param.ZERO)
}.build()
private val PIPELINE2 = URenderPipeline.builderWithDefaultShader("elementa:basic_font", UGraphics.DrawMode.QUADS, UGraphics.CommonVertexFormats.POSITION_COLOR).apply {
blendState = BlendState.ALPHA
}.build()
}
}
6 changes: 5 additions & 1 deletion src/main/kotlin/gg/essential/elementa/font/FontRenderer.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gg.essential.elementa.font

import gg.essential.elementa.ElementaVersion
import gg.essential.elementa.UIComponent
import gg.essential.elementa.constraints.ConstraintType
import gg.essential.elementa.constraints.resolution.ConstraintVisitor
Expand All @@ -25,7 +26,7 @@ import kotlin.math.max
/**
* [MSDF](https://github.com/Chlumsky/msdfgen) Font Renderer
*/
@Deprecated("Not well maintained. Does not currently support 1.21.5+ at all.")
@Deprecated("Not well maintained. Does not currently support 1.21.5+ or ElementaVersion.V9 at all.")
@Suppress("DEPRECATION")
class FontRenderer(
private val regularFont: Font,
Expand Down Expand Up @@ -133,6 +134,9 @@ class FontRenderer(
shadow: Boolean,
shadowColor: Color?
) {
if (ElementaVersion.atLeastV9Active) {
return
}
val effectiveSize = originalPointSize * scale * 1.3623059867f
val adjustedY = y - effectiveSize / 5
if (shadow) {
Expand Down
Loading