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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
package dev.isxander.yacl.gui.controllers;
import dev.isxander.yacl.api.Controller;
import dev.isxander.yacl.api.NameableEnum;
import dev.isxander.yacl.api.Option;
import dev.isxander.yacl.api.utils.Dimension;
import dev.isxander.yacl.gui.YACLScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import org.jetbrains.annotations.ApiStatus;
import org.lwjgl.glfw.GLFW;
import java.util.function.Function;
/**
* Simple controller type that displays the enum on the right.
* <p>
* Cycles forward with left click, cycles backward with right click or when shift is held
*
* @param <T> enum type
*/
public class EnumController<T extends Enum<T>> implements Controller<T> {
private final Option<T> option;
private final Function<T, Text> valueFormatter;
private final Class<T> enumClass;
/**
* Constructs a cycling enum controller with a default value formatter.
* The default value formatter first searches if the
* enum is a {@link NameableEnum} else, just use {@link Enum#name()}
*
* @param option bound option
* @param enumClass class of enum
*/
public EnumController(Option<T> option, Class<T> enumClass) {
this(option, enumClass, value -> {
if (value instanceof NameableEnum nameableEnum)
return nameableEnum.getDisplayName();
return Text.of(value.name());
});
}
/**
* Constructs a cycling enum controller.
*
* @param option bound option
* @param enumClass class of enum
* @param valueFormatter format the enum into any {@link Text}
*/
public EnumController(Option<T> option, Class<T> enumClass, Function<T, Text> valueFormatter) {
this.option = option;
this.valueFormatter = valueFormatter;
this.enumClass = enumClass;
}
/**
* {@inheritDoc}
*/
@Override
public Option<T> option() {
return option;
}
/**
* {@inheritDoc}
*/
@Override
public Text formatValue() {
return valueFormatter.apply(option().pendingValue());
}
/**
* {@inheritDoc}
*/
@Override
public ControllerWidget<EnumController<T>> provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) {
return new EnumControllerElement<>(this, screen, widgetDimension, enumClass.getEnumConstants());
}
@ApiStatus.Internal
public static class EnumControllerElement<T extends Enum<T>> extends ControllerWidget<EnumController<T>> {
private final T[] values;
public EnumControllerElement(EnumController<T> control, YACLScreen screen, Dimension<Integer> dim, T[] values) {
super(control, screen, dim);
this.values = values;
}
public void cycleValue(int increment) {
int targetIdx = control.option().pendingValue().ordinal() + increment;
if (targetIdx >= values.length) {
targetIdx -= values.length;
} else if (targetIdx < 0) {
targetIdx += values.length;
}
control.option().requestSet(values[targetIdx]);
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (!isMouseOver(mouseX, mouseY) || (button != 0 && button != 1))
return false;
playDownSound();
cycleValue(button == 1 || Screen.hasShiftDown() || Screen.hasControlDown() ? -1 : 1);
return true;
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
switch (keyCode) {
case GLFW.GLFW_KEY_LEFT, GLFW.GLFW_KEY_DOWN ->
cycleValue(-1);
case GLFW.GLFW_KEY_RIGHT, GLFW.GLFW_KEY_UP ->
cycleValue(1);
case GLFW.GLFW_KEY_ENTER, GLFW.GLFW_KEY_SPACE, GLFW.GLFW_KEY_KP_ENTER ->
cycleValue(Screen.hasControlDown() || Screen.hasShiftDown() ? -1 : 1);
default -> {
return false;
}
}
return true;
}
@Override
protected int getHoveredControlWidth() {
return getUnhoveredControlWidth();
}
}
}
|