| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 4 | import java.util.function.Consumer;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.gui.Font;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 11 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 12 | import net.minecraft.client.input.CharacterEvent;
|
| 13 | import net.minecraft.client.input.KeyEvent;
|
| 14 | import net.minecraft.client.input.MouseButtonEvent;
|
| 15 | import net.minecraft.client.input.PreeditEvent;
|
| 16 | import net.minecraft.network.chat.CommonComponents;
|
| 17 | import net.minecraft.network.chat.Component;
|
| 18 | import net.minecraft.util.ARGB;
|
| 19 | import net.minecraft.util.Util;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class MultiLineEditBox extends AbstractTextAreaWidget {
|
| 24 | private static final int CURSOR_COLOR = -3092272;
|
| 25 | private static final int PLACEHOLDER_TEXT_COLOR = ARGB.color(204, -2039584);
|
| 26 | private final Font font;
|
| 27 | private final Component placeholder;
|
| 28 | private final MultilineTextField textField;
|
| 29 | private final int textColor;
|
| 30 | private final boolean textShadow;
|
| 31 | private final int cursorColor;
|
| 32 | private @Nullable IMEPreeditOverlay preeditOverlay;
|
| 33 | private long focusedTime = Util.getMillis();
|
| 34 |
|
| 35 | private MultiLineEditBox(
|
| 36 | final Font font,
|
| 37 | final int x,
|
| 38 | final int y,
|
| 39 | final int width,
|
| 40 | final int height,
|
| 41 | final Component placeholder,
|
| 42 | final Component narration,
|
| 43 | final int textColor,
|
| 44 | final boolean textShadow,
|
| 45 | final int cursorColor,
|
| 46 | final boolean showBackground,
|
| 47 | final boolean showDecorations
|
| 48 | ) {
|
| 49 | super(x, y, width, height, narration, AbstractScrollArea.defaultSettings((int)(9.0 / 2.0)), showBackground, showDecorations);
|
| 50 | this.font = font;
|
| 51 | this.textShadow = textShadow;
|
| 52 | this.textColor = textColor;
|
| 53 | this.cursorColor = cursorColor;
|
| 54 | this.placeholder = placeholder;
|
| 55 | this.textField = new MultilineTextField(font, width - this.totalInnerPadding());
|
| 56 | this.textField.setCursorListener(this::scrollToCursor);
|
| 57 | }
|
| 58 |
|
| 59 | public void setCharacterLimit(final int characterLimit) {
|
| 60 | this.textField.setCharacterLimit(characterLimit);
|
| 61 | }
|
| 62 |
|
| 63 | public void setLineLimit(final int lineLimit) {
|
| 64 | this.textField.setLineLimit(lineLimit);
|
| 65 | }
|
| 66 |
|
| 67 | public void setValueListener(final Consumer<String> valueListener) {
|
| 68 | this.textField.setValueListener(valueListener);
|
| 69 | }
|
| 70 |
|
| 71 | public void setValue(final String value) {
|
| 72 | this.setValue(value, false);
|
| 73 | }
|
| 74 |
|
| 75 | public void setValue(final String value, final boolean allowOverflowLineLimit) {
|
| 76 | this.textField.setValue(value, allowOverflowLineLimit);
|
| 77 | }
|
| 78 |
|
| 79 | public String getValue() {
|
| 80 | return this.textField.value();
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 85 | output.add(NarratedElementType.TITLE, Component.translatable("gui.narrate.editBox", this.getMessage(), this.getValue()));
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void onClick(final MouseButtonEvent event, final boolean doubleClick) {
|
| 90 | if (doubleClick) {
|
| 91 | this.textField.selectWordAtCursor();
|
| 92 | } else {
|
| 93 | this.textField.setSelecting(event.hasShiftDown());
|
| 94 | this.seekCursorScreen(event.x(), event.y());
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | protected void onDrag(final MouseButtonEvent event, final double dx, final double dy) {
|
| 100 | this.textField.setSelecting(true);
|
| 101 | this.seekCursorScreen(event.x(), event.y());
|
| 102 | this.textField.setSelecting(event.hasShiftDown());
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public boolean keyPressed(final KeyEvent event) {
|
| 107 | return this.textField.keyPressed(event);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public boolean charTyped(final CharacterEvent event) {
|
| 112 | if (this.visible && this.isFocused() && event.isAllowedChatCharacter()) {
|
| 113 | this.textField.insertText(event.codepointAsString());
|
| 114 | return true;
|
| 115 | } else {
|
| 116 | return false;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public boolean capturesInput() {
|
| 122 | return this.visible && this.isFocused();
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public boolean preeditUpdated(final @Nullable PreeditEvent event) {
|
| 127 | this.preeditOverlay = event != null ? new IMEPreeditOverlay(event, this.font, 9 + 1) : null;
|
| 128 | return true;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | protected void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 133 | String value = this.textField.value();
|
| 134 | if (value.isEmpty() && !this.isFocused()) {
|
| 135 | graphics.textWithWordWrap(
|
| 136 | this.font, this.placeholder, this.getInnerLeft(), this.getInnerTop(), this.width - this.totalInnerPadding(), PLACEHOLDER_TEXT_COLOR
|
| 137 | );
|
| 138 | } else {
|
| 139 | int cursor = this.textField.cursor();
|
| 140 | boolean showCursor = this.isFocused() && TextCursorUtils.isCursorVisible(Util.getMillis() - this.focusedTime);
|
| 141 | boolean needsValidCursorPos = this.capturesInput();
|
| 142 | boolean insertCursor = cursor < value.length();
|
| 143 | int cursorX = 0;
|
| 144 | int cursorY = 0;
|
| 145 | int drawTop = this.getInnerTop();
|
| 146 | int innerLeft = this.getInnerLeft();
|
| 147 | boolean hasDrawnCursor = false;
|
| 148 |
|
| 149 | for (MultilineTextField.StringView lineView : this.textField.iterateLines()) {
|
| 150 | boolean lineWithinVisibleBounds = this.withinContentAreaTopBottom(drawTop, drawTop + 9);
|
| 151 | if (!hasDrawnCursor && (needsValidCursorPos || showCursor) && insertCursor && cursor >= lineView.beginIndex() && cursor <= lineView.endIndex()) {
|
| 152 | String textBeforeCursor = value.substring(lineView.beginIndex(), cursor);
|
| 153 | int textBeforeCursorPosRight = innerLeft + this.font.width(textBeforeCursor);
|
| 154 | cursorX = textBeforeCursorPosRight;
|
| 155 | cursorY = drawTop;
|
| 156 | if (lineWithinVisibleBounds) {
|
| 157 | String textAfterCursor = value.substring(cursor, lineView.endIndex());
|
| 158 | graphics.text(this.font, textBeforeCursor, innerLeft, drawTop, this.textColor, this.textShadow);
|
| 159 | graphics.text(this.font, textAfterCursor, textBeforeCursorPosRight, drawTop, this.textColor, this.textShadow);
|
| 160 | if (showCursor) {
|
| 161 | TextCursorUtils.extractInsertCursor(graphics, cursorX, cursorY, this.cursorColor, 9 + 1);
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | hasDrawnCursor = true;
|
| 166 | } else {
|
| 167 | String substring = value.substring(lineView.beginIndex(), lineView.endIndex());
|
| 168 | if (lineWithinVisibleBounds) {
|
| 169 | graphics.text(this.font, substring, innerLeft, drawTop, this.textColor, this.textShadow);
|
| 170 | }
|
| 171 |
|
| 172 | if ((needsValidCursorPos || showCursor) && !insertCursor) {
|
| 173 | cursorX = innerLeft + this.font.width(substring);
|
| 174 | cursorY = drawTop;
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | drawTop += 9;
|
| 179 | }
|
| 180 |
|
| 181 | if (showCursor && !insertCursor && this.withinContentAreaTopBottom(cursorY, cursorY + 9)) {
|
| 182 | TextCursorUtils.extractAppendCursor(graphics, this.font, cursorX, cursorY, this.cursorColor, this.textShadow);
|
| 183 | }
|
| 184 |
|
| 185 | if (this.textField.hasSelection()) {
|
| 186 | MultilineTextField.StringView selection = this.textField.getSelected();
|
| 187 | int drawX = this.getInnerLeft();
|
| 188 | drawTop = this.getInnerTop();
|
| 189 |
|
| 190 | for (MultilineTextField.StringView lineView : this.textField.iterateLines()) {
|
| 191 | if (selection.beginIndex() > lineView.endIndex()) {
|
| 192 | drawTop += 9;
|
| 193 | } else {
|
| 194 | if (lineView.beginIndex() > selection.endIndex()) {
|
| 195 | break;
|
| 196 | }
|
| 197 |
|
| 198 | if (this.withinContentAreaTopBottom(drawTop, drawTop + 9)) {
|
| 199 | int drawBegin = this.font.width(value.substring(lineView.beginIndex(), Math.max(selection.beginIndex(), lineView.beginIndex())));
|
| 200 | int drawEnd;
|
| 201 | if (selection.endIndex() > lineView.endIndex()) {
|
| 202 | drawEnd = this.width - this.innerPadding();
|
| 203 | } else {
|
| 204 | drawEnd = this.font.width(value.substring(lineView.beginIndex(), selection.endIndex()));
|
| 205 | }
|
| 206 |
|
| 207 | graphics.textHighlight(drawX + drawBegin, drawTop, drawX + drawEnd, drawTop + 9, true);
|
| 208 | }
|
| 209 |
|
| 210 | drawTop += 9;
|
| 211 | }
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | if (this.isHovered()) {
|
| 216 | graphics.requestCursor(CursorTypes.IBEAM);
|
| 217 | }
|
| 218 |
|
| 219 | if (this.preeditOverlay == null) {
|
| 220 | if (this.capturesInput()) {
|
| 221 | Minecraft.getInstance().textInputManager().setTextInputArea(cursorX, cursorY, cursorX + 1, cursorY + 9 + 1);
|
| 222 | }
|
| 223 | } else {
|
| 224 | this.preeditOverlay.updateInputPosition(cursorX, cursorY);
|
| 225 | graphics.setPreeditOverlay(this.preeditOverlay);
|
| 226 | }
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | protected void extractDecorations(final GuiGraphicsExtractor graphics) {
|
| 232 | super.extractDecorations(graphics);
|
| 233 | if (this.textField.hasCharacterLimit()) {
|
| 234 | int characterLimit = this.textField.characterLimit();
|
| 235 | Component countText = Component.translatable("gui.multiLineEditBox.character_limit", this.textField.value().length(), characterLimit);
|
| 236 | graphics.text(this.font, countText, this.getX() + this.width - this.font.width(countText), this.getY() + this.height + 4, -6250336);
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | @Override
|
| 241 | public int getInnerHeight() {
|
| 242 | return 9 * this.textField.getLineCount();
|
| 243 | }
|
| 244 |
|
| 245 | private void scrollToCursor() {
|
| 246 | double scrollAmount = this.scrollAmount();
|
| 247 | MultilineTextField.StringView firstFullyVisibleLine = this.textField.getLineView((int)(scrollAmount / 9.0));
|
| 248 | if (this.textField.cursor() <= firstFullyVisibleLine.beginIndex()) {
|
| 249 | scrollAmount = this.textField.getLineAtCursor() * 9;
|
| 250 | } else {
|
| 251 | MultilineTextField.StringView lastFullyVisibleLine = this.textField.getLineView((int)((scrollAmount + this.height) / 9.0) - 1);
|
| 252 | if (this.textField.cursor() > lastFullyVisibleLine.endIndex()) {
|
| 253 | scrollAmount = this.textField.getLineAtCursor() * 9 - this.height + 9 + this.totalInnerPadding();
|
| 254 | }
|
| 255 | }
|
| 256 |
|
| 257 | this.setScrollAmount(scrollAmount);
|
| 258 | }
|
| 259 |
|
| 260 | private void seekCursorScreen(final double x, final double y) {
|
| 261 | double mouseX = x - this.getX() - this.innerPadding();
|
| 262 | double mouseY = y - this.getY() - this.innerPadding() + this.scrollAmount();
|
| 263 | this.textField.seekCursorToPoint(mouseX, mouseY);
|
| 264 | }
|
| 265 |
|
| 266 | @Override
|
| 267 | public void setFocused(final boolean focused) {
|
| 268 | super.setFocused(focused);
|
| 269 | if (focused) {
|
| 270 | this.focusedTime = Util.getMillis();
|
| 271 | }
|
| 272 |
|
| 273 | Minecraft.getInstance().onTextInputFocusChange(this, focused);
|
| 274 | }
|
| 275 |
|
| 276 | public static MultiLineEditBox.Builder builder() {
|
| 277 | return new MultiLineEditBox.Builder();
|
| 278 | }
|
| 279 |
|
| 280 | @Environment(EnvType.CLIENT)
|
| 281 | public static class Builder {
|
| 282 | private int x;
|
| 283 | private int y;
|
| 284 | private Component placeholder = CommonComponents.EMPTY;
|
| 285 | private int textColor = -2039584;
|
| 286 | private boolean textShadow = true;
|
| 287 | private int cursorColor = -3092272;
|
| 288 | private boolean showBackground = true;
|
| 289 | private boolean showDecorations = true;
|
| 290 |
|
| 291 | public MultiLineEditBox.Builder setX(final int x) {
|
| 292 | this.x = x;
|
| 293 | return this;
|
| 294 | }
|
| 295 |
|
| 296 | public MultiLineEditBox.Builder setY(final int y) {
|
| 297 | this.y = y;
|
| 298 | return this;
|
| 299 | }
|
| 300 |
|
| 301 | public MultiLineEditBox.Builder setPlaceholder(final Component placeholder) {
|
| 302 | this.placeholder = placeholder;
|
| 303 | return this;
|
| 304 | }
|
| 305 |
|
| 306 | public MultiLineEditBox.Builder setTextColor(final int textColor) {
|
| 307 | this.textColor = textColor;
|
| 308 | return this;
|
| 309 | }
|
| 310 |
|
| 311 | public MultiLineEditBox.Builder setTextShadow(final boolean textShadow) {
|
| 312 | this.textShadow = textShadow;
|
| 313 | return this;
|
| 314 | }
|
| 315 |
|
| 316 | public MultiLineEditBox.Builder setCursorColor(final int cursorColor) {
|
| 317 | this.cursorColor = cursorColor;
|
| 318 | return this;
|
| 319 | }
|
| 320 |
|
| 321 | public MultiLineEditBox.Builder setShowBackground(final boolean showBackground) {
|
| 322 | this.showBackground = showBackground;
|
| 323 | return this;
|
| 324 | }
|
| 325 |
|
| 326 | public MultiLineEditBox.Builder setShowDecorations(final boolean showDecorations) {
|
| 327 | this.showDecorations = showDecorations;
|
| 328 | return this;
|
| 329 | }
|
| 330 |
|
| 331 | public MultiLineEditBox build(final Font font, final int width, final int height, final Component narration) {
|
| 332 | return new MultiLineEditBox(
|
| 333 | font,
|
| 334 | this.x,
|
| 335 | this.y,
|
| 336 | width,
|
| 337 | height,
|
| 338 | this.placeholder,
|
| 339 | narration,
|
| 340 | this.textColor,
|
| 341 | this.textShadow,
|
| 342 | this.cursorColor,
|
| 343 | this.showBackground,
|
| 344 | this.showDecorations
|
| 345 | );
|
| 346 | }
|
| 347 | }
|
| 348 | }
|
| 349 |
|