| 1 | package net.minecraft.client.gui.screens.advancements;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Optional;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.advancements.AdvancementHolder;
|
| 9 | import net.minecraft.advancements.AdvancementNode;
|
| 10 | import net.minecraft.advancements.AdvancementProgress;
|
| 11 | import net.minecraft.advancements.DisplayInfo;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.StringSplitter;
|
| 14 | import net.minecraft.client.gui.Font;
|
| 15 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 16 | import net.minecraft.client.renderer.RenderPipelines;
|
| 17 | import net.minecraft.locale.Language;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.network.chat.ComponentUtils;
|
| 20 | import net.minecraft.network.chat.FormattedText;
|
| 21 | import net.minecraft.network.chat.Style;
|
| 22 | import net.minecraft.resources.Identifier;
|
| 23 | import net.minecraft.util.FormattedCharSequence;
|
| 24 | import net.minecraft.util.Mth;
|
| 25 | import net.minecraft.world.item.ItemStack;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public class AdvancementWidget {
|
| 30 | private static final Identifier TITLE_BOX_SPRITE = Identifier.withDefaultNamespace("advancements/title_box");
|
| 31 | private static final int HEIGHT = 26;
|
| 32 | private static final int BOX_X = 0;
|
| 33 | private static final int BOX_WIDTH = 200;
|
| 34 | private static final int FRAME_WIDTH = 26;
|
| 35 | private static final int ICON_X = 8;
|
| 36 | private static final int ICON_Y = 5;
|
| 37 | private static final int ICON_WIDTH = 26;
|
| 38 | private static final int TITLE_PADDING_LEFT = 3;
|
| 39 | private static final int TITLE_PADDING_RIGHT = 5;
|
| 40 | private static final int TITLE_X = 32;
|
| 41 | private static final int TITLE_PADDING_TOP = 9;
|
| 42 | private static final int TITLE_PADDING_BOTTOM = 8;
|
| 43 | private static final int TITLE_MAX_WIDTH = 163;
|
| 44 | private static final int TITLE_MIN_WIDTH = 80;
|
| 45 | private static final int[] TEST_SPLIT_OFFSETS = new int[]{0, 10, -10, 25, -25};
|
| 46 | private final AdvancementNode advancementNode;
|
| 47 | private final DisplayInfo display;
|
| 48 | private final ItemStack icon;
|
| 49 | private final List<FormattedCharSequence> titleLines;
|
| 50 | private final int width;
|
| 51 | private final List<FormattedCharSequence> description;
|
| 52 | private final Minecraft minecraft;
|
| 53 | private @Nullable AdvancementWidget parent;
|
| 54 | private final List<AdvancementWidget> children = Lists.newArrayList();
|
| 55 | private @Nullable AdvancementProgress progress;
|
| 56 | private final int x;
|
| 57 | private final int y;
|
| 58 |
|
| 59 | private AdvancementWidget(final Minecraft minecraft, final AdvancementNode advancementNode, final DisplayInfo display) {
|
| 60 | this.advancementNode = advancementNode;
|
| 61 | this.display = display;
|
| 62 | this.minecraft = minecraft;
|
| 63 | this.titleLines = minecraft.font.split(display.title(), 163);
|
| 64 | this.x = Mth.floor(advancementNode.x() * 28.0F);
|
| 65 | this.y = Mth.floor(advancementNode.y() * 27.0F);
|
| 66 | int titleWidth = Math.max(this.titleLines.stream().mapToInt(minecraft.font::width).max().orElse(0), 80);
|
| 67 | int maxProgressWidth = this.getMaxProgressWidth();
|
| 68 | int longestDescLine = 29 + titleWidth + maxProgressWidth;
|
| 69 | this.description = Language.getInstance()
|
| 70 | .getVisualOrder(
|
| 71 | this.findOptimalLines(ComponentUtils.mergeStyles(display.description(), Style.EMPTY.withColor(display.type().getChatColor())), longestDescLine)
|
| 72 | );
|
| 73 |
|
| 74 | for (FormattedCharSequence line : this.description) {
|
| 75 | longestDescLine = Math.max(longestDescLine, minecraft.font.width(line));
|
| 76 | }
|
| 77 |
|
| 78 | this.width = longestDescLine + 3 + 5;
|
| 79 | this.icon = display.icon().create();
|
| 80 | }
|
| 81 |
|
| 82 | public static @Nullable AdvancementWidget createWidget(final Minecraft minecraft, final AdvancementNode node) {
|
| 83 | Optional<DisplayInfo> display = node.advancement().display();
|
| 84 | return display.isEmpty() ? null : new AdvancementWidget(minecraft, node, display.get());
|
| 85 | }
|
| 86 |
|
| 87 | private int getMaxProgressWidth() {
|
| 88 | int maxCriteraRequired = this.advancementNode.advancement().requirements().size();
|
| 89 | if (maxCriteraRequired <= 1) {
|
| 90 | return 0;
|
| 91 | }
|
| 92 |
|
| 93 | int spacing = 8;
|
| 94 | Component fakeMaxProgress = Component.translatable("advancements.progress", maxCriteraRequired, maxCriteraRequired);
|
| 95 | return this.minecraft.font.width(fakeMaxProgress) + 8;
|
| 96 | }
|
| 97 |
|
| 98 | private static float getMaxWidth(final StringSplitter splitter, final List<FormattedText> input) {
|
| 99 | return (float)input.stream().mapToDouble(splitter::stringWidth).max().orElse(0.0);
|
| 100 | }
|
| 101 |
|
| 102 | private List<FormattedText> findOptimalLines(final Component input, final int preferredWidth) {
|
| 103 | StringSplitter splitter = this.minecraft.font.getSplitter();
|
| 104 | List<FormattedText> bestSplit = null;
|
| 105 | float bestDistance = Float.MAX_VALUE;
|
| 106 |
|
| 107 | for (int testMargin : TEST_SPLIT_OFFSETS) {
|
| 108 | List<FormattedText> testSplit = splitter.splitLines(input, preferredWidth - testMargin, Style.EMPTY);
|
| 109 | float distance = Math.abs(getMaxWidth(splitter, testSplit) - preferredWidth);
|
| 110 | if (distance <= 10.0F) {
|
| 111 | return testSplit;
|
| 112 | }
|
| 113 |
|
| 114 | if (distance < bestDistance) {
|
| 115 | bestDistance = distance;
|
| 116 | bestSplit = testSplit;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | return bestSplit;
|
| 121 | }
|
| 122 |
|
| 123 | private static @Nullable AdvancementHolder findFirstVisibleParent(AdvancementNode node) {
|
| 124 | do {
|
| 125 | node = node.parent();
|
| 126 | } while (node != null && node.advancement().display().isEmpty());
|
| 127 |
|
| 128 | return node != null && !node.advancement().display().isEmpty() ? node.holder() : null;
|
| 129 | }
|
| 130 |
|
| 131 | public void extractConnectivity(final GuiGraphicsExtractor graphics, final int xo, final int yo, final boolean background) {
|
| 132 | if (this.parent != null) {
|
| 133 | int depX = xo + this.parent.x + 13;
|
| 134 | int splitX = xo + this.parent.x + 26 + 4;
|
| 135 | int depY = yo + this.parent.y + 13;
|
| 136 | int myX = xo + this.x + 13;
|
| 137 | int myY = yo + this.y + 13;
|
| 138 | int col = background ? -16777216 : -1;
|
| 139 | if (background) {
|
| 140 | graphics.horizontalLine(splitX, depX, depY - 1, col);
|
| 141 | graphics.horizontalLine(splitX + 1, depX, depY, col);
|
| 142 | graphics.horizontalLine(splitX, depX, depY + 1, col);
|
| 143 | graphics.horizontalLine(myX, splitX - 1, myY - 1, col);
|
| 144 | graphics.horizontalLine(myX, splitX - 1, myY, col);
|
| 145 | graphics.horizontalLine(myX, splitX - 1, myY + 1, col);
|
| 146 | graphics.verticalLine(splitX - 1, myY, depY, col);
|
| 147 | graphics.verticalLine(splitX + 1, myY, depY, col);
|
| 148 | } else {
|
| 149 | graphics.horizontalLine(splitX, depX, depY, col);
|
| 150 | graphics.horizontalLine(myX, splitX, myY, col);
|
| 151 | graphics.verticalLine(splitX, myY, depY, col);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | for (AdvancementWidget child : this.children) {
|
| 156 | child.extractConnectivity(graphics, xo, yo, background);
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int xo, final int yo) {
|
| 161 | if (!this.display.hidden() || this.progress != null && this.progress.isDone()) {
|
| 162 | float amount = this.progress == null ? 0.0F : this.progress.getPercent();
|
| 163 | AdvancementWidgetType iconFrame;
|
| 164 | if (amount >= 1.0F) {
|
| 165 | iconFrame = AdvancementWidgetType.OBTAINED;
|
| 166 | } else {
|
| 167 | iconFrame = AdvancementWidgetType.UNOBTAINED;
|
| 168 | }
|
| 169 |
|
| 170 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, iconFrame.frameSprite(this.display.type()), xo + this.x + 3, yo + this.y, 26, 26);
|
| 171 | graphics.fakeItem(this.icon, xo + this.x + 8, yo + this.y + 5);
|
| 172 | }
|
| 173 |
|
| 174 | for (AdvancementWidget child : this.children) {
|
| 175 | child.extractRenderState(graphics, xo, yo);
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | public int getWidth() {
|
| 180 | return this.width;
|
| 181 | }
|
| 182 |
|
| 183 | public void setProgress(final AdvancementProgress progress) {
|
| 184 | this.progress = progress;
|
| 185 | }
|
| 186 |
|
| 187 | public void addChild(final AdvancementWidget widget) {
|
| 188 | this.children.add(widget);
|
| 189 | }
|
| 190 |
|
| 191 | public void extractHover(
|
| 192 | final GuiGraphicsExtractor graphics, final int xo, final int yo, final float fade, final int screenxo, final int screenyo, final int screenWidth
|
| 193 | ) {
|
| 194 | Font font = this.minecraft.font;
|
| 195 | int titleBarHeight = 9 * this.titleLines.size() + 9 + 8;
|
| 196 | int titleTop = yo + this.y + (26 - titleBarHeight) / 2;
|
| 197 | int titleBarBottom = titleTop + titleBarHeight;
|
| 198 | int descriptionTextHeight = this.description.size() * 9;
|
| 199 | int descriptionHeight = 6 + descriptionTextHeight;
|
| 200 | boolean leftSide = screenxo + xo + this.x + this.width + 26 >= screenWidth;
|
| 201 | Component progressText = this.progress == null ? null : this.progress.getProgressText();
|
| 202 | int progressWidth = progressText == null ? 0 : font.width(progressText);
|
| 203 | boolean topSide = titleBarBottom + descriptionHeight >= 113;
|
| 204 | float amount = this.progress == null ? 0.0F : this.progress.getPercent();
|
| 205 | int firstHalfWidth = Mth.floor(amount * this.width);
|
| 206 | AdvancementWidgetType firstHalf;
|
| 207 | AdvancementWidgetType secondHalf;
|
| 208 | AdvancementWidgetType iconFrame;
|
| 209 | if (amount >= 1.0F) {
|
| 210 | firstHalfWidth = this.width / 2;
|
| 211 | firstHalf = AdvancementWidgetType.OBTAINED;
|
| 212 | secondHalf = AdvancementWidgetType.OBTAINED;
|
| 213 | iconFrame = AdvancementWidgetType.OBTAINED;
|
| 214 | } else if (firstHalfWidth < 2) {
|
| 215 | firstHalfWidth = this.width / 2;
|
| 216 | firstHalf = AdvancementWidgetType.UNOBTAINED;
|
| 217 | secondHalf = AdvancementWidgetType.UNOBTAINED;
|
| 218 | iconFrame = AdvancementWidgetType.UNOBTAINED;
|
| 219 | } else if (firstHalfWidth > this.width - 2) {
|
| 220 | firstHalfWidth = this.width / 2;
|
| 221 | firstHalf = AdvancementWidgetType.OBTAINED;
|
| 222 | secondHalf = AdvancementWidgetType.OBTAINED;
|
| 223 | iconFrame = AdvancementWidgetType.UNOBTAINED;
|
| 224 | } else {
|
| 225 | firstHalf = AdvancementWidgetType.OBTAINED;
|
| 226 | secondHalf = AdvancementWidgetType.UNOBTAINED;
|
| 227 | iconFrame = AdvancementWidgetType.UNOBTAINED;
|
| 228 | }
|
| 229 |
|
| 230 | int secondBarWidth = this.width - firstHalfWidth;
|
| 231 | int titleLeft;
|
| 232 | if (leftSide) {
|
| 233 | titleLeft = xo + this.x - this.width + 26 + 6;
|
| 234 | } else {
|
| 235 | titleLeft = xo + this.x;
|
| 236 | }
|
| 237 |
|
| 238 | int backgroundHeight = titleBarHeight + descriptionHeight;
|
| 239 | if (!this.description.isEmpty()) {
|
| 240 | if (topSide) {
|
| 241 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TITLE_BOX_SPRITE, titleLeft, titleBarBottom - backgroundHeight, this.width, backgroundHeight);
|
| 242 | } else {
|
| 243 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, TITLE_BOX_SPRITE, titleLeft, titleTop, this.width, backgroundHeight);
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | if (firstHalf != secondHalf) {
|
| 248 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, firstHalf.boxSprite(), 200, titleBarHeight, 0, 0, titleLeft, titleTop, firstHalfWidth, titleBarHeight);
|
| 249 | graphics.blitSprite(
|
| 250 | RenderPipelines.GUI_TEXTURED,
|
| 251 | secondHalf.boxSprite(),
|
| 252 | 200,
|
| 253 | titleBarHeight,
|
| 254 | 200 - secondBarWidth,
|
| 255 | 0,
|
| 256 | titleLeft + firstHalfWidth,
|
| 257 | titleTop,
|
| 258 | secondBarWidth,
|
| 259 | titleBarHeight
|
| 260 | );
|
| 261 | } else {
|
| 262 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, firstHalf.boxSprite(), titleLeft, titleTop, this.width, titleBarHeight);
|
| 263 | }
|
| 264 |
|
| 265 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, iconFrame.frameSprite(this.display.type()), xo + this.x + 3, yo + this.y, 26, 26);
|
| 266 | int descriptionLeft = titleLeft + 5;
|
| 267 | if (leftSide) {
|
| 268 | this.extractMultilineText(graphics, this.titleLines, descriptionLeft, titleTop + 9, -1);
|
| 269 | if (progressText != null) {
|
| 270 | graphics.text(font, progressText, xo + this.x - progressWidth, titleTop + 9, -1);
|
| 271 | }
|
| 272 | } else {
|
| 273 | this.extractMultilineText(graphics, this.titleLines, xo + this.x + 32, titleTop + 9, -1);
|
| 274 | if (progressText != null) {
|
| 275 | graphics.text(font, progressText, xo + this.x + this.width - progressWidth - 5, titleTop + 9, -1);
|
| 276 | }
|
| 277 | }
|
| 278 |
|
| 279 | if (topSide) {
|
| 280 | this.extractMultilineText(graphics, this.description, descriptionLeft, titleTop - descriptionTextHeight + 1, -16711936);
|
| 281 | } else {
|
| 282 | this.extractMultilineText(graphics, this.description, descriptionLeft, titleBarBottom, -16711936);
|
| 283 | }
|
| 284 |
|
| 285 | graphics.fakeItem(this.icon, xo + this.x + 8, yo + this.y + 5);
|
| 286 | }
|
| 287 |
|
| 288 | private void extractMultilineText(final GuiGraphicsExtractor graphics, final List<FormattedCharSequence> lines, final int x, final int y, final int color) {
|
| 289 | Font font = this.minecraft.font;
|
| 290 |
|
| 291 | for (int i = 0; i < lines.size(); i++) {
|
| 292 | graphics.text(font, lines.get(i), x, y + i * 9, color);
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | public boolean isMouseOver(final int xo, final int yo, final int mouseX, final int mouseY) {
|
| 297 | if (!this.display.hidden() || this.progress != null && this.progress.isDone()) {
|
| 298 | int x0 = xo + this.x;
|
| 299 | int x1 = x0 + 26;
|
| 300 | int y0 = yo + this.y;
|
| 301 | int y1 = y0 + 26;
|
| 302 | return mouseX >= x0 && mouseX <= x1 && mouseY >= y0 && mouseY <= y1;
|
| 303 | } else {
|
| 304 | return false;
|
| 305 | }
|
| 306 | }
|
| 307 |
|
| 308 | public void attachToParent(final AdvancementTab tab) {
|
| 309 | if (this.parent == null) {
|
| 310 | AdvancementHolder result = findFirstVisibleParent(this.advancementNode);
|
| 311 | if (result != null) {
|
| 312 | this.parent = tab.getWidget(result);
|
| 313 | if (this.parent != null) {
|
| 314 | this.parent.addChild(this);
|
| 315 | }
|
| 316 | }
|
| 317 | }
|
| 318 | }
|
| 319 |
|
| 320 | public int getY() {
|
| 321 | return this.y;
|
| 322 | }
|
| 323 |
|
| 324 | public int getX() {
|
| 325 | return this.x;
|
| 326 | }
|
| 327 |
|
| 328 | public AdvancementHolder getAdvancement() {
|
| 329 | return this.advancementNode.holder();
|
| 330 | }
|
| 331 |
|
| 332 | public DisplayInfo getDisplay() {
|
| 333 | return this.display;
|
| 334 | }
|
| 335 | }
|
| 336 |
|