| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import java.util.OptionalInt;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 7 | import net.minecraft.client.gui.Font;
|
| 8 | import net.minecraft.client.gui.TextAlignment;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.util.SingleKeyCache;
|
| 11 | import net.minecraft.util.Util;
|
| 12 |
|
| 13 | @Environment(EnvType.CLIENT)
|
| 14 | public class MultiLineTextWidget extends AbstractStringWidget {
|
| 15 | private OptionalInt maxWidth = OptionalInt.empty();
|
| 16 | private OptionalInt maxRows = OptionalInt.empty();
|
| 17 | private final SingleKeyCache<MultiLineTextWidget.CacheKey, MultiLineLabel> cache;
|
| 18 | private boolean centered = false;
|
| 19 |
|
| 20 | public MultiLineTextWidget(final Component message, final Font font) {
|
| 21 | this(0, 0, message, font);
|
| 22 | }
|
| 23 |
|
| 24 | public MultiLineTextWidget(final int x, final int y, final Component message, final Font font) {
|
| 25 | super(x, y, 0, 0, message, font);
|
| 26 | this.cache = Util.singleKeyCache(
|
| 27 | key -> key.maxRows.isPresent()
|
| 28 | ? MultiLineLabel.create(font, key.maxWidth, key.maxRows.getAsInt(), key.message)
|
| 29 | : MultiLineLabel.create(font, key.message, key.maxWidth)
|
| 30 | );
|
| 31 | this.active = false;
|
| 32 | }
|
| 33 |
|
| 34 | public MultiLineTextWidget setMaxWidth(final int maxWidth) {
|
| 35 | this.maxWidth = OptionalInt.of(maxWidth);
|
| 36 | return this;
|
| 37 | }
|
| 38 |
|
| 39 | public MultiLineTextWidget setMaxRows(final int maxRows) {
|
| 40 | this.maxRows = OptionalInt.of(maxRows);
|
| 41 | return this;
|
| 42 | }
|
| 43 |
|
| 44 | public MultiLineTextWidget setCentered(final boolean centered) {
|
| 45 | this.centered = centered;
|
| 46 | return this;
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public int getWidth() {
|
| 51 | return this.cache.getValue(this.getFreshCacheKey()).getWidth();
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public int getHeight() {
|
| 56 | return this.cache.getValue(this.getFreshCacheKey()).getLineCount() * 9;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public void visitLines(final ActiveTextCollector output) {
|
| 61 | MultiLineLabel multilineLabel = this.cache.getValue(this.getFreshCacheKey());
|
| 62 | int x = this.getTextX();
|
| 63 | int y = this.getTextY();
|
| 64 | int lineHeight = 9;
|
| 65 | if (this.centered) {
|
| 66 | int midX = this.getX() + this.getWidth() / 2;
|
| 67 | multilineLabel.visitLines(TextAlignment.CENTER, midX, y, lineHeight, output);
|
| 68 | } else {
|
| 69 | multilineLabel.visitLines(TextAlignment.LEFT, x, y, lineHeight, output);
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | protected int getTextX() {
|
| 74 | return this.getX();
|
| 75 | }
|
| 76 |
|
| 77 | protected int getTextY() {
|
| 78 | return this.getY();
|
| 79 | }
|
| 80 |
|
| 81 | private MultiLineTextWidget.CacheKey getFreshCacheKey() {
|
| 82 | return new MultiLineTextWidget.CacheKey(this.getMessage(), this.maxWidth.orElse(Integer.MAX_VALUE), this.maxRows);
|
| 83 | }
|
| 84 |
|
| 85 | @Environment(EnvType.CLIENT)
|
| 86 | private record CacheKey(Component message, int maxWidth, OptionalInt maxRows) {
|
| 87 | }
|
| 88 | }
|
| 89 |
|