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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
package dev.isxander.yacl.gui;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import dev.isxander.yacl.api.*;
import dev.isxander.yacl.api.utils.Dimension;
import dev.isxander.yacl.api.utils.MutableDimension;
import dev.isxander.yacl.api.utils.OptionUtils;
import dev.isxander.yacl.gui.tab.ScrollableNavigationBar;
import dev.isxander.yacl.gui.tab.ListHolderWidget;
import dev.isxander.yacl.gui.tab.TabExt;
import dev.isxander.yacl.gui.utils.GuiUtils;
import dev.isxander.yacl.impl.utils.YACLConstants;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.MultiLineLabel;
import net.minecraft.client.gui.components.Tooltip;
import net.minecraft.client.gui.components.tabs.Tab;
import net.minecraft.client.gui.components.tabs.TabManager;
import net.minecraft.client.gui.components.tabs.TabNavigationBar;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.tooltip.TooltipRenderUtil;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
public class YACLScreen extends Screen {
public final YetAnotherConfigLib config;
private final Screen parent;
public final TabManager tabManager = new TabManager(this::addRenderableWidget, this::removeWidget);
public TabNavigationBar tabNavigationBar;
public Tab[] tabs;
public ScreenRectangle tabArea;
public Component saveButtonMessage, saveButtonTooltipMessage;
private int saveButtonMessageTime;
public YACLScreen(YetAnotherConfigLib config, Screen parent) {
super(config.title());
this.config = config;
this.parent = parent;
}
@Override
protected void init() {
tabNavigationBar = new ScrollableNavigationBar(this.width, tabManager, config.categories()
.stream()
.map(category -> {
if (category instanceof PlaceholderCategory placeholder)
return new PlaceholderTab(placeholder);
return new CategoryTab(category);
}).toList());
tabNavigationBar.selectTab(0, false);
tabNavigationBar.arrangeElements();
ScreenRectangle navBarArea = tabNavigationBar.getRectangle();
tabArea = new ScreenRectangle(0, navBarArea.height() - 1, this.width, this.height - navBarArea.height() + 1);
tabManager.setTabArea(tabArea);
addRenderableWidget(tabNavigationBar);
config.initConsumer().accept(this);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
renderDirtBackground(graphics);
super.render(graphics, mouseX, mouseY, delta);
}
protected void finishOrSave() {
saveButtonMessage = null;
if (pendingChanges()) {
Set<OptionFlag> flags = new HashSet<>();
OptionUtils.forEachOptions(config, option -> {
if (option.applyValue()) {
flags.addAll(option.flags());
}
});
OptionUtils.forEachOptions(config, option -> {
if (option.changed()) {
// if still changed after applying, reset to the current value from binding
// as something has gone wrong.
option.forgetPendingValue();
YACLConstants.LOGGER.error("Option '{}' value mismatch after applying! Reset to binding's getter.", option.name().getString());
}
});
config.saveFunction().run();
flags.forEach(flag -> flag.accept(minecraft));
} else onClose();
}
protected void cancelOrReset() {
if (pendingChanges()) { // if pending changes, button acts as a cancel button
OptionUtils.forEachOptions(config, Option::forgetPendingValue);
onClose();
} else { // if not, button acts as a reset button
OptionUtils.forEachOptions(config, Option::requestSetDefault);
}
}
protected void undo() {
OptionUtils.forEachOptions(config, Option::forgetPendingValue);
}
@Override
public void tick() {
tabManager.tickCurrent();
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(Component message, Component 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();
}
@Override
public boolean shouldCloseOnEsc() {
if (pendingChanges()) {
setSaveButtonMessage(Component.translatable("yacl.gui.save_before_exit").withStyle(ChatFormatting.RED), Component.translatable("yacl.gui.save_before_exit.tooltip"));
return false;
}
return true;
}
@Override
public void onClose() {
minecraft.setScreen(parent);
}
public static void renderMultilineTooltip(GuiGraphics graphics, Font font, MultiLineLabel text, int centerX, int yAbove, int yBelow, int screenWidth, int screenHeight) {
if (text.getLineCount() > 0) {
int maxWidth = text.getWidth();
int lineHeight = font.lineHeight + 1;
int height = text.getLineCount() * lineHeight - 1;
int belowY = yBelow + 12;
int aboveY = yAbove - height + 12;
int maxBelow = screenHeight - (belowY + height);
int minAbove = aboveY - height;
int y = aboveY;
if (minAbove < 8)
y = maxBelow > minAbove ? belowY : aboveY;
int x = Math.max(centerX - text.getWidth() / 2 - 12, -6);
int drawX = x + 12;
int drawY = y - 12;
graphics.pose().pushPose();
Tesselator tesselator = Tesselator.getInstance();
BufferBuilder bufferBuilder = tesselator.getBuilder();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
TooltipRenderUtil.renderTooltipBackground(
graphics,
drawX,
drawY,
maxWidth,
height,
400
);
RenderSystem.enableDepthTest();
RenderSystem.enableBlend();
RenderSystem.defaultBlendFunc();
BufferUploader.drawWithShader(bufferBuilder.end());
RenderSystem.disableBlend();
graphics.pose().translate(0.0, 0.0, 400.0);
text.renderLeftAligned(graphics, drawX, drawY, lineHeight, -1);
graphics.pose().popPose();
}
}
public class CategoryTab implements TabExt {
private final ConfigCategory category;
private final Tooltip tooltip;
private ListHolderWidget<OptionListWidget> optionList;
private final Button saveFinishedButton;
private final Button cancelResetButton;
private final Button undoButton;
private final SearchFieldWidget searchField;
private OptionDescriptionWidget descriptionWidget;
public CategoryTab(ConfigCategory category) {
this.category = category;
this.tooltip = Tooltip.create(category.tooltip());
int columnWidth = width / 3;
int padding = columnWidth / 20;
columnWidth = Math.min(columnWidth, 400);
int paddedWidth = columnWidth - padding * 2;
MutableDimension<Integer> actionDim = Dimension.ofInt(width / 3 * 2 + width / 6, height - padding - 20, paddedWidth, 20);
saveFinishedButton = Button.builder(Component.literal("Done"), btn -> finishOrSave())
.pos(actionDim.x() - actionDim.width() / 2, actionDim.y())
.size(actionDim.width(), actionDim.height())
.build();
actionDim.expand(-actionDim.width() / 2 - 2, 0).move(-actionDim.width() / 2 - 2, -22);
cancelResetButton = Button.builder(Component.literal("Cancel"), btn -> cancelOrReset())
.pos(actionDim.x() - actionDim.width() / 2, actionDim.y())
.size(actionDim.width(), actionDim.height())
.build();
actionDim.move(actionDim.width() + 4, 0);
undoButton = Button.builder(Component.translatable("yacl.gui.undo"), btn -> undo())
.pos(actionDim.x() - actionDim.width() / 2, actionDim.y())
.size(actionDim.width(), actionDim.height())
.tooltip(Tooltip.create(Component.translatable("yacl.gui.undo.tooltip")))
.build();
searchField = new SearchFieldWidget(
YACLScreen.this,
font,
width / 3 * 2 + width / 6 - paddedWidth / 2 + 1,
undoButton.getY() - 22,
paddedWidth - 2, 18,
Component.translatable("gui.recipebook.search_hint"),
Component.translatable("gui.recipebook.search_hint"),
searchQuery -> optionList.getList().updateSearchQuery(searchQuery)
);
this.optionList = new ListHolderWidget<>(
() -> new ScreenRectangle(tabArea.position(), tabArea.width() / 3 * 2 + 1, tabArea.height()),
new OptionListWidget(YACLScreen.this, category, minecraft, 0, 0, width / 3 * 2 + 1, height, desc -> {
descriptionWidget.setOptionDescription(desc);
})
);
descriptionWidget = new OptionDescriptionWidget(
() -> new ScreenRectangle(
width / 3 * 2 + padding,
tabArea.top() + padding,
paddedWidth,
searchField.getY() - 1 - tabArea.top() - padding * 2
),
null
);
updateButtons();
}
@Override
public Component getTabTitle() {
return category.name();
}
@Override
public void visitChildren(Consumer<AbstractWidget> consumer) {
consumer.accept(optionList);
consumer.accept(saveFinishedButton);
consumer.accept(cancelResetButton);
consumer.accept(undoButton);
consumer.accept(searchField);
consumer.accept(descriptionWidget);
}
@Override
public void doLayout(ScreenRectangle screenRectangle) {
}
@Override
public void tick() {
updateButtons();
searchField.tick();
descriptionWidget.tick();
}
@Nullable
@Override
public Tooltip getTooltip() {
return tooltip;
}
private void updateButtons() {
boolean pendingChanges = pendingChanges();
undoButton.active = pendingChanges;
saveFinishedButton.setMessage(pendingChanges ? Component.translatable("yacl.gui.save") : GuiUtils.translatableFallback("yacl.gui.done", CommonComponents.GUI_DONE));
saveFinishedButton.setTooltip(Tooltip.create(pendingChanges ? Component.translatable("yacl.gui.save.tooltip") : Component.translatable("yacl.gui.finished.tooltip")));
cancelResetButton.setMessage(pendingChanges ? GuiUtils.translatableFallback("yacl.gui.cancel", CommonComponents.GUI_CANCEL) : Component.translatable("controls.reset"));
cancelResetButton.setTooltip(Tooltip.create(pendingChanges ? Component.translatable("yacl.gui.cancel.tooltip") : Component.translatable("yacl.gui.reset.tooltip")));
}
}
public class PlaceholderTab implements TabExt {
private final PlaceholderCategory category;
private final Tooltip tooltip;
public PlaceholderTab(PlaceholderCategory category) {
this.category = category;
this.tooltip = Tooltip.create(category.tooltip());
}
@Override
public Component getTabTitle() {
return category.name();
}
@Override
public void visitChildren(Consumer<AbstractWidget> consumer) {
}
@Override
public void doLayout(ScreenRectangle screenRectangle) {
minecraft.setScreen(category.screen().apply(minecraft, YACLScreen.this));
}
@Override
public @Nullable Tooltip getTooltip() {
return this.tooltip;
}
}
}
|