| 1 | package net.minecraft.client.gui.components.debugchart;
|
| 2 |
|
| 3 | import java.util.Locale;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.Font;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.util.debugchart.SampleStorage;
|
| 10 |
|
| 11 | @Environment(EnvType.CLIENT)
|
| 12 | public class BandwidthDebugChart extends AbstractDebugChart {
|
| 13 | private static final int MIN_COLOR = -16711681;
|
| 14 | private static final int MID_COLOR = -6250241;
|
| 15 | private static final int MAX_COLOR = -65536;
|
| 16 | private static final int KILOBYTE = 1024;
|
| 17 | private static final int MEGABYTE = 1048576;
|
| 18 | private static final int CHART_TOP_VALUE = 1048576;
|
| 19 |
|
| 20 | public BandwidthDebugChart(final Font font, final SampleStorage sampleStorage) {
|
| 21 | super(font, sampleStorage);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | protected void extractAdditionalLinesAndLabels(final GuiGraphicsExtractor graphics, final int left, final int width, final int bottom) {
|
| 26 | this.extractLabeledLineAtValue(graphics, left, width, bottom, 64);
|
| 27 | this.extractLabeledLineAtValue(graphics, left, width, bottom, 1024);
|
| 28 | this.extractLabeledLineAtValue(graphics, left, width, bottom, 16384);
|
| 29 | this.extractStringWithShade(graphics, toDisplayStringInternal(1048576.0), left + 1, bottom - getSampleHeightInternal(1048576.0) + 1);
|
| 30 | }
|
| 31 |
|
| 32 | private void extractLabeledLineAtValue(final GuiGraphicsExtractor graphics, final int left, final int width, final int bottom, final int bytesPerSecond) {
|
| 33 | this.extractLineWithLabel(graphics, left, width, bottom - getSampleHeightInternal(bytesPerSecond), toDisplayStringInternal(bytesPerSecond));
|
| 34 | }
|
| 35 |
|
| 36 | private void extractLineWithLabel(final GuiGraphicsExtractor graphics, final int x, final int width, final int y, final String label) {
|
| 37 | this.extractStringWithShade(graphics, label, x + 1, y + 1);
|
| 38 | graphics.horizontalLine(x, x + width - 1, y, -1);
|
| 39 | }
|
| 40 |
|
| 41 | @Override
|
| 42 | protected String toDisplayString(final double bytesPerTick) {
|
| 43 | return toDisplayStringInternal(toBytesPerSecond(bytesPerTick));
|
| 44 | }
|
| 45 |
|
| 46 | private static String toDisplayStringInternal(final double bytesPerSecond) {
|
| 47 | if (bytesPerSecond >= 1048576.0) {
|
| 48 | return String.format(Locale.ROOT, "%.1f MiB/s", bytesPerSecond / 1048576.0);
|
| 49 | } else {
|
| 50 | return bytesPerSecond >= 1024.0
|
| 51 | ? String.format(Locale.ROOT, "%.1f KiB/s", bytesPerSecond / 1024.0)
|
| 52 | : String.format(Locale.ROOT, "%d B/s", Mth.floor(bytesPerSecond));
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | protected int getSampleHeight(final double bytesPerTick) {
|
| 58 | return getSampleHeightInternal(toBytesPerSecond(bytesPerTick));
|
| 59 | }
|
| 60 |
|
| 61 | private static int getSampleHeightInternal(final double bytesPerSecond) {
|
| 62 | return (int)Math.round(Math.log(bytesPerSecond + 1.0) * 60.0 / Math.log(1048576.0));
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected int getSampleColor(final long bytesPerTick) {
|
| 67 | return this.getSampleColor(toBytesPerSecond(bytesPerTick), 0.0, -16711681, 8192.0, -6250241, 1.048576E7, -65536);
|
| 68 | }
|
| 69 |
|
| 70 | private static double toBytesPerSecond(final double bytesPerTick) {
|
| 71 | return bytesPerTick * 20.0;
|
| 72 | }
|
| 73 | }
|
| 74 |
|