implement step into ranges
Some checks failed
/ Build BaseBand DSM & Broadway (push) Has been cancelled

This commit is contained in:
Daniella / Tove 2024-06-15 11:54:35 +02:00
parent 915c3f7634
commit a0200c0323
Signed by: TudbuT
GPG key ID: B3CF345217F202D3
4 changed files with 25 additions and 15 deletions

View file

@ -143,10 +143,12 @@ public abstract class Feature extends ToggleButton implements SetCommand {
if (f.getType() == int.class) { if (f.getType() == int.class) {
Range range = f.getDeclaredAnnotation(Range.class); Range range = f.getDeclaredAnnotation(Range.class);
if (range != null) { if (range != null) {
String[] r = range.value().split("\\.\\."); String[] rStep = range.value().split("@");
String[] r = rStep[0].split("\\.\\.");
int min = Integer.parseInt(r[0]); int min = Integer.parseInt(r[0]);
int max = Integer.parseInt(r[1]); int max = Integer.parseInt(r[1]);
subComponents.add(new IntSlider(config.value(), settings, config.value(), Object::toString, max - min, min).gate(gate).hover(description)); int step = rStep.length == 1 ? 1 : Integer.parseInt(rStep[1]);
subComponents.add(new IntSlider(config.value(), settings, config.value(), Object::toString, max - min, min, step).gate(gate).hover(description));
} else { } else {
throw new RuntimeException("No range specified for slider"); throw new RuntimeException("No range specified for slider");
} }
@ -154,10 +156,12 @@ public abstract class Feature extends ToggleButton implements SetCommand {
if (f.getType() == float.class) { if (f.getType() == float.class) {
Range range = f.getDeclaredAnnotation(Range.class); Range range = f.getDeclaredAnnotation(Range.class);
if (range != null) { if (range != null) {
String[] r = range.value().split("\\.\\."); String[] rStep = range.value().split("@");
String[] r = rStep[0].split("\\.\\.");
float min = Float.parseFloat(r[0]); float min = Float.parseFloat(r[0]);
float max = Float.parseFloat(r[1]); float max = Float.parseFloat(r[1]);
subComponents.add(new Slider(config.value(), settings, config.value(), n -> String.valueOf(Math.round(n * 100) / 100f), max - min, min).gate(gate).hover(description)); float step = rStep.length == 1 ? 1f : Float.parseFloat(rStep[1]);
subComponents.add(new Slider(config.value(), settings, config.value(), n -> String.valueOf(Math.round(n * 100) / 100f), max - min, min, step).gate(gate).hover(description));
} else { } else {
throw new RuntimeException("No range specified for slider"); throw new RuntimeException("No range specified for slider");
} }

View file

@ -18,21 +18,23 @@ public class IntSlider extends Component {
Function<Integer, Boolean> updateMethod; Function<Integer, Boolean> updateMethod;
int mapper; int mapper;
int adder; int adder;
int step;
{green = true;} {green = true;}
public IntSlider(String s, ConfigHandle handle, String field, Function<Integer, String> text, int mapper, int adder, Function<Integer, Boolean> updateMethod) { public IntSlider(String s, ConfigHandle handle, String field, Function<Integer, String> text, int mapper, int adder, int step, Function<Integer, Boolean> updateMethod) {
this.text = s; this.text = s;
this.handle = handle; this.handle = handle;
this.field = field; this.field = field;
this.sliderText = text; this.sliderText = text;
this.mapper = mapper; this.mapper = mapper;
this.adder = adder; this.adder = adder;
this.step = step;
this.updateMethod = updateMethod; this.updateMethod = updateMethod;
} }
public IntSlider(String s, ConfigHandle handle, String field, Function<Integer, String> text, int mapper, int adder) { public IntSlider(String s, ConfigHandle handle, String field, Function<Integer, String> text, int mapper, int adder, int step) {
this(s, handle, field, text, mapper, adder, t -> true); this(s, handle, field, text, mapper, adder, step, t -> true);
} }
int countdown = 0; int countdown = 0;
@ -96,9 +98,10 @@ public class IntSlider extends Component {
if(mouseButton == 0) { if(mouseButton == 0) {
f = Math.max(Math.min(x, 140), 0) / 140f; f = Math.max(Math.min(x, 140), 0) / 140f;
handle.getContent().set(field, Math.round(f * mapper + adder)); int i = Math.round((f * mapper + adder) / step) * step;
handle.getContent().set(field, i);
handle.updated(field); handle.updated(field);
if (!updateMethod.apply(Math.round(f * mapper + adder))) { if (!updateMethod.apply(i)) {
System.out.println("Something went wrong handling the sliders!"); System.out.println("Something went wrong handling the sliders!");
throw new RuntimeException(); throw new RuntimeException();
} }

View file

@ -18,21 +18,23 @@ public class Slider extends Component {
Function<Float, Boolean> updateMethod; Function<Float, Boolean> updateMethod;
float mapper; float mapper;
float adder; float adder;
float step;
{green = true;} {green = true;}
public Slider(String s, ConfigHandle handle, String field, Function<Float, String> text, float mapper, float adder, Function<Float, Boolean> updateMethod) { public Slider(String s, ConfigHandle handle, String field, Function<Float, String> text, float mapper, float adder, float step, Function<Float, Boolean> updateMethod) {
this.text = s; this.text = s;
this.handle = handle; this.handle = handle;
this.field = field; this.field = field;
this.sliderText = text; this.sliderText = text;
this.mapper = mapper; this.mapper = mapper;
this.adder = adder; this.adder = adder;
this.step = step;
this.updateMethod = updateMethod; this.updateMethod = updateMethod;
} }
public Slider(String s, ConfigHandle handle, String field, Function<Float, String> text, float mapper, float adder) { public Slider(String s, ConfigHandle handle, String field, Function<Float, String> text, float mapper, float adder, float step) {
this(s, handle, field, text, mapper, adder, t -> true); this(s, handle, field, text, mapper, adder, step, t -> true);
} }
int countdown = 0; int countdown = 0;
@ -94,9 +96,10 @@ public class Slider extends Component {
if(mouseButton == 0) { if(mouseButton == 0) {
f = Math.max(Math.min(x, 140), 0) / 140f; f = Math.max(Math.min(x, 140), 0) / 140f;
handle.getContent().set(field, f * mapper + adder); float f = Math.round((this.f * mapper + adder) / step) * step;
handle.getContent().set(field, f);
handle.updated(field); handle.updated(field);
if (!updateMethod.apply(f * mapper + adder)) { if (!updateMethod.apply(f)) {
System.out.println("Something went wrong handling the sliders!"); System.out.println("Something went wrong handling the sliders!");
throw new RuntimeException(); throw new RuntimeException();
} }

View file

@ -13,7 +13,7 @@ import java.lang.annotation.Target;
public @interface Range { public @interface Range {
/** /**
* n..m * begin..end[@step]
* @return the range * @return the range
*/ */
String value(); String value();