Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

Expand All @@ -24,8 +26,8 @@ public class TextFieldHandler {

private static final Joiner JOINER = Joiner.on('\n');

@Getter
private final List<String> text = new ArrayList<>();
private final List<String> textView = Collections.unmodifiableList(this.text);
private final Point cursor = new Point(), cursorEnd = new Point();
private final BaseTextFieldWidget<?> textFieldWidget;
@Setter
Expand Down Expand Up @@ -135,6 +137,51 @@ public void setCursor(Point cursor, boolean animate) {
setOffsetCursor(cursor);
}

/**
* @return an unmodifiable view of the lines of text. Use {@link #setText(Collection)}, {@link #setText(String)} or
* {@link #setLine(int, String)} to change the text.
*/
public List<String> getText() {
return this.textView;
}

public void setText(Collection<String> lines) {
this.text.clear();
this.text.addAll(lines);
clampCursors();
}

public void setText(String text) {
setText(Arrays.asList(text.split("\n", -1)));
}

public void setLine(int line, String text) {
if (line == this.text.size()) {
this.text.add(text);
} else {
this.text.set(line, text);
}
clampCursors();
}

/**
* Clamps both cursors to the current text.
*/
public void clampCursors() {
clampCursor(this.cursor);
clampCursor(this.cursorEnd);
}

private void clampCursor(Point point) {
if (this.text.isEmpty()) {
point.set(0, 0);
return;
}
int y = Math.max(0, Math.min(point.y, this.text.size() - 1));
int x = Math.max(0, Math.min(point.x, this.text.get(y).length()));
point.set(x, y);
}

public void putMainCursorAtStart() {
if (hasTextMarked() && getMainCursor() != getStartCursor()) {
switchCursors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,16 @@ public String getText() {
}

public void setText(@NotNull String text) {
if (this.handler.getText().isEmpty()) {
this.handler.getText().add(text);
} else {
this.handler.getText().set(0, text);
}
this.handler.setLine(0, text);
}

@Override
public void onRemoveFocus(ModularGuiContext context) {
super.onRemoveFocus(context);
if (this.handler.getText().isEmpty()) {
this.handler.getText().add(this.validator.apply(""));
this.handler.setLine(0, this.validator.apply(""));
} else if (this.handler.getText().size() == 1) {
this.handler.getText().set(0, this.validator.apply(this.handler.getText().get(0)));
this.handler.setLine(0, this.validator.apply(this.handler.getText().get(0)));
markTooltipDirty();
} else {
throw new IllegalStateException("TextFieldWidget can only have one line!");
Expand Down
Loading