diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java index 5829eec441..b1f0b28310 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java @@ -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; @@ -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)); } }