aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-07-31 00:08:41 +0200
committerLinnea Gräf <nea@nea.moe>2025-07-31 00:08:41 +0200
commit9d707728b45017cfff2e222569e5edfc13730ab5 (patch)
tree8c81467082874e152170c06ab425bc7cd06384d9 /src
parentaf3d76561468cfbffddd19efcb1a4fe238f2ac15 (diff)
downloadFirmament-9d707728b45017cfff2e222569e5edfc13730ab5.tar.gz
Firmament-9d707728b45017cfff2e222569e5edfc13730ab5.tar.bz2
Firmament-9d707728b45017cfff2e222569e5edfc13730ab5.zip
fix: line rendering on 1.21.7
Diffstat (limited to 'src')
-rw-r--r--src/main/java/moe/nea/firmament/mixins/MixinHandledScreen.java5
-rw-r--r--src/main/kotlin/features/inventory/SlotLocking.kt2
-rw-r--r--src/main/kotlin/util/render/DrawContextExt.kt157
3 files changed, 113 insertions, 51 deletions
diff --git a/src/main/java/moe/nea/firmament/mixins/MixinHandledScreen.java b/src/main/java/moe/nea/firmament/mixins/MixinHandledScreen.java
index a5fec35..530fe47 100644
--- a/src/main/java/moe/nea/firmament/mixins/MixinHandledScreen.java
+++ b/src/main/java/moe/nea/firmament/mixins/MixinHandledScreen.java
@@ -62,12 +62,9 @@ public abstract class MixinHandledScreen<T extends ScreenHandler> {
}
}
- @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/ingame/HandledScreen;renderMain(Lnet/minecraft/client/gui/DrawContext;IIF)V", shift = At.Shift.AFTER))
+ @Inject(method = "renderMain", at = @At("HEAD"))
public void onAfterRenderForeground(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
- context.getMatrices().pushMatrix();
- context.getMatrices().translate(-x, -y);
HandledScreenForegroundEvent.Companion.publish(new HandledScreenForegroundEvent((HandledScreen<?>) (Object) this, context, mouseX, mouseY, delta));
- context.getMatrices().popMatrix();
}
@Inject(method = "onMouseClick(Lnet/minecraft/screen/slot/Slot;IILnet/minecraft/screen/slot/SlotActionType;)V", at = @At("HEAD"), cancellable = true)
diff --git a/src/main/kotlin/features/inventory/SlotLocking.kt b/src/main/kotlin/features/inventory/SlotLocking.kt
index f413ef5..eefc6d1 100644
--- a/src/main/kotlin/features/inventory/SlotLocking.kt
+++ b/src/main/kotlin/features/inventory/SlotLocking.kt
@@ -431,7 +431,7 @@ object SlotLocking : FirmamentFeature {
return if (isHotbar()) {
x + 9 to y
} else {
- x + 9 to y + 17
+ x + 9 to y + 16
}
}
diff --git a/src/main/kotlin/util/render/DrawContextExt.kt b/src/main/kotlin/util/render/DrawContextExt.kt
index 898281f..59c4ec9 100644
--- a/src/main/kotlin/util/render/DrawContextExt.kt
+++ b/src/main/kotlin/util/render/DrawContextExt.kt
@@ -2,16 +2,20 @@ package moe.nea.firmament.util.render
import com.mojang.blaze3d.systems.RenderSystem
import me.shedaniel.math.Color
+import org.joml.Vector2f
+import org.joml.Vector3f
import util.render.CustomRenderLayers
+import kotlin.math.abs
import net.minecraft.client.gl.RenderPipelines
import net.minecraft.client.gui.DrawContext
import net.minecraft.client.gui.ScreenRect
-import net.minecraft.client.gui.render.state.special.SpecialGuiElementRenderState
+import net.minecraft.client.render.VertexConsumerProvider
+import net.minecraft.client.util.math.MatrixStack
import net.minecraft.util.Identifier
import moe.nea.firmament.util.MC
fun DrawContext.isUntranslatedGuiDrawContext(): Boolean {
- return matrices.m00 == 1F && matrices.m11 == 1f && matrices.m01 == 0F && matrices.m10 == 0F && matrices.m20 == 0f && matrices.m21 == 0F
+ return matrices.m00 == 1F && matrices.m11 == 1f && matrices.m01 == -1F && matrices.m10 == -1F && matrices.m20 == -1F && matrices.m21 == -1F
}
@Deprecated("Use the other drawGuiTexture")
@@ -51,53 +55,114 @@ fun DrawContext.drawTexture(
)
}
-fun DrawContext.drawLine(fromX: Int, fromY: Int, toX: Int, toY: Int, color: Color) {
- // TODO: push scissors
- // TODO: use matrix translations and a different render layer
+data class LineRenderState(
+ override val x1: Int,
+ override val x2: Int,
+ override val y1: Int,
+ override val y2: Int,
+ override val scale: Float,
+ override val bounds: ScreenRect,
+ val lineWidth: Float,
+ val w: Int,
+ val h: Int,
+ val color: Int,
+ val direction: LineDirection,
+) : MultiSpecialGuiRenderState() {
+ enum class LineDirection {
+ TOP_LEFT_TO_BOTTOM_RIGHT,
+ BOTTOM_LEFT_TO_TOP_RIGHT,
+ }
+
+ override fun createRenderer(vertexConsumers: VertexConsumerProvider.Immediate): MultiSpecialGuiRenderer<out MultiSpecialGuiRenderState> {
+ return LineRenderer(vertexConsumers)
+ }
+
+ override val scissorArea = null
+}
+
+class LineRenderer(vertexConsumers: VertexConsumerProvider.Immediate) :
+ MultiSpecialGuiRenderer<LineRenderState>(vertexConsumers) {
+ override fun getElementClass(): Class<LineRenderState> {
+ return LineRenderState::class.java
+ }
+
+ override fun getYOffset(height: Int, windowScaleFactor: Int): Float {
+ return height / 2F
+ }
+
+ override fun render(
+ state: LineRenderState,
+ matrices: MatrixStack
+ ) {
+ val gr = MC.instance.gameRenderer
+ val client = MC.instance
+ gr.globalSettings
+ .set(
+ state.bounds.width,
+ state.bounds.height,
+ client.options.glintStrength.getValue(),
+ client.world?.time ?: 0L,
+ client.renderTickCounter,
+ client.options.menuBackgroundBlurrinessValue
+ )
+
+ RenderSystem.lineWidth(state.lineWidth)
+ val buf = vertexConsumers.getBuffer(CustomRenderLayers.LINES)
+ val matrix = matrices.peek()
+ val wh = state.w / 2F
+ val hh = state.h / 2F
+ val lowX = -wh
+ val lowY = if (state.direction == LineRenderState.LineDirection.BOTTOM_LEFT_TO_TOP_RIGHT) hh else -hh
+ val highX = wh
+ val highY = -lowY
+ val norm = Vector3f(highX - lowX, highY - lowY, 0F).normalize()
+ buf.vertex(matrix, lowX, lowY, 0F).color(state.color)
+ .normal(matrix, norm)
+ buf.vertex(matrix, highX, highY, 0F).color(state.color)
+ .normal(matrix, norm)
+ vertexConsumers.draw()
+ gr.globalSettings
+ .set(
+ client.window.framebufferWidth,
+ client.window.framebufferHeight,
+ client.options.glintStrength.getValue(),
+ client.world?.getTime() ?: 0L,
+ client.renderTickCounter,
+ client.options.menuBackgroundBlurrinessValue
+ )
+
+ }
+
+ override fun getName(): String? {
+ return "Firmament Line Renderer"
+ }
+}
+
+
+fun DrawContext.drawLine(fromX: Int, fromY: Int, toX: Int, toY: Int, color: Color, lineWidth: Float = 1F) {
if (toY < fromY) {
drawLine(toX, toY, fromX, fromY, color)
return
}
- val rect = ScreenRect(fromX, fromY, toX - fromX, toY - fromY).transform(matrices)
- RenderSystem.lineWidth(MC.window.scaleFactor.toFloat())
- // TODO: this also requires adding a List<SpecialGuiElementRenderer<?>> entry in guirenderer
- // TODO: state.addSpecialElement(object : SpecialGuiElementRenderState {
- // override fun x1(): Int {
- // return fromX
- // }
- //
- // override fun x2(): Int {
- // return toY
- // }
- //
- // override fun y1(): Int {
- // return fromY
- // }
- //
- // override fun y2(): Int {
- // return toY
- // }
- //
- // override fun scale(): Float {
- // return 1f
- // }
- //
- // override fun scissorArea(): ScreenRect? {
- // return rect
- // }
- //
- // override fun bounds(): ScreenRect? {
- // return rect
- // }
- //
- // })
-// draw { vertexConsumers ->
-// val buf = vertexConsumers.getBuffer(CustomRenderLayers.LINES)
-// val matrix = this.matrices.peek()
-// buf.vertex(matrix, fromX.toFloat(), fromY.toFloat(), 0F).color(color.color)
-// .normal(toX - fromX.toFloat(), toY - fromY.toFloat(), 0F)
-// buf.vertex(matrix, toX.toFloat(), toY.toFloat(), 0F).color(color.color)
-// .normal(toX - fromX.toFloat(), toY - fromY.toFloat(), 0F)
-// }
+ val originalRect = ScreenRect(
+ minOf(fromX, toX), minOf(toY, fromY),
+ abs(toX - fromX), abs(toY - fromY)
+ ).transform(matrices)
+ val expansionFactor = 3
+ val rect = ScreenRect(
+ originalRect.left - expansionFactor,
+ originalRect.top - expansionFactor,
+ originalRect.width + expansionFactor * 2,
+ originalRect.height + expansionFactor * 2
+ )
+ // TODO: expand the bounds so that the thickness of the line can be used
+ // TODO: fix this up to work with scissorarea
+ state.addSpecialElement(
+ LineRenderState(
+ rect.left, rect.right, rect.top, rect.bottom, 1F, rect, lineWidth,
+ originalRect.width, originalRect.height, color.color,
+ if (fromX < toX) LineRenderState.LineDirection.TOP_LEFT_TO_BOTTOM_RIGHT else LineRenderState.LineDirection.BOTTOM_LEFT_TO_TOP_RIGHT
+ )
+ )
}