MultilineTextField .java

net.minecraft.client.gui.components.MultilineTextField (class, 415 lines): members: LINE_SEEK_PIXEL_BIAS, selectWordAtCursor, hasCharacterLimit, lineLimit.

net.minecraft.client.gui.componentsclass415 linesclient12.0 KB

API Surface (29)

int characterLimit()
void setCharacterLimit(final int characterLimit)
void setLineLimit(final int lineLimit)
boolean hasCharacterLimit()
boolean hasLineLimit()
void setValueListener(final Consumer<String> valueListener)
void setCursorListener(final Runnable cursorListener)
void setValue(final String value)
void setValue(final String value, final boolean allowOverflowLineLimit)
String value()
void insertText(final String input)
void deleteText(final int dir)
int cursor()
void setSelecting(final boolean selecting)
MultilineTextField.StringView getSelected()
int getLineCount()
int getLineAtCursor()
MultilineTextField.StringView getLineView(final int lineIndex)
void seekCursor(final Whence whence, final int cursor)
void seekCursorLine(final int lineOffset)
void seekCursorToPoint(final double x, final double y)
void selectWordAtCursor()
boolean keyPressed(final KeyEvent event)
Iterable<MultilineTextField.StringView> iterateLines()
boolean hasSelection()
String getSelectedText()
MultilineTextField.StringView getPreviousWord()
MultilineTextField.StringView getNextWord()
record StringView(int beginIndex, int endIndex)

Fields & Constants (14)

LOGGER
NO_LIMIT
LINE_SEEK_PIXEL_BIAS
font
displayLines
value
cursor
selectCursor
selecting
characterLimit
lineLimit
width
valueListener
cursorListener

String literals (1)

"Character limit cannot be negative"

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

MultilineTextField.java415 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.components;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import com.google.common.collect.Lists;
5
import com.mojang.logging.LogUtils;
6
import java.util.List;
7
import java.util.function.Consumer;
8
import net.fabricmc.api.EnvType;
9
import net.fabricmc.api.Environment;
10
import net.minecraft.client.Minecraft;
11
import net.minecraft.client.gui.Font;
12
import net.minecraft.client.input.KeyEvent;
13
import net.minecraft.network.chat.Style;
14
import net.minecraft.util.Mth;
15
import net.minecraft.util.StringUtil;
16
import org.slf4j.Logger;
17
18
@Environment(EnvType.CLIENT)
19
public class MultilineTextField {
20
	private static final Logger LOGGER = LogUtils.getLogger();
21
	public static final int NO_LIMIT = Integer.MAX_VALUE;
22
	private static final int LINE_SEEK_PIXEL_BIAS = 2;
23
	private final Font font;
24
	private final List<MultilineTextField.StringView> displayLines = Lists.newArrayList();
25
	private String value;
26
	private int cursor;
27
	private int selectCursor;
28
	private boolean selecting;
29
	private int characterLimit = Integer.MAX_VALUE;
30
	private int lineLimit = Integer.MAX_VALUE;
31
	private final int width;
32
	private Consumer<String> valueListener = s -> {};
33
	private Runnable cursorListener = () -> {};
34
35
	public MultilineTextField(final Font font, final int width) {
36
		this.font = font;
37
		this.width = width;
38
		this.setValue("");
39
	}
40
41
	public int characterLimit() {
42
		return this.characterLimit;
43
	}
44
45
	public void setCharacterLimit(final int characterLimit) {
46
		if (characterLimit < 0) {
47
			throw new IllegalArgumentException("Character limit cannot be negative");
48
		}
49
50
		this.characterLimit = characterLimit;
51
	}
52
53
	public void setLineLimit(final int lineLimit) {
54
		if (lineLimit < 0) {
55
			throw new IllegalArgumentException("Character limit cannot be negative");
56
		}
57
58
		this.lineLimit = lineLimit;
59
	}
60
61
	public boolean hasCharacterLimit() {
62
		return this.characterLimit != Integer.MAX_VALUE;
63
	}
64
65
	public boolean hasLineLimit() {
66
		return this.lineLimit != Integer.MAX_VALUE;
67
	}
68
69
	public void setValueListener(final Consumer<String> valueListener) {
70
		this.valueListener = valueListener;
71
	}
72
73
	public void setCursorListener(final Runnable cursorListener) {
74
		this.cursorListener = cursorListener;
75
	}
76
77
	public void setValue(final String value) {
78
		this.setValue(value, false);
79
	}
80
81
	public void setValue(final String value, final boolean allowOverflowLineLimit) {
82
		String newValue = this.truncateFullText(value);
83
		if (allowOverflowLineLimit || !this.overflowsLineLimit(newValue)) {
84
			this.value = newValue;
85
			this.cursor = this.value.length();
86
			this.selectCursor = this.cursor;
87
			this.onValueChange();
88
		}
89
	}
90
91
	public String value() {
92
		return this.value;
93
	}
94
95
	public void insertText(final String input) {
96
		if (!input.isEmpty() || this.hasSelection()) {
97
			String text = this.truncateInsertionText(StringUtil.filterText(input, true));
98
			MultilineTextField.StringView selected = this.getSelected();
99
			String newValue = new StringBuilder(this.value).replace(selected.beginIndex, selected.endIndex, text).toString();
100
			if (!this.overflowsLineLimit(newValue)) {
101
				this.value = newValue;
102
				this.cursor = selected.beginIndex + text.length();
103
				this.selectCursor = this.cursor;
104
				this.onValueChange();
105
			}
106
		}
107
	}
108
109
	public void deleteText(final int dir) {
110
		if (!this.hasSelection()) {
111
			this.selectCursor = Mth.clamp(this.cursor + dir, 0, this.value.length());
112
		}
113
114
		this.insertText("");
115
	}
116
117
	public int cursor() {
118
		return this.cursor;
119
	}
120
121
	public void setSelecting(final boolean selecting) {
122
		this.selecting = selecting;
123
	}
124
125
	public MultilineTextField.StringView getSelected() {
126
		return new MultilineTextField.StringView(Math.min(this.selectCursor, this.cursor), Math.max(this.selectCursor, this.cursor));
127
	}
128
129
	public int getLineCount() {
130
		return this.displayLines.size();
131
	}
132
133
	public int getLineAtCursor() {
134
		for (int i = 0; i < this.displayLines.size(); i++) {
135
			MultilineTextField.StringView view = this.displayLines.get(i);
136
			if (this.cursor >= view.beginIndex && this.cursor <= view.endIndex) {
137
				return i;
138
			}
139
		}
140
141
		return -1;
142
	}
143
144
	public MultilineTextField.StringView getLineView(final int lineIndex) {
145
		return this.displayLines.get(Mth.clamp(lineIndex, 0, this.displayLines.size() - 1));
146
	}
147
148
	public void seekCursor(final Whence whence, final int cursor) {
149
		switch (whence) {
150
			case ABSOLUTE:
151
				this.cursor = cursor;
152
				break;
153
			case RELATIVE:
154
				this.cursor += cursor;
155
				break;
156
			case END:
157
				this.cursor = this.value.length() + cursor;
158
		}
159
160
		this.cursor = Mth.clamp(this.cursor, 0, this.value.length());
161
		this.cursorListener.run();
162
		if (!this.selecting) {
163
			this.selectCursor = this.cursor;
164
		}
165
	}
166
167
	public void seekCursorLine(final int lineOffset) {
168
		if (lineOffset != 0) {
169
			int oldCursorLeft = this.font.width(this.value.substring(this.getCursorLineView().beginIndex, this.cursor)) + 2;
170
			MultilineTextField.StringView lineView = this.getCursorLineView(lineOffset);
171
			int newCursor = this.font.plainSubstrByWidth(this.value.substring(lineView.beginIndex, lineView.endIndex), oldCursorLeft).length();
172
			this.seekCursor(Whence.ABSOLUTE, lineView.beginIndex + newCursor);
173
		}
174
	}
175
176
	public void seekCursorToPoint(final double x, final double y) {
177
		int left = Mth.floor(x);
178
		int top = Mth.floor(y / 9.0);
179
		MultilineTextField.StringView lineView = this.displayLines.get(Mth.clamp(top, 0, this.displayLines.size() - 1));
180
		int clickedColumn = this.font.plainSubstrByWidth(this.value.substring(lineView.beginIndex, lineView.endIndex), left).length();
181
		this.seekCursor(Whence.ABSOLUTE, lineView.beginIndex + clickedColumn);
182
	}
183
184
	public void selectWordAtCursor() {
185
		MultilineTextField.StringView wordView = this.getPreviousWord();
186
		this.seekCursor(Whence.ABSOLUTE, wordView.beginIndex);
187
		this.setSelecting(true);
188
		this.seekCursor(Whence.ABSOLUTE, wordView.endIndex);
189
	}
190
191
	public boolean keyPressed(final KeyEvent event) {
192
		this.selecting = event.hasShiftDown();
193
		if (event.isSelectAll()) {
194
			this.cursor = this.value.length();
195
			this.selectCursor = 0;
196
			return true;
197
		}
198
199
		if (event.isCopy()) {
200
			Minecraft.getInstance().keyboardHandler.setClipboard(this.getSelectedText());
201
			return true;
202
		}
203
204
		if (event.isPaste()) {
205
			this.insertText(Minecraft.getInstance().keyboardHandler.getClipboard());
206
			return true;
207
		}
208
209
		if (event.isCut()) {
210
			Minecraft.getInstance().keyboardHandler.setClipboard(this.getSelectedText());
211
			this.insertText("");
212
			return true;
213
		}
214
215
		switch (event.shortcutKey()) {
216
			case 8:
217
				if (event.hasControlDownWithQuirk()) {
218
					MultilineTextField.StringView wordView = this.getPreviousWord();
219
					this.deleteText(wordView.beginIndex - this.cursor);
220
				} else {
221
					this.deleteText(-1);
222
				}
223
224
				return true;
225
			case 13:
226
			case 1073741912:
227
				this.insertText("\n");
228
				return true;
229
			case 127:
230
				if (event.hasControlDownWithQuirk()) {
231
					MultilineTextField.StringView wordView = this.getNextWord();
232
					this.deleteText(wordView.beginIndex - this.cursor);
233
				} else {
234
					this.deleteText(1);
235
				}
236
237
				return true;
238
			case 1073741898:
239
				if (event.hasControlDownWithQuirk()) {
240
					this.seekCursor(Whence.ABSOLUTE, 0);
241
				} else {
242
					this.seekCursor(Whence.ABSOLUTE, this.getCursorLineView().beginIndex);
243
				}
244
245
				return true;
246
			case 1073741899:
247
				this.seekCursor(Whence.ABSOLUTE, 0);
248
				return true;
249
			case 1073741901:
250
				if (event.hasControlDownWithQuirk()) {
251
					this.seekCursor(Whence.END, 0);
252
				} else {
253
					this.seekCursor(Whence.ABSOLUTE, this.getCursorLineView().endIndex);
254
				}
255
256
				return true;
257
			case 1073741902:
258
				this.seekCursor(Whence.END, 0);
259
				return true;
260
			case 1073741903:
261
				if (event.hasControlDownWithQuirk()) {
262
					MultilineTextField.StringView wordView = this.getNextWord();
263
					this.seekCursor(Whence.ABSOLUTE, wordView.beginIndex);
264
				} else {
265
					this.seekCursor(Whence.RELATIVE, 1);
266
				}
267
268
				return true;
269
			case 1073741904:
270
				if (event.hasControlDownWithQuirk()) {
271
					MultilineTextField.StringView wordView = this.getPreviousWord();
272
					this.seekCursor(Whence.ABSOLUTE, wordView.beginIndex);
273
				} else {
274
					this.seekCursor(Whence.RELATIVE, -1);
275
				}
276
277
				return true;
278
			case 1073741905:
279
				if (!event.hasControlDownWithQuirk()) {
280
					this.seekCursorLine(1);
281
				}
282
283
				return true;
284
			case 1073741906:
285
				if (!event.hasControlDownWithQuirk()) {
286
					this.seekCursorLine(-1);
287
				}
288
289
				return true;
290
			default:
291
				return false;
292
		}
293
	}
294
295
	public Iterable<MultilineTextField.StringView> iterateLines() {
296
		return this.displayLines;
297
	}
298
299
	public boolean hasSelection() {
300
		return this.selectCursor != this.cursor;
301
	}
302
303
	@VisibleForTesting
304
	public String getSelectedText() {
305
		MultilineTextField.StringView selected = this.getSelected();
306
		return this.value.substring(selected.beginIndex, selected.endIndex);
307
	}
308
309
	private MultilineTextField.StringView getCursorLineView() {
310
		return this.getCursorLineView(0);
311
	}
312
313
	private MultilineTextField.StringView getCursorLineView(final int lineOffset) {
314
		int lineIndex = this.getLineAtCursor();
315
		if (lineIndex < 0) {
316
			LOGGER.error("Cursor is not within text (cursor = {}, length = {})", this.cursor, this.value.length());
317
			return this.displayLines.getLast();
318
		} else {
319
			return this.displayLines.get(Mth.clamp(lineIndex + lineOffset, 0, this.displayLines.size() - 1));
320
		}
321
	}
322
323
	@VisibleForTesting
324
	public MultilineTextField.StringView getPreviousWord() {
325
		if (this.value.isEmpty()) {
326
			return MultilineTextField.StringView.EMPTY;
327
		}
328
329
		int startPosition = Mth.clamp(this.cursor, 0, this.value.length() - 1);
330
331
		while (startPosition > 0 && Character.isWhitespace(this.value.charAt(startPosition - 1))) {
332
			startPosition--;
333
		}
334
335
		while (startPosition > 0 && !Character.isWhitespace(this.value.charAt(startPosition - 1))) {
336
			startPosition--;
337
		}
338
339
		return new MultilineTextField.StringView(startPosition, this.getWordEndPosition(startPosition));
340
	}
341
342
	@VisibleForTesting
343
	public MultilineTextField.StringView getNextWord() {
344
		if (this.value.isEmpty()) {
345
			return MultilineTextField.StringView.EMPTY;
346
		}
347
348
		int startPosition = Mth.clamp(this.cursor, 0, this.value.length() - 1);
349
350
		while (startPosition < this.value.length() && !Character.isWhitespace(this.value.charAt(startPosition))) {
351
			startPosition++;
352
		}
353
354
		while (startPosition < this.value.length() && Character.isWhitespace(this.value.charAt(startPosition))) {
355
			startPosition++;
356
		}
357
358
		return new MultilineTextField.StringView(startPosition, this.getWordEndPosition(startPosition));
359
	}
360
361
	private int getWordEndPosition(final int from) {
362
		int end = from;
363
364
		while (end < this.value.length() && !Character.isWhitespace(this.value.charAt(end))) {
365
			end++;
366
		}
367
368
		return end;
369
	}
370
371
	private void onValueChange() {
372
		this.reflowDisplayLines();
373
		this.valueListener.accept(this.value);
374
		this.cursorListener.run();
375
	}
376
377
	private void reflowDisplayLines() {
378
		this.displayLines.clear();
379
		if (this.value.isEmpty()) {
380
			this.displayLines.add(MultilineTextField.StringView.EMPTY);
381
		} else {
382
			this.font
383
				.getSplitter()
384
				.splitLines(this.value, this.width, Style.EMPTY, false, (style, start, end) -> this.displayLines.add(new MultilineTextField.StringView(start, end)));
385
			if (this.value.charAt(this.value.length() - 1) == '\n') {
386
				this.displayLines.add(new MultilineTextField.StringView(this.value.length(), this.value.length()));
387
			}
388
		}
389
	}
390
391
	private String truncateFullText(final String input) {
392
		return this.hasCharacterLimit() ? StringUtil.truncateStringIfNecessary(input, this.characterLimit, false) : input;
393
	}
394
395
	private String truncateInsertionText(final String input) {
396
		String truncatedInput = input;
397
		if (this.hasCharacterLimit()) {
398
			int remainingCharacters = this.characterLimit - this.value.length();
399
			truncatedInput = StringUtil.truncateStringIfNecessary(input, remainingCharacters, false);
400
		}
401
402
		return truncatedInput;
403
	}
404
405
	private boolean overflowsLineLimit(final String newValue) {
406
		return this.hasLineLimit()
407
			&& this.font.getSplitter().splitLines(newValue, this.width, Style.EMPTY).size() + (StringUtil.endsWithNewLine(newValue) ? 1 : 0) > this.lineLimit;
408
	}
409
410
	@Environment(EnvType.CLIENT)
411
	protected record StringView(int beginIndex, int endIndex) {
412
		private static final MultilineTextField.StringView EMPTY = new MultilineTextField.StringView(0, 0);
413
	}
414
}
415

Decompiled Minecraft 26.3 source of MultilineTextField (net/minecraft/client/gui/components/MultilineTextField.java) with Mojang official mappings, stored verbatim on the site.