| 1 | package net.minecraft.client.gui.font;
|
| 2 |
|
| 3 | import java.util.function.Consumer;
|
| 4 | import java.util.function.Predicate;
|
| 5 | import java.util.function.Supplier;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.ChatFormatting;
|
| 9 | import net.minecraft.client.Minecraft;
|
| 10 | import net.minecraft.client.StringSplitter;
|
| 11 | import net.minecraft.client.input.CharacterEvent;
|
| 12 | import net.minecraft.client.input.KeyEvent;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.util.Util;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class TextFieldHelper {
|
| 18 | private final Supplier<String> getMessageFn;
|
| 19 | private final Consumer<String> setMessageFn;
|
| 20 | private final Supplier<String> getClipboardFn;
|
| 21 | private final Consumer<String> setClipboardFn;
|
| 22 | private final Predicate<String> stringValidator;
|
| 23 | private int cursorPos;
|
| 24 | private int selectionPos;
|
| 25 |
|
| 26 | public TextFieldHelper(
|
| 27 | final Supplier<String> getMessageFn,
|
| 28 | final Consumer<String> setMessageFn,
|
| 29 | final Supplier<String> getClipboardFn,
|
| 30 | final Consumer<String> setClipboardFn,
|
| 31 | final Predicate<String> stringValidator
|
| 32 | ) {
|
| 33 | this.getMessageFn = getMessageFn;
|
| 34 | this.setMessageFn = setMessageFn;
|
| 35 | this.getClipboardFn = getClipboardFn;
|
| 36 | this.setClipboardFn = setClipboardFn;
|
| 37 | this.stringValidator = stringValidator;
|
| 38 | this.setCursorToEnd();
|
| 39 | }
|
| 40 |
|
| 41 | public static Supplier<String> createClipboardGetter(final Minecraft minecraft) {
|
| 42 | return () -> getClipboardContents(minecraft);
|
| 43 | }
|
| 44 |
|
| 45 | public static String getClipboardContents(final Minecraft minecraft) {
|
| 46 | return ChatFormatting.stripFormatting(minecraft.keyboardHandler.getClipboard().replaceAll("\\r", ""));
|
| 47 | }
|
| 48 |
|
| 49 | public static Consumer<String> createClipboardSetter(final Minecraft minecraft) {
|
| 50 | return text -> setClipboardContents(minecraft, text);
|
| 51 | }
|
| 52 |
|
| 53 | public static void setClipboardContents(final Minecraft minecraft, final String text) {
|
| 54 | minecraft.keyboardHandler.setClipboard(text);
|
| 55 | }
|
| 56 |
|
| 57 | public boolean charTyped(final CharacterEvent event) {
|
| 58 | if (event.isAllowedChatCharacter()) {
|
| 59 | this.insertText(this.getMessageFn.get(), event.codepointAsString());
|
| 60 | }
|
| 61 |
|
| 62 | return true;
|
| 63 | }
|
| 64 |
|
| 65 | public boolean keyPressed(final KeyEvent event) {
|
| 66 | if (event.isSelectAll()) {
|
| 67 | this.selectAll();
|
| 68 | return true;
|
| 69 | }
|
| 70 |
|
| 71 | if (event.isCopy()) {
|
| 72 | this.copy();
|
| 73 | return true;
|
| 74 | }
|
| 75 |
|
| 76 | if (event.isPaste()) {
|
| 77 | this.paste();
|
| 78 | return true;
|
| 79 | }
|
| 80 |
|
| 81 | if (event.isCut()) {
|
| 82 | this.cut();
|
| 83 | return true;
|
| 84 | }
|
| 85 |
|
| 86 | TextFieldHelper.CursorStep cursorStep = event.hasControlDownWithQuirk() ? TextFieldHelper.CursorStep.WORD : TextFieldHelper.CursorStep.CHARACTER;
|
| 87 | if (event.shortcutKey() == 8) {
|
| 88 | this.removeFromCursor(-1, cursorStep);
|
| 89 | return true;
|
| 90 | }
|
| 91 |
|
| 92 | if (event.shortcutKey() == 127) {
|
| 93 | this.removeFromCursor(1, cursorStep);
|
| 94 | } else {
|
| 95 | if (event.isLeft()) {
|
| 96 | this.moveBy(-1, event.hasShiftDown(), cursorStep);
|
| 97 | return true;
|
| 98 | }
|
| 99 |
|
| 100 | if (event.isRight()) {
|
| 101 | this.moveBy(1, event.hasShiftDown(), cursorStep);
|
| 102 | return true;
|
| 103 | }
|
| 104 |
|
| 105 | if (event.shortcutKey() == 1073741898) {
|
| 106 | this.setCursorToStart(event.hasShiftDown());
|
| 107 | return true;
|
| 108 | }
|
| 109 |
|
| 110 | if (event.shortcutKey() == 1073741901) {
|
| 111 | this.setCursorToEnd(event.hasShiftDown());
|
| 112 | return true;
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | return false;
|
| 117 | }
|
| 118 |
|
| 119 | private int clampToMsgLength(final int value) {
|
| 120 | return Mth.clamp(value, 0, this.getMessageFn.get().length());
|
| 121 | }
|
| 122 |
|
| 123 | private void insertText(String message, final String text) {
|
| 124 | if (this.selectionPos != this.cursorPos) {
|
| 125 | message = this.deleteSelection(message);
|
| 126 | }
|
| 127 |
|
| 128 | this.cursorPos = Mth.clamp(this.cursorPos, 0, message.length());
|
| 129 | String newPageText = new StringBuilder(message).insert(this.cursorPos, text).toString();
|
| 130 | if (this.stringValidator.test(newPageText)) {
|
| 131 | this.setMessageFn.accept(newPageText);
|
| 132 | this.selectionPos = this.cursorPos = Math.min(newPageText.length(), this.cursorPos + text.length());
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | public void insertText(final String text) {
|
| 137 | this.insertText(this.getMessageFn.get(), text);
|
| 138 | }
|
| 139 |
|
| 140 | private void resetSelectionIfNeeded(final boolean selecting) {
|
| 141 | if (!selecting) {
|
| 142 | this.selectionPos = this.cursorPos;
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | public void moveBy(final int count, final boolean selecting, final TextFieldHelper.CursorStep scope) {
|
| 147 | switch (scope) {
|
| 148 | case CHARACTER:
|
| 149 | this.moveByChars(count, selecting);
|
| 150 | break;
|
| 151 | case WORD:
|
| 152 | this.moveByWords(count, selecting);
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | public void moveByChars(final int count) {
|
| 157 | this.moveByChars(count, false);
|
| 158 | }
|
| 159 |
|
| 160 | public void moveByChars(final int count, final boolean selecting) {
|
| 161 | this.cursorPos = Util.offsetByCodepoints(this.getMessageFn.get(), this.cursorPos, count);
|
| 162 | this.resetSelectionIfNeeded(selecting);
|
| 163 | }
|
| 164 |
|
| 165 | public void moveByWords(final int count) {
|
| 166 | this.moveByWords(count, false);
|
| 167 | }
|
| 168 |
|
| 169 | public void moveByWords(final int count, final boolean selecting) {
|
| 170 | this.cursorPos = StringSplitter.getWordPosition(this.getMessageFn.get(), count, this.cursorPos, true);
|
| 171 | this.resetSelectionIfNeeded(selecting);
|
| 172 | }
|
| 173 |
|
| 174 | public void removeFromCursor(final int count, final TextFieldHelper.CursorStep scope) {
|
| 175 | switch (scope) {
|
| 176 | case CHARACTER:
|
| 177 | this.removeCharsFromCursor(count);
|
| 178 | break;
|
| 179 | case WORD:
|
| 180 | this.removeWordsFromCursor(count);
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | public void removeWordsFromCursor(final int count) {
|
| 185 | int wordPosition = StringSplitter.getWordPosition(this.getMessageFn.get(), count, this.cursorPos, true);
|
| 186 | this.removeCharsFromCursor(wordPosition - this.cursorPos);
|
| 187 | }
|
| 188 |
|
| 189 | public void removeCharsFromCursor(final int count) {
|
| 190 | String message = this.getMessageFn.get();
|
| 191 | if (!message.isEmpty()) {
|
| 192 | String newMessage;
|
| 193 | if (this.selectionPos != this.cursorPos) {
|
| 194 | newMessage = this.deleteSelection(message);
|
| 195 | } else {
|
| 196 | int otherPos = Util.offsetByCodepoints(message, this.cursorPos, count);
|
| 197 | int start = Math.min(otherPos, this.cursorPos);
|
| 198 | int end = Math.max(otherPos, this.cursorPos);
|
| 199 | newMessage = new StringBuilder(message).delete(start, end).toString();
|
| 200 | if (count < 0) {
|
| 201 | this.selectionPos = this.cursorPos = start;
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | this.setMessageFn.accept(newMessage);
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | public void cut() {
|
| 210 | String message = this.getMessageFn.get();
|
| 211 | this.setClipboardFn.accept(this.getSelected(message));
|
| 212 | this.setMessageFn.accept(this.deleteSelection(message));
|
| 213 | }
|
| 214 |
|
| 215 | public void paste() {
|
| 216 | this.insertText(this.getMessageFn.get(), this.getClipboardFn.get());
|
| 217 | this.selectionPos = this.cursorPos;
|
| 218 | }
|
| 219 |
|
| 220 | public void copy() {
|
| 221 | this.setClipboardFn.accept(this.getSelected(this.getMessageFn.get()));
|
| 222 | }
|
| 223 |
|
| 224 | public void selectAll() {
|
| 225 | this.selectionPos = 0;
|
| 226 | this.cursorPos = this.getMessageFn.get().length();
|
| 227 | }
|
| 228 |
|
| 229 | private String getSelected(final String text) {
|
| 230 | int startIndex = Math.min(this.cursorPos, this.selectionPos);
|
| 231 | int endIndex = Math.max(this.cursorPos, this.selectionPos);
|
| 232 | return text.substring(startIndex, endIndex);
|
| 233 | }
|
| 234 |
|
| 235 | private String deleteSelection(final String message) {
|
| 236 | if (this.selectionPos == this.cursorPos) {
|
| 237 | return message;
|
| 238 | }
|
| 239 |
|
| 240 | int startIndex = Math.min(this.cursorPos, this.selectionPos);
|
| 241 | int endIndex = Math.max(this.cursorPos, this.selectionPos);
|
| 242 | String updatedText = message.substring(0, startIndex) + message.substring(endIndex);
|
| 243 | this.selectionPos = this.cursorPos = startIndex;
|
| 244 | return updatedText;
|
| 245 | }
|
| 246 |
|
| 247 | public void setCursorToStart() {
|
| 248 | this.setCursorToStart(false);
|
| 249 | }
|
| 250 |
|
| 251 | public void setCursorToStart(final boolean selecting) {
|
| 252 | this.cursorPos = 0;
|
| 253 | this.resetSelectionIfNeeded(selecting);
|
| 254 | }
|
| 255 |
|
| 256 | public void setCursorToEnd() {
|
| 257 | this.setCursorToEnd(false);
|
| 258 | }
|
| 259 |
|
| 260 | public void setCursorToEnd(final boolean selecting) {
|
| 261 | this.cursorPos = this.getMessageFn.get().length();
|
| 262 | this.resetSelectionIfNeeded(selecting);
|
| 263 | }
|
| 264 |
|
| 265 | public int getCursorPos() {
|
| 266 | return this.cursorPos;
|
| 267 | }
|
| 268 |
|
| 269 | public void setCursorPos(final int value) {
|
| 270 | this.setCursorPos(value, true);
|
| 271 | }
|
| 272 |
|
| 273 | public void setCursorPos(final int value, final boolean selecting) {
|
| 274 | this.cursorPos = this.clampToMsgLength(value);
|
| 275 | this.resetSelectionIfNeeded(selecting);
|
| 276 | }
|
| 277 |
|
| 278 | public int getSelectionPos() {
|
| 279 | return this.selectionPos;
|
| 280 | }
|
| 281 |
|
| 282 | public void setSelectionPos(final int value) {
|
| 283 | this.selectionPos = this.clampToMsgLength(value);
|
| 284 | }
|
| 285 |
|
| 286 | public void setSelectionRange(final int start, final int end) {
|
| 287 | int maxSize = this.getMessageFn.get().length();
|
| 288 | this.cursorPos = Mth.clamp(start, 0, maxSize);
|
| 289 | this.selectionPos = Mth.clamp(end, 0, maxSize);
|
| 290 | }
|
| 291 |
|
| 292 | public boolean isSelecting() {
|
| 293 | return this.cursorPos != this.selectionPos;
|
| 294 | }
|
| 295 |
|
| 296 | @Environment(EnvType.CLIENT)
|
| 297 | public enum CursorStep {
|
| 298 | CHARACTER,
|
| 299 | WORD;
|
| 300 | }
|
| 301 | }
|
| 302 |
|