| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.gui.ComponentPath;
|
| 8 | import net.minecraft.client.gui.components.events.ContainerEventHandler;
|
| 9 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 10 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 11 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 12 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 13 | import net.minecraft.client.gui.narration.NarrationTrigger;
|
| 14 | import net.minecraft.client.gui.navigation.FocusNavigationEvent;
|
| 15 | import net.minecraft.client.gui.navigation.ScreenAxis;
|
| 16 | import net.minecraft.client.gui.navigation.ScreenDirection;
|
| 17 | import net.minecraft.client.gui.screens.Screen;
|
| 18 | import net.minecraft.client.input.MouseButtonEvent;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.util.Mth;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public abstract class ContainerObjectSelectionList<E extends ContainerObjectSelectionList.Entry<E>> extends AbstractSelectionList<E> {
|
| 25 | public ContainerObjectSelectionList(final Minecraft minecraft, final int width, final int height, final int y, final int itemHeight) {
|
| 26 | super(minecraft, width, height, y, itemHeight);
|
| 27 | }
|
| 28 |
|
| 29 | @Override
|
| 30 | public @Nullable ComponentPath nextFocusPath(final FocusNavigationEvent navigationEvent) {
|
| 31 | if (this.getItemCount() == 0) {
|
| 32 | return null;
|
| 33 | } else if (!(navigationEvent instanceof FocusNavigationEvent.ArrowNavigation arrowNavigation)) {
|
| 34 | return super.nextFocusPath(navigationEvent);
|
| 35 | } else {
|
| 36 | E focused = this.getFocused();
|
| 37 | if (arrowNavigation.direction().getAxis() == ScreenAxis.HORIZONTAL && focused != null) {
|
| 38 | return ComponentPath.path(this, focused.nextFocusPath(navigationEvent));
|
| 39 | }
|
| 40 |
|
| 41 | int index = -1;
|
| 42 | ScreenDirection direction = arrowNavigation.direction();
|
| 43 | if (focused != null) {
|
| 44 | index = focused.children().indexOf(focused.getFocused());
|
| 45 | }
|
| 46 |
|
| 47 | if (index == -1) {
|
| 48 | switch (direction) {
|
| 49 | case LEFT:
|
| 50 | index = Integer.MAX_VALUE;
|
| 51 | direction = ScreenDirection.DOWN;
|
| 52 | break;
|
| 53 | case RIGHT:
|
| 54 | index = 0;
|
| 55 | direction = ScreenDirection.DOWN;
|
| 56 | break;
|
| 57 | default:
|
| 58 | index = 0;
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | E entry = focused;
|
| 63 |
|
| 64 | ComponentPath componentPath;
|
| 65 | do {
|
| 66 | entry = this.nextEntry(direction, e -> !e.children().isEmpty(), entry);
|
| 67 | if (entry == null) {
|
| 68 | return null;
|
| 69 | }
|
| 70 |
|
| 71 | componentPath = entry.focusPathAtIndex(arrowNavigation, index);
|
| 72 | } while (componentPath == null);
|
| 73 |
|
| 74 | return ComponentPath.path(this, componentPath);
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void setFocused(final @Nullable GuiEventListener focused) {
|
| 80 | if (this.getFocused() != focused) {
|
| 81 | super.setFocused(focused);
|
| 82 | if (focused == null) {
|
| 83 | this.setSelected(null);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public NarratableEntry.NarrationPriority narrationPriority() {
|
| 90 | return this.isFocused() ? NarratableEntry.NarrationPriority.FOCUSED : super.narrationPriority();
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | protected boolean entriesCanBeSelected() {
|
| 95 | return false;
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public void updateWidgetNarration(final NarrationElementOutput output) {
|
| 100 | E hovered = this.getHovered();
|
| 101 | NarrationTrigger narrationTrigger = output.narrationTrigger();
|
| 102 | if ((narrationTrigger == NarrationTrigger.MOUSE || narrationTrigger == NarrationTrigger.SYSTEM) && hovered != null) {
|
| 103 | hovered.updateNarration(output.nest());
|
| 104 | this.narrateListElementPosition(output, hovered);
|
| 105 | } else {
|
| 106 | E focused = this.getFocused();
|
| 107 | if (focused != null) {
|
| 108 | focused.updateNarration(output.nest());
|
| 109 | this.narrateListElementPosition(output, focused);
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | @Environment(EnvType.CLIENT)
|
| 115 | public abstract static class Entry<E extends ContainerObjectSelectionList.Entry<E>> extends AbstractSelectionList.Entry<E> implements ContainerEventHandler {
|
| 116 | private @Nullable GuiEventListener focused;
|
| 117 | private @Nullable NarratableEntry lastNarratable;
|
| 118 | private boolean dragging;
|
| 119 |
|
| 120 | @Override
|
| 121 | public boolean isDragging() {
|
| 122 | return this.dragging;
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public void setDragging(final boolean dragging) {
|
| 127 | this.dragging = dragging;
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 132 | return ContainerEventHandler.super.mouseClicked(event, doubleClick);
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public void setFocused(final @Nullable GuiEventListener focused) {
|
| 137 | if (this.focused != null) {
|
| 138 | this.focused.setFocused(false);
|
| 139 | }
|
| 140 |
|
| 141 | if (focused != null) {
|
| 142 | focused.setFocused(true);
|
| 143 | }
|
| 144 |
|
| 145 | this.focused = focused;
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public @Nullable GuiEventListener getFocused() {
|
| 150 | return this.focused;
|
| 151 | }
|
| 152 |
|
| 153 | public @Nullable ComponentPath focusPathAtIndex(final FocusNavigationEvent navigationEvent, final int currentIndex) {
|
| 154 | if (this.children().isEmpty()) {
|
| 155 | return null;
|
| 156 | }
|
| 157 |
|
| 158 | ComponentPath componentPath = this.children().get(Math.min(currentIndex, this.children().size() - 1)).nextFocusPath(navigationEvent);
|
| 159 | return ComponentPath.path(this, componentPath);
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | public @Nullable ComponentPath nextFocusPath(final FocusNavigationEvent navigationEvent) {
|
| 164 | if (navigationEvent instanceof FocusNavigationEvent.ArrowNavigation arrowNavigation) {
|
| 165 | int delta = switch (arrowNavigation.direction()) {
|
| 166 | case LEFT -> -1;
|
| 167 | case RIGHT -> 1;
|
| 168 | case UP, DOWN -> 0;
|
| 169 | };
|
| 170 | if (delta == 0) {
|
| 171 | return null;
|
| 172 | }
|
| 173 |
|
| 174 | int index = Mth.clamp(delta + this.children().indexOf(this.getFocused()), 0, this.children().size() - 1);
|
| 175 |
|
| 176 | for (int i = index; i >= 0 && i < this.children().size(); i += delta) {
|
| 177 | GuiEventListener child = this.children().get(i);
|
| 178 | ComponentPath componentPath = child.nextFocusPath(navigationEvent);
|
| 179 | if (componentPath != null) {
|
| 180 | return ComponentPath.path(this, componentPath);
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | return ContainerEventHandler.super.nextFocusPath(navigationEvent);
|
| 186 | }
|
| 187 |
|
| 188 | public abstract List<? extends NarratableEntry> narratables();
|
| 189 |
|
| 190 | void updateNarration(final NarrationElementOutput output) {
|
| 191 | List<? extends NarratableEntry> narratables = this.narratables();
|
| 192 | Screen.NarratableSearchResult result = Screen.findNarratableWidget(narratables, this.lastNarratable);
|
| 193 | if (result != null) {
|
| 194 | if (result.priority().isTerminal()) {
|
| 195 | this.lastNarratable = result.entry();
|
| 196 | }
|
| 197 |
|
| 198 | if (narratables.size() > 1) {
|
| 199 | output.add(NarratedElementType.POSITION, Component.translatable("narrator.position.object_list", result.index() + 1, narratables.size()));
|
| 200 | }
|
| 201 |
|
| 202 | result.entry().updateNarration(output.nest());
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 | }
|
| 207 |
|