From 8bba210dfff0df299895c8b604e4ce760af785e3 Mon Sep 17 00:00:00 2001 From: Aeltumn Date: Tue, 12 Nov 2024 19:09:34 +0100 Subject: [PATCH] Add group splitting and fix wrong indices --- .../feature/ui/LayerWithReference.java | 1 + .../feature/ui/layer/NoxesiumLayer.java | 20 ++++++++++--------- .../feature/ui/layer/NoxesiumLayeredDraw.java | 4 ++-- .../feature/ui/render/DynamicElement.java | 8 ++++++++ .../feature/ui/render/ElementBufferGroup.java | 18 +++++++++-------- .../ui/render/NoxesiumUiRenderState.java | 11 +++++++++- 6 files changed, 42 insertions(+), 20 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 4eb0530..258caf5 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 @@ -6,6 +6,7 @@ * Holds a layer and a reference to its group. */ public record LayerWithReference( + int index, NoxesiumLayer.Layer layer, 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 77aae57..1778eb3 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 @@ -109,15 +109,17 @@ record Layer( STANDARD_LAYER_NAMES.put(3, "XP Level"); STANDARD_LAYER_NAMES.put(4, "Effects"); STANDARD_LAYER_NAMES.put(5, "Bossbar"); - STANDARD_LAYER_NAMES.put(6, "Sleep Overlay"); - STANDARD_LAYER_NAMES.put(7, "Demo Overlay"); - STANDARD_LAYER_NAMES.put(8, "Debug Overlay"); - STANDARD_LAYER_NAMES.put(9, "Scoreboard"); - STANDARD_LAYER_NAMES.put(10, "Actionbar"); - STANDARD_LAYER_NAMES.put(11, "Title"); - STANDARD_LAYER_NAMES.put(12, "Chat"); - STANDARD_LAYER_NAMES.put(13, "Tab List"); - STANDARD_LAYER_NAMES.put(14, "Subtitles"); + STANDARD_LAYER_NAMES.put(6, "Demo Overlay"); + STANDARD_LAYER_NAMES.put(7, "Debug Overlay"); + STANDARD_LAYER_NAMES.put(8, "Scoreboard"); + STANDARD_LAYER_NAMES.put(9, "Actionbar"); + STANDARD_LAYER_NAMES.put(10, "Title"); + STANDARD_LAYER_NAMES.put(11, "Chat"); + STANDARD_LAYER_NAMES.put(12, "Tab List"); + STANDARD_LAYER_NAMES.put(13, "Subtitles"); + + // The sleep overlay ends up ordered as 7th but is defined last. + STANDARD_LAYER_NAMES.put(14, "Sleep Overlay"); } public Layer(LayeredDraw.Layer layer) { 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 d5bf5db..790696d 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 @@ -110,7 +110,7 @@ public List flatten() { var result = new ArrayList(); for (var layer : layers) { switch (layer) { - case NoxesiumLayer.Layer single -> result.add(new LayerWithReference(single, null)); + case NoxesiumLayer.Layer single -> result.add(new LayerWithReference(result.size(), single, null)); case NoxesiumLayer.NestedLayers group -> process(group, result); } } @@ -123,7 +123,7 @@ public List flatten() { 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.Layer single -> list.add(new LayerWithReference(list.size(), single, target)); case NoxesiumLayer.NestedLayers group -> process(group, list); } } diff --git a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/DynamicElement.java b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/DynamicElement.java index f3adb83..e333656 100644 --- a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/DynamicElement.java +++ b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/DynamicElement.java @@ -82,6 +82,14 @@ public int getTextureId() { return bufferEmpty ? -1 : buffer.getTextureId(); } + /** + * Returns whether this element is always changing. Used to determine + * when it should be split up this buffer. + */ + public boolean isAlwaysChanging() { + return failedCheckCount >= 50; + } + /** * Process recently taken snapshots to determine changes. */ diff --git a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/ElementBufferGroup.java b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/ElementBufferGroup.java index 1d26152..d6673f3 100644 --- a/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/ElementBufferGroup.java +++ b/fabric/src/main/java/com/noxcrew/noxesium/feature/ui/render/ElementBufferGroup.java @@ -48,7 +48,7 @@ public ElementBuffer buffer() { public void drawDirectly(GuiGraphics guiGraphics, DeltaTracker deltaTracker) { for (var layer : layers) { if (layer.group() == null || layer.group().test()) { - renderLayer(guiGraphics, deltaTracker, layer.layer()); + renderLayer(guiGraphics, deltaTracker, layer.layer(), layer.index()); } } } @@ -58,6 +58,7 @@ public void drawDirectly(GuiGraphics guiGraphics, DeltaTracker deltaTracker) { */ public void addLayers(Collection layers) { this.layers.addAll(layers); + dynamic.redraw(); } /** @@ -65,13 +66,14 @@ public void addLayers(Collection layers) { */ public void removeLayers(Collection layers) { this.layers.removeAll(layers); + dynamic.redraw(); } /** - * Returns whether this group can be split. + * Returns whether this group should be split up. */ - public boolean canSplit() { - return size() > 1; + public boolean shouldSplit() { + return size() > 1 && dynamic.isAlwaysChanging(); } /** @@ -89,8 +91,7 @@ public ElementBufferGroup split() { var total = size(); if (total < 2) throw new IllegalArgumentException("Cannot split up an un-splittable group"); var half = (int) Math.ceil(((double) total) / 2.0); - var toSplit = layers.subList(half, total); - + var toSplit = new ArrayList<>(layers.subList(half, total)); removeLayers(toSplit); var newGroup = new ElementBufferGroup(); newGroup.addLayers(toSplit); @@ -102,16 +103,17 @@ public ElementBufferGroup split() { */ public void join(ElementBufferGroup other) { addLayers(other.layers); + other.close(); } /** * Renders a given layer. */ - public void renderLayer(GuiGraphics guiGraphics, DeltaTracker deltaTracker, NoxesiumLayer.Layer layer) { + public void renderLayer(GuiGraphics guiGraphics, DeltaTracker deltaTracker, NoxesiumLayer.Layer layer, int index) { // Set up the pose for each layer separately so their locations are correct // even if other layers are skipped. guiGraphics.pose().pushPose(); - guiGraphics.pose().translate(0f, 0f, layer.index() * LayeredDraw.Z_SEPARATION); + guiGraphics.pose().translate(0f, 0f, index * LayeredDraw.Z_SEPARATION); layer.layer().render(guiGraphics, deltaTracker); guiGraphics.pose().popPose(); } 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 acbfaae..12ef5d6 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 @@ -53,6 +53,15 @@ public void render(GuiGraphics guiGraphics, DeltaTracker deltaTracker, NoxesiumL group.update(); } + // Try to split up or merge together groups + var index = 0; + while (index < groups.size()) { + var group = groups.get(index++); + if (group.shouldSplit()) { + groups.add(index++, group.split()); + } + } + // Tick the groups, possibly redrawing the buffer contents, if any buffers got drawn to // we want to unbind the buffer afterwards var bound = false; @@ -70,7 +79,7 @@ public void render(GuiGraphics guiGraphics, DeltaTracker deltaTracker, NoxesiumL if (group.dynamic().update(nanoTime, guiGraphics, () -> { for (var layer : group.layers()) { if (layer.group() == null || layer.group().test()) { - group.renderLayer(guiGraphics, deltaTracker, layer.layer()); + group.renderLayer(guiGraphics, deltaTracker, layer.layer(), layer.index()); } } })) {