| 1 | package net.minecraft.client.gui.components.debugchart;
|
| 2 |
|
| 3 | import java.text.DecimalFormat;
|
| 4 | import java.text.DecimalFormatSymbols;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Locale;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.gui.Font;
|
| 11 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 12 | import net.minecraft.util.profiling.ProfileResults;
|
| 13 | import net.minecraft.util.profiling.ResultField;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class ProfilerPieChart {
|
| 18 | public static final int RADIUS = 105;
|
| 19 | public static final int PIE_CHART_THICKNESS = 10;
|
| 20 | private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("##0.00", DecimalFormatSymbols.getInstance(Locale.ROOT));
|
| 21 | private static final int MARGIN = 5;
|
| 22 | private static final int WIDTH = 260;
|
| 23 | private static final int SUBSEQUENT_LINES_INDENT = 10;
|
| 24 | private final Font font;
|
| 25 | private @Nullable ProfileResults profilerPieChartResults;
|
| 26 | private String profilerTreePath = "root";
|
| 27 | private int bottomOffset = 0;
|
| 28 |
|
| 29 | public ProfilerPieChart(final Font font) {
|
| 30 | this.font = font;
|
| 31 | }
|
| 32 |
|
| 33 | public void setPieChartResults(final @Nullable ProfileResults results) {
|
| 34 | this.profilerPieChartResults = results;
|
| 35 | }
|
| 36 |
|
| 37 | public void setBottomOffset(final int bottomOffset) {
|
| 38 | this.bottomOffset = bottomOffset;
|
| 39 | }
|
| 40 |
|
| 41 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int scaledScreenWidth, final int scaledScreenHeight) {
|
| 42 | if (this.profilerPieChartResults != null) {
|
| 43 | List<ResultField> list = this.profilerPieChartResults.getTimes(this.profilerTreePath);
|
| 44 | ResultField currentNode = list.removeFirst();
|
| 45 | int chartCenterX = scaledScreenWidth - 130 - 10;
|
| 46 | int left = chartCenterX - 130;
|
| 47 | int right = chartCenterX + 130;
|
| 48 | int textUnderChartHeight = list.size() * 9;
|
| 49 | int bottom = scaledScreenHeight - this.bottomOffset - 5;
|
| 50 | int textStartY = bottom - textUnderChartHeight;
|
| 51 | int chartHalfSizeY = 62;
|
| 52 | int chartCenterY = textStartY - 62 - 5;
|
| 53 | String globalPercentage = PERCENTAGE_FORMAT.format(currentNode.globalPercentage) + "%";
|
| 54 | int globalPercentageWidth = this.font.width(globalPercentage);
|
| 55 | int zeroPrefixWidth = this.font.width("[0] ");
|
| 56 | int topTextMaxWidth = right - globalPercentageWidth - 5 - left - zeroPrefixWidth;
|
| 57 | String currentNodeName = ProfileResults.demanglePath(currentNode.name);
|
| 58 | List<String> currentNodeNameLines = this.splitNodeName(currentNodeName, topTextMaxWidth, topTextMaxWidth - 10);
|
| 59 | int currentNodeNameTop = chartCenterY - 62 - (currentNodeNameLines.size() - 1) * 9;
|
| 60 | graphics.fill(left - 5, currentNodeNameTop - 5, right + 5, bottom + 5, -1873784752);
|
| 61 | graphics.profilerChart(list, left, chartCenterY - 62 + 10, right, chartCenterY + 62);
|
| 62 | String firstLineText = "";
|
| 63 | if (!"unspecified".equals(currentNodeName) && !"root".equals(currentNodeName)) {
|
| 64 | firstLineText = firstLineText + "[0] ";
|
| 65 | }
|
| 66 |
|
| 67 | firstLineText = firstLineText + currentNodeNameLines.getFirst();
|
| 68 | int col = -1;
|
| 69 | graphics.text(this.font, firstLineText, left, currentNodeNameTop, -1);
|
| 70 |
|
| 71 | for (int i = 1; i < currentNodeNameLines.size(); i++) {
|
| 72 | graphics.text(this.font, currentNodeNameLines.get(i), left + 10 + zeroPrefixWidth, currentNodeNameTop + i * 9, -1);
|
| 73 | }
|
| 74 |
|
| 75 | graphics.text(this.font, globalPercentage, right - globalPercentageWidth, currentNodeNameTop, -1);
|
| 76 |
|
| 77 | for (int i = 0; i < list.size(); i++) {
|
| 78 | ResultField result = list.get(i);
|
| 79 | StringBuilder string = new StringBuilder();
|
| 80 | if ("unspecified".equals(result.name)) {
|
| 81 | string.append("[?] ");
|
| 82 | } else {
|
| 83 | string.append("[").append(i + 1).append("] ");
|
| 84 | }
|
| 85 |
|
| 86 | String msg = string.append(result.name).toString();
|
| 87 | int textY = textStartY + i * 9;
|
| 88 | graphics.text(this.font, msg, left, textY, result.getColor());
|
| 89 | msg = PERCENTAGE_FORMAT.format(result.percentage) + "%";
|
| 90 | graphics.text(this.font, msg, right - 50 - this.font.width(msg), textY, result.getColor());
|
| 91 | msg = PERCENTAGE_FORMAT.format(result.globalPercentage) + "%";
|
| 92 | graphics.text(this.font, msg, right - this.font.width(msg), textY, result.getColor());
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | private List<String> splitNodeName(final String nodeName, final int firstLineMaxWidth, final int maxWidth) {
|
| 98 | String[] nodeNameSplit = nodeName.split("\\.");
|
| 99 | List<String> lines = new ArrayList<>();
|
| 100 | String currentLine = "";
|
| 101 | int nameIndex = 0;
|
| 102 |
|
| 103 | while (nameIndex < nodeNameSplit.length) {
|
| 104 | String currentName = nodeNameSplit[nameIndex];
|
| 105 | String currentNameWithPeriod = (nameIndex != 0 ? "." : "") + currentName;
|
| 106 | String newLine = currentLine + currentNameWithPeriod;
|
| 107 | int newWidth = this.font.width(newLine);
|
| 108 | if (newWidth > (!lines.isEmpty() ? maxWidth : firstLineMaxWidth)) {
|
| 109 | if (currentLine.isEmpty()) {
|
| 110 | lines.add(currentNameWithPeriod);
|
| 111 | nameIndex++;
|
| 112 | } else {
|
| 113 | lines.add(currentLine);
|
| 114 | currentLine = "";
|
| 115 | }
|
| 116 | } else {
|
| 117 | currentLine = newLine;
|
| 118 | nameIndex++;
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | if (!currentLine.isEmpty()) {
|
| 123 | lines.add(currentLine);
|
| 124 | }
|
| 125 |
|
| 126 | return lines;
|
| 127 | }
|
| 128 |
|
| 129 | public void profilerPieChartKeyPress(int key) {
|
| 130 | if (this.profilerPieChartResults != null) {
|
| 131 | List<ResultField> list = this.profilerPieChartResults.getTimes(this.profilerTreePath);
|
| 132 | if (!list.isEmpty()) {
|
| 133 | ResultField node = list.remove(0);
|
| 134 | if (key == 0) {
|
| 135 | if (!node.name.isEmpty()) {
|
| 136 | int pos = this.profilerTreePath.lastIndexOf(30);
|
| 137 | if (pos >= 0) {
|
| 138 | this.profilerTreePath = this.profilerTreePath.substring(0, pos);
|
| 139 | }
|
| 140 | }
|
| 141 | } else {
|
| 142 | key--;
|
| 143 | if (key < list.size() && !"unspecified".equals(list.get(key).name)) {
|
| 144 | if (!this.profilerTreePath.isEmpty()) {
|
| 145 | this.profilerTreePath = this.profilerTreePath + "\u001e";
|
| 146 | }
|
| 147 |
|
| 148 | this.profilerTreePath = this.profilerTreePath + list.get(key).name;
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 | }
|
| 154 | }
|
| 155 |
|