ContainerObjectSelectionList.java

net.minecraft.client.gui.components.ContainerObjectSelectionList

信息

  • 全限定名:net.minecraft.client.gui.components.ContainerObjectSelectionList
  • 类型:public abstract class
  • 包:net.minecraft.client.gui.components
  • 源码路径:src/main/java/net/minecraft/client/gui/components/ContainerObjectSelectionList.java
  • 起始行号:L23
  • 继承:AbstractSelectionList
  • 职责:

    TODO

字段/常量

内部类/嵌套类型

  • net.minecraft.client.gui.components.ContainerObjectSelectionList.Entry
    • 类型: class
    • 修饰符: public abstract static
    • 源码定位: L109
    • 说明:

      TODO

构造器

public ContainerObjectSelectionList(Minecraft minecraft, int width, int height, int y, int itemHeight) @ L24

  • 构造器名:ContainerObjectSelectionList
  • 源码定位:L24
  • 修饰符:public

参数:

  • minecraft: Minecraft
  • width: int
  • height: int
  • y: int
  • itemHeight: int

说明:

TODO

方法

下面的方法块按源码顺序生成。

public ComponentPath nextFocusPath(FocusNavigationEvent navigationEvent) @ L28

  • 方法名:nextFocusPath
  • 源码定位:L28
  • 返回类型:ComponentPath
  • 修饰符:public

参数:

  • navigationEvent: FocusNavigationEvent

说明:

TODO

public void setFocused(GuiEventListener focused) @ L77

  • 方法名:setFocused
  • 源码定位:L77
  • 返回类型:void
  • 修饰符:public

参数:

  • focused: GuiEventListener

说明:

TODO

public NarratableEntry.NarrationPriority narrationPriority() @ L87

  • 方法名:narrationPriority
  • 源码定位:L87
  • 返回类型:NarratableEntry.NarrationPriority
  • 修饰符:public

参数:

说明:

TODO

protected boolean entriesCanBeSelected() @ L92

  • 方法名:entriesCanBeSelected
  • 源码定位:L92
  • 返回类型:boolean
  • 修饰符:protected

参数:

说明:

TODO

public void updateWidgetNarration(NarrationElementOutput output) @ L97

  • 方法名:updateWidgetNarration
  • 源码定位:L97
  • 返回类型:void
  • 修饰符:public

参数:

  • output: NarrationElementOutput

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public abstract class ContainerObjectSelectionList<E extends ContainerObjectSelectionList.Entry<E>> extends AbstractSelectionList<E> {
    public ContainerObjectSelectionList(Minecraft minecraft, int width, int height, int y, int itemHeight) {
        super(minecraft, width, height, y, itemHeight);
    }
 
    @Override
    public @Nullable ComponentPath nextFocusPath(FocusNavigationEvent navigationEvent) {
        if (this.getItemCount() == 0) {
            return null;
        } else if (!(navigationEvent instanceof FocusNavigationEvent.ArrowNavigation arrowNavigation)) {
            return super.nextFocusPath(navigationEvent);
        } else {
            E focused = this.getFocused();
            if (arrowNavigation.direction().getAxis() == ScreenAxis.HORIZONTAL && focused != null) {
                return ComponentPath.path(this, focused.nextFocusPath(navigationEvent));
            } else {
                int index = -1;
                ScreenDirection direction = arrowNavigation.direction();
                if (focused != null) {
                    index = focused.children().indexOf(focused.getFocused());
                }
 
                if (index == -1) {
                    switch (direction) {
                        case LEFT:
                            index = Integer.MAX_VALUE;
                            direction = ScreenDirection.DOWN;
                            break;
                        case RIGHT:
                            index = 0;
                            direction = ScreenDirection.DOWN;
                            break;
                        default:
                            index = 0;
                    }
                }
 
                E entry = focused;
 
                ComponentPath componentPath;
                do {
                    entry = this.nextEntry(direction, e -> !e.children().isEmpty(), entry);
                    if (entry == null) {
                        return null;
                    }
 
                    componentPath = entry.focusPathAtIndex(arrowNavigation, index);
                } while (componentPath == null);
 
                return ComponentPath.path(this, componentPath);
            }
        }
    }
 
    @Override
    public void setFocused(@Nullable GuiEventListener focused) {
        if (this.getFocused() != focused) {
            super.setFocused(focused);
            if (focused == null) {
                this.setSelected(null);
            }
        }
    }
 
    @Override
    public NarratableEntry.NarrationPriority narrationPriority() {
        return this.isFocused() ? NarratableEntry.NarrationPriority.FOCUSED : super.narrationPriority();
    }
 
    @Override
    protected boolean entriesCanBeSelected() {
        return false;
    }
 
    @Override
    public void updateWidgetNarration(NarrationElementOutput output) {
        if (this.getHovered() instanceof E hovered) {
            hovered.updateNarration(output.nest());
            this.narrateListElementPosition(output, hovered);
        } else if (this.getFocused() instanceof E focused) {
            focused.updateNarration(output.nest());
            this.narrateListElementPosition(output, focused);
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public abstract static class Entry<E extends ContainerObjectSelectionList.Entry<E>> extends AbstractSelectionList.Entry<E> implements ContainerEventHandler {
        private @Nullable GuiEventListener focused;
        private @Nullable NarratableEntry lastNarratable;
        private boolean dragging;
 
        @Override
        public boolean isDragging() {
            return this.dragging;
        }
 
        @Override
        public void setDragging(boolean dragging) {
            this.dragging = dragging;
        }
 
        @Override
        public boolean mouseClicked(MouseButtonEvent event, boolean doubleClick) {
            return ContainerEventHandler.super.mouseClicked(event, doubleClick);
        }
 
        @Override
        public void setFocused(@Nullable GuiEventListener focused) {
            if (this.focused != null) {
                this.focused.setFocused(false);
            }
 
            if (focused != null) {
                focused.setFocused(true);
            }
 
            this.focused = focused;
        }
 
        @Override
        public @Nullable GuiEventListener getFocused() {
            return this.focused;
        }
 
        public @Nullable ComponentPath focusPathAtIndex(FocusNavigationEvent navigationEvent, int currentIndex) {
            if (this.children().isEmpty()) {
                return null;
            } else {
                ComponentPath componentPath = this.children().get(Math.min(currentIndex, this.children().size() - 1)).nextFocusPath(navigationEvent);
                return ComponentPath.path(this, componentPath);
            }
        }
 
        @Override
        public @Nullable ComponentPath nextFocusPath(FocusNavigationEvent navigationEvent) {
            if (navigationEvent instanceof FocusNavigationEvent.ArrowNavigation arrowNavigation) {
                int delta = switch (arrowNavigation.direction()) {
                    case LEFT -> -1;
                    case RIGHT -> 1;
                    case UP, DOWN -> 0;
                };
                if (delta == 0) {
                    return null;
                }
 
                int index = Mth.clamp(delta + this.children().indexOf(this.getFocused()), 0, this.children().size() - 1);
 
                for (int i = index; i >= 0 && i < this.children().size(); i += delta) {
                    GuiEventListener child = this.children().get(i);
                    ComponentPath componentPath = child.nextFocusPath(navigationEvent);
                    if (componentPath != null) {
                        return ComponentPath.path(this, componentPath);
                    }
                }
            }
 
            return ContainerEventHandler.super.nextFocusPath(navigationEvent);
        }
 
        public abstract List<? extends NarratableEntry> narratables();
 
        void updateNarration(NarrationElementOutput output) {
            List<? extends NarratableEntry> narratables = this.narratables();
            Screen.NarratableSearchResult result = Screen.findNarratableWidget(narratables, this.lastNarratable);
            if (result != null) {
                if (result.priority().isTerminal()) {
                    this.lastNarratable = result.entry();
                }
 
                if (narratables.size() > 1) {
                    output.add(NarratedElementType.POSITION, Component.translatable("narrator.position.object_list", result.index() + 1, narratables.size()));
                }
 
                result.entry().updateNarration(output.nest());
            }
        }
    }
}

引用的其他类