| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Minecraft;
|
| 6 | import net.minecraft.client.gui.ComponentPath;
|
| 7 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 8 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 9 | import net.minecraft.client.gui.narration.NarrationSupplier;
|
| 10 | import net.minecraft.client.gui.navigation.FocusNavigationEvent;
|
| 11 | import net.minecraft.client.input.MouseButtonEvent;
|
| 12 | import net.minecraft.network.chat.Component;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public abstract class ObjectSelectionList<E extends ObjectSelectionList.Entry<E>> extends AbstractSelectionList<E> {
|
| 17 | private static final Component USAGE_NARRATION = Component.translatable("narration.selection.usage");
|
| 18 |
|
| 19 | public ObjectSelectionList(final Minecraft minecraft, final int width, final int height, final int y, final int itemHeight) {
|
| 20 | super(minecraft, width, height, y, itemHeight);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public @Nullable ComponentPath nextFocusPath(final FocusNavigationEvent navigationEvent) {
|
| 25 | if (this.getItemCount() == 0) {
|
| 26 | return null;
|
| 27 | }
|
| 28 |
|
| 29 | if (this.isFocused() && navigationEvent instanceof FocusNavigationEvent.ArrowNavigation arrowNavigation) {
|
| 30 | E entry = this.nextEntry(arrowNavigation.direction());
|
| 31 | if (entry != null) {
|
| 32 | return ComponentPath.path(this, ComponentPath.leaf(entry));
|
| 33 | }
|
| 34 |
|
| 35 | this.setFocused(null);
|
| 36 | this.setSelected(null);
|
| 37 | return null;
|
| 38 | } else if (!this.isFocused()) {
|
| 39 | E entry = this.getSelected();
|
| 40 | if (entry == null) {
|
| 41 | entry = this.nextEntry(navigationEvent.getVerticalDirectionForInitialFocus());
|
| 42 | }
|
| 43 |
|
| 44 | return entry == null ? null : ComponentPath.path(this, ComponentPath.leaf(entry));
|
| 45 | } else {
|
| 46 | return null;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 52 | E hovered = this.getHovered();
|
| 53 | if (hovered != null) {
|
| 54 | this.narrateListElementPosition(output.nest(), hovered);
|
| 55 | hovered.updateNarration(output);
|
| 56 | } else {
|
| 57 | E selected = this.getSelected();
|
| 58 | if (selected != null) {
|
| 59 | this.narrateListElementPosition(output.nest(), selected);
|
| 60 | selected.updateNarration(output);
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | if (this.isFocused()) {
|
| 65 | output.add(NarratedElementType.USAGE, USAGE_NARRATION);
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Environment(EnvType.CLIENT)
|
| 70 | public abstract static class Entry<E extends ObjectSelectionList.Entry<E>> extends AbstractSelectionList.Entry<E> implements NarrationSupplier {
|
| 71 | public abstract Component getNarration();
|
| 72 |
|
| 73 | @Override
|
| 74 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 75 | return true;
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void updateNarration(final NarrationElementOutput output) {
|
| 80 | output.add(NarratedElementType.TITLE, this.getNarration());
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 |
|