Skip to content

Commit abf6dec

Browse files
committed
add debug graph to graph and render data change
1 parent b6930aa commit abf6dec

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package doggytalents.client.debug;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
import java.util.Map;
8+
import org.apache.commons.lang3.tuple.Pair;
9+
import com.mojang.blaze3d.systems.RenderSystem;
10+
import com.mojang.blaze3d.vertex.BufferUploader;
11+
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
12+
import com.mojang.blaze3d.vertex.Tesselator;
13+
import com.mojang.blaze3d.vertex.VertexFormat.Mode;
14+
15+
import net.minecraft.client.gui.GuiGraphics;
16+
import net.minecraft.client.renderer.GameRenderer;
17+
import net.minecraft.util.FastColor;
18+
import net.minecraft.util.Mth;
19+
import net.neoforged.neoforge.client.event.RenderGuiEvent;
20+
21+
public class DebugGraph {
22+
public static final DebugGraph COMMON_INSTANCE = new DebugGraph(0, 0, 480, 120);
23+
24+
public static DebugGraph shared() {
25+
return COMMON_INSTANCE;
26+
}
27+
28+
public int x, y, width, height;
29+
30+
public DebugGraph(int x, int y, int w, int h) {
31+
this.x = x;
32+
this.y = y;
33+
this.width = w;
34+
this.height = h;
35+
}
36+
37+
private int maxHistory = 100;
38+
private final Map<String, RecordEntry> historyMap = new HashMap<>();
39+
40+
public void reset(int maxHistory, EntriesBuilder builder) {
41+
this.maxHistory = maxHistory;
42+
historyMap.clear();
43+
if (maxHistory <= 0 || builder.entries.isEmpty()) {
44+
maxHistory = 0;
45+
return;
46+
}
47+
builder.entries.forEach(pair -> {
48+
historyMap.put(pair.getLeft(), pair.getRight());
49+
});
50+
}
51+
52+
public void recordValue(String name, float val) {
53+
if (maxHistory <= 0)
54+
return;
55+
var entry = this.historyMap.get(name);
56+
if (entry == null)
57+
return;
58+
val = Mth.clamp(val, entry.minValue(), entry.maxValue());
59+
var history = entry.history();
60+
history.add(val);
61+
if (history.size() > maxHistory)
62+
history.removeFirst();
63+
}
64+
65+
public void stop() {
66+
this.reset(0, EntriesBuilder.NULL);
67+
}
68+
69+
public void render(GuiGraphics graphics, float pticks) {
70+
71+
//Background
72+
int cl = 0x005e5d5d | 0x48000000;
73+
graphics.fill(x, y, x+width, y+height, cl);
74+
75+
for (var entry : this.historyMap.entrySet()) {
76+
renderEntry(x, y, width, height, entry.getValue());
77+
}
78+
}
79+
80+
private void renderEntry(int x, int y, int w, int h, RecordEntry entry) {
81+
final float min_value = entry.minValue();
82+
final float max_value = entry.maxValue();
83+
final var history = entry.history();
84+
85+
if (history.size() < 2)
86+
return;
87+
88+
//RenderSystem.enableBlend();
89+
//RenderSystem.defaultBlendFunc();
90+
RenderSystem.setShader(GameRenderer::getPositionColorShader);
91+
92+
var tessellator = Tesselator.getInstance();
93+
var buffer = tessellator.begin(Mode.DEBUG_LINES, DefaultVertexFormat.POSITION_COLOR);
94+
95+
for (int i = 0; i < history.size() - 1; ++i) {
96+
float val = history.get(i);
97+
float val1 = history.get(i + 1);
98+
99+
//normalize & clamp
100+
val = (val - min_value)/ (max_value - min_value);
101+
val1 = (val1 - min_value)/ (max_value - min_value);
102+
val = Mth.clamp(val, 0, 1);
103+
val1 = Mth.clamp(val1, 0, 1);
104+
105+
float val_x = x + ((float)i / maxHistory) * w;
106+
float val1_x = x + ((float)(i + 1) / maxHistory) * w;
107+
108+
float val_y = y + h - val * h;
109+
float val1_y = y + h - val1 * h;
110+
111+
int r = FastColor.ARGB32.red(entry.color());
112+
int g = FastColor.ARGB32.green(entry.color());
113+
int b = FastColor.ARGB32.blue(entry.color());
114+
int a = FastColor.ARGB32.alpha(entry.color());
115+
buffer.addVertex(val_x, val_y, 0).setColor(r, g, b, a);
116+
buffer.addVertex(val1_x, val1_y, 0).setColor(r, g, b, a);
117+
}
118+
119+
BufferUploader.drawWithShader(buffer.buildOrThrow());
120+
}
121+
122+
// public static void afterGuiRender(RenderGuiEvent.Post event) {
123+
// var shared = shared();
124+
// if (shared.maxHistory <= 0)
125+
// return;
126+
// shared.render(event.getGuiGraphics(), event.getPartialTick().getGameTimeDeltaPartialTick(true));
127+
// }
128+
129+
public static EntriesBuilder entriesBuilder() {
130+
return new EntriesBuilder();
131+
}
132+
133+
public static class EntriesBuilder {
134+
private static final EntriesBuilder NULL = new EntriesBuilder();
135+
136+
private final List<Pair<String, RecordEntry>> entries = new ArrayList<>();
137+
138+
private EntriesBuilder() {}
139+
140+
public EntriesBuilder entry(String id, float min, float max, int color) {
141+
if (min > max)
142+
throw new IllegalArgumentException("min cannot be greater than max");
143+
this.entries.add(Pair.of(id, new RecordEntry(new LinkedList<>(), min, max, color)));
144+
return this;
145+
}
146+
}
147+
148+
private static record RecordEntry(
149+
LinkedList<Float> history,
150+
float minValue, float maxValue,
151+
int color
152+
) {}
153+
154+
}

0 commit comments

Comments
 (0)