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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
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.AbstractWidget;
import dev.isxander.yacl.gui.YACLScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
import net.minecraft.util.TranslatableOption;
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 T[] availableValues;
public static <T extends Enum<T>> Function<T, Text> getDefaultFormatter() {
return value -> {
if (value instanceof NameableEnum nameableEnum)
return nameableEnum.getDisplayName();
if (value instanceof TranslatableOption translatableOption)
return translatableOption.getText();
return Text.of(value.toString());
};
}
/**
* Constructs a cycling enum controller with a default value formatter and all values being available.
* The default value formatter first searches if the
* enum is a {@link NameableEnum} else, just uses {@link Enum#toString()}
*
* @param option bound option
*/
public EnumController(Option<T> option) {
this(option, getDefaultFormatter());
}
/**
* Constructs a cycling enum controller with all values being available.
*
* @param option bound option
* @param valueFormatter format the enum into any {@link Text}
*/
public EnumController(Option<T> option, Function<T, Text> valueFormatter) {
this(option, valueFormatter, option.typeClass().getEnumConstants());
}
/**
* Constructs a cycling enum controller.
*
* @param option bound option
* @param valueFormatter format the enum into any {@link Text}
* @param availableValues all enum constants that can be cycled through
*/
public EnumController(Option<T> option, Function<T, Text> valueFormatter, T[] availableValues) {
this.option = option;
this.valueFormatter = valueFormatter;
this.availableValues = availableValues;
}
/**
* {@inheritDoc}
*/
@Override
public Option<T> option() {
return option;
}
/**
* {@inheritDoc}
*/
@Override
public Text formatValue() {
return valueFormatter.apply(option().pendingValue());
}
/**
* {@inheritDoc}
*/
@Override
public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) {
return new EnumControllerElement<>(this, screen, widgetDimension, availableValues);
}
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) || !isAvailable())
return false;
playDownSound();
cycleValue(button == 1 || Screen.hasShiftDown() || Screen.hasControlDown() ? -1 : 1);
return true;
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (!focused)
return false;
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();
}
}
}
|