From eac29444deae2a148262348a4d502c2277e64e7a Mon Sep 17 00:00:00 2001 From: isXander Date: Tue, 22 Oct 2024 18:29:43 +0100 Subject: Fix #214 --- .../controllers/string/number/NumberFieldController.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java b/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java index 3c06876..8e1a399 100644 --- a/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java +++ b/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java @@ -44,7 +44,8 @@ public abstract class NumberFieldController implements ISlider @Override public void setFromString(String value) { try { - setPendingValue(Mth.clamp(NUMBER_FORMAT.parse(value).doubleValue(), min(), max())); + String transformed = transformInput(value); + setPendingValue(Mth.clamp(NUMBER_FORMAT.parse(transformed).doubleValue(), min(), max())); } catch (ParseException ignore) { YACLConstants.LOGGER.warn("Failed to parse number: {}", value); } @@ -57,7 +58,8 @@ public abstract class NumberFieldController implements ISlider @Override public boolean isInputValid(String input) { - input = input.replace(DECIMAL_FORMAT_SYMBOLS.getGroupingSeparator() + "", ""); + input = transformInput(input); + ParsePosition parsePosition = new ParsePosition(0); NUMBER_FORMAT.parse(input, parsePosition); return parsePosition.getIndex() == input.length(); @@ -77,4 +79,11 @@ public abstract class NumberFieldController implements ISlider public double interval() { return -1; } + + protected String transformInput(String input) { + if (input.isEmpty()) input = "0"; + if (input.equals("-")) input = "-0"; + + return input.replace(DECIMAL_FORMAT_SYMBOLS.getGroupingSeparator() + "", ""); + } } -- cgit