From c282cb898abedd15f4c77bda900ac47d66010dfc Mon Sep 17 00:00:00 2001 From: "Joseph T. McQuigg" Date: Wed, 4 Dec 2024 21:30:09 -0500 Subject: [PATCH] Hud Post and Pre Render Callbacks --- .../rendering/v1/HudPostRenderCallback.java | 23 ++++++++++++++++ .../rendering/v1/HudPreRenderCallback.java | 23 ++++++++++++++++ .../rendering/v1/HudRenderCallback.java | 23 +++++++--------- .../client/rendering/InGameHudMixin.java | 15 ++++++++--- .../rendering/client/HudAndShaderTest.java | 27 ++++++++++++++++--- 5 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPostRenderCallback.java create mode 100644 fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPreRenderCallback.java diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPostRenderCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPostRenderCallback.java new file mode 100644 index 0000000000..3447767b1b --- /dev/null +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPostRenderCallback.java @@ -0,0 +1,23 @@ +package net.fabricmc.fabric.api.client.rendering.v1; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; + +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.render.RenderTickCounter; + +public interface HudPostRenderCallback { + Event EVENT = EventFactory.createArrayBacked(HudPostRenderCallback.class, (listeners) -> (matrixStack, delta) -> { + for (HudPostRenderCallback event : listeners) { + event.onHudPostRender(matrixStack, delta); + } + }); + + /** + * Called after rendering the whole hud, which is displayed in game, in a world. + * + * @param drawContext the {@link DrawContext} instance + * @param tickCounter the {@link RenderTickCounter} instance + */ + void onHudPostRender(DrawContext drawContext, RenderTickCounter tickCounter); +} diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPreRenderCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPreRenderCallback.java new file mode 100644 index 0000000000..4b0ce8a718 --- /dev/null +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPreRenderCallback.java @@ -0,0 +1,23 @@ +package net.fabricmc.fabric.api.client.rendering.v1; + +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; + +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.render.RenderTickCounter; + +public interface HudPreRenderCallback { + Event EVENT = EventFactory.createArrayBacked(HudPreRenderCallback.class, (listeners) -> (matrixStack, delta) -> { + for (HudPreRenderCallback event : listeners) { + event.onHudPreRender(matrixStack, delta); + } + }); + + /** + * Called before rendering the whole hud, which is displayed in game, in a world. + * + * @param drawContext the {@link DrawContext} instance + * @param tickCounter the {@link RenderTickCounter} instance + */ + void onHudPreRender(DrawContext drawContext, RenderTickCounter tickCounter); +} diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudRenderCallback.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudRenderCallback.java index 4890d53ed6..2a55a01d18 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudRenderCallback.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudRenderCallback.java @@ -16,24 +16,19 @@ package net.fabricmc.fabric.api.client.rendering.v1; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.render.RenderTickCounter; - import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; +/** + * Callback for rendering the hud. + * + * @deprecated Use {@link HudPostRenderCallback} instead. + */ +@Deprecated public interface HudRenderCallback { - Event EVENT = EventFactory.createArrayBacked(HudRenderCallback.class, (listeners) -> (matrixStack, delta) -> { - for (HudRenderCallback event : listeners) { - event.onHudRender(matrixStack, delta); + Event EVENT = EventFactory.createArrayBacked(HudPostRenderCallback.class, (listeners) -> (matrixStack, delta) -> { + for (HudPostRenderCallback event : listeners) { + event.onHudPostRender(matrixStack, delta); } }); - - /** - * Called after rendering the whole hud, which is displayed in game, in a world. - * - * @param drawContext the {@link DrawContext} instance - * @param tickCounter the {@link RenderTickCounter} instance - */ - void onHudRender(DrawContext drawContext, RenderTickCounter tickCounter); } diff --git a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/InGameHudMixin.java b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/InGameHudMixin.java index 61f48b3ca2..d515d5e728 100644 --- a/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/InGameHudMixin.java +++ b/fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/InGameHudMixin.java @@ -16,6 +16,9 @@ package net.fabricmc.fabric.mixin.client.rendering; +import net.fabricmc.fabric.api.client.rendering.v1.HudPostRenderCallback; +import net.fabricmc.fabric.api.client.rendering.v1.HudPreRenderCallback; + import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -25,12 +28,16 @@ import net.minecraft.client.gui.hud.InGameHud; import net.minecraft.client.render.RenderTickCounter; -import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; - @Mixin(InGameHud.class) public class InGameHudMixin { + + @Inject(method = "render", at = @At(value = "HEAD")) + public void renderHead(DrawContext drawContext, RenderTickCounter tickCounter, CallbackInfo callbackInfo) { + HudPreRenderCallback.EVENT.invoker().onHudPreRender(drawContext, tickCounter); + } + @Inject(method = "render", at = @At(value = "TAIL")) - public void render(DrawContext drawContext, RenderTickCounter tickCounter, CallbackInfo callbackInfo) { - HudRenderCallback.EVENT.invoker().onHudRender(drawContext, tickCounter); + public void renderTail(DrawContext drawContext, RenderTickCounter tickCounter, CallbackInfo callbackInfo) { + HudPostRenderCallback.EVENT.invoker().onHudPostRender(drawContext, tickCounter); } } diff --git a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/HudAndShaderTest.java b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/HudAndShaderTest.java index 8aa2b8d0db..7933bb2fea 100644 --- a/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/HudAndShaderTest.java +++ b/fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/HudAndShaderTest.java @@ -17,6 +17,10 @@ package net.fabricmc.fabric.test.rendering.client; import com.mojang.blaze3d.systems.RenderSystem; + +import net.fabricmc.fabric.api.client.rendering.v1.HudPostRenderCallback; +import net.fabricmc.fabric.api.client.rendering.v1.HudPreRenderCallback; + import org.joml.Matrix4f; import net.minecraft.client.MinecraftClient; @@ -31,10 +35,9 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.rendering.v1.CoreShaderRegistrationCallback; -import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; /** - * Tests {@link HudRenderCallback} and {@link CoreShaderRegistrationCallback} by drawing a green rectangle + * Tests {@link HudPostRenderCallback}, {@link HudPreRenderCallback} and {@link CoreShaderRegistrationCallback} by drawing a green rectangle * in the lower-right corner of the screen. */ public class HudAndShaderTest implements ClientModInitializer { @@ -48,7 +51,25 @@ public void onInitializeClient() { context.register(id, VertexFormats.POSITION, program -> testShader = program); }); - HudRenderCallback.EVENT.register((drawContext, tickDelta) -> { + HudPreRenderCallback.EVENT.register((drawContext, tickDelta) -> { + MinecraftClient client = MinecraftClient.getInstance(); + Window window = client.getWindow(); + int x = 15; + int y = window.getScaledHeight() - 15; + RenderSystem.setShader(() -> testShader); + RenderSystem.setShaderColor(0f, 1f, 0f, 1f); + Matrix4f positionMatrix = drawContext.getMatrices().peek().getPositionMatrix(); + BufferBuilder buffer = Tessellator.getInstance().begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION); + buffer.vertex(positionMatrix, x, y, 50); + buffer.vertex(positionMatrix, x, y + 10, 50); + buffer.vertex(positionMatrix, x + 10, y + 10, 50); + buffer.vertex(positionMatrix, x + 10, y, 50); + BufferRenderer.drawWithGlobalProgram(buffer.end()); + // Reset shader color + RenderSystem.setShaderColor(1f, 1f, 1f, 1f); + }); + + HudPostRenderCallback.EVENT.register((drawContext, tickDelta) -> { MinecraftClient client = MinecraftClient.getInstance(); Window window = client.getWindow(); int x = window.getScaledWidth() - 15;