Skip to content
Merged
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 @@ -11,7 +11,9 @@ public class WLongEdit extends WHorizontalList {
private long value;

public final long min, max;

public Runnable action;
public Runnable actionOnRelease;

private WTextBox textBox;

Expand All @@ -23,28 +25,59 @@ public WLongEdit(long value, long min, long max) {

@Override
public void init() {
textBox = add(theme.textBox(Long.toString(value), (text, c) -> Character.isDigit(c) || c == '-' && !text.contains("-"))).minWidth(75).expandX().widget();
textBox = add(theme.textBox(Long.toString(value), this::filter)).minWidth(75).expandX().widget();

textBox.actionOnUnfocused = () -> {
long lastValue = value;

try {
long parsed = Long.parseLong(textBox.get().trim());
if (parsed >= min && parsed <= max) value = parsed;
} catch (NumberFormatException _) {}
if (textBox.get().isEmpty() || textBox.get().equals("-")) {
value = Math.clamp(0L, min, max);
} else {
try {
long parsed = Long.parseLong(textBox.get().trim());
value = Math.clamp(parsed, min, max);
} catch (NumberFormatException _) {
// Retain current value if input cannot be parsed as a long
}
}

textBox.set(Long.toString(value));

if (action != null && value != lastValue) action.run();
if (value != lastValue) {
if (action != null) action.run();
if (actionOnRelease != null) actionOnRelease.run();
}
};
}

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

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

if (good && validate) {
try {
Long.parseLong(text + c);
} catch (NumberFormatException _) {
good = false;
}
}

return good;
}

public long get() {
return value;
}

public void set(long value) {
this.value = value;
textBox.set(Long.toString(value));
textBox.set(Long.toString(this.value));
}
}
Loading