| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.ChatFormatting;
|
| 10 | import net.minecraft.client.Minecraft;
|
| 11 | import net.minecraft.client.gui.Font;
|
| 12 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 13 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 14 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 15 | import net.minecraft.client.input.CharacterEvent;
|
| 16 | import net.minecraft.client.input.KeyEvent;
|
| 17 | import net.minecraft.client.input.MouseButtonEvent;
|
| 18 | import net.minecraft.client.input.PreeditEvent;
|
| 19 | import net.minecraft.client.renderer.RenderPipelines;
|
| 20 | import net.minecraft.client.sounds.SoundManager;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import net.minecraft.network.chat.MutableComponent;
|
| 23 | import net.minecraft.network.chat.Style;
|
| 24 | import net.minecraft.resources.Identifier;
|
| 25 | import net.minecraft.util.FormattedCharSequence;
|
| 26 | import net.minecraft.util.Mth;
|
| 27 | import net.minecraft.util.StringUtil;
|
| 28 | import net.minecraft.util.Util;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | @Environment(EnvType.CLIENT)
|
| 32 | public class EditBox extends AbstractWidget {
|
| 33 | private static final WidgetSprites SPRITES = new WidgetSprites(
|
| 34 | Identifier.withDefaultNamespace("widget/text_field"), Identifier.withDefaultNamespace("widget/text_field_highlighted")
|
| 35 | );
|
| 36 | public static final int BACKWARDS = -1;
|
| 37 | public static final int FORWARDS = 1;
|
| 38 | public static final int DEFAULT_TEXT_COLOR = -2039584;
|
| 39 | public static final Style DEFAULT_HINT_STYLE = Style.EMPTY.withColor(ChatFormatting.DARK_GRAY);
|
| 40 | public static final Style SEARCH_HINT_STYLE = Style.EMPTY.applyFormats(ChatFormatting.GRAY, ChatFormatting.ITALIC);
|
| 41 | private final Font font;
|
| 42 | private String value = "";
|
| 43 | private int maxLength = 32;
|
| 44 | private boolean bordered = true;
|
| 45 | private boolean canLoseFocus = true;
|
| 46 | private boolean isEditable = true;
|
| 47 | private boolean centered = false;
|
| 48 | private boolean textShadow = true;
|
| 49 | private boolean invertHighlightedTextColor = true;
|
| 50 | private int displayPos;
|
| 51 | private int cursorPos;
|
| 52 | private int highlightPos;
|
| 53 | private int textColor = -2039584;
|
| 54 | private int textColorUneditable = -9408400;
|
| 55 | private @Nullable String suggestion;
|
| 56 | private @Nullable Consumer<String> responder;
|
| 57 | private final List<EditBox.TextFormatter> formatters = new ArrayList<>();
|
| 58 | private @Nullable Component hint;
|
| 59 | private @Nullable IMEPreeditOverlay preeditOverlay;
|
| 60 | private long focusedTime = Util.getMillis();
|
| 61 | private int textX;
|
| 62 | private int textY;
|
| 63 |
|
| 64 | public EditBox(final Font font, final Component narration) {
|
| 65 | this(font, 150, 20, narration);
|
| 66 | }
|
| 67 |
|
| 68 | public EditBox(final Font font, final int width, final int height, final Component narration) {
|
| 69 | this(font, 0, 0, width, height, narration);
|
| 70 | }
|
| 71 |
|
| 72 | public EditBox(final Font font, final int x, final int y, final int width, final int height, final Component narration) {
|
| 73 | this(font, x, y, width, height, null, narration);
|
| 74 | }
|
| 75 |
|
| 76 | public EditBox(final Font font, final int x, final int y, final int width, final int height, final @Nullable EditBox oldBox, final Component narration) {
|
| 77 | super(x, y, width, height, narration);
|
| 78 | this.font = font;
|
| 79 | if (oldBox != null) {
|
| 80 | this.setValue(oldBox.getValue());
|
| 81 | }
|
| 82 |
|
| 83 | this.updateTextPosition();
|
| 84 | }
|
| 85 |
|
| 86 | public void setResponder(final Consumer<String> responder) {
|
| 87 | this.responder = responder;
|
| 88 | }
|
| 89 |
|
| 90 | public void addFormatter(final EditBox.TextFormatter formatter) {
|
| 91 | this.formatters.add(formatter);
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected MutableComponent createNarrationMessage() {
|
| 96 | Component message = this.getMessage();
|
| 97 | return Component.translatable("gui.narrate.editBox", message, this.value);
|
| 98 | }
|
| 99 |
|
| 100 | public void setValue(final String value) {
|
| 101 | if (value.length() > this.maxLength) {
|
| 102 | this.value = value.substring(0, this.maxLength);
|
| 103 | } else {
|
| 104 | this.value = value;
|
| 105 | }
|
| 106 |
|
| 107 | this.moveCursorToEnd(false);
|
| 108 | this.setHighlightPos(this.cursorPos);
|
| 109 | this.onValueChange(value);
|
| 110 | }
|
| 111 |
|
| 112 | public String getValue() {
|
| 113 | return this.value;
|
| 114 | }
|
| 115 |
|
| 116 | public String getHighlighted() {
|
| 117 | int start = Math.min(this.cursorPos, this.highlightPos);
|
| 118 | int end = Math.max(this.cursorPos, this.highlightPos);
|
| 119 | return this.value.substring(start, end);
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | public void setX(final int x) {
|
| 124 | super.setX(x);
|
| 125 | this.updateTextPosition();
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public void setY(final int y) {
|
| 130 | super.setY(y);
|
| 131 | this.updateTextPosition();
|
| 132 | }
|
| 133 |
|
| 134 | public void insertText(final String input) {
|
| 135 | int start = Math.min(this.cursorPos, this.highlightPos);
|
| 136 | int end = Math.max(this.cursorPos, this.highlightPos);
|
| 137 | int maxInsertionLength = this.maxLength - this.value.length() - (start - end);
|
| 138 | if (maxInsertionLength > 0) {
|
| 139 | String text = StringUtil.filterText(input);
|
| 140 | int insertionLength = text.length();
|
| 141 | if (maxInsertionLength < insertionLength) {
|
| 142 | if (Character.isHighSurrogate(text.charAt(maxInsertionLength - 1))) {
|
| 143 | maxInsertionLength--;
|
| 144 | }
|
| 145 |
|
| 146 | text = text.substring(0, maxInsertionLength);
|
| 147 | insertionLength = maxInsertionLength;
|
| 148 | }
|
| 149 |
|
| 150 | this.value = new StringBuilder(this.value).replace(start, end, text).toString();
|
| 151 | this.setCursorPosition(start + insertionLength);
|
| 152 | this.setHighlightPos(this.cursorPos);
|
| 153 | this.onValueChange(this.value);
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | private void onValueChange(final String value) {
|
| 158 | if (this.responder != null) {
|
| 159 | this.responder.accept(value);
|
| 160 | }
|
| 161 |
|
| 162 | this.updateTextPosition();
|
| 163 | }
|
| 164 |
|
| 165 | private void deleteText(final int dir, final boolean wholeWord) {
|
| 166 | if (wholeWord) {
|
| 167 | this.deleteWords(dir);
|
| 168 | } else {
|
| 169 | this.deleteChars(dir);
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | public void deleteWords(final int dir) {
|
| 174 | if (!this.value.isEmpty()) {
|
| 175 | if (this.highlightPos != this.cursorPos) {
|
| 176 | this.insertText("");
|
| 177 | } else {
|
| 178 | this.deleteCharsToPos(this.getWordPosition(dir));
|
| 179 | }
|
| 180 | }
|
| 181 | }
|
| 182 |
|
| 183 | public void deleteChars(final int dir) {
|
| 184 | this.deleteCharsToPos(this.getCursorPos(dir));
|
| 185 | }
|
| 186 |
|
| 187 | public void deleteCharsToPos(final int pos) {
|
| 188 | if (!this.value.isEmpty()) {
|
| 189 | if (this.highlightPos != this.cursorPos) {
|
| 190 | this.insertText("");
|
| 191 | } else {
|
| 192 | int start = Math.min(pos, this.cursorPos);
|
| 193 | int end = Math.max(pos, this.cursorPos);
|
| 194 | if (start != end) {
|
| 195 | this.value = new StringBuilder(this.value).delete(start, end).toString();
|
| 196 | this.setCursorPosition(start);
|
| 197 | this.onValueChange(this.value);
|
| 198 | this.moveCursorTo(start, false);
|
| 199 | }
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 |
|
| 204 | public int getWordPosition(final int dir) {
|
| 205 | return this.getWordPosition(dir, this.getCursorPosition());
|
| 206 | }
|
| 207 |
|
| 208 | private int getWordPosition(final int dir, final int from) {
|
| 209 | return this.getWordPosition(dir, from, true);
|
| 210 | }
|
| 211 |
|
| 212 | private int getWordPosition(final int dir, final int from, final boolean stripSpaces) {
|
| 213 | int result = from;
|
| 214 | boolean reverse = dir < 0;
|
| 215 | int abs = Math.abs(dir);
|
| 216 |
|
| 217 | for (int i = 0; i < abs; i++) {
|
| 218 | if (!reverse) {
|
| 219 | int length = this.value.length();
|
| 220 | result = this.value.indexOf(32, result);
|
| 221 | if (result == -1) {
|
| 222 | result = length;
|
| 223 | } else {
|
| 224 | while (stripSpaces && result < length && this.value.charAt(result) == ' ') {
|
| 225 | result++;
|
| 226 | }
|
| 227 | }
|
| 228 | } else {
|
| 229 | while (stripSpaces && result > 0 && this.value.charAt(result - 1) == ' ') {
|
| 230 | result--;
|
| 231 | }
|
| 232 |
|
| 233 | while (result > 0 && this.value.charAt(result - 1) != ' ') {
|
| 234 | result--;
|
| 235 | }
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | return result;
|
| 240 | }
|
| 241 |
|
| 242 | public void moveCursor(final int dir, final boolean hasShiftDown) {
|
| 243 | this.moveCursorTo(this.getCursorPos(dir), hasShiftDown);
|
| 244 | }
|
| 245 |
|
| 246 | private int getCursorPos(final int dir) {
|
| 247 | return Util.offsetByCodepoints(this.value, this.cursorPos, dir);
|
| 248 | }
|
| 249 |
|
| 250 | public void moveCursorTo(final int dir, final boolean extendSelection) {
|
| 251 | this.setCursorPosition(dir);
|
| 252 | if (!extendSelection) {
|
| 253 | this.setHighlightPos(this.cursorPos);
|
| 254 | }
|
| 255 |
|
| 256 | this.updateTextPosition();
|
| 257 | }
|
| 258 |
|
| 259 | public void setCursorPosition(final int pos) {
|
| 260 | this.cursorPos = Mth.clamp(pos, 0, this.value.length());
|
| 261 | this.scrollTo(this.cursorPos);
|
| 262 | }
|
| 263 |
|
| 264 | public void moveCursorToStart(final boolean hasShiftDown) {
|
| 265 | this.moveCursorTo(0, hasShiftDown);
|
| 266 | }
|
| 267 |
|
| 268 | public void moveCursorToEnd(final boolean hasShiftDown) {
|
| 269 | this.moveCursorTo(this.value.length(), hasShiftDown);
|
| 270 | }
|
| 271 |
|
| 272 | @Override
|
| 273 | public boolean keyPressed(final KeyEvent event) {
|
| 274 | if (this.isActive() && this.isFocused()) {
|
| 275 | switch (event.shortcutKey()) {
|
| 276 | case 8:
|
| 277 | if (this.isEditable) {
|
| 278 | this.deleteText(-1, event.hasControlDownWithQuirk());
|
| 279 | }
|
| 280 |
|
| 281 | return true;
|
| 282 | case 127:
|
| 283 | if (this.isEditable) {
|
| 284 | this.deleteText(1, event.hasControlDownWithQuirk());
|
| 285 | }
|
| 286 |
|
| 287 | return true;
|
| 288 | case 1073741898:
|
| 289 | this.moveCursorToStart(event.hasShiftDown());
|
| 290 | return true;
|
| 291 | case 1073741901:
|
| 292 | this.moveCursorToEnd(event.hasShiftDown());
|
| 293 | return true;
|
| 294 | case 1073741903:
|
| 295 | if (event.hasControlDownWithQuirk()) {
|
| 296 | this.moveCursorTo(this.getWordPosition(1), event.hasShiftDown());
|
| 297 | } else {
|
| 298 | this.moveCursor(1, event.hasShiftDown());
|
| 299 | }
|
| 300 |
|
| 301 | return true;
|
| 302 | case 1073741904:
|
| 303 | if (event.hasControlDownWithQuirk()) {
|
| 304 | this.moveCursorTo(this.getWordPosition(-1), event.hasShiftDown());
|
| 305 | } else {
|
| 306 | this.moveCursor(-1, event.hasShiftDown());
|
| 307 | }
|
| 308 |
|
| 309 | return true;
|
| 310 | default:
|
| 311 | if (event.isSelectAll()) {
|
| 312 | this.moveCursorToEnd(false);
|
| 313 | this.setHighlightPos(0);
|
| 314 | return true;
|
| 315 | } else if (event.isCopy()) {
|
| 316 | Minecraft.getInstance().keyboardHandler.setClipboard(this.getHighlighted());
|
| 317 | return true;
|
| 318 | } else if (event.isPaste()) {
|
| 319 | if (this.isEditable()) {
|
| 320 | this.insertText(Minecraft.getInstance().keyboardHandler.getClipboard());
|
| 321 | }
|
| 322 |
|
| 323 | return true;
|
| 324 | } else if (event.isCut()) {
|
| 325 | Minecraft.getInstance().keyboardHandler.setClipboard(this.getHighlighted());
|
| 326 | if (this.isEditable()) {
|
| 327 | this.insertText("");
|
| 328 | }
|
| 329 |
|
| 330 | return true;
|
| 331 | } else {
|
| 332 | return false;
|
| 333 | }
|
| 334 | }
|
| 335 | } else {
|
| 336 | return false;
|
| 337 | }
|
| 338 | }
|
| 339 |
|
| 340 | public boolean canConsumeInput() {
|
| 341 | return this.isActive() && this.isFocused() && this.isEditable();
|
| 342 | }
|
| 343 |
|
| 344 | @Override
|
| 345 | public boolean capturesInput() {
|
| 346 | return this.canConsumeInput();
|
| 347 | }
|
| 348 |
|
| 349 | @Override
|
| 350 | public boolean charTyped(final CharacterEvent event) {
|
| 351 | if (!this.canConsumeInput()) {
|
| 352 | return false;
|
| 353 | }
|
| 354 |
|
| 355 | if (event.isAllowedChatCharacter()) {
|
| 356 | if (this.isEditable) {
|
| 357 | this.insertText(event.codepointAsString());
|
| 358 | }
|
| 359 |
|
| 360 | return true;
|
| 361 | } else {
|
| 362 | return false;
|
| 363 | }
|
| 364 | }
|
| 365 |
|
| 366 | @Override
|
| 367 | public boolean preeditUpdated(final @Nullable PreeditEvent event) {
|
| 368 | this.preeditOverlay = event != null ? new IMEPreeditOverlay(event, this.font, 9 + 1) : null;
|
| 369 | return true;
|
| 370 | }
|
| 371 |
|
| 372 | private int findClickedPositionInText(final MouseButtonEvent event) {
|
| 373 | int positionInText = Math.min(Mth.floor(event.x()) - this.textX, this.getInnerWidth());
|
| 374 | String displayed = this.value.substring(this.displayPos);
|
| 375 | return this.displayPos + this.font.plainSubstrByWidth(displayed, positionInText).length();
|
| 376 | }
|
| 377 |
|
| 378 | private void selectWord(final MouseButtonEvent event) {
|
| 379 | int clickedPosition = this.findClickedPositionInText(event);
|
| 380 | int wordStart = this.getWordPosition(-1, clickedPosition);
|
| 381 | int wordEnd = this.getWordPosition(1, clickedPosition);
|
| 382 | this.moveCursorTo(wordStart, false);
|
| 383 | this.moveCursorTo(wordEnd, true);
|
| 384 | }
|
| 385 |
|
| 386 | @Override
|
| 387 | public void onClick(final MouseButtonEvent event, final boolean doubleClick) {
|
| 388 | if (doubleClick) {
|
| 389 | this.selectWord(event);
|
| 390 | } else {
|
| 391 | this.moveCursorTo(this.findClickedPositionInText(event), event.hasShiftDown());
|
| 392 | }
|
| 393 | }
|
| 394 |
|
| 395 | @Override
|
| 396 | protected void onDrag(final MouseButtonEvent event, final double dx, final double dy) {
|
| 397 | this.moveCursorTo(this.findClickedPositionInText(event), true);
|
| 398 | }
|
| 399 |
|
| 400 | @Override
|
| 401 | public void playDownSound(final SoundManager soundManager) {
|
| 402 | }
|
| 403 |
|
| 404 | @Override
|
| 405 | public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 406 | if (this.isVisible()) {
|
| 407 | if (this.isBordered()) {
|
| 408 | Identifier sprite = SPRITES.get(this.isActive(), this.isFocused());
|
| 409 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
| 410 | }
|
| 411 |
|
| 412 | int color = this.isEditable ? this.textColor : this.textColorUneditable;
|
| 413 | int relCursorPos = this.cursorPos - this.displayPos;
|
| 414 | String displayed = this.font.plainSubstrByWidth(this.value.substring(this.displayPos), this.getInnerWidth());
|
| 415 | boolean cursorOnScreen = relCursorPos >= 0 && relCursorPos <= displayed.length();
|
| 416 | boolean showCursor = this.isFocused() && TextCursorUtils.isCursorVisible(Util.getMillis() - this.focusedTime) && cursorOnScreen;
|
| 417 | int drawX = this.textX;
|
| 418 | int relHighlightPos = Mth.clamp(this.highlightPos - this.displayPos, 0, displayed.length());
|
| 419 | if (!displayed.isEmpty()) {
|
| 420 | String half = cursorOnScreen ? displayed.substring(0, relCursorPos) : displayed;
|
| 421 | FormattedCharSequence charSequence = this.applyFormat(half, this.displayPos);
|
| 422 | graphics.text(this.font, charSequence, drawX, this.textY, color, this.textShadow);
|
| 423 | drawX += this.font.width(charSequence) + 1;
|
| 424 | }
|
| 425 |
|
| 426 | boolean insert = this.cursorPos < this.value.length() || this.value.length() >= this.getMaxLength();
|
| 427 | int cursorX = drawX;
|
| 428 | if (!cursorOnScreen) {
|
| 429 | cursorX = relCursorPos > 0 ? this.textX + this.width : this.textX;
|
| 430 | } else if (insert) {
|
| 431 | cursorX--;
|
| 432 | drawX--;
|
| 433 | }
|
| 434 |
|
| 435 | if (!displayed.isEmpty() && cursorOnScreen && relCursorPos < displayed.length()) {
|
| 436 | graphics.text(this.font, this.applyFormat(displayed.substring(relCursorPos), this.cursorPos), drawX, this.textY, color, this.textShadow);
|
| 437 | }
|
| 438 |
|
| 439 | if (this.hint != null && displayed.isEmpty() && !this.isFocused()) {
|
| 440 | graphics.text(this.font, this.hint, drawX, this.textY, color);
|
| 441 | }
|
| 442 |
|
| 443 | if (!insert && this.suggestion != null) {
|
| 444 | graphics.text(this.font, this.suggestion, cursorX - 1, this.textY, -8355712, this.textShadow);
|
| 445 | }
|
| 446 |
|
| 447 | if (relHighlightPos != relCursorPos) {
|
| 448 | int highlightX = this.textX + this.font.width(displayed.substring(0, relHighlightPos));
|
| 449 | graphics.textHighlight(
|
| 450 | Math.min(cursorX, this.getX() + this.width),
|
| 451 | this.textY - 1,
|
| 452 | Math.min(highlightX - 1, this.getX() + this.width),
|
| 453 | this.textY + 1 + 9,
|
| 454 | this.invertHighlightedTextColor
|
| 455 | );
|
| 456 | }
|
| 457 |
|
| 458 | if (showCursor) {
|
| 459 | if (insert) {
|
| 460 | TextCursorUtils.extractInsertCursor(graphics, cursorX, this.textY, color, 9 + 1);
|
| 461 | } else {
|
| 462 | TextCursorUtils.extractAppendCursor(graphics, this.font, cursorX, this.textY, color, this.textShadow);
|
| 463 | }
|
| 464 | }
|
| 465 |
|
| 466 | if (this.isHovered()) {
|
| 467 | graphics.requestCursor(this.isEditable() ? CursorTypes.IBEAM : CursorTypes.NOT_ALLOWED);
|
| 468 | }
|
| 469 |
|
| 470 | if (this.preeditOverlay == null) {
|
| 471 | if (this.canConsumeInput()) {
|
| 472 | Minecraft.getInstance().textInputManager().setTextInputArea(cursorX, this.textY, cursorX + 1, this.textY + 9 + 1);
|
| 473 | }
|
| 474 | } else {
|
| 475 | this.preeditOverlay.updateInputPosition(cursorX, this.textY);
|
| 476 | graphics.setPreeditOverlay(this.preeditOverlay);
|
| 477 | }
|
| 478 | }
|
| 479 | }
|
| 480 |
|
| 481 | private FormattedCharSequence applyFormat(final String text, final int offset) {
|
| 482 | for (EditBox.TextFormatter formatter : this.formatters) {
|
| 483 | FormattedCharSequence formattedCharSequence = formatter.format(text, offset);
|
| 484 | if (formattedCharSequence != null) {
|
| 485 | return formattedCharSequence;
|
| 486 | }
|
| 487 | }
|
| 488 |
|
| 489 | return FormattedCharSequence.forward(text, Style.EMPTY);
|
| 490 | }
|
| 491 |
|
| 492 | private void updateTextPosition() {
|
| 493 | if (this.font != null) {
|
| 494 | String displayed = this.font.plainSubstrByWidth(this.value.substring(this.displayPos), this.getInnerWidth());
|
| 495 | this.textX = this.getX() + (this.isCentered() ? (this.getWidth() - this.font.width(displayed)) / 2 : (this.bordered ? 4 : 0));
|
| 496 | this.textY = this.bordered ? this.getY() + (this.height - 8) / 2 : this.getY();
|
| 497 | }
|
| 498 | }
|
| 499 |
|
| 500 | public void setMaxLength(final int maxLength) {
|
| 501 | this.maxLength = maxLength;
|
| 502 | if (this.value.length() > maxLength) {
|
| 503 | this.value = this.value.substring(0, maxLength);
|
| 504 | this.onValueChange(this.value);
|
| 505 | }
|
| 506 | }
|
| 507 |
|
| 508 | private int getMaxLength() {
|
| 509 | return this.maxLength;
|
| 510 | }
|
| 511 |
|
| 512 | public int getCursorPosition() {
|
| 513 | return this.cursorPos;
|
| 514 | }
|
| 515 |
|
| 516 | public boolean isBordered() {
|
| 517 | return this.bordered;
|
| 518 | }
|
| 519 |
|
| 520 | public void setBordered(final boolean bordered) {
|
| 521 | this.bordered = bordered;
|
| 522 | this.updateTextPosition();
|
| 523 | }
|
| 524 |
|
| 525 | public void setTextColor(final int textColor) {
|
| 526 | this.textColor = textColor;
|
| 527 | }
|
| 528 |
|
| 529 | public void setTextColorUneditable(final int textColorUneditable) {
|
| 530 | this.textColorUneditable = textColorUneditable;
|
| 531 | }
|
| 532 |
|
| 533 | @Override
|
| 534 | public void setFocused(final boolean focused) {
|
| 535 | if (this.canLoseFocus || focused) {
|
| 536 | super.setFocused(focused);
|
| 537 | if (focused) {
|
| 538 | this.focusedTime = Util.getMillis();
|
| 539 | }
|
| 540 |
|
| 541 | if (this.isEditable()) {
|
| 542 | Minecraft.getInstance().onTextInputFocusChange(this, focused);
|
| 543 | }
|
| 544 | }
|
| 545 | }
|
| 546 |
|
| 547 | private boolean isEditable() {
|
| 548 | return this.isEditable;
|
| 549 | }
|
| 550 |
|
| 551 | public void setEditable(final boolean isEditable) {
|
| 552 | if (this.isFocused()) {
|
| 553 | Minecraft.getInstance().onTextInputFocusChange(this, isEditable);
|
| 554 | }
|
| 555 |
|
| 556 | this.isEditable = isEditable;
|
| 557 | }
|
| 558 |
|
| 559 | private boolean isCentered() {
|
| 560 | return this.centered;
|
| 561 | }
|
| 562 |
|
| 563 | public void setCentered(final boolean centered) {
|
| 564 | this.centered = centered;
|
| 565 | this.updateTextPosition();
|
| 566 | }
|
| 567 |
|
| 568 | public void setTextShadow(final boolean textShadow) {
|
| 569 | this.textShadow = textShadow;
|
| 570 | }
|
| 571 |
|
| 572 | public void setInvertHighlightedTextColor(final boolean invertHighlightedTextColor) {
|
| 573 | this.invertHighlightedTextColor = invertHighlightedTextColor;
|
| 574 | }
|
| 575 |
|
| 576 | public int getInnerWidth() {
|
| 577 | return this.isBordered() ? this.width - 8 : this.width;
|
| 578 | }
|
| 579 |
|
| 580 | public void setHighlightPos(final int pos) {
|
| 581 | this.highlightPos = Mth.clamp(pos, 0, this.value.length());
|
| 582 | this.scrollTo(this.highlightPos);
|
| 583 | }
|
| 584 |
|
| 585 | private void scrollTo(final int pos) {
|
| 586 | if (this.font != null) {
|
| 587 | this.displayPos = Math.min(this.displayPos, this.value.length());
|
| 588 | int innerWidth = this.getInnerWidth();
|
| 589 | String displayed = this.font.plainSubstrByWidth(this.value.substring(this.displayPos), innerWidth);
|
| 590 | int lastPos = displayed.length() + this.displayPos;
|
| 591 | if (pos == this.displayPos) {
|
| 592 | this.displayPos = this.displayPos - this.font.plainSubstrByWidth(this.value, innerWidth, true).length();
|
| 593 | }
|
| 594 |
|
| 595 | if (pos > lastPos) {
|
| 596 | this.displayPos += pos - lastPos;
|
| 597 | } else if (pos <= this.displayPos) {
|
| 598 | this.displayPos = this.displayPos - (this.displayPos - pos);
|
| 599 | }
|
| 600 |
|
| 601 | this.displayPos = Mth.clamp(this.displayPos, 0, this.value.length());
|
| 602 | }
|
| 603 | }
|
| 604 |
|
| 605 | public void setCanLoseFocus(final boolean canLoseFocus) {
|
| 606 | this.canLoseFocus = canLoseFocus;
|
| 607 | }
|
| 608 |
|
| 609 | public boolean isVisible() {
|
| 610 | return this.visible;
|
| 611 | }
|
| 612 |
|
| 613 | public void setVisible(final boolean visible) {
|
| 614 | this.visible = visible;
|
| 615 | }
|
| 616 |
|
| 617 | public void setSuggestion(final @Nullable String suggestion) {
|
| 618 | this.suggestion = suggestion;
|
| 619 | }
|
| 620 |
|
| 621 | public int getScreenX(final int charIndex) {
|
| 622 | return charIndex > this.value.length() ? this.getX() : this.getX() + this.font.width(this.value.substring(0, charIndex));
|
| 623 | }
|
| 624 |
|
| 625 | @Override
|
| 626 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 627 | output.add(NarratedElementType.TITLE, this.createNarrationMessage());
|
| 628 | }
|
| 629 |
|
| 630 | public void setHint(final Component hint) {
|
| 631 | boolean hasNoStyle = hint.getStyle().equals(Style.EMPTY);
|
| 632 | this.hint = hasNoStyle ? hint.copy().withStyle(DEFAULT_HINT_STYLE) : hint;
|
| 633 | }
|
| 634 |
|
| 635 | @FunctionalInterface
|
| 636 | @Environment(EnvType.CLIENT)
|
| 637 | public interface TextFormatter {
|
| 638 | @Nullable FormattedCharSequence format(final String text, final int offset);
|
| 639 | }
|
| 640 | }
|
| 641 |
|