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

Add Hud Render Events #4119

Open
wants to merge 11 commits into
base: 1.21.1
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;

/**
* @deprecated Use {@link HudRenderEvents} instead. For the closest equivalent, use {@link HudRenderEvents#LAST}.
*/
@Deprecated
public interface HudRenderCallback {
Event<HudRenderCallback> EVENT = EventFactory.createArrayBacked(HudRenderCallback.class, (listeners) -> (matrixStack, delta) -> {
Event<HudRenderCallback> EVENT = EventFactory.createArrayBacked(HudRenderCallback.class, (listeners) -> (context, tickCounter) -> {
for (HudRenderCallback event : listeners) {
event.onHudRender(matrixStack, delta);
event.onHudRender(context, tickCounter);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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;

/**
* Events for rendering elements on the HUD.
*
* <p>These events will not be called if the HUD is hidden with F1.
*/
public class HudRenderEvents {
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Called at the start of HUD rendering, right before anything is rendered.
*/
public static final Event<HudRenderStage> START = createEventForStage();

/**
* Called after misc overlays (vignette, spyglass, and powder snow) have been rendered, and before the crosshair is rendered.
*/
public static final Event<HudRenderStage> AFTER_MISC_OVERLAYS = createEventForStage();

/**
* Called after the hotbar, status bars, and experience bar have been rendered, and before the status effects overlays are rendered.
*/
public static final Event<HudRenderStage> AFTER_MAIN_HUD = createEventForStage();

/**
* Called after the debug HUD, scoreboard, overlay message (action bar), and title and subtitle have been rendered, and before the {@link net.minecraft.client.gui.hud.ChatHud} is rendered.
*/
public static final Event<HudRenderStage> BEFORE_CHAT = createEventForStage();

/**
* Called after the entire HUD is rendered.
*/
public static final Event<HudRenderStage> LAST = createEventForStage();

private static Event<HudRenderStage> createEventForStage() {
return EventFactory.createArrayBacked(HudRenderStage.class, listeners -> (context, tickDelta) -> {
for (HudRenderStage listener : listeners) {
listener.onRender(context, tickDelta);
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
}
});
}

kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
@FunctionalInterface
public interface HudRenderStage {
/**
* Called sometime during a specific HUD render stage.
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param context The {@link DrawContext} instance
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
* @param tickCounter The {@link RenderTickCounter} instance
*/
void onRender(DrawContext context, RenderTickCounter tickCounter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,54 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

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

@Mixin(InGameHud.class)
public class InGameHudMixin {
@ModifyArg(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/LayeredDrawer;addLayer(Lnet/minecraft/client/gui/LayeredDrawer$Layer;)Lnet/minecraft/client/gui/LayeredDrawer;", ordinal = 0))
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
private LayeredDrawer.Layer fabric$beforeStartAndAfterMiscOverlays(LayeredDrawer.Layer miscOverlaysLayer) {
return (context, tickCounter) -> {
HudRenderEvents.START.invoker().onRender(context, tickCounter);
miscOverlaysLayer.render(context, tickCounter);
HudRenderEvents.AFTER_MISC_OVERLAYS.invoker().onRender(context, tickCounter);
};
}

@ModifyArg(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/LayeredDrawer;addLayer(Lnet/minecraft/client/gui/LayeredDrawer$Layer;)Lnet/minecraft/client/gui/LayeredDrawer;", ordinal = 3))
private LayeredDrawer.Layer fabric$afterMainHudAndExperienceLevel(LayeredDrawer.Layer experienceLevelLayer) {
return (context, tickCounter) -> {
experienceLevelLayer.render(context, tickCounter);
HudRenderEvents.AFTER_MAIN_HUD.invoker().onRender(context, tickCounter);
};
}

@ModifyArg(method = "<init>", slice = @Slice(from = @At(value = "NEW", target = "Lnet/minecraft/client/gui/LayeredDrawer;", ordinal = 1)), at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/LayeredDrawer;addLayer(Lnet/minecraft/client/gui/LayeredDrawer$Layer;)Lnet/minecraft/client/gui/LayeredDrawer;", ordinal = 5))
private LayeredDrawer.Layer fabric$beforeChat(LayeredDrawer.Layer beforeChatLayer) {
return (context, tickCounter) -> {
HudRenderEvents.BEFORE_CHAT.invoker().onRender(context, tickCounter);
beforeChatLayer.render(context, tickCounter);
};
}

@ModifyArg(method = "<init>", slice = @Slice(from = @At(value = "NEW", target = "Lnet/minecraft/client/gui/LayeredDrawer;", ordinal = 1)), at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/LayeredDrawer;addLayer(Lnet/minecraft/client/gui/LayeredDrawer$Layer;)Lnet/minecraft/client/gui/LayeredDrawer;", ordinal = 7))
private LayeredDrawer.Layer fabric$AfterSubtitlesHud(LayeredDrawer.Layer subtitlesHudLayer) {
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
return (context, tickCounter) -> {
subtitlesHudLayer.render(context, tickCounter);
HudRenderEvents.LAST.invoker().onRender(context, tickCounter);
};
}

@Deprecated
@Inject(method = "render", at = @At(value = "TAIL"))
public void render(DrawContext drawContext, RenderTickCounter tickCounter, CallbackInfo callbackInfo) {
HudRenderCallback.EVENT.invoker().onHudRender(drawContext, tickCounter);
Expand Down
1 change: 1 addition & 0 deletions fabric-rendering-v1/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"net.fabricmc.fabric.test.rendering.client.DimensionalRenderingTest",
"net.fabricmc.fabric.test.rendering.client.FeatureRendererTest",
"net.fabricmc.fabric.test.rendering.client.HudAndShaderTest",
"net.fabricmc.fabric.test.rendering.client.HudRenderEventsTests",
"net.fabricmc.fabric.test.rendering.client.TooltipComponentTests",
"net.fabricmc.fabric.test.rendering.client.WorldRenderEventsTests"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.test.rendering.client;

import net.minecraft.client.MinecraftClient;

import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderEvents;

public class HudRenderEventsTests implements ClientModInitializer {
@Override
public void onInitializeClient() {
// Render a blue rectangle at the top right of the screen, and it should be blocked by misc overlays such as vignette, spyglass, and powder snow
HudRenderEvents.START.register((context, tickCounter) -> {
context.fill(context.getScaledWindowWidth() - 200, 0, context.getScaledWindowWidth(), 30, 0xFF0000FF);
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
context.drawTextWithShadow(MinecraftClient.getInstance().textRenderer, "1. Blue rectangle blocked by overlays", context.getScaledWindowWidth() - 196, 10, 0xFFFFFFFF);
context.drawTextWithShadow(MinecraftClient.getInstance().textRenderer, "such as powder snow", context.getScaledWindowWidth() - 111, 20, 0xFFFFFFFF);
});
// Render a red square in the center of the screen underneath the crosshair
HudRenderEvents.AFTER_MISC_OVERLAYS.register((context, tickCounter) -> {
context.fill(context.getScaledWindowWidth() / 2 - 10, context.getScaledWindowHeight() / 2 - 10, context.getScaledWindowWidth() / 2 + 10, context.getScaledWindowHeight() / 2 + 10, 0xFFFF0000);
context.drawCenteredTextWithShadow(MinecraftClient.getInstance().textRenderer, "2. Red square underneath crosshair", context.getScaledWindowWidth() / 2, context.getScaledWindowHeight() / 2 + 10, 0xFFFFFFFF);
});
// Render a green rectangle at the bottom of the screen, and it should block the hotbar and status bars
HudRenderEvents.AFTER_MAIN_HUD.register((context, tickCounter) -> {
context.fill(context.getScaledWindowWidth() / 2 - 50, context.getScaledWindowHeight() - 50, context.getScaledWindowWidth() / 2 + 50, context.getScaledWindowHeight() - 10, 0xFF00FF00);
context.drawCenteredTextWithShadow(MinecraftClient.getInstance().textRenderer, "3. This green rectangle should block the hotbar and status bars.", context.getScaledWindowWidth() / 2, context.getScaledWindowHeight() - 40, 0xFFFFFFFF);
});
// Render a blue rectangle at the bottom left of the screen, and it should be blocked by the chat
HudRenderEvents.BEFORE_CHAT.register((context, tickCounter) -> {
context.fill(0, context.getScaledWindowHeight() - 40, 300, context.getScaledWindowHeight() - 50, 0xFF0000FF);
context.drawTextWithShadow(MinecraftClient.getInstance().textRenderer, "4. This blue rectangle should be blocked by the chat.", 0, context.getScaledWindowHeight() - 50, 0xFFFFFFFF);
});
// Render a yellow rectangle at the top of the screen, and it should block the player list
HudRenderEvents.LAST.register((context, tickCounter) -> {
context.fill(0, 0, context.getScaledWindowWidth(), 10, 0xFFFFFF00);
context.drawCenteredTextWithShadow(MinecraftClient.getInstance().textRenderer, "5. This yellow rectangle should block the player list.", context.getScaledWindowWidth() / 2, 0, 0xFFFFFFFF);
});
}
}
Loading