-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathRangeOption.java
More file actions
66 lines (50 loc) · 1.97 KB
/
RangeOption.java
File metadata and controls
66 lines (50 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package net.vulkanmod.config;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.vulkanmod.config.widget.OptionWidget;
import net.vulkanmod.config.widget.RangeOptionWidget;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class RangeOption extends Option<Integer> {
int min;
int max;
int step;
Function<Integer, String> translator;
public RangeOption(String name, int min, int max, int step, Function<Integer, String> translator, Consumer<Integer> setter, Supplier<Integer> getter) {
super(name, setter, getter);
this.min = min;
this.max = max;
this.step = step;
this.translator = translator;
}
public RangeOption(String name, int min, int max, int step, Consumer<Integer> setter, Supplier<Integer> getter) {
this(name, min, max, step, String::valueOf, setter, getter);
}
@Override
public AbstractWidget createWidget(int x, int y, int width, int height) {
return new SliderWidgetImpl(this, x, y, width, height, getName(), this.getScaledValue());
}
public OptionWidget createOptionWidget(int x, int y, int width, int height) {
return new RangeOptionWidget(this, x, y, width, height, this.name);
}
public Component getName() {
return Component.nullToEmpty(this.name.getString() + ": " + this.getValue().toString());
}
public float getScaledValue() {
float value = this.getValue();
return (value - this.min) / (this.max - this.min);
}
public void setValue(Integer n) {
this.newValue = n;
}
public void setValue(float f) {
double n = Mth.lerp(f, min, max);
n = this.step * Math.round(n / this.step);
this.setValue(Integer.valueOf((int)n));
}
public String getDisplayedValue() {
return this.translator.apply(this.newValue);
}
}