Skip to content

Commit

Permalink
Better updating when layers are added late
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Nov 12, 2024
1 parent e0aa3e9 commit 5203e8e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
*/
public record LayerWithReference(
NoxesiumLayer.Layer layer,
NoxesiumLayer.LayerGroup group
NoxesiumLayer.NestedLayers group
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<NoxesiumLayer> layers;
private final List<NoxesiumLayer.LayerGroup> groups;
private final NoxesiumLayeredDraw inner;
private final List<NestedLayers> groups;
private final BooleanSupplier condition;
private boolean conditionResult = false;
private boolean changedRecently = false;

public LayerGroup(List<NoxesiumLayer> 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<NoxesiumLayer> layers() {
return layers;
return inner.layers();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,8 +22,9 @@
public class NoxesiumLayeredDraw implements LayeredDraw.Layer, NoxesiumRenderStateHolder<NoxesiumUiRenderState> {

private final List<NoxesiumLayer> layers = new ArrayList<>();
private final List<NoxesiumLayer.LayerGroup> subgroups = new ArrayList<>();
private int size;
private final List<NoxesiumLayer.NestedLayers> subgroups = new ArrayList<>();
private final List<NoxesiumLayeredDraw> parents = new ArrayList<>();
private int size = -1;
private NoxesiumUiRenderState state;

/**
Expand All @@ -37,7 +37,7 @@ public List<NoxesiumLayer> layers() {
/**
* Returns all groups within this layered draw.
*/
public List<NoxesiumLayer.LayerGroup> subgroups() {
public List<NoxesiumLayer.NestedLayers> subgroups() {
return subgroups;
}

Expand All @@ -46,15 +46,15 @@ public List<NoxesiumLayer.LayerGroup> 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
Expand Down Expand Up @@ -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);
Expand All @@ -111,7 +111,7 @@ public List<LayerWithReference> 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;
Expand All @@ -120,11 +120,11 @@ public List<LayerWithReference> flatten() {
/**
* Adds the contents of the layer group to the given list.
*/
private void process(NoxesiumLayer.LayerGroup target, List<LayerWithReference> list) {
private void process(NoxesiumLayer.NestedLayers target, List<LayerWithReference> 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);
}
}
}
Expand All @@ -133,11 +133,29 @@ private void process(NoxesiumLayer.LayerGroup target, List<LayerWithReference> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public List<ElementBufferGroup> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private LayeredDraw addLayer(LayeredDraw.Layer layer, Operation<LayeredDraw> 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<LayeredDraw> 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);
}

Expand Down

0 comments on commit 5203e8e

Please sign in to comment.