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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
package dev.isxander.yacl.gui;
import dev.isxander.yacl.api.ConfigCategory;
import dev.isxander.yacl.api.Option;
import dev.isxander.yacl.api.YetAnotherConfigLib;
import dev.isxander.yacl.api.utils.Dimension;
import dev.isxander.yacl.api.utils.OptionUtils;
import dev.isxander.yacl.impl.YACLConstants;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class YACLScreen extends Screen {
public final YetAnotherConfigLib config;
public int currentCategoryIdx;
private final Screen parent;
public OptionListWidget optionList;
public final List<CategoryWidget> categoryButtons;
public TooltipButtonWidget finishedSaveButton, cancelResetButton, undoButton;
public SearchFieldWidget searchFieldWidget;
public Text saveButtonMessage;
public Text saveButtonTooltipMessage;
private int saveButtonMessageTime;
public YACLScreen(YetAnotherConfigLib config, Screen parent) {
super(config.title());
this.config = config;
this.parent = parent;
this.categoryButtons = new ArrayList<>();
this.currentCategoryIdx = 0;
}
@Override
protected void init() {
categoryButtons.clear();
int columnWidth = width / 3;
int padding = columnWidth / 20;
columnWidth = Math.min(columnWidth, 400);
int paddedWidth = columnWidth - padding * 2;
Dimension<Integer> categoryDim = Dimension.ofInt(width / 3 / 2, padding, paddedWidth, 20);
int idx = 0;
for (ConfigCategory category : config.categories()) {
CategoryWidget categoryWidget = new CategoryWidget(
this,
category,
idx,
categoryDim.x() - categoryDim.width() / 2, categoryDim.y(),
categoryDim.width(), categoryDim.height()
);
categoryButtons.add(categoryWidget);
addDrawableChild(categoryWidget);
idx++;
categoryDim.move(0, 21);
}
searchFieldWidget = new SearchFieldWidget(this, textRenderer, width / 3 / 2 - paddedWidth / 2 + 1, height - 71, paddedWidth - 2, 18, Text.translatable("yacl.gui.search"), Text.translatable("yacl.gui.search"));
Dimension<Integer> actionDim = Dimension.ofInt(width / 3 / 2, height - padding - 20, paddedWidth, 20);
finishedSaveButton = new TooltipButtonWidget(this, actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.empty(), Text.empty(), (btn) -> {
saveButtonMessage = null;
if (pendingChanges()) {
AtomicBoolean requiresRestart = new AtomicBoolean(false);
OptionUtils.forEachOptions(config, option -> {
if (option.requiresRestart() && option.changed())
requiresRestart.set(true);
option.applyValue();
});
OptionUtils.forEachOptions(config, option -> {
if (option.changed()) {
YACLConstants.LOGGER.error("'{}' was saved as '{}' but the changes don't seem to have applied. (Maybe binding is immutable?)", option.name().getString(), option.pendingValue());
setSaveButtonMessage(Text.translatable("yacl.gui.fail_apply").formatted(Formatting.RED), Text.translatable("yacl.gui.fail_apply.tooltip"));
}
});
config.saveFunction().run();
if (requiresRestart.get()) {
client.setScreen(new RequireRestartScreen(this));
}
} else close();
});
actionDim.expand(-actionDim.width() / 2 - 2, 0).move(-actionDim.width() / 2 - 2, -22);
cancelResetButton = new TooltipButtonWidget(this, actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.empty(), Text.empty(), (btn) -> {
if (pendingChanges()) {
OptionUtils.forEachOptions(config, Option::forgetPendingValue);
close();
} else {
OptionUtils.forEachOptions(config, Option::requestSetDefault);
}
});
actionDim.move(actionDim.width() + 4, 0);
undoButton = new TooltipButtonWidget(this, actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.translatable("yacl.gui.undo"), Text.translatable("yacl.gui.undo.tooltip"), (btn) -> {
OptionUtils.forEachOptions(config, Option::forgetPendingValue);
});
updateActionAvailability();
addDrawableChild(searchFieldWidget);
addDrawableChild(cancelResetButton);
addDrawableChild(undoButton);
addDrawableChild(finishedSaveButton);
ConfigCategory currentCategory = config.categories().get(currentCategoryIdx);
optionList = new OptionListWidget(currentCategory, this, client, width, height);
addSelectableChild(optionList);
config.initConsumer().accept(this);
}
@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
renderBackground(matrices);
super.render(matrices, mouseX, mouseY, delta);
optionList.render(matrices, mouseX, mouseY, delta);
searchFieldWidget.render(matrices, mouseX, mouseY, delta);
for (Element child : children()) {
if (child instanceof TooltipButtonWidget tooltipButtonWidget) {
tooltipButtonWidget.renderTooltip(matrices, mouseX, mouseY);
}
}
}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (optionList.keyPressed(keyCode, scanCode, modifiers)) {
return true;
}
return super.keyPressed(keyCode, scanCode, modifiers);
}
@Override
public boolean charTyped(char chr, int modifiers) {
if (optionList.charTyped(chr, modifiers)) {
return true;
}
return super.charTyped(chr, modifiers);
}
public void changeCategory(int idx) {
currentCategoryIdx = idx;
refreshGUI();
}
private void updateActionAvailability() {
boolean pendingChanges = pendingChanges();
undoButton.active = pendingChanges;
finishedSaveButton.setMessage(pendingChanges ? Text.translatable("yacl.gui.save") : Text.translatable("gui.done"));
finishedSaveButton.setTooltip(pendingChanges ? Text.translatable("yacl.gui.save.tooltip") : Text.translatable("yacl.gui.finished.tooltip"));
cancelResetButton.setMessage(pendingChanges ? Text.translatable("gui.cancel") : Text.translatable("controls.reset"));
cancelResetButton.setTooltip(pendingChanges ? Text.translatable("yacl.gui.cancel.tooltip") : Text.translatable("yacl.gui.reset.tooltip"));
}
@Override
public void tick() {
searchFieldWidget.tick();
updateActionAvailability();
if (saveButtonMessage != null) {
if (saveButtonMessageTime > 140) {
saveButtonMessage = null;
saveButtonTooltipMessage = null;
saveButtonMessageTime = 0;
} else {
saveButtonMessageTime++;
finishedSaveButton.setMessage(saveButtonMessage);
if (saveButtonTooltipMessage != null) {
finishedSaveButton.setTooltip(saveButtonTooltipMessage);
}
}
}
}
private void setSaveButtonMessage(Text message, Text tooltip) {
saveButtonMessage = message;
saveButtonTooltipMessage = tooltip;
saveButtonMessageTime = 0;
}
private boolean pendingChanges() {
AtomicBoolean pendingChanges = new AtomicBoolean(false);
OptionUtils.consumeOptions(config, (option) -> {
if (option.changed()) {
pendingChanges.set(true);
return true;
}
return false;
});
return pendingChanges.get();
}
private void refreshGUI() {
init(client, width, height);
}
@Override
public boolean shouldCloseOnEsc() {
if (pendingChanges()) {
setSaveButtonMessage(Text.translatable("yacl.gui.save_before_exit").formatted(Formatting.RED), Text.translatable("yacl.gui.save_before_exit.tooltip"));
return false;
}
return true;
}
@Override
public void close() {
client.setScreen(parent);
}
}
|