TextFieldWidgetHelper
com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper
Wraps the game's text field with a text-change callback.
The callback receives accepted new text and is attached when the field is built.
Scripted setText(String) changes invoke it when accepted.
js
const screen = Hud.createScreen("a screen", true);
screen.setOnInit(JavaWrapper.methodToJava(function (self) {
const field = self.textFieldBuilder()
.pos(10, 20)
.size(120, 14)
.message("type a name")
.action(JavaWrapper.methodToJava(function (text) {
// the callback is given the text, not the field
Chat.log(`so far: ${text}`);
}))
.build();
// Accepted scripted changes invoke the callback too.
field.setText("hello");
// On 26.1+, rejected text restores the last accepted value, cursor, and selection.
field.setTextPredicate(JavaWrapper.methodToJava(function (text) {
return 8 >= String(text).length;
}));
field.setText("far too long to be allowed");
Chat.log(`the field still says ${field.getText()}`);
}));
Hud.openScreen(screen);Available in scripts as Widgets.TextField.
Nested types
Constructors
new TextFieldWidgetHelper
Wraps an existing text field with a helper whose drawing-order index is zero.
Parameters:
t: the text field to wrap
Methods
getMaxLength
Returns the maximum number of UTF-16 code units allowed in the field; a limit of zero allows no text.
Returns: the maximum number of UTF-16 code units allowed in this text field.
getSelectedText
getText
isEditable
Returns:
true if the text field is editable, false otherwise.resetTextPredicate
Removes the text filter, allowing all non-null text. Existing text is retained.
Returns: self for chaining.
setCursorPosition
Moves the cursor to a character offset and drops any selection; equivalent to calling setCursorPosition(int, boolean) with
false.Parameters:
position: the cursor position
Returns: self for chaining.
Moves the cursor to a character offset. With
shift enabled, the selection is extended or shrunk to that position; otherwise it is dropped.Parameters:
position: the cursor positionshift: whether to keep the selection and extend it to the new position
Returns: self for chaining.
setCursorToEnd
Moves the cursor to the end of the text and drops the selection.
Returns: self for chaining.
Moves the cursor to the end of the text. With
shift enabled, the existing selection is extended or shrunk to the end; otherwise it is dropped.Parameters:
shift: whether to keep the selection and extend it to the new position
Returns: self for chaining.
setCursorToStart
Moves the cursor to the start of the text and drops the selection.
Returns: self for chaining.
Moves the cursor to the start of the text. With
shift enabled, the existing selection is extended or shrunk to the start; otherwise it is dropped.Parameters:
shift: whether to keep the selection and extend it to the new position
Returns: self for chaining.
setEditable
Sets whether the field accepts text input. When false, it uses the uneditable text color.
Parameters:
edit: whether the field should be editable
Returns: self for chaining.
setEditableColor
Sets the color used to draw text while the field is editable.
Parameters:
color: the packed rgb colour to draw editable text in
Returns: self for chaining.
setMaxLength
Sets the maximum number of UTF-16 code units allowed. If the current text exceeds the new limit, it is truncated immediately; a limit of zero allows no text.
Parameters:
length: the new maximum number of UTF-16 code units
Returns: self for chaining.
setSelection
Selects text between two character offsets:
start is the cursor position and end is the other selection endpoint. Offsets are passed to the game without validation.Parameters:
start: the offset of the cursorend: the offset of the other end of the selection
Returns: self for chaining.
setSuggestion
Sets the hint drawn in the field while it is empty; this does not change the text returned by getText().
Parameters:
suggestion: the suggestion to set
Returns: self for chaining.
setText
Sets the field's text and waits for the change to be applied. Any configured text predicate applies, and the callback runs if the text is accepted.
Parameters:
text: the text to put in the field
Returns: self for chaining.
Sets the field's text, optionally waiting for the change to be applied. When called from the client thread there is nothing to hand off and nothing to wait for, so the
await flag makes no difference there. Off the client thread the change is queued onto it, and the flag decides whether this call blocks until its own task has run. Blocking also waits out whatever else was queued ahead of it. Any configured text predicate applies to scripted changes; the callback runs for accepted changes.Parameters:
text: the text to put in the fieldawait: whether to wait for the change to be applied
Returns: self for chaining.
setTextPredicate
Sets a predicate over the complete proposed text. On 26.1+, rejected input restores the last accepted text, cursor, and selection without running the change callback; this is enforced by the responder because
EditBox has no filter. Earlier versions use the field's native filter.Parameters:
predicate: the text filter
Returns: self for chaining.
setUneditableColor
Sets the color used to draw text while the field is not editable.
Parameters:
color: the packed rgb colour to draw uneditable text in
Returns: self for chaining.