| 1 | package net.minecraft.client.renderer.state.gui;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.Window;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.Collections;
|
| 6 | import java.util.Comparator;
|
| 7 | import java.util.HashSet;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.function.Consumer;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.fabricmc.fabric.api.client.rendering.v1.FabricRenderState;
|
| 14 | import net.minecraft.SharedConstants;
|
| 15 | import net.minecraft.client.Minecraft;
|
| 16 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 17 | import net.minecraft.client.gui.render.TextureSetup;
|
| 18 | import net.minecraft.client.renderer.RenderPipelines;
|
| 19 | import net.minecraft.client.renderer.state.gui.pip.PictureInPictureRenderState;
|
| 20 | import org.joml.Matrix3x2f;
|
| 21 | import org.joml.Vector4f;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class GuiRenderState implements FabricRenderState {
|
| 26 | private static final int DEBUG_RECTANGLE_COLOR = 2000962815;
|
| 27 | private final List<GuiRenderState.Node> strata = new ArrayList<>();
|
| 28 | private int firstStratumAfterBlur = Integer.MAX_VALUE;
|
| 29 | private GuiRenderState.Node current;
|
| 30 | private final Set<Object> itemModelIdentities = new HashSet<>();
|
| 31 | private @Nullable ScreenRectangle lastElementBounds;
|
| 32 | public @Nullable PanoramaRenderState panoramaRenderState;
|
| 33 | public Vector4f clearColorOverride = new Vector4f(0.0F);
|
| 34 | public boolean isHudHidden;
|
| 35 | private @Nullable ScreenRectangle windowRectangleForDebug;
|
| 36 |
|
| 37 | public GuiRenderState() {
|
| 38 | this.nextStratum();
|
| 39 | }
|
| 40 |
|
| 41 | public void nextStratum() {
|
| 42 | this.current = new GuiRenderState.Node(null);
|
| 43 | this.strata.add(this.current);
|
| 44 | }
|
| 45 |
|
| 46 | public void blurBeforeThisStratum() {
|
| 47 | if (this.firstStratumAfterBlur != Integer.MAX_VALUE) {
|
| 48 | throw new IllegalStateException("Can only blur once per frame");
|
| 49 | }
|
| 50 |
|
| 51 | this.firstStratumAfterBlur = this.strata.size() - 1;
|
| 52 | }
|
| 53 |
|
| 54 | public void up() {
|
| 55 | if (this.current.up == null) {
|
| 56 | this.current.up = new GuiRenderState.Node(this.current);
|
| 57 | }
|
| 58 |
|
| 59 | this.current = this.current.up;
|
| 60 | }
|
| 61 |
|
| 62 | public void addItem(final GuiItemRenderState itemState) {
|
| 63 | if (this.findAppropriateNode(itemState)) {
|
| 64 | this.itemModelIdentities.add(itemState.itemStackRenderState().getModelIdentity());
|
| 65 | this.current.addItem(itemState);
|
| 66 | this.addDebugRectangleIfEnabled(itemState.bounds());
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | public void addText(final GuiTextRenderState textState) {
|
| 71 | if (this.findAppropriateNode(textState)) {
|
| 72 | this.current.addText(textState);
|
| 73 | this.addDebugRectangleIfEnabled(textState.bounds());
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | public void addPicturesInPictureState(final PictureInPictureRenderState picturesInPictureState) {
|
| 78 | if (this.findAppropriateNode(picturesInPictureState)) {
|
| 79 | this.current.addPicturesInPictureState(picturesInPictureState);
|
| 80 | this.addDebugRectangleIfEnabled(picturesInPictureState.bounds());
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | public void addGuiElement(final GuiElementRenderState blitState) {
|
| 85 | if (this.findAppropriateNode(blitState)) {
|
| 86 | this.current.addGuiElement(blitState);
|
| 87 | this.addDebugRectangleIfEnabled(blitState.bounds());
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | private void addDebugRectangleIfEnabled(final @Nullable ScreenRectangle bounds) {
|
| 92 | if (SharedConstants.DEBUG_RENDER_UI_LAYERING_RECTANGLES && bounds != null) {
|
| 93 | this.up();
|
| 94 | if (this.windowRectangleForDebug == null) {
|
| 95 | Window window = Minecraft.getInstance().getWindow();
|
| 96 | this.windowRectangleForDebug = new ScreenRectangle(0, 0, window.getGuiScaledWidth(), window.getGuiScaledHeight());
|
| 97 | }
|
| 98 |
|
| 99 | ScreenRectangle clippedBounds = this.windowRectangleForDebug.intersection(bounds);
|
| 100 | if (clippedBounds != null) {
|
| 101 | this.current
|
| 102 | .addGuiElement(
|
| 103 | new ColoredRectangleRenderState(
|
| 104 | RenderPipelines.GUI, TextureSetup.noTexture(), new Matrix3x2f(), 0, 0, 10000, 10000, 2000962815, 2000962815, clippedBounds
|
| 105 | )
|
| 106 | );
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | private boolean findAppropriateNode(final ScreenArea screenArea) {
|
| 112 | ScreenRectangle bounds = screenArea.bounds();
|
| 113 | if (bounds == null) {
|
| 114 | return false;
|
| 115 | }
|
| 116 |
|
| 117 | if (this.lastElementBounds != null && this.lastElementBounds.encompasses(bounds)) {
|
| 118 | this.up();
|
| 119 | } else {
|
| 120 | this.navigateToAboveHighestElementWithIntersectingBounds(bounds);
|
| 121 | }
|
| 122 |
|
| 123 | this.lastElementBounds = bounds;
|
| 124 | return true;
|
| 125 | }
|
| 126 |
|
| 127 | private void navigateToAboveHighestElementWithIntersectingBounds(final ScreenRectangle bounds) {
|
| 128 | GuiRenderState.Node node = this.strata.getLast();
|
| 129 |
|
| 130 | while (node.up != null) {
|
| 131 | node = node.up;
|
| 132 | }
|
| 133 |
|
| 134 | boolean found = false;
|
| 135 |
|
| 136 | while (!found) {
|
| 137 | found = this.hasIntersection(bounds, node.elementStates)
|
| 138 | || this.hasIntersection(bounds, node.itemStates)
|
| 139 | || this.hasIntersection(bounds, node.textStates)
|
| 140 | || this.hasIntersection(bounds, node.picturesInPictureStates);
|
| 141 | if (node.parent == null) {
|
| 142 | break;
|
| 143 | }
|
| 144 |
|
| 145 | if (!found) {
|
| 146 | node = node.parent;
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | this.current = node;
|
| 151 | if (found) {
|
| 152 | this.up();
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | private boolean hasIntersection(final ScreenRectangle bounds, final @Nullable List<? extends ScreenArea> states) {
|
| 157 | if (states != null) {
|
| 158 | for (ScreenArea area : states) {
|
| 159 | ScreenRectangle existingBounds = area.bounds();
|
| 160 | if (existingBounds != null && existingBounds.intersects(bounds)) {
|
| 161 | return true;
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | return false;
|
| 167 | }
|
| 168 |
|
| 169 | public void addBlitToCurrentLayer(final BlitRenderState blitState) {
|
| 170 | this.current.addGuiElement(blitState);
|
| 171 | }
|
| 172 |
|
| 173 | public void addGlyphToCurrentLayer(final GuiElementRenderState glyphState) {
|
| 174 | this.current.addGlyph(glyphState);
|
| 175 | }
|
| 176 |
|
| 177 | public Set<Object> getItemModelIdentities() {
|
| 178 | return this.itemModelIdentities;
|
| 179 | }
|
| 180 |
|
| 181 | public void forEachElement(final Consumer<GuiElementRenderState> consumer, final GuiRenderState.TraverseRange range) {
|
| 182 | this.traverse(node -> {
|
| 183 | if (node.elementStates != null || node.glyphStates != null) {
|
| 184 | if (node.elementStates != null) {
|
| 185 | for (GuiElementRenderState elementState : node.elementStates) {
|
| 186 | consumer.accept(elementState);
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | if (node.glyphStates != null) {
|
| 191 | for (GuiElementRenderState glyphState : node.glyphStates) {
|
| 192 | consumer.accept(glyphState);
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 | }, range);
|
| 197 | }
|
| 198 |
|
| 199 | public void forEachItem(final Consumer<GuiItemRenderState> consumer) {
|
| 200 | GuiRenderState.Node currentBackup = this.current;
|
| 201 | this.traverse(node -> {
|
| 202 | if (node.itemStates != null) {
|
| 203 | this.current = node;
|
| 204 |
|
| 205 | for (GuiItemRenderState itemState : node.itemStates) {
|
| 206 | consumer.accept(itemState);
|
| 207 | }
|
| 208 | }
|
| 209 | }, GuiRenderState.TraverseRange.ALL);
|
| 210 | this.current = currentBackup;
|
| 211 | }
|
| 212 |
|
| 213 | public void forEachText(final Consumer<GuiTextRenderState> consumer) {
|
| 214 | GuiRenderState.Node currentBackup = this.current;
|
| 215 | this.traverse(node -> {
|
| 216 | if (node.textStates != null) {
|
| 217 | for (GuiTextRenderState textState : node.textStates) {
|
| 218 | this.current = node;
|
| 219 | consumer.accept(textState);
|
| 220 | }
|
| 221 | }
|
| 222 | }, GuiRenderState.TraverseRange.ALL);
|
| 223 | this.current = currentBackup;
|
| 224 | }
|
| 225 |
|
| 226 | public void forEachPictureInPicture(final Consumer<PictureInPictureRenderState> consumer) {
|
| 227 | GuiRenderState.Node currentBackup = this.current;
|
| 228 | this.traverse(node -> {
|
| 229 | if (node.picturesInPictureStates != null) {
|
| 230 | this.current = node;
|
| 231 |
|
| 232 | for (PictureInPictureRenderState pictureInPictureState : node.picturesInPictureStates) {
|
| 233 | consumer.accept(pictureInPictureState);
|
| 234 | }
|
| 235 | }
|
| 236 | }, GuiRenderState.TraverseRange.ALL);
|
| 237 | this.current = currentBackup;
|
| 238 | }
|
| 239 |
|
| 240 | public void sortElements(final Comparator<GuiElementRenderState> comparator) {
|
| 241 | this.traverse(node -> {
|
| 242 | if (node.elementStates != null) {
|
| 243 | if (SharedConstants.DEBUG_SHUFFLE_UI_RENDERING_ORDER) {
|
| 244 | Collections.shuffle(node.elementStates);
|
| 245 | }
|
| 246 |
|
| 247 | node.elementStates.sort(comparator);
|
| 248 | }
|
| 249 | }, GuiRenderState.TraverseRange.ALL);
|
| 250 | }
|
| 251 |
|
| 252 | private void traverse(final Consumer<GuiRenderState.Node> consumer, final GuiRenderState.TraverseRange range) {
|
| 253 | int startIndex = 0;
|
| 254 | int endIndex = this.strata.size();
|
| 255 | if (range == GuiRenderState.TraverseRange.BEFORE_BLUR) {
|
| 256 | endIndex = Math.min(this.firstStratumAfterBlur, this.strata.size());
|
| 257 | } else if (range == GuiRenderState.TraverseRange.AFTER_BLUR) {
|
| 258 | startIndex = this.firstStratumAfterBlur;
|
| 259 | }
|
| 260 |
|
| 261 | for (int i = startIndex; i < endIndex; i++) {
|
| 262 | GuiRenderState.Node stratum = this.strata.get(i);
|
| 263 | this.traverse(stratum, consumer);
|
| 264 | }
|
| 265 | }
|
| 266 |
|
| 267 | private void traverse(final GuiRenderState.Node node, final Consumer<GuiRenderState.Node> consumer) {
|
| 268 | consumer.accept(node);
|
| 269 | if (node.up != null) {
|
| 270 | this.traverse(node.up, consumer);
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | public void reset() {
|
| 275 | this.itemModelIdentities.clear();
|
| 276 | this.strata.clear();
|
| 277 | this.firstStratumAfterBlur = Integer.MAX_VALUE;
|
| 278 | this.nextStratum();
|
| 279 | this.panoramaRenderState = null;
|
| 280 | this.clearColorOverride.set(0.0F);
|
| 281 | this.windowRectangleForDebug = null;
|
| 282 | }
|
| 283 |
|
| 284 | @Environment(EnvType.CLIENT)
|
| 285 | private static class Node {
|
| 286 | public final GuiRenderState.@Nullable Node parent;
|
| 287 | public GuiRenderState.@Nullable Node up;
|
| 288 | public @Nullable List<GuiElementRenderState> elementStates;
|
| 289 | public @Nullable List<GuiElementRenderState> glyphStates;
|
| 290 | public @Nullable List<GuiItemRenderState> itemStates;
|
| 291 | public @Nullable List<GuiTextRenderState> textStates;
|
| 292 | public @Nullable List<PictureInPictureRenderState> picturesInPictureStates;
|
| 293 |
|
| 294 | private Node(final GuiRenderState.@Nullable Node parent) {
|
| 295 | this.parent = parent;
|
| 296 | }
|
| 297 |
|
| 298 | public void addItem(final GuiItemRenderState itemState) {
|
| 299 | if (this.itemStates == null) {
|
| 300 | this.itemStates = new ArrayList<>();
|
| 301 | }
|
| 302 |
|
| 303 | this.itemStates.add(itemState);
|
| 304 | }
|
| 305 |
|
| 306 | public void addText(final GuiTextRenderState textState) {
|
| 307 | if (this.textStates == null) {
|
| 308 | this.textStates = new ArrayList<>();
|
| 309 | }
|
| 310 |
|
| 311 | this.textStates.add(textState);
|
| 312 | }
|
| 313 |
|
| 314 | public void addPicturesInPictureState(final PictureInPictureRenderState picturesInPictureState) {
|
| 315 | if (this.picturesInPictureStates == null) {
|
| 316 | this.picturesInPictureStates = new ArrayList<>();
|
| 317 | }
|
| 318 |
|
| 319 | this.picturesInPictureStates.add(picturesInPictureState);
|
| 320 | }
|
| 321 |
|
| 322 | public void addGuiElement(final GuiElementRenderState blitState) {
|
| 323 | if (this.elementStates == null) {
|
| 324 | this.elementStates = new ArrayList<>();
|
| 325 | }
|
| 326 |
|
| 327 | this.elementStates.add(blitState);
|
| 328 | }
|
| 329 |
|
| 330 | public void addGlyph(final GuiElementRenderState glyphState) {
|
| 331 | if (this.glyphStates == null) {
|
| 332 | this.glyphStates = new ArrayList<>();
|
| 333 | }
|
| 334 |
|
| 335 | this.glyphStates.add(glyphState);
|
| 336 | }
|
| 337 | }
|
| 338 |
|
| 339 | @Environment(EnvType.CLIENT)
|
| 340 | public enum TraverseRange {
|
| 341 | ALL,
|
| 342 | BEFORE_BLUR,
|
| 343 | AFTER_BLUR;
|
| 344 | }
|
| 345 | }
|
| 346 |
|