From 06ab48a8da5a9d01864123739104d7f90834a731 Mon Sep 17 00:00:00 2001 From: chyzman <32403637+chyzman@users.noreply.github.com> Date: Mon, 23 Sep 2024 19:34:53 -0400 Subject: [PATCH] made range constraint better (but its unfinished) --- .../config/annotation/RangeConstraint.java | 21 +++++++++++++-- .../owo/config/ui/OptionComponentFactory.java | 22 +++++++++------- .../owo/config/ui/OptionComponents.java | 8 ++++-- .../wispforest/uwu/config/UwuConfigModel.java | 25 ++++++++++++++++-- .../resources/assets/uwu/lang/en_us.json | 26 ++++++++++++++++--- src/testmod/resources/fabric.mod.json | 5 +++- 6 files changed, 87 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/wispforest/owo/config/annotation/RangeConstraint.java b/src/main/java/io/wispforest/owo/config/annotation/RangeConstraint.java index b9839cb7..c967a700 100644 --- a/src/main/java/io/wispforest/owo/config/annotation/RangeConstraint.java +++ b/src/main/java/io/wispforest/owo/config/annotation/RangeConstraint.java @@ -1,5 +1,7 @@ package io.wispforest.owo.config.annotation; +import net.fabricmc.fabric.api.util.TriState; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -12,13 +14,28 @@ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface RangeConstraint { - double min(); + double min() default -Double.MAX_VALUE; - double max(); + double max() default Double.MAX_VALUE; /** * @return How many decimals places to show in the config * screen, if this is a floating point option */ int decimalPlaces() default 2; + + /** + * @return Can this range be configured with a slider? + */ + boolean allowSlider() default true; + + /** + * @return the default option type for this range constraint + */ + DefaultOptionType defaultOption() default DefaultOptionType.SLIDER; + + enum DefaultOptionType { + SLIDER, + TEXT_BOX + } } diff --git a/src/main/java/io/wispforest/owo/config/ui/OptionComponentFactory.java b/src/main/java/io/wispforest/owo/config/ui/OptionComponentFactory.java index 09df8b83..f6264ef9 100644 --- a/src/main/java/io/wispforest/owo/config/ui/OptionComponentFactory.java +++ b/src/main/java/io/wispforest/owo/config/ui/OptionComponentFactory.java @@ -34,17 +34,19 @@ public interface OptionComponentFactory { var field = option.backingField().field(); if (field.isAnnotationPresent(RangeConstraint.class)) { - return OptionComponents.createRangeControls( - model, option, - NumberReflection.isFloatingPointType(field.getType()) - ? field.getAnnotation(RangeConstraint.class).decimalPlaces() - : 0 - ); - } else { - return OptionComponents.createTextBox(model, option, configTextBox -> { - configTextBox.configureForNumber(option.clazz()); - }); + var constraint = field.getAnnotation(RangeConstraint.class); + if (constraint.allowSlider() && constraint.min() != -Double.MAX_VALUE && constraint.max() != Double.MAX_VALUE) { + return OptionComponents.createRangeControls( + model, option, + NumberReflection.isFloatingPointType(field.getType()) + ? constraint.decimalPlaces() + : 0 + ); + } } + return OptionComponents.createTextBox(model, option, configTextBox -> { + configTextBox.configureForNumber(option.clazz()); + }); }; OptionComponentFactory STRING = (model, option) -> { diff --git a/src/main/java/io/wispforest/owo/config/ui/OptionComponents.java b/src/main/java/io/wispforest/owo/config/ui/OptionComponents.java index 433d5ec2..e0f0db8a 100644 --- a/src/main/java/io/wispforest/owo/config/ui/OptionComponents.java +++ b/src/main/java/io/wispforest/owo/config/ui/OptionComponents.java @@ -117,7 +117,8 @@ public static OptionComponentFactory.Result cre var toggleButton = optionComponent.childById(ButtonComponent.class, "toggle-button"); var textMode = new MutableBoolean(false); - toggleButton.onPress(button -> { + + Consumer toggleAction = button -> { textMode.setValue(textMode.isFalse()); if (textMode.isTrue()) { @@ -136,7 +137,10 @@ public static OptionComponentFactory.Result cre ? Text.translatable("text.owo.config.button.range.edit_with_slider") : Text.translatable("text.owo.config.button.range.edit_as_text") ); - }); + }; + toggleButton.onPress(toggleAction); + + if (constraint.defaultOption().equals(RangeConstraint.DefaultOptionType.TEXT_BOX)) toggleAction.accept(toggleButton); optionComponent.child(new SearchAnchorComponent( optionComponent, diff --git a/src/testmod/java/io/wispforest/uwu/config/UwuConfigModel.java b/src/testmod/java/io/wispforest/uwu/config/UwuConfigModel.java index 6b2d26e7..24ae7aaa 100644 --- a/src/testmod/java/io/wispforest/uwu/config/UwuConfigModel.java +++ b/src/testmod/java/io/wispforest/uwu/config/UwuConfigModel.java @@ -4,6 +4,7 @@ import io.wispforest.owo.config.Option; import io.wispforest.owo.config.annotation.*; import io.wispforest.owo.ui.core.Color; +import net.fabricmc.fabric.api.util.TriState; import java.util.ArrayList; import java.util.List; @@ -28,8 +29,28 @@ public class UwuConfigModel { @PredicateConstraint("predicateFunction") public List someOption = new ArrayList<>(List.of("1", "2", "3", "4", "5")); - @RangeConstraint(min = 0, max = 10, decimalPlaces = 1) - public float floting = 6.9f; + @Nest + public RangeConstraintTest rangeTest = new RangeConstraintTest(); + + public static class RangeConstraintTest { + @RangeConstraint() + public double completelyEmptyRangeConstraint = 69; + + @RangeConstraint(min = 0) + public double minOnly = 69; + + @RangeConstraint(max = 100) + public double maxOnly = 69; + + @RangeConstraint(min = 0, max = 100) + public double minAndMax = 69; + + @RangeConstraint(min = 0, max = 100, allowSlider = false) + public double noSlider = 69; + + @RangeConstraint(min = 0, max = 100, defaultOption = RangeConstraint.DefaultOptionType.TEXT_BOX) + public double startWithTextBox = 69; + } public String thisIsAStringValue = "\\bruh?"; diff --git a/src/testmod/resources/assets/uwu/lang/en_us.json b/src/testmod/resources/assets/uwu/lang/en_us.json index 0f317a89..2cecda3c 100644 --- a/src/testmod/resources/assets/uwu/lang/en_us.json +++ b/src/testmod/resources/assets/uwu/lang/en_us.json @@ -45,8 +45,28 @@ "text.config.uwu.category.nestingTime.nestingTimeIntensifies": "Nesting Time Intensifies", "text.config.uwu.option.nestingTime.nestingTimeIntensifies.wowSoNested": "Wow so nested", "text.config.uwu.option.someOption": "Hello, yes, this is the predicate constraint tutorial", - "text.config.uwu.option.floting": "Floting", - "text.config.uwu.option.floting.tooltip": "messes with button?", + + "text.config.uwu.category.rangeTest": "Range Constraint testing", + "text.config.uwu.category.rangeTest.tooltip": "chyzman did this part", + + "text.config.uwu.option.rangeTest.completelyEmptyRangeConstraint": "no values?", + "text.config.uwu.option.rangeTest.completelyEmptyRangeConstraint.tooltip": "no values :(", + + "text.config.uwu.option.rangeTest.minOnly": "min only", + "text.config.uwu.option.rangeTest.minOnly.tooltip": "(it's 0)", + + "text.config.uwu.option.rangeTest.maxOnly": "max only", + "text.config.uwu.option.rangeTest.maxOnly.tooltip": "(it's 100)", + + "text.config.uwu.option.rangeTest.minAndMax": "min and max", + "text.config.uwu.option.rangeTest.minAndMax.tooltip": "(read the previous 2)", + + "text.config.uwu.option.rangeTest.noSlider": "no slider", + "text.config.uwu.option.rangeTest.noSlider.tooltip": "i stole it :3", + + "text.config.uwu.option.rangeTest.startWithTextBox": "start with text box", + "text.config.uwu.option.rangeTest.startWithTextBox.tooltip": "u can make it the slider but it starts with the text box.\nI literally just said that, pay attention", + "text.config.uwu.section.bottom": "Bottom Text", "text.config.uwu.section.nesting_yo?": "That's Nestnite", "text.config.uwu.option.thisIsAStringValue": "Clearly, this is a string value", @@ -97,4 +117,4 @@ {"text": " to", "color": "#FF9052"}, {"text": " Title", "color": "#FF5272"} ] -} \ No newline at end of file +} diff --git a/src/testmod/resources/fabric.mod.json b/src/testmod/resources/fabric.mod.json index 4b59fced..92107598 100644 --- a/src/testmod/resources/fabric.mod.json +++ b/src/testmod/resources/fabric.mod.json @@ -5,7 +5,10 @@ "name": "uωu", "description": "oωo testmod", "authors": [ - "glisco" + "glisco", + "chyzman", + "Blodhgarm", + "ur mom" ], "contact": {}, "license": "MIT",