AbstractSelectionList .java

net.minecraft.client.gui.components.AbstractSelectionList (class, 572 lines): members: INWORLD_MENU_LIST_BACKGROUND, updateSizeAndPosition, centerScrollOn.

net.minecraft.client.gui.componentsclass572 linesclientextends AbstractSelectionList.Entry<E>>15.9 KB

API Surface (67)

void setSelected(final @Nullable E selected)
List<E> children()
void sort(final Comparator<E> comparator)
void swap(final int firstIndex, final int secondIndex)
void clearEntries()
void clearEntriesExcept(final E exception)
void replaceEntries(final Collection<E> newChildren)
int getNextY()
int addEntry(final E entry)
int addEntry(final E entry, final int height)
void addEntryToTop(final E entry)
void addEntryToTop(final E entry, final int height)
void removeEntryFromTop(final E entry)
int getItemCount()
boolean entriesCanBeSelected()
void updateSize(final int width, final HeaderAndFooterLayout layout)
void updateSizeAndPosition(final int width, final int height, final int y)
void updateSizeAndPosition(final int width, final int height, final int x, final int y)
int contentHeight()
void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
void extractListSeparators(final GuiGraphicsExtractor graphics)
void extractListBackground(final GuiGraphicsExtractor graphics)
void enableScissor(final GuiGraphicsExtractor graphics)
void scrollToEntry(final E entry)
void centerScrollOn(final E entry)
void setScrollAmount(final double scrollAmount)
int scrollBarX()
Optional<GuiEventListener> getChildAt(final double x, final double y)
void setFocused(final @Nullable GuiEventListener focused)
void extractListItems(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a)
void extractItem(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a, final E entry)
void extractSelection(final GuiGraphicsExtractor graphics, final E entry, final int outlineColor)
int getRowLeft()
int getRowRight()
int getRowTop(final int row)
int getRowBottom(final int row)
int getRowWidth()
NarratableEntry.NarrationPriority narrationPriority()
void removeEntries(final List<E> entries)
void removeEntry(final E entry)
void narrateListElementPosition(final NarrationElementOutput output, final E element)
void setFocused(final boolean focused)
boolean isFocused()
boolean isMouseOver(final double mx, final double my)
void setX(final int x)
void setY(final int y)
void setWidth(final int width)
void setHeight(final int height)
int getContentX()
int getContentY()
int getContentHeight()
int getContentYMiddle()
int getContentBottom()
int getContentWidth()
int getContentXMiddle()
int getContentRight()
int getX()
int getY()
int getWidth()
int getHeight()
void visitWidgets(final Consumer<AbstractWidget> widgetVisitor)
ScreenRectangle getRectangle()
E get(final int index)
int size()
E set(final int index, final E element)
void add(final int index, final E element)
E remove(final int index)

Fields & Constants (10)

MENU_LIST_BACKGROUND
INWORLD_MENU_LIST_BACKGROUND
SEPARATOR_HEIGHT
minecraft
defaultEntryHeight
children
centerListVertically
selected
hovered
LayoutElement

String literals (3)

"textures/gui/menu_list_background.png"
"textures/gui/inworld_menu_list_background.png"
"narrator.position.list"

Full Source

AbstractSelectionList.java572 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui.components;
2
3
import com.google.common.collect.Lists;
4
import java.util.AbstractList;
5
import java.util.Collection;
6
import java.util.Collections;
7
import java.util.Comparator;
8
import java.util.List;
9
import java.util.Objects;
10
import java.util.Optional;
11
import java.util.function.Consumer;
12
import java.util.function.Predicate;
13
import net.fabricmc.api.EnvType;
14
import net.fabricmc.api.Environment;
15
import net.minecraft.client.Minecraft;
16
import net.minecraft.client.gui.GuiGraphicsExtractor;
17
import net.minecraft.client.gui.components.events.ContainerEventHandler;
18
import net.minecraft.client.gui.components.events.GuiEventListener;
19
import net.minecraft.client.gui.layouts.HeaderAndFooterLayout;
20
import net.minecraft.client.gui.layouts.LayoutElement;
21
import net.minecraft.client.gui.narration.NarratableEntry;
22
import net.minecraft.client.gui.narration.NarratedElementType;
23
import net.minecraft.client.gui.narration.NarrationElementOutput;
24
import net.minecraft.client.gui.navigation.ScreenDirection;
25
import net.minecraft.client.gui.navigation.ScreenRectangle;
26
import net.minecraft.client.gui.screens.Screen;
27
import net.minecraft.client.renderer.RenderPipelines;
28
import net.minecraft.network.chat.CommonComponents;
29
import net.minecraft.network.chat.Component;
30
import net.minecraft.resources.Identifier;
31
import net.minecraft.util.Mth;
32
import org.jspecify.annotations.Nullable;
33
34
@Environment(EnvType.CLIENT)
35
public abstract class AbstractSelectionList<E extends AbstractSelectionList.Entry<E>> extends AbstractContainerWidget {
36
	private static final Identifier MENU_LIST_BACKGROUND = Identifier.withDefaultNamespace("textures/gui/menu_list_background.png");
37
	public static final Identifier INWORLD_MENU_LIST_BACKGROUND = Identifier.withDefaultNamespace("textures/gui/inworld_menu_list_background.png");
38
	private static final int SEPARATOR_HEIGHT = 2;
39
	protected final Minecraft minecraft;
40
	protected final int defaultEntryHeight;
41
	private final List<E> children = new AbstractSelectionList.TrackedList();
42
	protected boolean centerListVertically = true;
43
	private @Nullable E selected;
44
	private @Nullable E hovered;
45
46
	public AbstractSelectionList(final Minecraft minecraft, final int width, final int height, final int y, final int defaultEntryHeight) {
47
		super(0, y, width, height, CommonComponents.EMPTY, AbstractScrollArea.defaultSettings(defaultEntryHeight / 2));
48
		this.minecraft = minecraft;
49
		this.defaultEntryHeight = defaultEntryHeight;
50
	}
51
52
	public @Nullable E getSelected() {
53
		return this.selected;
54
	}
55
56
	public void setSelected(final @Nullable E selected) {
57
		this.selected = selected;
58
		if (selected != null) {
59
			boolean topClipped = selected.getContentY() < this.getY();
60
			boolean bottomClipped = selected.getContentBottom() > this.getBottom();
61
			if (this.minecraft.getLastInputType().isKeyboard() || topClipped || bottomClipped) {
62
				this.scrollToEntry(selected);
63
			}
64
		}
65
	}
66
67
	public @Nullable E getFocused() {
68
		return (E)super.getFocused();
69
	}
70
71
	@Override
72
	public final List<E> children() {
73
		return Collections.unmodifiableList(this.children);
74
	}
75
76
	protected void sort(final Comparator<E> comparator) {
77
		this.children.sort(comparator);
78
		this.repositionEntries();
79
	}
80
81
	protected void swap(final int firstIndex, final int secondIndex) {
82
		Collections.swap(this.children, firstIndex, secondIndex);
83
		this.repositionEntries();
84
		this.scrollToEntry(this.children.get(secondIndex));
85
	}
86
87
	protected void clearEntries() {
88
		this.children.clear();
89
		this.selected = null;
90
	}
91
92
	protected void clearEntriesExcept(final E exception) {
93
		this.children.removeIf(entry -> entry != exception);
94
		if (this.selected != exception) {
95
			this.setSelected(null);
96
		}
97
	}
98
99
	public void replaceEntries(final Collection<E> newChildren) {
100
		this.clearEntries();
101
102
		for (E newChild : newChildren) {
103
			this.addEntry(newChild);
104
		}
105
	}
106
107
	private int getFirstEntryY() {
108
		return this.getY() + 2;
109
	}
110
111
	public int getNextY() {
112
		int y = this.getFirstEntryY() - (int)this.scrollAmount();
113
114
		for (E child : this.children) {
115
			y += child.getHeight();
116
		}
117
118
		return y;
119
	}
120
121
	protected int addEntry(final E entry) {
122
		return this.addEntry(entry, this.defaultEntryHeight);
123
	}
124
125
	protected int addEntry(final E entry, final int height) {
126
		entry.setX(this.getRowLeft());
127
		entry.setWidth(this.getRowWidth());
128
		entry.setY(this.getNextY());
129
		entry.setHeight(height);
130
		this.children.add(entry);
131
		return this.children.size() - 1;
132
	}
133
134
	protected void addEntryToTop(final E entry) {
135
		this.addEntryToTop(entry, this.defaultEntryHeight);
136
	}
137
138
	protected void addEntryToTop(final E entry, final int height) {
139
		double scrollFromBottom = this.maxScrollAmount() - this.scrollAmount();
140
		entry.setHeight(height);
141
		this.children.addFirst(entry);
142
		this.repositionEntries();
143
		this.setScrollAmount(this.maxScrollAmount() - scrollFromBottom);
144
	}
145
146
	private void repositionEntries() {
147
		int y = this.getFirstEntryY() - (int)this.scrollAmount();
148
149
		for (E child : this.children) {
150
			child.setY(y);
151
			y += child.getHeight();
152
			child.setX(this.getRowLeft());
153
			child.setWidth(this.getRowWidth());
154
		}
155
	}
156
157
	protected void removeEntryFromTop(final E entry) {
158
		double scrollFromBottom = this.maxScrollAmount() - this.scrollAmount();
159
		this.removeEntry(entry);
160
		this.setScrollAmount(this.maxScrollAmount() - scrollFromBottom);
161
	}
162
163
	protected int getItemCount() {
164
		return this.children().size();
165
	}
166
167
	protected boolean entriesCanBeSelected() {
168
		return true;
169
	}
170
171
	protected final @Nullable E getEntryAtPosition(final double posX, final double posY) {
172
		for (E child : this.children) {
173
			if (child.isMouseOver(posX, posY)) {
174
				return child;
175
			}
176
		}
177
178
		return null;
179
	}
180
181
	public void updateSize(final int width, final HeaderAndFooterLayout layout) {
182
		this.updateSizeAndPosition(width, layout.getContentHeight(), layout.getHeaderHeight());
183
	}
184
185
	public void updateSizeAndPosition(final int width, final int height, final int y) {
186
		this.updateSizeAndPosition(width, height, 0, y);
187
	}
188
189
	public void updateSizeAndPosition(final int width, final int height, final int x, final int y) {
190
		this.setSize(width, height);
191
		this.setPosition(x, y);
192
		this.repositionEntries();
193
		if (this.getSelected() != null) {
194
			this.scrollToEntry(this.getSelected());
195
		}
196
197
		this.refreshScrollAmount();
198
	}
199
200
	@Override
201
	protected int contentHeight() {
202
		int totalHeight = 0;
203
204
		for (E child : this.children) {
205
			totalHeight += child.getHeight();
206
		}
207
208
		return totalHeight + 4;
209
	}
210
211
	@Override
212
	public void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
213
		this.hovered = this.isMouseOver(mouseX, mouseY) ? this.getEntryAtPosition(mouseX, mouseY) : null;
214
		this.extractListBackground(graphics);
215
		this.enableScissor(graphics);
216
		this.extractListItems(graphics, mouseX, mouseY, a);
217
		graphics.disableScissor();
218
		this.extractListSeparators(graphics);
219
		this.extractScrollbar(graphics, mouseX, mouseY);
220
	}
221
222
	protected void extractListSeparators(final GuiGraphicsExtractor graphics) {
223
		Identifier headerSeparator = this.minecraft.level == null ? Screen.HEADER_SEPARATOR : Screen.INWORLD_HEADER_SEPARATOR;
224
		Identifier footerSeparator = this.minecraft.level == null ? Screen.FOOTER_SEPARATOR : Screen.INWORLD_FOOTER_SEPARATOR;
225
		graphics.blit(RenderPipelines.GUI_TEXTURED, headerSeparator, this.getX(), this.getY() - 2, 0.0F, 0.0F, this.getWidth(), 2, 32, 2);
226
		graphics.blit(RenderPipelines.GUI_TEXTURED, footerSeparator, this.getX(), this.getBottom(), 0.0F, 0.0F, this.getWidth(), 2, 32, 2);
227
	}
228
229
	protected void extractListBackground(final GuiGraphicsExtractor graphics) {
230
		Identifier menuListBackground = this.minecraft.level == null ? MENU_LIST_BACKGROUND : INWORLD_MENU_LIST_BACKGROUND;
231
		graphics.blit(
232
			RenderPipelines.GUI_TEXTURED,
233
			menuListBackground,
234
			this.getX(),
235
			this.getY(),
236
			this.getRight(),
237
			this.getBottom() + (int)this.scrollAmount(),
238
			this.getWidth(),
239
			this.getHeight(),
240
			32,
241
			32
242
		);
243
	}
244
245
	protected void enableScissor(final GuiGraphicsExtractor graphics) {
246
		graphics.enableScissor(
247
			Mth.clamp(this.getX(), 0, graphics.guiWidth()),
248
			Mth.clamp(this.getY(), 0, graphics.guiHeight()),
249
			Mth.clamp(this.getRight(), 0, graphics.guiWidth()),
250
			Mth.clamp(this.getBottom(), 0, graphics.guiHeight())
251
		);
252
	}
253
254
	protected void scrollToEntry(final E entry) {
255
		int topDelta = entry.getY() - this.getY() - 2;
256
		if (topDelta < 0) {
257
			this.scroll(topDelta);
258
		}
259
260
		int bottomDelta = this.getBottom() - entry.getY() - entry.getHeight() - 2;
261
		if (bottomDelta < 0) {
262
			this.scroll(-bottomDelta);
263
		}
264
	}
265
266
	protected void centerScrollOn(final E entry) {
267
		int y = 0;
268
269
		for (E child : this.children) {
270
			if (child == entry) {
271
				y += child.getHeight() / 2;
272
				break;
273
			}
274
275
			y += child.getHeight();
276
		}
277
278
		this.setScrollAmount(y - this.height / 2.0);
279
	}
280
281
	private void scroll(final int amount) {
282
		this.setScrollAmount(this.scrollAmount() + amount);
283
	}
284
285
	@Override
286
	public void setScrollAmount(final double scrollAmount) {
287
		super.setScrollAmount(scrollAmount);
288
		this.repositionEntries();
289
	}
290
291
	@Override
292
	protected int scrollBarX() {
293
		return this.getRowRight() + this.scrollbarWidth() + 2;
294
	}
295
296
	@Override
297
	public Optional<GuiEventListener> getChildAt(final double x, final double y) {
298
		return Optional.ofNullable(this.getEntryAtPosition(x, y));
299
	}
300
301
	@Override
302
	public void setFocused(final @Nullable GuiEventListener focused) {
303
		E oldFocus = this.getFocused();
304
		if (oldFocus != focused && oldFocus instanceof ContainerEventHandler oldFocusContainer) {
305
			oldFocusContainer.setFocused(null);
306
		}
307
308
		super.setFocused(focused);
309
		int index = this.children.indexOf(focused);
310
		if (index >= 0) {
311
			E magicallyCastEntry = this.children.get(index);
312
			this.setSelected(magicallyCastEntry);
313
		}
314
	}
315
316
	protected @Nullable E nextEntry(final ScreenDirection dir) {
317
		return this.nextEntry(dir, entry -> true);
318
	}
319
320
	protected @Nullable E nextEntry(final ScreenDirection dir, final Predicate<E> canSelect) {
321
		return this.nextEntry(dir, canSelect, this.getSelected());
322
	}
323
324
	protected @Nullable E nextEntry(final ScreenDirection dir, final Predicate<E> canSelect, final @Nullable E startEntry) {
325
		int delta = switch (dir) {
326
			case RIGHT, LEFT -> 0;
327
			case UP -> -1;
328
			case DOWN -> 1;
329
		};
330
		if (!this.children().isEmpty() && delta != 0) {
331
			int index;
332
			if (startEntry == null) {
333
				index = delta > 0 ? 0 : this.children().size() - 1;
334
			} else {
335
				index = this.children().indexOf(startEntry) + delta;
336
			}
337
338
			for (int i = index; i >= 0 && i < this.children.size(); i += delta) {
339
				E selected = this.children().get(i);
340
				if (canSelect.test(selected)) {
341
					return selected;
342
				}
343
			}
344
		}
345
346
		return null;
347
	}
348
349
	protected void extractListItems(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
350
		for (E child : this.children) {
351
			if (child.getY() + child.getHeight() >= this.getY() && child.getY() <= this.getBottom()) {
352
				this.extractItem(graphics, mouseX, mouseY, a, child);
353
			}
354
		}
355
	}
356
357
	protected void extractItem(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a, final E entry) {
358
		if (this.entriesCanBeSelected() && this.getSelected() == entry) {
359
			int outlineColor = this.isFocused() ? -1 : -8355712;
360
			this.extractSelection(graphics, entry, outlineColor);
361
		}
362
363
		entry.extractContent(graphics, mouseX, mouseY, Objects.equals(this.hovered, entry), a);
364
	}
365
366
	protected void extractSelection(final GuiGraphicsExtractor graphics, final E entry, final int outlineColor) {
367
		int outlineX0 = entry.getX();
368
		int outlineY0 = entry.getY();
369
		int outlineX1 = outlineX0 + entry.getWidth();
370
		int outlineY1 = outlineY0 + entry.getHeight();
371
		graphics.fill(outlineX0, outlineY0, outlineX1, outlineY1, outlineColor);
372
		graphics.fill(outlineX0 + 1, outlineY0 + 1, outlineX1 - 1, outlineY1 - 1, -16777216);
373
	}
374
375
	public int getRowLeft() {
376
		return this.getX() + this.width / 2 - this.getRowWidth() / 2;
377
	}
378
379
	public int getRowRight() {
380
		return this.getRowLeft() + this.getRowWidth();
381
	}
382
383
	public int getRowTop(final int row) {
384
		return this.children.get(row).getY();
385
	}
386
387
	public int getRowBottom(final int row) {
388
		E child = this.children.get(row);
389
		return child.getY() + child.getHeight();
390
	}
391
392
	public int getRowWidth() {
393
		return 220;
394
	}
395
396
	@Override
397
	public NarratableEntry.NarrationPriority narrationPriority() {
398
		if (this.isFocused()) {
399
			return NarratableEntry.NarrationPriority.FOCUSED;
400
		} else {
401
			return this.hovered != null ? NarratableEntry.NarrationPriority.HOVERED : NarratableEntry.NarrationPriority.NONE;
402
		}
403
	}
404
405
	protected void removeEntries(final List<E> entries) {
406
		entries.forEach(this::removeEntry);
407
	}
408
409
	protected void removeEntry(final E entry) {
410
		boolean removed = this.children.remove(entry);
411
		if (removed) {
412
			this.repositionEntries();
413
			if (entry == this.getSelected()) {
414
				this.setSelected(null);
415
			}
416
		}
417
	}
418
419
	protected @Nullable E getHovered() {
420
		return this.hovered;
421
	}
422
423
	private void bindEntryToSelf(final AbstractSelectionList.Entry<E> entry) {
424
		entry.list = this;
425
	}
426
427
	protected void narrateListElementPosition(final NarrationElementOutput output, final E element) {
428
		List<E> children = this.children();
429
		if (children.size() > 1) {
430
			int index = children.indexOf(element);
431
			if (index != -1) {
432
				output.add(NarratedElementType.POSITION, Component.translatable("narrator.position.list", index + 1, children.size()));
433
			}
434
		}
435
	}
436
437
	@Environment(EnvType.CLIENT)
438
	protected abstract static class Entry<E extends AbstractSelectionList.Entry<E>> implements LayoutElement, GuiEventListener {
439
		public static final int CONTENT_PADDING = 2;
440
		private int x = 0;
441
		private int y = 0;
442
		private int width = 0;
443
		private int height;
444
		@Deprecated
445
		private AbstractSelectionList<E> list;
446
447
		@Override
448
		public void setFocused(final boolean focused) {
449
		}
450
451
		@Override
452
		public boolean isFocused() {
453
			return this.list.getFocused() == this;
454
		}
455
456
		public abstract void extractContent(final GuiGraphicsExtractor graphics, int mouseX, int mouseY, boolean hovered, float a);
457
458
		@Override
459
		public boolean isMouseOver(final double mx, final double my) {
460
			return this.getRectangle().containsPoint((int)mx, (int)my);
461
		}
462
463
		@Override
464
		public void setX(final int x) {
465
			this.x = x;
466
		}
467
468
		@Override
469
		public void setY(final int y) {
470
			this.y = y;
471
		}
472
473
		public void setWidth(final int width) {
474
			this.width = width;
475
		}
476
477
		public void setHeight(final int height) {
478
			this.height = height;
479
		}
480
481
		public int getContentX() {
482
			return this.getX() + 2;
483
		}
484
485
		public int getContentY() {
486
			return this.getY() + 2;
487
		}
488
489
		public int getContentHeight() {
490
			return this.getHeight() - 4;
491
		}
492
493
		public int getContentYMiddle() {
494
			return this.getContentY() + this.getContentHeight() / 2;
495
		}
496
497
		public int getContentBottom() {
498
			return this.getContentY() + this.getContentHeight();
499
		}
500
501
		public int getContentWidth() {
502
			return this.getWidth() - 4;
503
		}
504
505
		public int getContentXMiddle() {
506
			return this.getContentX() + this.getContentWidth() / 2;
507
		}
508
509
		public int getContentRight() {
510
			return this.getContentX() + this.getContentWidth();
511
		}
512
513
		@Override
514
		public int getX() {
515
			return this.x;
516
		}
517
518
		@Override
519
		public int getY() {
520
			return this.y;
521
		}
522
523
		@Override
524
		public int getWidth() {
525
			return this.width;
526
		}
527
528
		@Override
529
		public int getHeight() {
530
			return this.height;
531
		}
532
533
		@Override
534
		public void visitWidgets(final Consumer<AbstractWidget> widgetVisitor) {
535
		}
536
537
		@Override
538
		public ScreenRectangle getRectangle() {
539
			return LayoutElement.super.getRectangle();
540
		}
541
	}
542
543
	@Environment(EnvType.CLIENT)
544
	private class TrackedList extends AbstractList<E> {
545
		private final List<E> delegate = Lists.newArrayList();
546
547
		public E get(final int index) {
548
			return this.delegate.get(index);
549
		}
550
551
		@Override
552
		public int size() {
553
			return this.delegate.size();
554
		}
555
556
		public E set(final int index, final E element) {
557
			E entry = this.delegate.set(index, element);
558
			AbstractSelectionList.this.bindEntryToSelf(element);
559
			return entry;
560
		}
561
562
		public void add(final int index, final E element) {
563
			this.delegate.add(index, element);
564
			AbstractSelectionList.this.bindEntryToSelf(element);
565
		}
566
567
		public E remove(final int index) {
568
			return this.delegate.remove(index);
569
		}
570
	}
571
}
572

Decompiled Minecraft 26.3 source of AbstractSelectionList (net/minecraft/client/gui/components/AbstractSelectionList.java) with Mojang official mappings, stored verbatim on the site.