Skip to content

Commit

Permalink
Add group splitting and fix wrong indices
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Nov 12, 2024
1 parent 0241318 commit 8bba210
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Holds a layer and a reference to its group.
*/
public record LayerWithReference(
int index,
NoxesiumLayer.Layer layer,
NoxesiumLayer.NestedLayers group
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public List<LayerWithReference> flatten() {
var result = new ArrayList<LayerWithReference>();
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);
}
}
Expand All @@ -123,7 +123,7 @@ public List<LayerWithReference> flatten() {
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.Layer single -> list.add(new LayerWithReference(list.size(), single, target));
case NoxesiumLayer.NestedLayers group -> process(group, list);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand All @@ -58,20 +58,22 @@ public void drawDirectly(GuiGraphics guiGraphics, DeltaTracker deltaTracker) {
*/
public void addLayers(Collection<LayerWithReference> layers) {
this.layers.addAll(layers);
dynamic.redraw();
}

/**
* Removes the given layers from this group.
*/
public void removeLayers(Collection<LayerWithReference> 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();
}

/**
Expand All @@ -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);
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
}
})) {
Expand Down

0 comments on commit 8bba210

Please sign in to comment.