Skip to content
Open
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 @@ -91,21 +91,27 @@ public void init() {
}
}

private String insertAtCursor(String text, char c) {
int cursor = Math.min(textBox.selectionStart, textBox.selectionEnd);
cursor = Math.clamp(cursor, 0, text.length());
return text.substring(0, cursor) + c + text.substring(cursor);
}

private boolean filter(String text, char c) {
boolean good;
boolean validate = true;

if (c == '-' && !text.contains("-") && textBox.cursor == 0) {
if (c == '-' && !text.contains("-") && Math.min(textBox.selectionStart, textBox.selectionEnd) == 0) {
good = true;
validate = false;
} else if (c == '.' && !text.contains(".")) {
good = true;
if (text.isEmpty()) validate = false;
if (text.isEmpty() || text.equals("-")) validate = false;
} else good = Character.isDigit(c);

if (good && validate) {
try {
Double.parseDouble(text + c);
Double.parseDouble(insertAtCursor(text, c));
} catch (NumberFormatException _) {
good = false;
}
Expand Down Expand Up @@ -143,4 +149,4 @@ public void set(double value) {
private String valueString() {
return String.format(Locale.US, "%." + decimalPlaces + "f", value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,25 @@ public void init() {
}
}

private String insertAtCursor(String text, char c) {
int cursor = Math.min(textBox.selectionStart, textBox.selectionEnd);
cursor = Math.clamp(cursor, 0, text.length());
return text.substring(0, cursor) + c + text.substring(cursor);
}

private boolean filter(String text, char c) {
boolean good;
boolean validate = true;

if (c == '-' && !text.contains("-") && textBox.cursor == 0) {
if (c == '-' && !text.contains("-") && Math.min(textBox.selectionStart, textBox.selectionEnd) == 0) {
good = true;
validate = false;
}
else good = Character.isDigit(c);

if (good && validate) {
try {
Integer.parseInt(text + c);
Integer.parseInt(insertAtCursor(text, c));
} catch (NumberFormatException _) {
good = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ public void init() {
};
}

private String insertAtCursor(String text, char c) {
int cursor = Math.min(textBox.selectionStart, textBox.selectionEnd);
cursor = Math.clamp(cursor, 0, text.length());
return text.substring(0, cursor) + c + text.substring(cursor);
}

private boolean filter(String text, char c) {
boolean good;
boolean validate = true;

if (c == '-' && !text.contains("-") && textBox.cursor == 0) {
if (c == '-' && !text.contains("-") && Math.min(textBox.selectionStart, textBox.selectionEnd) == 0) {
good = true;
validate = false;
} else {
Expand All @@ -63,7 +69,7 @@ private boolean filter(String text, char c) {

if (good && validate) {
try {
Long.parseLong(text + c);
Long.parseLong(insertAtCursor(text, c));
} catch (NumberFormatException _) {
good = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,19 +559,28 @@ private int getSelectedCompletion() {
public boolean onCharTyped(CharacterEvent input) {
if (!focused) return false;

if (filter.filter(text, input.codepoint())) {
clearSelection();
int start = Math.min(selectionStart, selectionEnd);
int end = Math.max(selectionStart, selectionEnd);

text = text.substring(0, cursor) + input.codepointAsString() + text.substring(cursor);
String newText = text;
if (start != end) {
newText = text.substring(0, start) + text.substring(end);
}

cursor++;
resetSelection();
if (!filter.filter(newText, input.codepoint())) return false;

runAction();
return true;
}
String inserted = input.codepointAsString();

return false;
text = newText.substring(0, start)
+ inserted
+ newText.substring(start);

cursor = start + inserted.length();

resetSelection();
runAction();

return true;
}

@Override
Expand Down
Loading