Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.21.1] Add HudPreRenderCallback Event and HudPostRenderCallback.EVENT #4275

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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;

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.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.RenderTickCounter;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;

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 @@ -25,12 +25,19 @@
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.render.RenderTickCounter;

import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.client.rendering.v1.HudPostRenderCallback;
import net.fabricmc.fabric.api.client.rendering.v1.HudPreRenderCallback;

@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,7 +17,8 @@
package net.fabricmc.fabric.test.rendering.client;

import com.mojang.blaze3d.systems.RenderSystem;

import org.joml.Matrix4f;

Check failure on line 21 in fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/HudAndShaderTest.java

View workflow job for this annotation

GitHub Actions / build (21-ubuntu)

Extra separation in import group before 'org.joml.Matrix4f'

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.ShaderProgram;
Expand All @@ -31,10 +32,11 @@

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.CoreShaderRegistrationCallback;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.fabric.api.client.rendering.v1.HudPostRenderCallback;
import net.fabricmc.fabric.api.client.rendering.v1.HudPreRenderCallback;

/**
* 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 +50,25 @@
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
Loading