| 1 | package net.minecraft.client.gui.components.debugchart;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.gui.Font;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.util.ARGB;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.util.debugchart.SampleStorage;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public abstract class AbstractDebugChart {
|
| 13 | protected static final int CHART_HEIGHT = 60;
|
| 14 | protected static final int LINE_WIDTH = 1;
|
| 15 | protected final Font font;
|
| 16 | protected final SampleStorage sampleStorage;
|
| 17 |
|
| 18 | protected AbstractDebugChart(final Font font, final SampleStorage sampleStorage) {
|
| 19 | this.font = font;
|
| 20 | this.sampleStorage = sampleStorage;
|
| 21 | }
|
| 22 |
|
| 23 | public int getWidth(final int maxWidth) {
|
| 24 | return Math.min(this.sampleStorage.capacity() + 2, maxWidth);
|
| 25 | }
|
| 26 |
|
| 27 | public int getFullHeight() {
|
| 28 | return 60 + 9;
|
| 29 | }
|
| 30 |
|
| 31 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int left, final int width, final int bottom) {
|
| 32 | graphics.fill(left, bottom - 60, left + width, bottom, -1873784752);
|
| 33 | long avg = 0L;
|
| 34 | long min = 2147483647L;
|
| 35 | long max = -2147483648L;
|
| 36 | int startIndex = Math.max(0, this.sampleStorage.capacity() - (width - 2));
|
| 37 | int sampleCount = this.sampleStorage.size() - startIndex;
|
| 38 |
|
| 39 | for (int i = 0; i < sampleCount; i++) {
|
| 40 | int currentX = left + i + 1;
|
| 41 | int sampleIndex = startIndex + i;
|
| 42 | long valueForAggregation = this.getValueForAggregation(sampleIndex);
|
| 43 | min = Math.min(min, valueForAggregation);
|
| 44 | max = Math.max(max, valueForAggregation);
|
| 45 | avg += valueForAggregation;
|
| 46 | this.extractSampleBars(graphics, bottom, currentX, sampleIndex);
|
| 47 | }
|
| 48 |
|
| 49 | graphics.horizontalLine(left, left + width - 1, bottom - 60, -1);
|
| 50 | graphics.horizontalLine(left, left + width - 1, bottom - 1, -1);
|
| 51 | graphics.verticalLine(left, bottom - 60, bottom, -1);
|
| 52 | graphics.verticalLine(left + width - 1, bottom - 60, bottom, -1);
|
| 53 | if (sampleCount > 0) {
|
| 54 | String minText = this.toDisplayString(min) + " min";
|
| 55 | String avgText = this.toDisplayString((double)avg / sampleCount) + " avg";
|
| 56 | String maxText = this.toDisplayString(max) + " max";
|
| 57 | graphics.text(this.font, minText, left + 2, bottom - 60 - 9, -2039584);
|
| 58 | graphics.centeredText(this.font, avgText, left + width / 2, bottom - 60 - 9, -2039584);
|
| 59 | graphics.text(this.font, maxText, left + width - this.font.width(maxText) - 2, bottom - 60 - 9, -2039584);
|
| 60 | }
|
| 61 |
|
| 62 | this.extractAdditionalLinesAndLabels(graphics, left, width, bottom);
|
| 63 | }
|
| 64 |
|
| 65 | protected void extractSampleBars(final GuiGraphicsExtractor graphics, final int bottom, final int currentX, final int sampleIndex) {
|
| 66 | this.extractMainSampleBar(graphics, bottom, currentX, sampleIndex);
|
| 67 | this.extractAdditionalSampleBars(graphics, bottom, currentX, sampleIndex);
|
| 68 | }
|
| 69 |
|
| 70 | protected void extractMainSampleBar(final GuiGraphicsExtractor graphics, final int bottom, final int currentX, final int sampleIndex) {
|
| 71 | long value = this.sampleStorage.get(sampleIndex);
|
| 72 | int sampleHeight = this.getSampleHeight(value);
|
| 73 | int color = this.getSampleColor(value);
|
| 74 | graphics.fill(currentX, bottom - sampleHeight, currentX + 1, bottom, color);
|
| 75 | }
|
| 76 |
|
| 77 | protected void extractAdditionalSampleBars(final GuiGraphicsExtractor graphics, final int bottom, final int currentX, final int sampleIndex) {
|
| 78 | }
|
| 79 |
|
| 80 | protected long getValueForAggregation(final int sampleIndex) {
|
| 81 | return this.sampleStorage.get(sampleIndex);
|
| 82 | }
|
| 83 |
|
| 84 | protected void extractAdditionalLinesAndLabels(final GuiGraphicsExtractor graphics, final int left, final int width, final int bottom) {
|
| 85 | }
|
| 86 |
|
| 87 | protected void extractStringWithShade(final GuiGraphicsExtractor graphics, final String str, final int x, final int y) {
|
| 88 | graphics.fill(x, y, x + this.font.width(str) + 1, y + 9, -1873784752);
|
| 89 | graphics.text(this.font, str, x + 1, y + 1, -2039584, false);
|
| 90 | }
|
| 91 |
|
| 92 | protected abstract String toDisplayString(double sample);
|
| 93 |
|
| 94 | protected abstract int getSampleHeight(double sample);
|
| 95 |
|
| 96 | protected abstract int getSampleColor(long sample);
|
| 97 |
|
| 98 | protected int getSampleColor(double sample, final double min, final int minColor, final double mid, final int midColor, final double max, final int maxColor) {
|
| 99 | sample = Mth.clamp(sample, min, max);
|
| 100 | return sample < mid
|
| 101 | ? ARGB.srgbLerp((float)((sample - min) / (mid - min)), minColor, midColor)
|
| 102 | : ARGB.srgbLerp((float)((sample - mid) / (max - mid)), midColor, maxColor);
|
| 103 | }
|
| 104 | }
|
| 105 |
|