| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 6 | import net.minecraft.client.gui.Font;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 |
|
| 9 | @Environment(EnvType.CLIENT)
|
| 10 | public class StringWidget extends AbstractStringWidget {
|
| 11 | private static final int TEXT_MARGIN = 2;
|
| 12 | private int maxWidth = 0;
|
| 13 | private int cachedWidth = 0;
|
| 14 | private boolean cachedWidthDirty = true;
|
| 15 | private StringWidget.TextOverflow textOverflow = StringWidget.TextOverflow.CLAMPED;
|
| 16 |
|
| 17 | public StringWidget(final Component message, final Font font) {
|
| 18 | this(0, 0, font.width(message.getVisualOrderText()), 9, message, font);
|
| 19 | }
|
| 20 |
|
| 21 | public StringWidget(final int width, final int height, final Component message, final Font font) {
|
| 22 | this(0, 0, width, height, message, font);
|
| 23 | }
|
| 24 |
|
| 25 | public StringWidget(final int x, final int y, final int width, final int height, final Component message, final Font font) {
|
| 26 | super(x, y, width, height, message, font);
|
| 27 | this.active = false;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public void setMessage(final Component message) {
|
| 32 | super.setMessage(message);
|
| 33 | this.cachedWidthDirty = true;
|
| 34 | }
|
| 35 |
|
| 36 | public StringWidget setMaxWidth(final int maxWidth) {
|
| 37 | return this.setMaxWidth(maxWidth, StringWidget.TextOverflow.CLAMPED);
|
| 38 | }
|
| 39 |
|
| 40 | public StringWidget setMaxWidth(final int maxWidth, final StringWidget.TextOverflow textOverflow) {
|
| 41 | this.maxWidth = maxWidth;
|
| 42 | this.textOverflow = textOverflow;
|
| 43 | return this;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public int getWidth() {
|
| 48 | if (this.maxWidth > 0) {
|
| 49 | if (this.cachedWidthDirty) {
|
| 50 | this.cachedWidth = Math.min(this.maxWidth, this.getFont().width(this.getMessage().getVisualOrderText()));
|
| 51 | this.cachedWidthDirty = false;
|
| 52 | }
|
| 53 |
|
| 54 | return this.cachedWidth;
|
| 55 | } else {
|
| 56 | return super.getWidth();
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public void visitLines(final ActiveTextCollector output) {
|
| 62 | Component message = this.getMessage();
|
| 63 | Font font = this.getFont();
|
| 64 | int maxWidth = this.maxWidth > 0 ? this.maxWidth : this.getWidth();
|
| 65 | int textWidth = font.width(message);
|
| 66 | int x = this.getX();
|
| 67 | int y = this.getY() + (this.getHeight() - 9) / 2;
|
| 68 | boolean textOverflow = textWidth > maxWidth;
|
| 69 | if (textOverflow) {
|
| 70 | switch (this.textOverflow) {
|
| 71 | case CLAMPED:
|
| 72 | output.accept(x, y, ComponentRenderUtils.clipText(message, font, maxWidth));
|
| 73 | break;
|
| 74 | case SCROLLING:
|
| 75 | this.extractScrollingStringOverContents(output, message, 2);
|
| 76 | }
|
| 77 | } else {
|
| 78 | output.accept(x, y, message.getVisualOrderText());
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | @Environment(EnvType.CLIENT)
|
| 83 | public enum TextOverflow {
|
| 84 | CLAMPED,
|
| 85 | SCROLLING;
|
| 86 | }
|
| 87 | }
|
| 88 |
|