From 5203e8ee18cfac108d888b1d0a430c68adbaa5b3 Mon Sep 17 00:00:00 2001 From: Aeltumn Date: Tue, 12 Nov 2024 17:52:32 +0100 Subject: [PATCH] Better updating when layers are added late --- .../feature/ui/LayerWithReference.java | 2 +- .../feature/ui/layer/NoxesiumLayer.java | 27 ++++++---- .../feature/ui/layer/NoxesiumLayeredDraw.java | 52 +++++++++++++------ .../ui/render/NoxesiumUiRenderState.java | 2 - .../noxesium/mixin/ui/LayeredDrawMixin.java | 2 +- 5 files changed, 55 insertions(+), 30 deletions(-) diff --git a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/LayerWithReference.java b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/LayerWithReference.java index 52de637..4eb0530 100644 --- a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/LayerWithReference.java +++ b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/LayerWithReference.java @@ -7,6 +7,6 @@ */ public record LayerWithReference( NoxesiumLayer.Layer layer, - NoxesiumLayer.LayerGroup group + NoxesiumLayer.NestedLayers group ) { } diff --git a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/layer/NoxesiumLayer.java b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/layer/NoxesiumLayer.java index b637cf3..77aae57 100644 --- a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/layer/NoxesiumLayer.java +++ b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/layer/NoxesiumLayer.java @@ -14,34 +14,43 @@ public sealed interface NoxesiumLayer { /** - * Stores a collection of layers. + * Stores a collection of nested layers. */ - final class LayerGroup implements NoxesiumLayer { + final class NestedLayers implements NoxesiumLayer { - private final List layers; - private final List groups; + private final NoxesiumLayeredDraw inner; + private final List groups; private final BooleanSupplier condition; private boolean conditionResult = false; private boolean changedRecently = false; - public LayerGroup(List layers, BooleanSupplier condition) { - this.layers = layers; + public NestedLayers(NoxesiumLayeredDraw inner, BooleanSupplier condition) { + this.inner = inner; this.condition = condition; // Pre-filter which groups are a layer group object this.groups = new ArrayList<>(); - for (var layer : layers) { - if (layer instanceof NoxesiumLayer.LayerGroup group) { + for (var layer : layers()) { + if (layer instanceof NestedLayers group) { this.groups.add(group); } } } + /** + * Returns the inner layered draw object. + * We keep this object around as some other mods may add + * layers after initialization. + */ + public NoxesiumLayeredDraw inner() { + return inner; + } + /** * Returns the layers in this group. */ public List layers() { - return layers; + return inner.layers(); } /** diff --git a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/layer/NoxesiumLayeredDraw.java b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/layer/NoxesiumLayeredDraw.java index f11e6e7..d5bf5db 100644 --- a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/layer/NoxesiumLayeredDraw.java +++ b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/layer/NoxesiumLayeredDraw.java @@ -1,11 +1,10 @@ package com.noxcrew.noxesium.feature.ui.layer; import com.noxcrew.noxesium.NoxesiumMod; -import com.noxcrew.noxesium.feature.rule.ServerRules; import com.noxcrew.noxesium.feature.ui.LayerWithReference; +import com.noxcrew.noxesium.feature.ui.render.NoxesiumUiRenderState; import com.noxcrew.noxesium.feature.ui.render.api.NoxesiumRenderState; import com.noxcrew.noxesium.feature.ui.render.api.NoxesiumRenderStateHolder; -import com.noxcrew.noxesium.feature.ui.render.NoxesiumUiRenderState; import net.minecraft.client.DeltaTracker; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.LayeredDraw; @@ -23,8 +22,9 @@ public class NoxesiumLayeredDraw implements LayeredDraw.Layer, NoxesiumRenderStateHolder { private final List layers = new ArrayList<>(); - private final List subgroups = new ArrayList<>(); - private int size; + private final List subgroups = new ArrayList<>(); + private final List parents = new ArrayList<>(); + private int size = -1; private NoxesiumUiRenderState state; /** @@ -37,7 +37,7 @@ public List layers() { /** * Returns all groups within this layered draw. */ - public List subgroups() { + public List subgroups() { return subgroups; } @@ -46,15 +46,15 @@ public List subgroups() { */ public void add(NoxesiumLayer layer) { layers.add(layer); - if (layer instanceof NoxesiumLayer.LayerGroup group) { - subgroups.add(group); + if (layer instanceof NoxesiumLayer.NestedLayers nested) { + subgroups.add(nested); + + // Add this to the parents of the other so we can update it. + nested.inner().parents.add(this); } - // We naively assume groups are not edited - // after being added so we don't need to actually - // track the size we just want to know if there - // were new layers added. - size++; + // Trigger a recursive update to the parents + update(); } @Override @@ -93,7 +93,7 @@ private void renderLayerDirectly(GuiGraphics guiGraphics, DeltaTracker deltaTrac single.layer().render(guiGraphics, deltaTracker); guiGraphics.pose().translate(0f, 0f, LayeredDraw.Z_SEPARATION); } - case NoxesiumLayer.LayerGroup group -> { + case NoxesiumLayer.NestedLayers group -> { if (group.condition().getAsBoolean()) { for (var subLayer : group.layers()) { renderLayerDirectly(guiGraphics, deltaTracker, subLayer); @@ -111,7 +111,7 @@ public List flatten() { for (var layer : layers) { switch (layer) { case NoxesiumLayer.Layer single -> result.add(new LayerWithReference(single, null)); - case NoxesiumLayer.LayerGroup group -> process(group, result); + case NoxesiumLayer.NestedLayers group -> process(group, result); } } return result; @@ -120,11 +120,11 @@ public List flatten() { /** * Adds the contents of the layer group to the given list. */ - private void process(NoxesiumLayer.LayerGroup target, List list) { + private void process(NoxesiumLayer.NestedLayers target, List list) { for (var layer : target.layers()) { switch (layer) { case NoxesiumLayer.Layer single -> list.add(new LayerWithReference(single, target)); - case NoxesiumLayer.LayerGroup group -> process(group, list); + case NoxesiumLayer.NestedLayers group -> process(group, list); } } } @@ -133,11 +133,29 @@ private void process(NoxesiumLayer.LayerGroup target, List l * Returns the size of this layered draw. */ public int size() { + // When the size is -1 we need to re-determine the value + if (size == -1) { + for (var layer : layers) { + switch (layer) { + case NoxesiumLayer.Layer ignored -> size++; + case NoxesiumLayer.NestedLayers group -> size += group.inner().size(); + } + } + } return size; } + /** + * Updates the size of this object and its parents. + */ + private void update() { + size = -1; + parents.forEach(NoxesiumLayeredDraw::update); + } + + @Nullable @Override - public @Nullable NoxesiumRenderState get() { + public NoxesiumRenderState get() { return state; } diff --git a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/NoxesiumUiRenderState.java b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/NoxesiumUiRenderState.java index 1212543..acbfaae 100644 --- a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/NoxesiumUiRenderState.java +++ b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/NoxesiumUiRenderState.java @@ -30,8 +30,6 @@ public List groups() { public void render(GuiGraphics guiGraphics, DeltaTracker deltaTracker, NoxesiumLayeredDraw layeredDraw) { var nanoTime = System.nanoTime(); - // TODO Merge together neighboring buffers that are on the same cycle - // Update which groups exist if (lastSize != layeredDraw.size()) { lastSize = layeredDraw.size(); diff --git a/fabric/src/main/java/com/noxcrew/noxesium/mixin/ui/LayeredDrawMixin.java b/fabric/src/main/java/com/noxcrew/noxesium/mixin/ui/LayeredDrawMixin.java index b12027a..8a11506 100644 --- a/fabric/src/main/java/com/noxcrew/noxesium/mixin/ui/LayeredDrawMixin.java +++ b/fabric/src/main/java/com/noxcrew/noxesium/mixin/ui/LayeredDrawMixin.java @@ -40,7 +40,7 @@ private LayeredDraw addLayer(LayeredDraw.Layer layer, Operation ori @WrapMethod(method = "add(Lnet/minecraft/client/gui/LayeredDraw;Ljava/util/function/BooleanSupplier;)Lnet/minecraft/client/gui/LayeredDraw;") private LayeredDraw addGroup(LayeredDraw layeredDraw, BooleanSupplier booleanSupplier, Operation original) { - noxesium$layeredDraw.add(new NoxesiumLayer.LayerGroup(((LayeredDrawExtension) layeredDraw).noxesium$get().layers(), booleanSupplier)); + noxesium$layeredDraw.add(new NoxesiumLayer.NestedLayers(((LayeredDrawExtension) layeredDraw).noxesium$get(), booleanSupplier)); return ((LayeredDraw) (Object) this); }