Skip to content

Commit

Permalink
Hud Post and Pre Render Callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
JT122406 committed Dec 5, 2024
1 parent 96c409c commit c282cb8
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Check failure on line 6 in fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPostRenderCallback.java

View workflow job for this annotation

GitHub Actions / build (21-ubuntu)

Wrong order for 'net.minecraft.client.gui.DrawContext' import.
import net.minecraft.client.render.RenderTickCounter;

public interface HudPostRenderCallback {
Event<HudPostRenderCallback> 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);
}
Original file line number Diff line number Diff line change
@@ -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;

Check failure on line 6 in fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudPreRenderCallback.java

View workflow job for this annotation

GitHub Actions / build (21-ubuntu)

Wrong order for 'net.minecraft.client.gui.DrawContext' import.
import net.minecraft.client.render.RenderTickCounter;

public interface HudPreRenderCallback {
Event<HudPreRenderCallback> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<HudRenderCallback> EVENT = EventFactory.createArrayBacked(HudRenderCallback.class, (listeners) -> (matrixStack, delta) -> {
for (HudRenderCallback event : listeners) {
event.onHudRender(matrixStack, delta);
Event<HudPostRenderCallback> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Check failure on line 22 in fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/InGameHudMixin.java

View workflow job for this annotation

GitHub Actions / build (21-ubuntu)

Wrong order for 'org.spongepowered.asm.mixin.Mixin' import.
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -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 {

Check failure on line 32 in fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/InGameHudMixin.java

View workflow job for this annotation

GitHub Actions / build (21-ubuntu)

blank line after {

@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit c282cb8

Please sign in to comment.