From a2d8b2de90b76c081d55e82853a0ec67f7c868d9 Mon Sep 17 00:00:00 2001 From: Rasmion <42215949+Rasmion@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:08:01 +0200 Subject: Bug fixes, quality of life changes, visual changes (#108) --- .../yacl3/gui/controllers/BooleanController.java | 2 + .../controllers/cycling/CyclingListController.java | 2 + .../string/StringControllerElement.java | 73 +++++++++++++++------- .../string/number/DoubleFieldController.java | 4 +- .../string/number/FloatFieldController.java | 4 +- .../string/number/IntegerFieldController.java | 9 +-- .../string/number/LongFieldController.java | 9 +-- .../string/number/NumberFieldController.java | 24 ++++--- 8 files changed, 84 insertions(+), 43 deletions(-) (limited to 'common/src/main') diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/BooleanController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/BooleanController.java index 6d2c2b3..cbd6ba5 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/BooleanController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/BooleanController.java @@ -11,6 +11,7 @@ import net.minecraft.ChatFormatting; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.ApiStatus; import java.util.function.Function; @@ -72,6 +73,7 @@ public class BooleanController implements Controller { this.coloured = coloured; } + @ApiStatus.Internal public static BooleanController createInternal(Option option, ValueFormatter formatter, boolean coloured) { return new BooleanController(option, formatter::format, coloured); } diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingListController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingListController.java index 28bc623..3fce3cf 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingListController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/cycling/CyclingListController.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableList; import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.ValueFormatter; import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.ApiStatus; import java.util.function.Function; @@ -38,6 +39,7 @@ public class CyclingListController implements ICyclingController { this.values = ImmutableList.copyOf(values); } + @ApiStatus.Internal public static CyclingListController createInternal(Option option, Iterable values, ValueFormatter formatter) { return new CyclingListController<>(option, values, formatter::format); } diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/StringControllerElement.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/StringControllerElement.java index 383e188..689d8e2 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/StringControllerElement.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/StringControllerElement.java @@ -21,12 +21,14 @@ public class StringControllerElement extends ControllerWidget text.length()) caretPos = text.length(); - int caretX = textX + textRenderer.width(text.substring(0, caretPos)) - 1; + int caretX = textX + textRenderer.width(text.substring(0, caretPos)); if (text.isEmpty()) caretX = inputFieldBounds.x() + inputFieldBounds.width() / 2; - if (ticks % 20 <= 10) { - graphics.fill(caretX, inputFieldBounds.y(), caretX + 1, inputFieldBounds.yLimit(), -1); - } - if (selectionLength != 0) { int selectionX = textX + textRenderer.width(text.substring(0, caretPos + selectionLength)); - graphics.fill(caretX, inputFieldBounds.y() - 1, selectionX, inputFieldBounds.yLimit(), 0x803030FF); + graphics.fill(caretX, inputFieldBounds.y() - 2, selectionX, inputFieldBounds.yLimit() - 1, 0x803030FF); } + + if(caretPos != previousCaretPos) { + previousCaretPos = caretPos; + caretTicks = 0; + } + + if ((caretTicks += delta) % 20 <= 10) + graphics.fill(caretX, inputFieldBounds.y() - 2, caretX + 1, inputFieldBounds.yLimit() - 1, -1); } } graphics.disableScissor(); @@ -119,7 +125,7 @@ public class StringControllerElement extends ControllerWidget 0) { - if (selectionLength != 0) - caretPos += Math.min(selectionLength, 0); - else - caretPos--; + if (Screen.hasControlDown()) { + caretPos = findSpaceIndex(true); + } else { + if (selectionLength != 0) { + caretPos += Math.min(selectionLength, 0); + } else caretPos--; + } } checkRenderOffset(); selectionLength = 0; @@ -176,10 +185,13 @@ public class StringControllerElement extends ControllerWidget { + if (Screen.hasShiftDown()) { + selectionLength -= inputField.length() - caretPos; + } else selectionLength = 0; + caretPos = inputField.length(); + checkRenderOffset(); + return true; + } + case InputConstants.KEY_HOME -> { + if (Screen.hasShiftDown()) { + selectionLength += caretPos; + caretPos = 0; + } else { + caretPos = 0; + selectionLength = 0; + } + checkRenderOffset(); + return true; + } // case InputConstants.KEY_Z -> { // if (Screen.hasControlDown()) { // UndoRedoHelper.FieldState updated = Screen.hasShiftDown() ? undoRedoHelper.redo() : undoRedoHelper.undo(); @@ -262,7 +293,7 @@ public class StringControllerElement extends ControllerWidget 0) - fromIndex -= 1; - i = this.inputField.lastIndexOf(" ", fromIndex); - - if (i == -1) i = 0; + fromIndex -= 2; + i = this.inputField.lastIndexOf(" ", fromIndex) + 1; } else { if (caretPos < inputField.length()) fromIndex += 1; - i = this.inputField.indexOf(" ", fromIndex); + i = this.inputField.indexOf(" ", fromIndex) + 1; - if (i == -1) i = inputField.length(); + if (i == 0) i = inputField.length(); } return i; diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/DoubleFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/DoubleFieldController.java index 8445839..1fe3e41 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/DoubleFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/DoubleFieldController.java @@ -4,6 +4,7 @@ import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.DoubleSliderController; import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.ApiStatus; import java.util.function.Function; @@ -63,6 +64,7 @@ public class DoubleFieldController extends NumberFieldController { this(option, -Double.MAX_VALUE, Double.MAX_VALUE, DoubleSliderController.DEFAULT_FORMATTER); } + @ApiStatus.Internal public static DoubleFieldController createInternal(Option option, double min, double max, ValueFormatter formatter) { return new DoubleFieldController(option, min, max, formatter::format); } @@ -88,7 +90,7 @@ public class DoubleFieldController extends NumberFieldController { */ @Override public String getString() { - return String.valueOf(option().pendingValue()); + return NUMBER_FORMAT.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/FloatFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/FloatFieldController.java index 78281c8..8c81b49 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/FloatFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/FloatFieldController.java @@ -4,6 +4,7 @@ import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.FloatSliderController; import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.ApiStatus; import java.util.function.Function; @@ -63,6 +64,7 @@ public class FloatFieldController extends NumberFieldController { this(option, -Float.MAX_VALUE, Float.MAX_VALUE, FloatSliderController.DEFAULT_FORMATTER); } + @ApiStatus.Internal public static FloatFieldController createInternal(Option option, float min, float max, ValueFormatter formatter) { return new FloatFieldController(option, min, max, formatter::format); } @@ -88,7 +90,7 @@ public class FloatFieldController extends NumberFieldController { */ @Override public String getString() { - return String.valueOf(option().pendingValue()); + return NUMBER_FORMAT.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/IntegerFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/IntegerFieldController.java index 476da9c..6286978 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/IntegerFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/IntegerFieldController.java @@ -4,6 +4,7 @@ import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.IntegerSliderController; import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.ApiStatus; import java.util.function.Function; @@ -63,15 +64,11 @@ public class IntegerFieldController extends NumberFieldController { this(option, -Integer.MAX_VALUE, Integer.MAX_VALUE, IntegerSliderController.DEFAULT_FORMATTER); } + @ApiStatus.Internal public static IntegerFieldController createInternal(Option option, int min, int max, ValueFormatter formatter) { return new IntegerFieldController(option, min, max, formatter::format); } - @Override - public boolean isInputValid(String input) { - return input.matches("(?:-?\\d+|)"); - } - /** * {@inheritDoc} */ @@ -93,7 +90,7 @@ public class IntegerFieldController extends NumberFieldController { */ @Override public String getString() { - return String.valueOf(option().pendingValue()); + return NUMBER_FORMAT.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/LongFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/LongFieldController.java index d6f4f19..906a2b5 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/LongFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/LongFieldController.java @@ -4,6 +4,7 @@ import dev.isxander.yacl3.api.Option; import dev.isxander.yacl3.api.controller.ValueFormatter; import dev.isxander.yacl3.gui.controllers.slider.LongSliderController; import net.minecraft.network.chat.Component; +import org.jetbrains.annotations.ApiStatus; import java.util.function.Function; @@ -63,15 +64,11 @@ public class LongFieldController extends NumberFieldController { this(option, -Long.MAX_VALUE, Long.MAX_VALUE, LongSliderController.DEFAULT_FORMATTER); } + @ApiStatus.Internal public static LongFieldController createInternal(Option option, long min, long max, ValueFormatter formatter) { return new LongFieldController(option, min, max, formatter::format); } - @Override - public boolean isInputValid(String input) { - return input.matches("(?:-?\\d+|)"); - } - /** * {@inheritDoc} */ @@ -93,7 +90,7 @@ public class LongFieldController extends NumberFieldController { */ @Override public String getString() { - return String.valueOf(option().pendingValue()); + return NUMBER_FORMAT.format(option().pendingValue()); } /** diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java index b164ea3..3c06876 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/string/number/NumberFieldController.java @@ -8,10 +8,14 @@ import dev.isxander.yacl3.gui.YACLScreen; import dev.isxander.yacl3.gui.controllers.slider.ISliderController; import dev.isxander.yacl3.gui.controllers.string.IStringController; import dev.isxander.yacl3.gui.controllers.string.StringControllerElement; +import dev.isxander.yacl3.impl.utils.YACLConstants; import net.minecraft.network.chat.Component; import net.minecraft.util.Mth; import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.text.ParseException; +import java.text.ParsePosition; import java.util.function.Function; /** @@ -20,6 +24,10 @@ import java.util.function.Function; * @param number type */ public abstract class NumberFieldController implements ISliderController, IStringController { + + protected static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance(); + private static final DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS = DecimalFormatSymbols.getInstance(); + private final Option option; private final ValueFormatter displayFormatter; @@ -35,8 +43,11 @@ public abstract class NumberFieldController implements ISlider @Override public void setFromString(String value) { - if (value.isEmpty() || value.equals(".") || value.equals("-")) value = "0"; - setPendingValue(Mth.clamp(Double.parseDouble(cleanupNumberString(value)), min(), max())); + try { + setPendingValue(Mth.clamp(NUMBER_FORMAT.parse(value).doubleValue(), min(), max())); + } catch (ParseException ignore) { + YACLConstants.LOGGER.warn("Failed to parse number: {}", value); + } } @Override @@ -46,7 +57,10 @@ public abstract class NumberFieldController implements ISlider @Override public boolean isInputValid(String input) { - return input.matches("[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)|[.]||-"); + input = input.replace(DECIMAL_FORMAT_SYMBOLS.getGroupingSeparator() + "", ""); + ParsePosition parsePosition = new ParsePosition(0); + NUMBER_FORMAT.parse(input, parsePosition); + return parsePosition.getIndex() == input.length(); } @Override @@ -59,10 +73,6 @@ public abstract class NumberFieldController implements ISlider return new StringControllerElement(this, screen, widgetDimension, false); } - protected String cleanupNumberString(String number) { - return number.replace(String.valueOf(DecimalFormatSymbols.getInstance().getGroupingSeparator()), ""); - } - @Override public double interval() { return -1; -- cgit