Skip to content

Commit

Permalink
fix empty lines not collapsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts committed Jan 14, 2025
1 parent 72e3570 commit f8d9021
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/github/lunatrius/ingameinfo/Alignment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.github.lunatrius.ingameinfo;

import java.util.ArrayList;
import java.util.Collection;

import com.github.lunatrius.ingameinfo.client.gui.InfoText;

public enum Alignment {

TOPLEFT(2, 2),
Expand All @@ -12,6 +17,8 @@ public enum Alignment {
BOTTOMCENTER(0, -45),
BOTTOMRIGHT(-2, -2);

public static final Alignment[] VALUES = values();

private static final int MASK_X = 0x0C;
private static final int MASK_Y = 0x03;

Expand All @@ -23,6 +30,7 @@ public enum Alignment {
private static final int CENTER = 0x0C;
private static final int RIGHT = 0x08;

private final Collection<InfoText> lines = new ArrayList<>();
private int alignment;
public final int defaultX;
public final int defaultY;
Expand Down Expand Up @@ -105,6 +113,10 @@ public void setXY(String str) {
} catch (Exception ignored) {}
}

public Collection<InfoText> getLines() {
return this.lines;
}

static {
TOPLEFT.alignment = TOP | LEFT;
TOPCENTER.alignment = TOP | CENTER;
Expand Down
32 changes: 20 additions & 12 deletions src/main/java/com/github/lunatrius/ingameinfo/InGameInfoCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
Expand Down Expand Up @@ -56,9 +54,9 @@ public class InGameInfoCore {
minecraft,
minecraft.displayWidth,
minecraft.displayHeight);
private final Set<InfoText> infoTexts = new HashSet<>();
public int scaledWidth;
public int scaledHeight;
private boolean needsRefresh = true;

private InGameInfoCore() {}

Expand Down Expand Up @@ -119,12 +117,19 @@ public void onTickClient() {
scaledHeight = (int) (scaledResolution.getScaledHeight() / scale);
Tag.update();

if (infoTexts.isEmpty()) {
if (needsRefresh) {
refreshInfoTexts();
needsRefresh = false;
}

for (InfoText infoText : infoTexts) {
infoText.update();
for (Alignment alignment : Alignment.VALUES) {
int lastActiveIndex = 0;
for (InfoText infoText : alignment.getLines()) {
infoText.update(lastActiveIndex);
if (infoText.isActive()) {
lastActiveIndex++;
}
}
}

Tag.releaseResources();
Expand All @@ -136,8 +141,10 @@ public void onTickRender(ScaledResolution resolution) {
GL11.glPushMatrix();
float scale = ClientConfigurationHandler.Scale / 10;
GL11.glScalef(scale, scale, scale);
for (InfoText info : infoTexts) {
info.draw();
for (Alignment alignment : Alignment.VALUES) {
for (InfoText info : alignment.getLines()) {
info.draw();
}
}
GL11.glPopMatrix();
}
Expand All @@ -147,7 +154,7 @@ public boolean loadConfig(String filename) {
}

public boolean reloadConfig() {
infoTexts.clear();
needsRefresh = true;
format.clear();

if (parser == null) {
Expand Down Expand Up @@ -188,15 +195,16 @@ private InputStream getInputStream() {
}

public void refreshInfoTexts() {
for (Alignment alignment : Alignment.values()) {
for (Alignment alignment : Alignment.VALUES) {
List<List<Value>> lines = format.get(alignment);
alignment.getLines().clear();

if (lines == null) {
continue;
}

for (int i = 0; i < lines.size(); i++) {
infoTexts.add(new InfoText(i, alignment, lines.get(i)));
for (List<Value> line : lines) {
alignment.getLines().add(new InfoText(alignment, line));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,38 @@ public class InfoText extends Info {
private String text;
private final List<Value> values;
private final Alignment alignment;
private final int index;
private int index;
private boolean isActive;

public InfoText(int index, Alignment alignment, List<Value> values) {
public InfoText(Alignment alignment, List<Value> values) {
super(0, 0);
this.values = values;
this.alignment = alignment;
this.index = index;
for (Value value : values) {
value.setParent(this);
}
}

public void update() {
public void update(int index) {
this.index = index;
StringBuilder builder = new StringBuilder();
for (Value value : this.values) {
builder.append(getValue(value));
}

isActive = builder.length() > 0;
updateChildren(builder);
text = builder.toString();
updatePosition();
}

public boolean isActive() {
return isActive;
}

@Override
public void drawInfo() {
if (!isActive) return;
fontRenderer.drawStringWithShadow(text, getX(), getY(), 0x00FFFFFF);

for (Info child : attachedValues.values()) {
Expand Down

0 comments on commit f8d9021

Please sign in to comment.