Skip to content

Commit 1c8afc9

Browse files
committed
mostly done 1.21.9 port - one weird list scroll bug
1 parent aae5d73 commit 1c8afc9

17 files changed

Lines changed: 291 additions & 92 deletions

src/main/java/dev/isxander/yacl3/gui/AbstractWidget.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ public void playDownSound() {
132132

133133
//? if >=1.21.9 {
134134
@Override
135-
public final boolean mouseClicked(MouseButtonEvent mouseButtonEvent, boolean doubleClick) {
135+
public boolean mouseClicked(MouseButtonEvent mouseButtonEvent, boolean doubleClick) {
136136
return this.onMouseClicked(mouseButtonEvent.x(), mouseButtonEvent.y(), mouseButtonEvent.button());
137137
}
138138
//?} else {
139139
/*@Override
140-
public final boolean mouseClicked(double mouseX, double mouseY, int button) {
140+
public boolean mouseClicked(double mouseX, double mouseY, int button) {
141141
return this.onMouseClicked(mouseX, mouseY, button);
142142
}
143143
*///?}
@@ -147,12 +147,12 @@ public boolean onMouseClicked(double mouseX, double mouseY, int button) {
147147

148148
//? if >=1.21.9 {
149149
@Override
150-
public final boolean mouseReleased(MouseButtonEvent mouseButtonEvent) {
150+
public boolean mouseReleased(MouseButtonEvent mouseButtonEvent) {
151151
return this.onMouseReleased(mouseButtonEvent.x(), mouseButtonEvent.y(), mouseButtonEvent.button());
152152
}
153153
//?} else {
154154
/*@Override
155-
public final boolean mouseReleased(double mouseX, double mouseY, int button) {
155+
public boolean mouseReleased(double mouseX, double mouseY, int button) {
156156
return this.onMouseReleased(mouseX, mouseY, button);
157157
}
158158
*///?}
@@ -162,12 +162,12 @@ public boolean onMouseReleased(double mouseX, double mouseY, int button) {
162162

163163
//? if >=1.21.9 {
164164
@Override
165-
public final boolean mouseDragged(MouseButtonEvent mouseButtonEvent, double dx, double dy) {
165+
public boolean mouseDragged(MouseButtonEvent mouseButtonEvent, double dx, double dy) {
166166
return this.onMouseDragged(mouseButtonEvent.x(), mouseButtonEvent.y(), mouseButtonEvent.button(), dx, dy);
167167
}
168168
//?} else {
169169
/*@Override
170-
public final boolean mouseDragged(double mouseX, double mouseY, int button, double dx, double dy) {
170+
public boolean mouseDragged(double mouseX, double mouseY, int button, double dx, double dy) {
171171
return this.onMouseDragged(mouseX, mouseY, button, dx, dy);
172172
}
173173
*///?}
@@ -177,12 +177,12 @@ public boolean onMouseDragged(double mouseX, double mouseY, int button, double d
177177

178178
//? if >=1.21.9 {
179179
@Override
180-
public final boolean keyPressed(KeyEvent keyEvent) {
180+
public boolean keyPressed(KeyEvent keyEvent) {
181181
return this.onKeyPressed(keyEvent.key(), keyEvent.scancode(), keyEvent.modifiers());
182182
}
183183
//?} else {
184184
/*@Override
185-
public final boolean keyPressed(int keycode, int scancode, int modifiers) {
185+
public boolean keyPressed(int keycode, int scancode, int modifiers) {
186186
return this.onKeyPressed(keycode, scancode, modifiers);
187187
}
188188
*///?}
@@ -192,12 +192,12 @@ public boolean onKeyPressed(int key, int scancode, int modifiers) {
192192

193193
//? if >=1.21.9 {
194194
@Override
195-
public final boolean keyReleased(KeyEvent keyEvent) {
195+
public boolean keyReleased(KeyEvent keyEvent) {
196196
return this.onKeyReleased(keyEvent.key(), keyEvent.scancode(), keyEvent.modifiers());
197197
}
198198
//?} else {
199199
/*@Override
200-
public final boolean keyReleased(int keycode, int scancode, int modifiers) {
200+
public boolean keyReleased(int keycode, int scancode, int modifiers) {
201201
return this.onKeyReleased(keycode, scancode, modifiers);
202202
}
203203
*///?}
@@ -207,12 +207,12 @@ public boolean onKeyReleased(int key, int scancode, int modifiers) {
207207

208208
//? if >=1.21.9 {
209209
@Override
210-
public final boolean charTyped(CharacterEvent characterEvent) {
210+
public boolean charTyped(CharacterEvent characterEvent) {
211211
return this.onCharTyped((char) characterEvent.codepoint(), characterEvent.codepointAsString(), characterEvent.modifiers());
212212
}
213213
//?} else {
214214
/*@Override
215-
public final boolean charTyped(char codePoint, int modifiers) {
215+
public boolean charTyped(char codePoint, int modifiers) {
216216
return this.onCharTyped(codePoint, Character.toString(codePoint), modifiers);
217217
}
218218
*///?}

src/main/java/dev/isxander/yacl3/gui/LegacySelectionList.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public LegacySelectionList(Minecraft client, int y, int width, int height) {
3636
/^setRenderHeader(false, 0);^/
3737
}
3838
39+
// for parity with modern, no need in legacy as everything is positioned on render
40+
protected void repositionEntries() {}
41+
3942
/^
4043
The default implementation of scrollbarX does not respect left/right positioning of the list.
4144
^/

src/main/java/dev/isxander/yacl3/gui/ModernSelectionList.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,35 @@
22
package dev.isxander.yacl3.gui;
33

44
import net.minecraft.client.Minecraft;
5+
import net.minecraft.client.gui.GuiGraphics;
56
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
67
import net.minecraft.client.input.CharacterEvent;
78
import net.minecraft.client.input.KeyEvent;
89
import net.minecraft.client.input.MouseButtonEvent;
910
import net.minecraft.client.input.MouseButtonInfo;
1011

1112
public abstract class ModernSelectionList<E extends ModernSelectionList.Entry<E>> extends ContainerObjectSelectionList<E> {
13+
private Double prevScrollAmount = null;
1214

1315
public ModernSelectionList(Minecraft minecraft, int width, int height, int y, int defaultEntryHeight) {
1416
super(minecraft, width, height, y, defaultEntryHeight);
1517
}
1618

19+
@Override
20+
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
21+
if (this.prevScrollAmount == null || this.prevScrollAmount != this.scrollAmount()) {
22+
this.prevScrollAmount = this.scrollAmount();
23+
System.out.println(this.scrollAmount());
24+
}
25+
26+
super.renderWidget(guiGraphics, mouseX, mouseY, partialTick);
27+
}
28+
29+
protected void repositionEntries() {
30+
// triggers super.repositionEntries() without the need for a mixin accessor
31+
this.setScrollAmount(this.scrollAmount());
32+
}
33+
1734
protected boolean mouseClicked(double mouseX, double mouseY, int button) {
1835
return super.mouseClicked(new MouseButtonEvent(mouseX, mouseY, new MouseButtonInfo(button, 0)), false);
1936
}

src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java

Lines changed: 76 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
public class OptionListWidget extends YACLSelectionList<OptionListWidget.Entry> {
2929
private final YACLScreen yaclScreen;
3030
private final ConfigCategory category;
31-
private ImmutableList<Entry> viewableChildren;
3231
private String searchQuery = "";
3332
private final Consumer<DescriptionWithName> hoverEvent;
3433
private DescriptionWithName lastHoveredOption;
@@ -84,22 +83,28 @@ public void refreshOptions() {
8483
}
8584
}
8685

87-
recacheViewableChildren();
8886
setScrollAmount(0);
89-
//resetSmoothScrolling();
9087
}
9188

89+
//? if >=1.21.9 {
90+
@Override
91+
protected int addEntry(Entry entry) {
92+
// instead of using super.defaultEntryHeight, use the height the entry wants to be - our entries set their height in the constructor
93+
return this.addEntry(entry, entry.getHeight());
94+
}
95+
//?}
96+
9297
private void refreshListEntries(ListOption<?> listOption, ConfigCategory category) {
9398
// find group separator for group
94-
ListGroupSeparatorEntry groupSeparator = super.children().stream().filter(e -> e instanceof ListGroupSeparatorEntry gs && gs.group == listOption).map(ListGroupSeparatorEntry.class::cast).findAny().orElse(null);
99+
ListGroupSeparatorEntry groupSeparator = this.children().stream().filter(e -> e instanceof ListGroupSeparatorEntry gs && gs.group == listOption).map(ListGroupSeparatorEntry.class::cast).findAny().orElse(null);
95100

96101
if (groupSeparator == null) {
97102
YACLConstants.LOGGER.warn("Can't find group seperator to refresh list option entries for list option " + listOption.name());
98103
return;
99104
}
100105

101106
for (Entry entry : groupSeparator.childEntries)
102-
super.removeEntry(entry);
107+
this.removeEntry(entry);
103108
groupSeparator.childEntries.clear();
104109

105110
// if no entries, below loop won't run where addEntryBelow() recaches viewable children
@@ -143,8 +148,11 @@ public int getRowWidth() {
143148

144149
public void updateSearchQuery(String query) {
145150
this.searchQuery = query;
151+
for (Entry entry : this.children()) {
152+
entry.updateSearchQuery(query);
153+
}
146154
expandAllGroups();
147-
recacheViewableChildren();
155+
repositionEntries();
148156
}
149157

150158
@Override
@@ -197,31 +205,18 @@ public boolean charTyped(char chr, int modifiers) {
197205
return super.charTyped(chr, modifiers);
198206
}
199207

200-
public void recacheViewableChildren() {
201-
this.viewableChildren = ImmutableList.copyOf(super.children().stream().filter(Entry::isViewable).toList());
202-
203-
// update y positions before they need to be rendered are rendered
204-
int i = 0;
205-
for (Entry entry : viewableChildren) {
206-
if (entry instanceof OptionEntry optionEntry)
207-
optionEntry.widget.setDimension(optionEntry.widget.getDimension().withY(getRowTop(i)));
208-
i++;
209-
}
210-
}
211-
212-
@Override @NotNull
213-
public List<Entry> children() {
214-
return viewableChildren;
215-
}
216-
217208
private List<Entry> superModifiableChildren() {
209+
//? if >=1.21.9 {
218210
// noinspection unchecked
219211
return (List<Entry>) ((AbstractSelectionListAccessor) this).getChildren();
212+
//?} else {
213+
/*return this.children();
214+
*///?}
220215
}
221216

222217
public void addEntryAtIndex(int index, Entry entry) {
223218
superModifiableChildren().add(index, entry);
224-
recacheViewableChildren();
219+
this.repositionEntries();
225220
}
226221

227222
public void addEntryBelow(Entry below, Entry entry) {
@@ -239,21 +234,6 @@ public void addEntryBelowWithoutScroll(Entry below, Entry entry) {
239234
setScrollAmount(this.contentHeight() - d);
240235
}
241236

242-
//? if >=1.21.9 {
243-
@Override
244-
protected void removeEntry(Entry entry) {
245-
super.removeEntry(entry);
246-
recacheViewableChildren();
247-
}
248-
//?} else {
249-
/*@Override
250-
public boolean removeEntry(Entry entry) {
251-
boolean ret = super.removeEntry(entry);
252-
recacheViewableChildren();
253-
return ret;
254-
}
255-
*///?}
256-
257237
private void setHoverDescription(DescriptionWithName description) {
258238
if (description != lastHoveredOption) {
259239
lastHoveredOption = description;
@@ -273,12 +253,26 @@ protected boolean isValidMouseClick(int button) {
273253
}
274254

275255
public abstract class Entry extends YACLSelectionList.Entry<Entry> {
256+
protected boolean searchQueryMatches = true;
257+
276258
public Entry() {
277259
super(OptionListWidget.this);
278260
}
279261

262+
public boolean updateSearchQuery(String searchQuery) {
263+
return this.searchQueryMatches;
264+
}
265+
280266
public boolean isViewable() {
281-
return true;
267+
return this.searchQueryMatches;
268+
}
269+
270+
@Override
271+
public int getHeight() {
272+
if (!isViewable()) {
273+
return 0;
274+
}
275+
return super.getHeight();
282276
}
283277
}
284278

@@ -314,11 +308,16 @@ public OptionEntry(Option<?> option, ConfigCategory category, OptionGroup group,
314308
} else {
315309
this.resetButton = null;
316310
}
311+
this.updateHeight();
317312
}
318313

319314
@Override
320315
public void renderContent(GuiGraphics graphics, int mouseX, int mouseY, boolean hovered, float deltaTicks) {
321-
this.setHeight(Math.max(widget.getDimension().height(), resetButton != null ? resetButton.getHeight() : 0) + 2);
316+
if (!this.isViewable()) {
317+
return;
318+
}
319+
320+
this.updateHeight();
322321

323322
widget.setDimension(widget.getDimension().withY(this.getY()));
324323

@@ -350,11 +349,20 @@ public boolean charTyped(char chr, int modifiers) {
350349
}
351350

352351
@Override
353-
public boolean isViewable() {
354-
return (groupSeparatorEntry == null || groupSeparatorEntry.isExpanded())
355-
&& (searchQuery.isEmpty()
352+
public boolean updateSearchQuery(String searchQuery) {
353+
return this.searchQueryMatches = searchQuery.isEmpty()
356354
|| groupName.contains(searchQuery)
357-
|| widget.matchesSearch(searchQuery));
355+
|| widget.matchesSearch(searchQuery);
356+
}
357+
358+
@Override
359+
public boolean isViewable() {
360+
return super.isViewable()
361+
&& (groupSeparatorEntry == null || groupSeparatorEntry.isExpanded());
362+
}
363+
364+
private void updateHeight() {
365+
this.setHeight(Math.max(widget.getDimension().height(), resetButton != null ? resetButton.getHeight() : 0) + 2);
358366
}
359367

360368
@Override
@@ -403,11 +411,16 @@ private GroupSeparatorEntry(OptionGroup group, Screen screen) {
403411
this.groupExpanded = !group.collapsed();
404412
this.expandMinimizeButton = new LowProfileButtonWidget(0, 0, 20, 20, Component.empty(), btn -> onExpandButtonPress());
405413
updateExpandMinimizeText();
414+
updateHeight();
406415
}
407416

408417
@Override
409418
public void renderContent(GuiGraphics graphics, int mouseX, int mouseY, boolean hovered, float deltaTicks) {
410-
this.setHeight(Math.max(wrappedName.getLineCount(), 1) * font.lineHeight + getYPadding() * 2);
419+
if (!this.isViewable()) {
420+
return;
421+
}
422+
423+
this.updateHeight();
411424

412425
int buttonY = this.getY() + this.getHeight() / 2 - expandMinimizeButton.getHeight() / 2 + 1;
413426

@@ -436,7 +449,7 @@ public void setExpanded(boolean expanded) {
436449

437450
this.groupExpanded = expanded;
438451
updateExpandMinimizeText();
439-
recacheViewableChildren();
452+
repositionEntries();
440453
}
441454

442455
protected void onExpandButtonPress() {
@@ -453,8 +466,8 @@ public void setChildEntries(List<? extends Entry> childEntries) {
453466
}
454467

455468
@Override
456-
public boolean isViewable() {
457-
return searchQuery.isEmpty() || childEntries.stream().anyMatch(Entry::isViewable);
469+
public boolean updateSearchQuery(String searchQuery) {
470+
return this.searchQueryMatches = searchQuery.isEmpty() || childEntries.stream().anyMatch(e -> e.updateSearchQuery(searchQuery));
458471
}
459472

460473
private int getYPadding() {
@@ -468,6 +481,10 @@ public void setFocused(boolean focused) {
468481
setHoverDescription(DescriptionWithName.of(group.name(), group.description()));
469482
}
470483

484+
private void updateHeight() {
485+
this.setHeight(Math.max(wrappedName.getLineCount(), 1) * font.lineHeight + getYPadding() * 2);
486+
}
487+
471488
@Override
472489
public @NotNull List<? extends NarratableEntry> narratables() {
473490
return ImmutableList.of(new NarratableEntry() {
@@ -517,6 +534,10 @@ private ListGroupSeparatorEntry(ListOption<?> group, Screen screen) {
517534

518535
@Override
519536
public void renderContent(GuiGraphics graphics, int mouseX, int mouseY, boolean hovered, float deltaTicks) {
537+
if (!this.isViewable()) {
538+
return;
539+
}
540+
520541
updateExpandMinimizeText(); // update every render because option could become available/unavailable at any time
521542

522543
super.renderContent(graphics, mouseX, mouseY, hovered, deltaTicks);
@@ -572,9 +593,14 @@ public void renderContent(GuiGraphics graphics, int mouseX, int mouseY, boolean
572593
graphics.drawCenteredString(Minecraft.getInstance().font, Component.translatable("yacl.list.empty").withStyle(ChatFormatting.DARK_GRAY, ChatFormatting.ITALIC), this.getX() + this.getWidth() / 2, this.getY(), -1);
573594
}
574595

596+
@Override
597+
public boolean updateSearchQuery(String searchQuery) {
598+
return this.searchQueryMatches = searchQuery.isEmpty() || groupName.contains(searchQuery);
599+
}
600+
575601
@Override
576602
public boolean isViewable() {
577-
return parent.isExpanded() && (searchQuery.isEmpty() || groupName.contains(searchQuery));
603+
return parent.isExpanded() && super.isViewable();
578604
}
579605

580606
@Override

0 commit comments

Comments
 (0)