| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.Font;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 9 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 10 | import net.minecraft.client.input.KeyEvent;
|
| 11 | import net.minecraft.client.sounds.SoundManager;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import net.minecraft.network.chat.Style;
|
| 14 | import net.minecraft.util.ARGB;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class FocusableTextWidget extends MultiLineTextWidget {
|
| 19 | public static final int DEFAULT_PADDING = 4;
|
| 20 | private final int padding;
|
| 21 | private final int maxWidth;
|
| 22 | private final boolean alwaysShowBorder;
|
| 23 | private final FocusableTextWidget.BackgroundFill backgroundFill;
|
| 24 | private boolean narrateMessage = true;
|
| 25 | private @Nullable Component focusedUsageNarration;
|
| 26 | private @Nullable Component hoveredUsageNarration;
|
| 27 |
|
| 28 | private FocusableTextWidget(
|
| 29 | final Component message,
|
| 30 | final Font font,
|
| 31 | final int padding,
|
| 32 | final int maxWidth,
|
| 33 | final FocusableTextWidget.BackgroundFill backgroundFill,
|
| 34 | final boolean alwaysShowBorder
|
| 35 | ) {
|
| 36 | super(message, font);
|
| 37 | this.active = true;
|
| 38 | this.padding = padding;
|
| 39 | this.maxWidth = maxWidth;
|
| 40 | this.alwaysShowBorder = alwaysShowBorder;
|
| 41 | this.backgroundFill = backgroundFill;
|
| 42 | this.updateWidth();
|
| 43 | this.updateHeight();
|
| 44 | this.setCentered(true);
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected void updateWidgetNarration(final NarrationElementOutput output) {
|
| 49 | if (this.narrateMessage) {
|
| 50 | output.add(NarratedElementType.TITLE, this.getMessage());
|
| 51 | }
|
| 52 |
|
| 53 | if (this.active) {
|
| 54 | Component usage = this.isFocused() ? this.focusedUsageNarration : this.hoveredUsageNarration;
|
| 55 | if (usage != null) {
|
| 56 | output.add(NarratedElementType.USAGE, usage);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | public void setNarrateMessage(final boolean narrateMessage) {
|
| 62 | this.narrateMessage = narrateMessage;
|
| 63 | }
|
| 64 |
|
| 65 | public void setUsageNarration(final @Nullable Component focused, final @Nullable Component hovered) {
|
| 66 | this.focusedUsageNarration = focused;
|
| 67 | this.hoveredUsageNarration = hovered;
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 72 | int borderColor = this.alwaysShowBorder && !this.isFocused() ? ARGB.color(this.alpha, -6250336) : ARGB.white(this.alpha);
|
| 73 | switch (this.backgroundFill) {
|
| 74 | case ALWAYS:
|
| 75 | graphics.fill(this.getX() + 1, this.getY(), this.getRight(), this.getBottom(), ARGB.black(this.alpha));
|
| 76 | break;
|
| 77 | case ON_FOCUS:
|
| 78 | if (this.isFocused()) {
|
| 79 | graphics.fill(this.getX() + 1, this.getY(), this.getRight(), this.getBottom(), ARGB.black(this.alpha));
|
| 80 | }
|
| 81 | case NEVER:
|
| 82 | }
|
| 83 |
|
| 84 | if (this.isFocused() || this.alwaysShowBorder) {
|
| 85 | graphics.outline(this.getX(), this.getY(), this.getWidth(), this.getHeight(), borderColor);
|
| 86 | }
|
| 87 |
|
| 88 | super.extractWidgetRenderState(graphics, mouseX, mouseY, a);
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected int getTextX() {
|
| 93 | return this.getX() + this.padding;
|
| 94 | }
|
| 95 |
|
| 96 | @Override
|
| 97 | protected int getTextY() {
|
| 98 | return super.getTextY() + this.padding;
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public MultiLineTextWidget setMaxWidth(final int maxWidth) {
|
| 103 | return super.setMaxWidth(maxWidth - this.padding * 2);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public int getWidth() {
|
| 108 | return this.width;
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public int getHeight() {
|
| 113 | return this.height;
|
| 114 | }
|
| 115 |
|
| 116 | public int getPadding() {
|
| 117 | return this.padding;
|
| 118 | }
|
| 119 |
|
| 120 | public void updateWidth() {
|
| 121 | if (this.maxWidth != -1) {
|
| 122 | this.setWidth(this.maxWidth);
|
| 123 | this.setMaxWidth(this.maxWidth);
|
| 124 | } else {
|
| 125 | this.setWidth(this.getFont().width(this.getMessage()) + this.padding * 2);
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | public void updateHeight() {
|
| 130 | int textHeight = 9 * this.getFont().split(this.getMessage(), super.getWidth()).size();
|
| 131 | this.setHeight(textHeight + this.padding * 2);
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public void setMessage(final Component message) {
|
| 136 | this.message = message;
|
| 137 | int width;
|
| 138 | if (this.maxWidth != -1) {
|
| 139 | width = this.maxWidth;
|
| 140 | } else {
|
| 141 | width = this.getFont().width(message) + this.padding * 2;
|
| 142 | }
|
| 143 |
|
| 144 | this.setWidth(width);
|
| 145 | this.updateHeight();
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public void playDownSound(final SoundManager soundManager) {
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public boolean keyPressed(final KeyEvent event) {
|
| 154 | if (this.isActive() && event.isSelection()) {
|
| 155 | Optional<Style> clickableStyle = this.getMessage()
|
| 156 | .visit((style, text) -> style.getClickEvent() != null ? Optional.of(style) : Optional.empty(), Style.EMPTY);
|
| 157 | if (clickableStyle.isPresent() && this.handleStyleClick(clickableStyle.get())) {
|
| 158 | return true;
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | return super.keyPressed(event);
|
| 163 | }
|
| 164 |
|
| 165 | public static FocusableTextWidget.Builder builder(final Component message, final Font font) {
|
| 166 | return new FocusableTextWidget.Builder(message, font);
|
| 167 | }
|
| 168 |
|
| 169 | public static FocusableTextWidget.Builder builder(final Component message, final Font font, final int padding) {
|
| 170 | return new FocusableTextWidget.Builder(message, font, padding);
|
| 171 | }
|
| 172 |
|
| 173 | @Environment(EnvType.CLIENT)
|
| 174 | public enum BackgroundFill {
|
| 175 | ALWAYS,
|
| 176 | ON_FOCUS,
|
| 177 | NEVER;
|
| 178 | }
|
| 179 |
|
| 180 | @Environment(EnvType.CLIENT)
|
| 181 | public static class Builder {
|
| 182 | private final Component message;
|
| 183 | private final Font font;
|
| 184 | private final int padding;
|
| 185 | private int maxWidth = -1;
|
| 186 | private boolean alwaysShowBorder = true;
|
| 187 | private FocusableTextWidget.BackgroundFill backgroundFill = FocusableTextWidget.BackgroundFill.ALWAYS;
|
| 188 |
|
| 189 | private Builder(final Component message, final Font font) {
|
| 190 | this(message, font, 4);
|
| 191 | }
|
| 192 |
|
| 193 | private Builder(final Component message, final Font font, final int padding) {
|
| 194 | this.message = message;
|
| 195 | this.font = font;
|
| 196 | this.padding = padding;
|
| 197 | }
|
| 198 |
|
| 199 | public FocusableTextWidget.Builder maxWidth(final int maxWidth) {
|
| 200 | this.maxWidth = maxWidth;
|
| 201 | return this;
|
| 202 | }
|
| 203 |
|
| 204 | public FocusableTextWidget.Builder textWidth(final int textWidth) {
|
| 205 | this.maxWidth = textWidth + this.padding * 2;
|
| 206 | return this;
|
| 207 | }
|
| 208 |
|
| 209 | public FocusableTextWidget.Builder alwaysShowBorder(final boolean alwaysShowBorder) {
|
| 210 | this.alwaysShowBorder = alwaysShowBorder;
|
| 211 | return this;
|
| 212 | }
|
| 213 |
|
| 214 | public FocusableTextWidget.Builder backgroundFill(final FocusableTextWidget.BackgroundFill backgroundFill) {
|
| 215 | this.backgroundFill = backgroundFill;
|
| 216 | return this;
|
| 217 | }
|
| 218 |
|
| 219 | public FocusableTextWidget build() {
|
| 220 | return new FocusableTextWidget(this.message, this.font, this.padding, this.maxWidth, this.backgroundFill, this.alwaysShowBorder);
|
| 221 | }
|
| 222 | }
|
| 223 | }
|
| 224 |
|