Skip to content

Commit

Permalink
made range constraint better (but its unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
chyzman committed Sep 23, 2024
1 parent 5c5709f commit 06ab48a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ public interface OptionComponentFactory<T> {
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<? extends CharSequence> STRING = (model, option) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public static OptionComponentFactory.Result<FlowLayout, OptionValueProvider> cre
var toggleButton = optionComponent.childById(ButtonComponent.class, "toggle-button");

var textMode = new MutableBoolean(false);
toggleButton.onPress(button -> {

Consumer<ButtonComponent> toggleAction = button -> {
textMode.setValue(textMode.isFalse());

if (textMode.isTrue()) {
Expand All @@ -136,7 +137,10 @@ public static OptionComponentFactory.Result<FlowLayout, OptionValueProvider> 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,
Expand Down
25 changes: 23 additions & 2 deletions src/testmod/java/io/wispforest/uwu/config/UwuConfigModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,8 +29,28 @@ public class UwuConfigModel {
@PredicateConstraint("predicateFunction")
public List<String> 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?";

Expand Down
26 changes: 23 additions & 3 deletions src/testmod/resources/assets/uwu/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -97,4 +117,4 @@
{"text": " to", "color": "#FF9052"},
{"text": " Title", "color": "#FF5272"}
]
}
}
5 changes: 4 additions & 1 deletion src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"name": "uωu",
"description": "oωo testmod",
"authors": [
"glisco"
"glisco",
"chyzman",
"Blodhgarm",
"ur mom"
],
"contact": {},
"license": "MIT",
Expand Down

0 comments on commit 06ab48a

Please sign in to comment.