-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathOptionListWidget.java
More file actions
682 lines (551 loc) · 24.4 KB
/
Copy pathOptionListWidget.java
File metadata and controls
682 lines (551 loc) · 24.4 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
package dev.isxander.yacl3.gui;
import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.platform.InputConstants;
import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.api.utils.Dimension;
import dev.isxander.yacl3.impl.utils.YACLConstants;
import dev.isxander.yacl3.mixin.AbstractSelectionListAccessor;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.components.MultiLineLabel;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.narration.NarratedElementType;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.client.gui.navigation.ScreenDirection;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.TextAlignment;
import net.minecraft.client.input.CharacterEvent;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.client.input.PreeditEvent;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jspecify.annotations.NonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class OptionListWidget extends YACLSelectionList<OptionListWidget.Entry> {
private final YACLScreen yaclScreen;
private final ConfigCategory category;
private String searchQuery = "";
private final Consumer<DescriptionWithName> hoverEvent;
private DescriptionWithName lastHoveredOption;
public OptionListWidget(YACLScreen screen, ConfigCategory category, Minecraft client, int x, int y, int width, int height, Consumer<DescriptionWithName> hoverEvent) {
super(client, width, height, y);
this.yaclScreen = screen;
this.category = category;
this.hoverEvent = hoverEvent;
refreshOptions();
for (OptionGroup group : category.groups()) {
if (group instanceof ListOption<?> listOption) {
listOption.addRefreshListener(() -> refreshListEntries(listOption, category));
}
}
}
public void refreshOptions() {
clearEntries();
for (OptionGroup group : category.groups()) {
GroupSeparatorEntry groupSeparatorEntry;
if (!group.isRoot()) {
groupSeparatorEntry = group instanceof ListOption<?> listOption
? new ListGroupSeparatorEntry(listOption, yaclScreen)
: new GroupSeparatorEntry(group, yaclScreen);
addEntry(groupSeparatorEntry);
} else {
groupSeparatorEntry = null;
}
List<Entry> optionEntries = new ArrayList<>();
// add empty entry to make sure users know it's empty not just bugging out
if (groupSeparatorEntry instanceof ListGroupSeparatorEntry listGroupSeparatorEntry) {
if (listGroupSeparatorEntry.listOption.options().isEmpty()) {
EmptyListLabel emptyListLabel = new EmptyListLabel(listGroupSeparatorEntry, category);
addEntry(emptyListLabel);
optionEntries.add(emptyListLabel);
}
}
for (Option<?> option : group.options()) {
OptionEntry entry = new OptionEntry(option, category, group, groupSeparatorEntry, option.controller().provideWidget(yaclScreen, getDefaultEntryDimension()));
addEntry(entry);
optionEntries.add(entry);
}
if (groupSeparatorEntry != null) {
groupSeparatorEntry.setChildEntries(optionEntries);
}
}
setScrollAmount(0);
repositionEntries();
}
@Override
protected int addEntry(Entry entry) {
// instead of using super.defaultEntryHeight, use the height the entry wants to be - our entries set their height in the constructor
return this.addEntry(entry, entry.getHeight());
}
private void refreshListEntries(ListOption<?> listOption, ConfigCategory category) {
// find group separator for group
ListGroupSeparatorEntry groupSeparator = this.children().stream().filter(e -> e instanceof ListGroupSeparatorEntry gs && gs.group == listOption).map(ListGroupSeparatorEntry.class::cast).findAny().orElse(null);
if (groupSeparator == null) {
YACLConstants.LOGGER.warn("Can't find group seperator to refresh list option entries for list option " + listOption.name());
return;
}
for (Entry entry : groupSeparator.childEntries)
this.removeEntry(entry);
groupSeparator.childEntries.clear();
// if no entries, below loop won't run where addEntryBelow() recaches viewable children
if (listOption.options().isEmpty()) {
EmptyListLabel emptyListLabel;
addEntryBelow(groupSeparator, emptyListLabel = new EmptyListLabel(groupSeparator, category));
groupSeparator.childEntries.add(emptyListLabel);
return;
}
Entry lastEntry = groupSeparator;
for (ListOptionEntry<?> listOptionEntry : listOption.options()) {
OptionEntry optionEntry = new OptionEntry(listOptionEntry, category, listOption, groupSeparator, listOptionEntry.controller().provideWidget(yaclScreen, getDefaultEntryDimension()));
addEntryBelow(lastEntry, optionEntry);
groupSeparator.childEntries.add(optionEntry);
lastEntry = optionEntry;
}
}
public Dimension<Integer> getDefaultEntryDimension() {
return Dimension.ofInt(getRowLeft(), 0, getRowWidth(), 20);
}
public void expandAllGroups() {
for (Entry entry : super.children()) {
if (entry instanceof GroupSeparatorEntry groupSeparatorEntry) {
groupSeparatorEntry.setExpanded(true);
}
}
}
@Override
public int getRowLeft() {
return super.getRowLeft() - SCROLLBAR_WIDTH;
}
@Override
public int getRowWidth() {
return getWidth() - SCROLLBAR_WIDTH - 20; // 10 padding each side
}
public void updateSearchQuery(String query) {
this.searchQuery = query;
for (Entry entry : this.children()) {
entry.updateSearchQuery(query);
}
expandAllGroups();
repositionEntries();
}
@Override
public boolean mouseClicked(MouseButtonEvent event, boolean bl) {
for (Entry child : children()) {
if (child != getEntryAtPosition(event.x(), event.y()) && child instanceof OptionEntry optionEntry)
optionEntry.widget.unfocus();
}
return super.mouseClicked(event, bl);
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double horizontal, double vertical) {
super.mouseScrolled(mouseX, mouseY, horizontal, vertical);
for (Entry child : children()) {
if (child.mouseScrolled(mouseX, mouseY, horizontal, vertical))
break;
}
return true;
}
@Override
public boolean mouseDragged(MouseButtonEvent event, double deltaX, double deltaY) {
if (this.getFocused() != null && this.isDragging() && isValidMouseClick(event.button())) {
GuiEventListener l = this.getFocused();
return l.mouseDragged(event, deltaX, deltaY);
}
return super.mouseDragged(event, deltaX, deltaY);
}
@Override
public boolean keyPressed(KeyEvent keyEvent) {
for (Entry child : children()) {
if (child.keyPressed(keyEvent))
return true;
}
return super.keyPressed(keyEvent);
}
@Override
public boolean charTyped(CharacterEvent characterEvent) {
for (Entry child : children()) {
if (child.charTyped(characterEvent))
return true;
}
return super.charTyped(characterEvent);
}
@Override
public boolean preeditUpdated(@Nullable PreeditEvent event) {
for (Entry child : children()) {
if (child.preeditUpdated(event))
return true;
}
return super.preeditUpdated(event);
}
private List<Entry> superModifiableChildren() {
// noinspection unchecked
return (List<Entry>) ((AbstractSelectionListAccessor) this).getChildren();
}
public void addEntryAtIndex(int index, Entry entry) {
superModifiableChildren().add(index, entry);
this.repositionEntries();
}
public void addEntryBelow(Entry below, Entry entry) {
int idx = superModifiableChildren().indexOf(below) + 1;
if (idx == 0)
throw new IllegalStateException("The entry to insert below does not exist!");
addEntryAtIndex(idx, entry);
}
public void addEntryBelowWithoutScroll(Entry below, Entry entry) {
double d = (double)this.contentHeight() - this.scrollAmount();
addEntryBelow(below, entry);
setScrollAmount(this.contentHeight() - d);
}
private void setHoverDescription(DescriptionWithName description) {
if (description != lastHoveredOption) {
lastHoveredOption = description;
hoverEvent.accept(description);
}
}
@Override
protected void extractListBackground(@NonNull GuiGraphicsExtractor graphics) {
}
protected boolean isValidMouseClick(int button) {
return button == InputConstants.MOUSE_BUTTON_LEFT || button == InputConstants.MOUSE_BUTTON_RIGHT || button == InputConstants.MOUSE_BUTTON_MIDDLE;
}
@Override
protected Entry nextEntry(@NotNull ScreenDirection direction, @NotNull Predicate<Entry> predicate, Entry selected) {
// ensure we don't focus unviewable entries
return super.nextEntry(direction, entry -> entry.isViewable() && predicate.test(entry), selected);
}
public abstract class Entry extends YACLSelectionList.Entry<Entry> {
protected boolean searchQueryMatches = true;
public Entry() {
super(OptionListWidget.this);
}
public boolean updateSearchQuery(String searchQuery) {
boolean matches = searchQuery.isEmpty();
if (this.searchQueryMatches != matches) {
this.searchQueryMatches = matches;
refreshVisibilityState();
}
return this.searchQueryMatches;
}
public boolean isViewable() {
return this.searchQueryMatches;
}
@Override
public int getHeight() {
if (!isViewable()) {
return 0;
}
return super.getHeight();
}
protected void refreshVisibilityState() {
if (isViewable()) {
onBecameViewable();
} else {
onBecameHidden();
}
}
protected void onBecameViewable() {
}
protected void onBecameHidden() {
this.setHeight(0);
}
public boolean preeditUpdated(@Nullable PreeditEvent event) {
return false;
}
}
public class OptionEntry extends Entry {
public final Option<?> option;
public final ConfigCategory category;
public final OptionGroup group;
public final @Nullable GroupSeparatorEntry groupSeparatorEntry;
public final AbstractWidget widget;
private final TextScaledButtonWidget resetButton;
private final String categoryName;
private final String groupName;
public OptionEntry(Option<?> option, ConfigCategory category, OptionGroup group, @Nullable GroupSeparatorEntry groupSeparatorEntry, AbstractWidget widget) {
this.option = option;
this.category = category;
this.group = group;
this.groupSeparatorEntry = groupSeparatorEntry;
this.widget = widget;
this.categoryName = category.name().getString().toLowerCase();
this.groupName = group.name().getString().toLowerCase();
if (option.canResetToDefault() && this.widget.canReset()) {
this.widget.setDimension(this.widget.getDimension().expanded(-20, 0));
this.resetButton = new TextScaledButtonWidget(yaclScreen, widget.getDimension().xLimit(), -50, 20, 20, 2f, Component.literal("\u21BB"), button -> {
option.requestSetDefault();
});
option.addListener((opt, val) -> this.resetButton.active = !opt.isPendingValueDefault() && opt.available());
this.resetButton.active = !option.isPendingValueDefault() && option.available();
} else {
this.resetButton = null;
}
this.updateHeight();
}
@Override
public void extractContent(@NonNull GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a) {
if (!this.isViewable()) {
return;
}
this.updateHeight();
widget.setDimension(widget.getDimension().withY(this.getY()));
widget.extractRenderState(graphics, mouseX, mouseY, a);
if (resetButton != null) {
resetButton.setY(this.getY());
resetButton.extractRenderState(graphics, mouseX, mouseY, a);
}
if (isMouseOver(mouseX, mouseY)) {
setHoverDescription(DescriptionWithName.of(option.name(), option.description()));
}
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double horizontal, double vertical) {
return widget.mouseScrolled(mouseX, mouseY, horizontal, vertical);
}
@Override
public boolean keyPressed(@NonNull KeyEvent event) {
return widget.keyPressed(event);
}
@Override
public boolean charTyped(@NonNull CharacterEvent event) {
return widget.charTyped(event);
}
@Override
public boolean preeditUpdated(@Nullable PreeditEvent event) {
return widget.preeditUpdated(event);
}
@Override
public boolean updateSearchQuery(String searchQuery) {
this.searchQueryMatches = searchQuery.isEmpty()
|| groupName.contains(searchQuery)
|| widget.matchesSearch(searchQuery);
refreshVisibilityState();
return this.searchQueryMatches;
}
@Override
public boolean isViewable() {
return super.isViewable()
&& (groupSeparatorEntry == null || groupSeparatorEntry.isExpanded());
}
@Override
protected void onBecameViewable() {
super.onBecameViewable();
updateHeight();
}
private void updateHeight() {
this.setHeight(Math.max(widget.getDimension().height(), resetButton != null ? resetButton.getHeight() : 0) + 2);
}
@Override
public void setFocused(boolean focused) {
super.setFocused(focused);
if (focused)
setHoverDescription(DescriptionWithName.of(option.name(), option.description()));
}
@Override
public List<? extends NarratableEntry> narratables() {
if (resetButton == null)
return ImmutableList.of(widget);
return ImmutableList.of(widget, resetButton);
}
@Override
public List<? extends GuiEventListener> children() {
if (resetButton == null)
return ImmutableList.of(widget);
return ImmutableList.of(widget, resetButton);
}
}
public class GroupSeparatorEntry extends Entry {
protected final OptionGroup group;
protected final MultiLineLabel wrappedName;
protected final MultiLineLabel wrappedTooltip;
protected final LowProfileButtonWidget expandMinimizeButton;
protected final Screen screen;
protected final Font font = Minecraft.getInstance().font;
protected boolean groupExpanded;
protected List<Entry> childEntries = new ArrayList<>();
private GroupSeparatorEntry(OptionGroup group, Screen screen) {
this.group = group;
this.screen = screen;
this.wrappedName = MultiLineLabel.create(font, group.name(), getRowWidth() - 45);
this.wrappedTooltip = MultiLineLabel.create(font, group.tooltip(), screen.width / 3 * 2 - 10);
this.groupExpanded = !group.collapsed();
this.expandMinimizeButton = new LowProfileButtonWidget(0, 0, 20, 20, Component.empty(), btn -> onExpandButtonPress());
updateExpandMinimizeText();
updateHeight();
}
@Override
public void extractContent(@NonNull GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a) {
if (!this.isViewable()) {
return;
}
this.updateHeight();
int buttonY = this.getY() + this.getHeight() / 2 - expandMinimizeButton.getHeight() / 2 + 1;
expandMinimizeButton.setY(buttonY);
expandMinimizeButton.setX(this.getX());
expandMinimizeButton.extractRenderState(graphics, mouseX, mouseY, a);
wrappedName.visitLines(TextAlignment.CENTER, this.getX() + this.getWidth() / 2, this.getY() + getYPadding(), font.lineHeight, graphics.textRenderer());
if (isMouseOver(mouseX, mouseY)) {
setHoverDescription(DescriptionWithName.of(group.name(), group.description()));
}
}
public boolean isExpanded() {
return groupExpanded;
}
public void setExpanded(boolean expanded) {
if (this.groupExpanded == expanded)
return;
this.groupExpanded = expanded;
updateExpandMinimizeText();
childEntries.forEach(Entry::refreshVisibilityState);
repositionEntries();
}
protected void onExpandButtonPress() {
setExpanded(!isExpanded());
}
protected void updateExpandMinimizeText() {
expandMinimizeButton.setMessage(Component.literal(isExpanded() ? "▼" : "▶"));
}
public void setChildEntries(List<? extends Entry> childEntries) {
this.childEntries.clear();
this.childEntries.addAll(childEntries);
}
@Override
public boolean updateSearchQuery(String searchQuery) {
return this.searchQueryMatches = searchQuery.isEmpty() || childEntries.stream().anyMatch(e -> e.updateSearchQuery(searchQuery));
}
private int getYPadding() {
return 6;
}
@Override
public void setFocused(boolean focused) {
super.setFocused(focused);
if (focused)
setHoverDescription(DescriptionWithName.of(group.name(), group.description()));
}
private void updateHeight() {
this.setHeight(Math.max(wrappedName.getLineCount(), 1) * font.lineHeight + getYPadding() * 2);
}
@Override
public @NotNull List<? extends NarratableEntry> narratables() {
return ImmutableList.of(new NarratableEntry() {
@Override
public @NotNull NarrationPriority narrationPriority() {
return NarrationPriority.HOVERED;
}
@Override
public void updateNarration(NarrationElementOutput builder) {
builder.add(NarratedElementType.TITLE, group.name());
builder.add(NarratedElementType.HINT, group.tooltip());
}
});
}
@Override
public @NotNull List<? extends GuiEventListener> children() {
return ImmutableList.of(expandMinimizeButton);
}
}
public class ListGroupSeparatorEntry extends GroupSeparatorEntry {
private final ListOption<?> listOption;
private final TextScaledButtonWidget resetListButton;
private final TooltipButtonWidget addListButton;
private ListGroupSeparatorEntry(ListOption<?> group, Screen screen) {
super(group, screen);
this.listOption = group;
this.resetListButton = new TextScaledButtonWidget(screen, getRowRight() - 20, -50, 20, 20, 1f, Component.literal("\u21BB"), button -> {
group.requestSetDefault();
});
group.addListener((opt, val) -> this.resetListButton.active = !opt.isPendingValueDefault() && opt.available());
this.resetListButton.active = !group.isPendingValueDefault() && group.available();
this.addListButton = new TooltipButtonWidget(yaclScreen, resetListButton.getX() - 20, -50, 20, 20, Component.literal("+"), Component.translatable("yacl.list.add_top"), btn -> {
group.insertNewEntry();
setExpanded(true);
});
updateExpandMinimizeText();
minimizeIfUnavailable();
}
@Override
public void extractContent(@NonNull GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a) {
if (!this.isViewable()) {
return;
}
updateExpandMinimizeText(); // update every render because option could become available/unavailable at any time
super.extractContent(graphics, mouseX, mouseY, hovered, a);
int buttonY = expandMinimizeButton.getY();
resetListButton.setY(buttonY);
addListButton.setY(buttonY);
resetListButton.extractRenderState(graphics, mouseX, mouseY, a);
addListButton.extractRenderState(graphics, mouseX, mouseY, a);
}
private void minimizeIfUnavailable() {
if (!listOption.available() && isExpanded()) {
setExpanded(false);
}
}
@Override
protected void updateExpandMinimizeText() {
super.updateExpandMinimizeText();
expandMinimizeButton.active = listOption == null || listOption.available();
if (addListButton != null)
addListButton.active = expandMinimizeButton.active && listOption.numberOfEntries() < listOption.maximumNumberOfEntries();
}
@Override
public void setExpanded(boolean expanded) {
super.setExpanded(listOption.available() && expanded);
}
@Override
public @NotNull List<? extends GuiEventListener> children() {
return ImmutableList.of(expandMinimizeButton, addListButton, resetListButton);
}
}
public class EmptyListLabel extends Entry {
private final ListGroupSeparatorEntry parent;
private final String groupName;
private final String categoryName;
public EmptyListLabel(ListGroupSeparatorEntry parent, ConfigCategory category) {
this.parent = parent;
this.groupName = parent.group.name().getString().toLowerCase();
this.categoryName = category.name().getString().toLowerCase();
this.setHeight(11);
}
@Override
public void extractContent(GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a) {
if (!this.isViewable()) {
return;
}
graphics.centeredText(Minecraft.getInstance().font, Component.translatable("yacl.list.empty").withStyle(ChatFormatting.DARK_GRAY, ChatFormatting.ITALIC), this.getX() + this.getWidth() / 2, this.getY(), -1);
}
@Override
public boolean updateSearchQuery(String searchQuery) {
return this.searchQueryMatches = searchQuery.isEmpty() || groupName.contains(searchQuery);
}
@Override
public boolean isViewable() {
return parent.isExpanded() && super.isViewable();
}
@Override
protected void onBecameViewable() {
super.onBecameViewable();
setHeight(11);
}
@Override
protected void onBecameHidden() {
super.onBecameHidden();
setHeight(0);
}
@Override
public List<? extends GuiEventListener> children() {
return ImmutableList.of();
}
@Override
public List<? extends NarratableEntry> narratables() {
return ImmutableList.of();
}
}
}