| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 8 | import net.minecraft.client.gui.Font;
|
| 9 | import net.minecraft.client.gui.TextAlignment;
|
| 10 | import net.minecraft.locale.Language;
|
| 11 | import net.minecraft.network.chat.CommonComponents;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.network.chat.FormattedText;
|
| 14 | import net.minecraft.util.FormattedCharSequence;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public interface MultiLineLabel {
|
| 19 | MultiLineLabel EMPTY = new MultiLineLabel() {
|
| 20 | @Override
|
| 21 | public int visitLines(final TextAlignment align, final int anchorX, final int topY, final int lineHeight, final ActiveTextCollector output) {
|
| 22 | return topY;
|
| 23 | }
|
| 24 |
|
| 25 | @Override
|
| 26 | public int getLineCount() {
|
| 27 | return 0;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public int getWidth() {
|
| 32 | return 0;
|
| 33 | }
|
| 34 | };
|
| 35 |
|
| 36 | static MultiLineLabel create(final Font font, final Component... messages) {
|
| 37 | return create(font, Integer.MAX_VALUE, Integer.MAX_VALUE, messages);
|
| 38 | }
|
| 39 |
|
| 40 | static MultiLineLabel create(final Font font, final int maxWidth, final Component... messages) {
|
| 41 | return create(font, maxWidth, Integer.MAX_VALUE, messages);
|
| 42 | }
|
| 43 |
|
| 44 | static MultiLineLabel create(final Font font, final Component message, final int maxWidth) {
|
| 45 | return create(font, maxWidth, Integer.MAX_VALUE, message);
|
| 46 | }
|
| 47 |
|
| 48 | static MultiLineLabel create(final Font font, final int maxWidth, final int maxLines, final Component... messages) {
|
| 49 | return messages.length == 0 ? EMPTY : new MultiLineLabel() {
|
| 50 | private @Nullable List<MultiLineLabel.TextAndWidth> cachedTextAndWidth;
|
| 51 | private @Nullable Language splitWithLanguage;
|
| 52 |
|
| 53 | @Override
|
| 54 | public int visitLines(final TextAlignment align, final int anchorX, final int topY, final int lineHeight, final ActiveTextCollector output) {
|
| 55 | int y = topY;
|
| 56 |
|
| 57 | for (MultiLineLabel.TextAndWidth splitLine : this.getSplitMessage()) {
|
| 58 | int leftX = align.calculateLeft(anchorX, splitLine.width);
|
| 59 | output.accept(leftX, y, splitLine.text);
|
| 60 | y += lineHeight;
|
| 61 | }
|
| 62 |
|
| 63 | return y;
|
| 64 | }
|
| 65 |
|
| 66 | private List<MultiLineLabel.TextAndWidth> getSplitMessage() {
|
| 67 | Language currentLanguage = Language.getInstance();
|
| 68 | if (this.cachedTextAndWidth != null && currentLanguage == this.splitWithLanguage) {
|
| 69 | return this.cachedTextAndWidth;
|
| 70 | }
|
| 71 |
|
| 72 | this.splitWithLanguage = currentLanguage;
|
| 73 | List<FormattedText> splitMessage = new ArrayList<>();
|
| 74 |
|
| 75 | for (Component message : messages) {
|
| 76 | splitMessage.addAll(font.splitIgnoringLanguage(message, maxWidth));
|
| 77 | }
|
| 78 |
|
| 79 | this.cachedTextAndWidth = new ArrayList<>();
|
| 80 | int actualMaxLines = Math.min(splitMessage.size(), maxLines);
|
| 81 | List<FormattedText> linesToAdd = splitMessage.subList(0, actualMaxLines);
|
| 82 |
|
| 83 | for (int i = 0; i < linesToAdd.size(); i++) {
|
| 84 | FormattedText formattedText = linesToAdd.get(i);
|
| 85 | FormattedCharSequence formattedCharSequence = Language.getInstance().getVisualOrder(formattedText);
|
| 86 | if (i == linesToAdd.size() - 1 && actualMaxLines == maxLines && actualMaxLines != splitMessage.size()) {
|
| 87 | FormattedText clippedText = font.substrByWidth(formattedText, font.width(formattedText) - font.width(CommonComponents.ELLIPSIS));
|
| 88 | FormattedText withEllipsis = FormattedText.composite(clippedText, CommonComponents.ELLIPSIS.copy().withStyle(messages[messages.length - 1].getStyle()));
|
| 89 | this.cachedTextAndWidth.add(new MultiLineLabel.TextAndWidth(Language.getInstance().getVisualOrder(withEllipsis), font.width(withEllipsis)));
|
| 90 | } else {
|
| 91 | this.cachedTextAndWidth.add(new MultiLineLabel.TextAndWidth(formattedCharSequence, font.width(formattedCharSequence)));
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | return this.cachedTextAndWidth;
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public int getLineCount() {
|
| 100 | return this.getSplitMessage().size();
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public int getWidth() {
|
| 105 | return Math.min(maxWidth, this.getSplitMessage().stream().mapToInt(MultiLineLabel.TextAndWidth::width).max().orElse(0));
|
| 106 | }
|
| 107 | };
|
| 108 | }
|
| 109 |
|
| 110 | int visitLines(TextAlignment align, int anchorX, int topY, int lineHeight, ActiveTextCollector output);
|
| 111 |
|
| 112 | int getLineCount();
|
| 113 |
|
| 114 | int getWidth();
|
| 115 |
|
| 116 | @Environment(EnvType.CLIENT)
|
| 117 | record TextAndWidth(FormattedCharSequence text, int width) {
|
| 118 | }
|
| 119 | }
|
| 120 |
|