| 1 | package net.minecraft.client.gui.components;
|
| 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.ChatFormatting;
|
| 9 | import net.minecraft.client.ComponentCollector;
|
| 10 | import net.minecraft.client.Minecraft;
|
| 11 | import net.minecraft.client.gui.Font;
|
| 12 | import net.minecraft.locale.Language;
|
| 13 | import net.minecraft.network.chat.CommonComponents;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.network.chat.FormattedText;
|
| 16 | import net.minecraft.network.chat.Style;
|
| 17 | import net.minecraft.util.FormattedCharSequence;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public class ComponentRenderUtils {
|
| 21 | private static final FormattedCharSequence INDENT = FormattedCharSequence.codepoint(32, Style.EMPTY);
|
| 22 |
|
| 23 | private static String stripColor(final String input) {
|
| 24 | return Minecraft.getInstance().options.chatColors().get() ? input : ChatFormatting.stripFormatting(input);
|
| 25 | }
|
| 26 |
|
| 27 | public static List<FormattedCharSequence> wrapComponents(final FormattedText message, final int maxWidth, final Font font) {
|
| 28 | ComponentCollector collector = new ComponentCollector();
|
| 29 | message.visit((style, contents) -> {
|
| 30 | collector.append(FormattedText.of(stripColor(contents), style));
|
| 31 | return Optional.empty();
|
| 32 | }, Style.EMPTY);
|
| 33 | List<FormattedCharSequence> result = Lists.newArrayList();
|
| 34 | font.getSplitter().splitLines(collector.getResultOrEmpty(), maxWidth, Style.EMPTY, (text, wrapped) -> {
|
| 35 | FormattedCharSequence reorderedText = Language.getInstance().getVisualOrder(text);
|
| 36 | result.add(wrapped ? FormattedCharSequence.composite(INDENT, reorderedText) : reorderedText);
|
| 37 | });
|
| 38 | return result.isEmpty() ? Lists.newArrayList(FormattedCharSequence.EMPTY) : result;
|
| 39 | }
|
| 40 |
|
| 41 | public static FormattedCharSequence clipText(final Component text, final Font font, final int width) {
|
| 42 | FormattedText clippedText = font.substrByWidth(text, width - font.width(CommonComponents.ELLIPSIS));
|
| 43 | return Language.getInstance().getVisualOrder(FormattedText.composite(clippedText, CommonComponents.ELLIPSIS));
|
| 44 | }
|
| 45 | }
|
| 46 |
|