-
Notifications
You must be signed in to change notification settings - Fork 413
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
kevinthegreat1
wants to merge
11
commits into
FabricMC:1.21.1
Choose a base branch
from
kevinthegreat1:hud-render-events
base: 1.21.1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−2
Open
Add Hud Render Events #4119
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9724bae
Add HudRenderEvents
kevinthegreat1 584d9e8
Add HudRenderEventsTests and deprecate HudRenderCallback
kevinthegreat1 a7ac0f1
Update tests
kevinthegreat1 668c533
Add client parameter and apply suggestions
kevinthegreat1 f781eb7
Split HudRenderEvents into separate interfaces
kevinthegreat1 f13daa5
Fix before chat and last
kevinthegreat1 ce182ed
Add after sleep overlay event and update after main hud injection point
kevinthegreat1 6362810
Add comments for injection points
kevinthegreat1 28557b1
Revert splitting HudRenderEvents into separate interfaces
kevinthegreat1 0d2bcf2
Use vanilla layered drawer layer interface
kevinthegreat1 4a9d3d8
Cleanup InGameHudMixin
kevinthegreat1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
...ering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/HudRenderEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* 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.MinecraftClient; | ||
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 final class HudRenderEvents { | ||
/** | ||
* Called at the start of HUD rendering, right before anything is rendered. | ||
*/ | ||
public static final Event<Start> START = EventFactory.createArrayBacked(Start.class, listeners -> (client, context, tickCounter) -> { | ||
for (Start listener : listeners) { | ||
listener.onHudStart(client, context, tickCounter); | ||
} | ||
}); | ||
|
||
/** | ||
* Called after misc overlays (vignette, spyglass, and powder snow) have been rendered, and before the crosshair is rendered. | ||
*/ | ||
public static final Event<AfterMiscOverlays> AFTER_MISC_OVERLAYS = EventFactory.createArrayBacked(AfterMiscOverlays.class, listeners -> (client, context, tickCounter) -> { | ||
for (AfterMiscOverlays listener : listeners) { | ||
listener.afterMiscOverlays(client, context, tickCounter); | ||
} | ||
}); | ||
|
||
/** | ||
* Called after the hotbar, status bars, and experience bar have been rendered, and before the status effects overlays are rendered. | ||
*/ | ||
public static final Event<AfterMainHud> AFTER_MAIN_HUD = EventFactory.createArrayBacked(AfterMainHud.class, listeners -> (client, context, tickCounter) -> { | ||
for (AfterMainHud listener : listeners) { | ||
listener.afterMainHud(client, context, tickCounter); | ||
} | ||
}); | ||
|
||
/** | ||
* 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<BeforeChat> BEFORE_CHAT = EventFactory.createArrayBacked(BeforeChat.class, listeners -> (client, context, tickCounter) -> { | ||
for (BeforeChat listener : listeners) { | ||
listener.beforeChat(client, context, tickCounter); | ||
} | ||
}); | ||
|
||
/** | ||
* Called after the entire HUD is rendered. | ||
*/ | ||
public static final Event<Last> LAST = EventFactory.createArrayBacked(Last.class, listeners -> (client, context, tickCounter) -> { | ||
for (Last listener : listeners) { | ||
listener.onHudLast(client, context, tickCounter); | ||
} | ||
}); | ||
|
||
private HudRenderEvents() { } | ||
|
||
@FunctionalInterface | ||
public interface Start { | ||
/** | ||
* Called at the start of HUD rendering, right before anything is rendered. | ||
* | ||
* @param client the {@link MinecraftClient} instance | ||
* @param context the {@link DrawContext} instance | ||
* @param tickCounter the {@link RenderTickCounter} instance with access to tick delta | ||
*/ | ||
void onHudStart(MinecraftClient client, DrawContext context, RenderTickCounter tickCounter); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface AfterMiscOverlays { | ||
kevinthegreat1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Called after misc overlays (vignette, spyglass, and powder snow) have been rendered, and before the crosshair is rendered. | ||
* | ||
* @param client the {@link MinecraftClient} instance | ||
* @param context the {@link DrawContext} instance | ||
* @param tickCounter the {@link RenderTickCounter} instance with access to tick delta | ||
*/ | ||
void afterMiscOverlays(MinecraftClient client, DrawContext context, RenderTickCounter tickCounter); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface AfterMainHud { | ||
/** | ||
* Called after the hotbar, status bars, and experience bar have been rendered, and before the status effects overlays are rendered. | ||
kevinthegreat1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param client the {@link MinecraftClient} instance | ||
* @param context the {@link DrawContext} instance | ||
* @param tickCounter the {@link RenderTickCounter} instance with access to tick delta | ||
*/ | ||
void afterMainHud(MinecraftClient client, DrawContext context, RenderTickCounter tickCounter); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface BeforeChat { | ||
/** | ||
* 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. | ||
* | ||
* @param client the {@link MinecraftClient} instance | ||
* @param context the {@link DrawContext} instance | ||
* @param tickCounter the {@link RenderTickCounter} instance with access to tick delta | ||
*/ | ||
void beforeChat(MinecraftClient client, DrawContext context, RenderTickCounter tickCounter); | ||
} | ||
kevinthegreat1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
kevinthegreat1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@FunctionalInterface | ||
public interface Last { | ||
/** | ||
* Called after the entire HUD is rendered. | ||
* | ||
* @param client the {@link MinecraftClient} instance | ||
* @param context the {@link DrawContext} instance | ||
* @param tickCounter the {@link RenderTickCounter} instance with access to tick delta | ||
*/ | ||
void onHudLast(MinecraftClient client, DrawContext context, RenderTickCounter tickCounter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...rc/testmodClient/java/net/fabricmc/fabric/test/rendering/client/HudRenderEventsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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.minecraft.util.Colors; | ||
|
||
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((client, context, tickCounter) -> { | ||
context.fill(context.getScaledWindowWidth() - 200, 0, context.getScaledWindowWidth(), 30, Colors.BLUE); | ||
context.drawTextWithShadow(MinecraftClient.getInstance().textRenderer, "1. Blue rectangle blocked by overlays", context.getScaledWindowWidth() - 196, 10, Colors.WHITE); | ||
context.drawTextWithShadow(MinecraftClient.getInstance().textRenderer, "such as powder snow", context.getScaledWindowWidth() - 111, 20, Colors.WHITE); | ||
}); | ||
// Render a red square in the center of the screen underneath the crosshair | ||
HudRenderEvents.AFTER_MISC_OVERLAYS.register((client, context, tickCounter) -> { | ||
context.fill(context.getScaledWindowWidth() / 2 - 10, context.getScaledWindowHeight() / 2 - 10, context.getScaledWindowWidth() / 2 + 10, context.getScaledWindowHeight() / 2 + 10, Colors.RED); | ||
context.drawCenteredTextWithShadow(MinecraftClient.getInstance().textRenderer, "2. Red square underneath crosshair", context.getScaledWindowWidth() / 2, context.getScaledWindowHeight() / 2 + 10, Colors.WHITE); | ||
}); | ||
// Render a green rectangle at the bottom of the screen, and it should block the hotbar and status bars | ||
HudRenderEvents.AFTER_MAIN_HUD.register((client, context, tickCounter) -> { | ||
context.fill(context.getScaledWindowWidth() / 2 - 50, context.getScaledWindowHeight() - 50, context.getScaledWindowWidth() / 2 + 50, context.getScaledWindowHeight() - 10, Colors.GREEN); | ||
context.drawCenteredTextWithShadow(MinecraftClient.getInstance().textRenderer, "3. This green rectangle should block the hotbar and status bars.", context.getScaledWindowWidth() / 2, context.getScaledWindowHeight() - 40, Colors.WHITE); | ||
}); | ||
// Render a blue rectangle at the bottom left of the screen, and it should be blocked by the chat | ||
HudRenderEvents.BEFORE_CHAT.register((client, context, tickCounter) -> { | ||
context.fill(0, context.getScaledWindowHeight() - 40, 300, context.getScaledWindowHeight() - 50, Colors.BLUE); | ||
context.drawTextWithShadow(MinecraftClient.getInstance().textRenderer, "4. This blue rectangle should be blocked by the chat.", 0, context.getScaledWindowHeight() - 50, Colors.WHITE); | ||
}); | ||
// Render a yellow rectangle at the top of the screen, and it should block the player list | ||
HudRenderEvents.LAST.register((client, context, tickCounter) -> { | ||
context.fill(context.getScaledWindowWidth() / 2 - 150, 0, context.getScaledWindowWidth() / 2 + 150, 15, Colors.YELLOW); | ||
context.drawCenteredTextWithShadow(MinecraftClient.getInstance().textRenderer, "5. This yellow rectangle should block the player list.", context.getScaledWindowWidth() / 2, 0, Colors.WHITE); | ||
}); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are events like this the best solution here? Would it be better to allow mods to register their own LayeredDrawer.Layer in a similar way to how the vanilla hud elements work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the code to use one interface, but I don't see a difference with registration methods. Wouldn't it still be implemented with an event?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking they could just be static? I dont think there is a need for an even unless im missing something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was under the impression that not using events is more complicated than just using events? So there's no need for registration methods? Are you saying just implement it with a synchronized list? Events also allow phase ordering if some mod wants to render before another.