-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathCyclingOption.java
More file actions
73 lines (57 loc) · 2.01 KB
/
CyclingOption.java
File metadata and controls
73 lines (57 loc) · 2.01 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
67
68
69
70
71
72
73
package net.vulkanmod.config;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.network.chat.Component;
import net.vulkanmod.config.widget.CyclingOptionWidget;
import net.vulkanmod.config.widget.OptionWidget;
import org.apache.commons.lang3.ArrayUtils;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class CyclingOption<E> extends Option<E> {
private final Function<E, Component> translator;
private E[] values;
private int index;
public CyclingOption(String name, E[] values, Function<E, Component> translator, Consumer<E> setter, Supplier<E> getter) {
super(name, setter, getter);
this.values = values;
this.translator = translator;
this.index = ArrayUtils.indexOf(this.values, this.getValue());
}
@Override
public AbstractWidget createWidget(int x, int y, int width, int height) {
return null;
}
@Override
public OptionWidget createOptionWidget(int x, int y, int width, int height) {
return new CyclingOptionWidget(this, x, y, width, height, this.name);
}
public void updateOption(E[] values, Consumer<E> setter, Supplier<E> getter) {
this.setter = setter;
this.getter = getter;
this.values = values;
this.index = ArrayUtils.indexOf(this.values, this.getValue());
}
public int index() { return this.index;}
@Override
public void setValue(E e) {
this.newValue = e;
}
public void prevValue() {
if(this.index > 0) this.index--;
this.updateValue();
}
public void nextValue() {
if(this.index < values.length - 1) this.index++;
this.updateValue();
}
private void updateValue() {
if(this.index >= 0 && this.index < this.values.length)
setValue(values[this.index]);
}
public Component getValueText() {
return translator.apply(this.newValue);
}
public E[] getValues() {
return values;
}
}