diff --git a/src/main/java/meteordevelopment/meteorclient/gui/DefaultSettingsWidgetFactory.java b/src/main/java/meteordevelopment/meteorclient/gui/DefaultSettingsWidgetFactory.java index 80a2b21ba76..987c3517cbd 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/DefaultSettingsWidgetFactory.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/DefaultSettingsWidgetFactory.java @@ -180,13 +180,13 @@ private void intW(WTable table, IntSetting setting) { } private void longW(WTable table, LongSetting setting) { - WTextBox textBox = table.add(theme.textBox(setting.get().toString(), (text, c) -> Character.isDigit(c) || c == '-' && !text.contains("-"))).expandX().widget(); - textBox.actionOnUnfocused = () -> { - setting.parse(textBox.get()); - textBox.set(setting.get().toString()); + WLongEdit edit = table.add(theme.longEdit(setting.get(), setting.min, setting.max)).expandX().widget(); + + edit.action = () -> { + if (!setting.set(edit.get())) edit.set(setting.get()); }; - reset(table, setting, () -> textBox.set(setting.get().toString())); + reset(table, setting, () -> edit.set(setting.get())); } private void doubleW(WTable table, DoubleSetting setting) { diff --git a/src/main/java/meteordevelopment/meteorclient/gui/GuiTheme.java b/src/main/java/meteordevelopment/meteorclient/gui/GuiTheme.java index 73c8e4d4e11..2eeeafb56a1 100644 --- a/src/main/java/meteordevelopment/meteorclient/gui/GuiTheme.java +++ b/src/main/java/meteordevelopment/meteorclient/gui/GuiTheme.java @@ -217,6 +217,10 @@ public WIntEdit intEdit(int value, int min, int max, boolean noSlider) { return w(new WIntEdit(value, min, max, 0, 0, noSlider)); } + public WLongEdit longEdit(long value, long min, long max) { + return w(new WLongEdit(value, min, max)); + } + public WDoubleEdit doubleEdit(double value, double min, double max, double sliderMin, double sliderMax, int decimalPlaces, boolean noSlider) { return w(new WDoubleEdit(value, min, max, sliderMin, sliderMax, decimalPlaces, noSlider)); } diff --git a/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java new file mode 100644 index 00000000000..5829eec441f --- /dev/null +++ b/src/main/java/meteordevelopment/meteorclient/gui/widgets/input/WLongEdit.java @@ -0,0 +1,50 @@ +/* + * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client). + * Copyright (c) Meteor Development. + */ + +package meteordevelopment.meteorclient.gui.widgets.input; + +import meteordevelopment.meteorclient.gui.widgets.containers.WHorizontalList; + +public class WLongEdit extends WHorizontalList { + private long value; + + public final long min, max; + public Runnable action; + + private WTextBox textBox; + + public WLongEdit(long value, long min, long max) { + this.value = value; + this.min = min; + this.max = 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.actionOnUnfocused = () -> { + long lastValue = value; + + try { + long parsed = Long.parseLong(textBox.get().trim()); + if (parsed >= min && parsed <= max) value = parsed; + } catch (NumberFormatException _) {} + + textBox.set(Long.toString(value)); + + if (action != null && value != lastValue) action.run(); + }; + } + + public long get() { + return value; + } + + public void set(long value) { + this.value = value; + textBox.set(Long.toString(value)); + } +}