BookViewScreen.java

net.minecraft.client.gui.screens.inventory.BookViewScreen

信息

  • 全限定名:net.minecraft.client.gui.screens.inventory.BookViewScreen
  • 类型:public class
  • 包:net.minecraft.client.gui.screens.inventory
  • 源码路径:src/main/java/net/minecraft/client/gui/screens/inventory/BookViewScreen.java
  • 起始行号:L34
  • 继承:Screen
  • 职责:

    TODO

字段/常量

  • PAGE_INDICATOR_TEXT_Y_OFFSET

    • 类型: int
    • 修饰符: public static final
    • 源码定位: L35
    • 说明:

      TODO

  • PAGE_TEXT_X_OFFSET

    • 类型: int
    • 修饰符: public static final
    • 源码定位: L36
    • 说明:

      TODO

  • PAGE_TEXT_Y_OFFSET

    • 类型: int
    • 修饰符: public static final
    • 源码定位: L37
    • 说明:

      TODO

  • BACKGROUND_TEXTURE_WIDTH

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L38
    • 说明:

      TODO

  • BACKGROUND_TEXTURE_HEIGHT

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L39
    • 说明:

      TODO

  • TITLE

    • 类型: Component
    • 修饰符: private static final
    • 源码定位: L40
    • 说明:

      TODO

  • PAGE_TEXT_STYLE

    • 类型: Style
    • 修饰符: private static final
    • 源码定位: L41
    • 说明:

      TODO

  • EMPTY_ACCESS

    • 类型: BookViewScreen.BookAccess
    • 修饰符: public static final
    • 源码定位: L42
    • 说明:

      TODO

  • BOOK_LOCATION

    • 类型: Identifier
    • 修饰符: public static final
    • 源码定位: L43
    • 说明:

      TODO

  • TEXT_WIDTH

    • 类型: int
    • 修饰符: protected static final
    • 源码定位: L44
    • 说明:

      TODO

  • TEXT_HEIGHT

    • 类型: int
    • 修饰符: protected static final
    • 源码定位: L45
    • 说明:

      TODO

  • IMAGE_WIDTH

    • 类型: int
    • 修饰符: protected static final
    • 源码定位: L46
    • 说明:

      TODO

  • PAGE_INDICATOR_X_OFFSET

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L47
    • 说明:

      TODO

  • IMAGE_HEIGHT

    • 类型: int
    • 修饰符: protected static final
    • 源码定位: L48
    • 说明:

      TODO

  • PAGE_BUTTON_Y

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L49
    • 说明:

      TODO

  • PAGE_BACK_BUTTON_X

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L50
    • 说明:

      TODO

  • PAGE_FORWARD_BUTTON_X

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L51
    • 说明:

      TODO

  • bookAccess

    • 类型: BookViewScreen.BookAccess
    • 修饰符: private
    • 源码定位: L52
    • 说明:

      TODO

  • currentPage

    • 类型: int
    • 修饰符: private
    • 源码定位: L53
    • 说明:

      TODO

  • cachedPageComponents

    • 类型: List<FormattedCharSequence>
    • 修饰符: private
    • 源码定位: L54
    • 说明:

      TODO

  • cachedPage

    • 类型: int
    • 修饰符: private
    • 源码定位: L55
    • 说明:

      TODO

  • pageMsg

    • 类型: Component
    • 修饰符: private
    • 源码定位: L56
    • 说明:

      TODO

  • forwardButton

    • 类型: PageButton
    • 修饰符: private
    • 源码定位: L57
    • 说明:

      TODO

  • backButton

    • 类型: PageButton
    • 修饰符: private
    • 源码定位: L58
    • 说明:

      TODO

  • playTurnSound

    • 类型: boolean
    • 修饰符: private final
    • 源码定位: L59
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.gui.screens.inventory.BookViewScreen.BookAccess
    • 类型: record
    • 修饰符: public
    • 源码定位: L268
    • 说明:

      TODO

构造器

public BookViewScreen(BookViewScreen.BookAccess bookAccess) @ L61

  • 构造器名:BookViewScreen
  • 源码定位:L61
  • 修饰符:public

参数:

  • bookAccess: BookViewScreen.BookAccess

说明:

TODO

public BookViewScreen() @ L65

  • 构造器名:BookViewScreen
  • 源码定位:L65
  • 修饰符:public

参数:

说明:

TODO

private BookViewScreen(BookViewScreen.BookAccess bookAccess, boolean playTurnSound) @ L69

  • 构造器名:BookViewScreen
  • 源码定位:L69
  • 修饰符:private

参数:

  • bookAccess: BookViewScreen.BookAccess
  • playTurnSound: boolean

说明:

TODO

方法

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

public void setBookAccess(BookViewScreen.BookAccess bookAccess) @ L75

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

参数:

  • bookAccess: BookViewScreen.BookAccess

说明:

TODO

public boolean setPage(int page) @ L82

  • 方法名:setPage
  • 源码定位:L82
  • 返回类型:boolean
  • 修饰符:public

参数:

  • page: int

说明:

TODO

protected boolean forcePage(int page) @ L94

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

参数:

  • page: int

说明:

TODO

protected void init() @ L98

  • 方法名:init
  • 源码定位:L98
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

public Component getNarrationMessage() @ L104

  • 方法名:getNarrationMessage
  • 源码定位:L104
  • 返回类型:Component
  • 修饰符:public

参数:

说明:

TODO

private Component getPageNumberMessage() @ L109

  • 方法名:getPageNumberMessage
  • 源码定位:L109
  • 返回类型:Component
  • 修饰符:private

参数:

说明:

TODO

protected void createMenuControls() @ L113

  • 方法名:createMenuControls
  • 源码定位:L113
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

protected void createPageControlButtons() @ L119

  • 方法名:createPageControlButtons
  • 源码定位:L119
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

private int getNumPages() @ L127

  • 方法名:getNumPages
  • 源码定位:L127
  • 返回类型:int
  • 修饰符:private

参数:

说明:

TODO

protected void pageBack() @ L131

  • 方法名:pageBack
  • 源码定位:L131
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

protected void pageForward() @ L139

  • 方法名:pageForward
  • 源码定位:L139
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

private void updateButtonVisibility() @ L147

  • 方法名:updateButtonVisibility
  • 源码定位:L147
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

public boolean keyPressed(KeyEvent event) @ L152

  • 方法名:keyPressed
  • 源码定位:L152
  • 返回类型:boolean
  • 修饰符:public

参数:

  • event: KeyEvent

说明:

TODO

public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L171

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

参数:

  • graphics: GuiGraphicsExtractor
  • mouseX: int
  • mouseY: int
  • a: float

说明:

TODO

private void visitText(ActiveTextCollector collector, boolean clickableOnly) @ L177

  • 方法名:visitText
  • 源码定位:L177
  • 返回类型:void
  • 修饰符:private

参数:

  • collector: ActiveTextCollector
  • clickableOnly: boolean

说明:

TODO

public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L199

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

参数:

  • graphics: GuiGraphicsExtractor
  • mouseX: int
  • mouseY: int
  • a: float

说明:

TODO

private int backgroundLeft() @ L205

  • 方法名:backgroundLeft
  • 源码定位:L205
  • 返回类型:int
  • 修饰符:private

参数:

说明:

TODO

private int backgroundTop() @ L209

  • 方法名:backgroundTop
  • 源码定位:L209
  • 返回类型:int
  • 修饰符:private

参数:

说明:

TODO

protected int menuControlsTop() @ L213

  • 方法名:menuControlsTop
  • 源码定位:L213
  • 返回类型:int
  • 修饰符:protected

参数:

说明:

TODO

public boolean mouseClicked(MouseButtonEvent event, boolean doubleClick) @ L217

  • 方法名:mouseClicked
  • 源码定位:L217
  • 返回类型:boolean
  • 修饰符:public

参数:

  • event: MouseButtonEvent
  • doubleClick: boolean

说明:

TODO

protected boolean handleClickEvent(ClickEvent event) @ L231

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

参数:

  • event: ClickEvent

说明:

TODO

protected void closeContainerOnServer() @ L259

  • 方法名:closeContainerOnServer
  • 源码定位:L259
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

public boolean isInGameUi() @ L262

  • 方法名:isInGameUi
  • 源码定位:L262
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class BookViewScreen extends Screen {
    public static final int PAGE_INDICATOR_TEXT_Y_OFFSET = 16;
    public static final int PAGE_TEXT_X_OFFSET = 36;
    public static final int PAGE_TEXT_Y_OFFSET = 30;
    private static final int BACKGROUND_TEXTURE_WIDTH = 256;
    private static final int BACKGROUND_TEXTURE_HEIGHT = 256;
    private static final Component TITLE = Component.translatable("book.view.title");
    private static final Style PAGE_TEXT_STYLE = Style.EMPTY.withoutShadow().withColor(-16777216);
    public static final BookViewScreen.BookAccess EMPTY_ACCESS = new BookViewScreen.BookAccess(List.of());
    public static final Identifier BOOK_LOCATION = Identifier.withDefaultNamespace("textures/gui/book.png");
    protected static final int TEXT_WIDTH = 114;
    protected static final int TEXT_HEIGHT = 128;
    protected static final int IMAGE_WIDTH = 192;
    private static final int PAGE_INDICATOR_X_OFFSET = 148;
    protected static final int IMAGE_HEIGHT = 192;
    private static final int PAGE_BUTTON_Y = 157;
    private static final int PAGE_BACK_BUTTON_X = 43;
    private static final int PAGE_FORWARD_BUTTON_X = 116;
    private BookViewScreen.BookAccess bookAccess;
    private int currentPage;
    private List<FormattedCharSequence> cachedPageComponents = Collections.emptyList();
    private int cachedPage = -1;
    private Component pageMsg = CommonComponents.EMPTY;
    private PageButton forwardButton;
    private PageButton backButton;
    private final boolean playTurnSound;
 
    public BookViewScreen(BookViewScreen.BookAccess bookAccess) {
        this(bookAccess, true);
    }
 
    public BookViewScreen() {
        this(EMPTY_ACCESS, false);
    }
 
    private BookViewScreen(BookViewScreen.BookAccess bookAccess, boolean playTurnSound) {
        super(TITLE);
        this.bookAccess = bookAccess;
        this.playTurnSound = playTurnSound;
    }
 
    public void setBookAccess(BookViewScreen.BookAccess bookAccess) {
        this.bookAccess = bookAccess;
        this.currentPage = Mth.clamp(this.currentPage, 0, bookAccess.getPageCount());
        this.updateButtonVisibility();
        this.cachedPage = -1;
    }
 
    public boolean setPage(int page) {
        int clampedPage = Mth.clamp(page, 0, this.bookAccess.getPageCount() - 1);
        if (clampedPage != this.currentPage) {
            this.currentPage = clampedPage;
            this.updateButtonVisibility();
            this.cachedPage = -1;
            return true;
        } else {
            return false;
        }
    }
 
    protected boolean forcePage(int page) {
        return this.setPage(page);
    }
 
    @Override
    protected void init() {
        this.createMenuControls();
        this.createPageControlButtons();
    }
 
    @Override
    public Component getNarrationMessage() {
        return CommonComponents.joinLines(super.getNarrationMessage(), this.getPageNumberMessage(), this.bookAccess.getPage(this.currentPage));
    }
 
    private Component getPageNumberMessage() {
        return Component.translatable("book.pageIndicator", this.currentPage + 1, Math.max(this.getNumPages(), 1)).withStyle(PAGE_TEXT_STYLE);
    }
 
    protected void createMenuControls() {
        this.addRenderableWidget(
            Button.builder(CommonComponents.GUI_DONE, button -> this.onClose()).pos((this.width - 200) / 2, this.menuControlsTop()).width(200).build()
        );
    }
 
    protected void createPageControlButtons() {
        int left = this.backgroundLeft();
        int top = this.backgroundTop();
        this.forwardButton = this.addRenderableWidget(new PageButton(left + 116, top + 157, true, button -> this.pageForward(), this.playTurnSound));
        this.backButton = this.addRenderableWidget(new PageButton(left + 43, top + 157, false, button -> this.pageBack(), this.playTurnSound));
        this.updateButtonVisibility();
    }
 
    private int getNumPages() {
        return this.bookAccess.getPageCount();
    }
 
    protected void pageBack() {
        if (this.currentPage > 0) {
            this.currentPage--;
        }
 
        this.updateButtonVisibility();
    }
 
    protected void pageForward() {
        if (this.currentPage < this.getNumPages() - 1) {
            this.currentPage++;
        }
 
        this.updateButtonVisibility();
    }
 
    private void updateButtonVisibility() {
        this.forwardButton.visible = this.currentPage < this.getNumPages() - 1;
        this.backButton.visible = this.currentPage > 0;
    }
 
    @Override
    public boolean keyPressed(KeyEvent event) {
        if (super.keyPressed(event)) {
            return true;
        } else {
            return switch (event.key()) {
                case 266 -> {
                    this.backButton.onPress(event);
                    yield true;
                }
                case 267 -> {
                    this.forwardButton.onPress(event);
                    yield true;
                }
                default -> false;
            };
        }
    }
 
    @Override
    public void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        super.extractRenderState(graphics, mouseX, mouseY, a);
        this.visitText(graphics.textRenderer(GuiGraphicsExtractor.HoveredTextEffects.TOOLTIP_AND_CURSOR), false);
    }
 
    private void visitText(ActiveTextCollector collector, boolean clickableOnly) {
        if (this.cachedPage != this.currentPage) {
            FormattedText pageText = ComponentUtils.mergeStyles(this.bookAccess.getPage(this.currentPage), PAGE_TEXT_STYLE);
            this.cachedPageComponents = this.font.split(pageText, 114);
            this.pageMsg = this.getPageNumberMessage();
            this.cachedPage = this.currentPage;
        }
 
        int left = this.backgroundLeft();
        int top = this.backgroundTop();
        if (!clickableOnly) {
            collector.accept(TextAlignment.RIGHT, left + 148, top + 16, this.pageMsg);
        }
 
        int shownLines = Math.min(128 / 9, this.cachedPageComponents.size());
 
        for (int i = 0; i < shownLines; i++) {
            FormattedCharSequence component = this.cachedPageComponents.get(i);
            collector.accept(left + 36, top + 30 + i * 9, component);
        }
    }
 
    @Override
    public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        super.extractBackground(graphics, mouseX, mouseY, a);
        graphics.blit(RenderPipelines.GUI_TEXTURED, BOOK_LOCATION, this.backgroundLeft(), this.backgroundTop(), 0.0F, 0.0F, 192, 192, 256, 256);
    }
 
    private int backgroundLeft() {
        return (this.width - 192) / 2;
    }
 
    private int backgroundTop() {
        return 2;
    }
 
    protected int menuControlsTop() {
        return this.backgroundTop() + 192 + 2;
    }
 
    @Override
    public boolean mouseClicked(MouseButtonEvent event, boolean doubleClick) {
        if (event.button() == 0) {
            ActiveTextCollector.ClickableStyleFinder finder = new ActiveTextCollector.ClickableStyleFinder(this.font, (int)event.x(), (int)event.y());
            this.visitText(finder, true);
            Style clickedStyle = finder.result();
            if (clickedStyle != null && this.handleClickEvent(clickedStyle.getClickEvent())) {
                return true;
            }
        }
 
        return super.mouseClicked(event, doubleClick);
    }
 
    protected boolean handleClickEvent(@Nullable ClickEvent event) {
        if (event == null) {
            return false;
        } else {
            LocalPlayer player = Objects.requireNonNull(this.minecraft.player, "Player not available");
            Objects.requireNonNull(event);
            switch (event) {
                case ClickEvent.ChangePage(int var15):
                    int var12 = var15;
                    if (true) {
                        this.forcePage(var12 - 1);
                        break;
                    }
 
                    byte var4 = 1;
                    break;
                case ClickEvent.RunCommand(String var9):
                    this.closeContainerOnServer();
                    clickCommandAction(player, var9, null);
                    break;
                default:
                    defaultHandleGameClickEvent(event, this.minecraft, this);
            }
 
            return true;
        }
    }
 
    protected void closeContainerOnServer() {
    }
 
    @Override
    public boolean isInGameUi() {
        return true;
    }
 
    @OnlyIn(Dist.CLIENT)
    public record BookAccess(List<Component> pages) {
        public int getPageCount() {
            return this.pages.size();
        }
 
        public Component getPage(int page) {
            return page >= 0 && page < this.getPageCount() ? this.pages.get(page) : CommonComponents.EMPTY;
        }
 
        public static BookViewScreen.@Nullable BookAccess fromItem(ItemStack itemStack) {
            boolean filterEnabled = Minecraft.getInstance().isTextFilteringEnabled();
            WrittenBookContent writtenContent = itemStack.get(DataComponents.WRITTEN_BOOK_CONTENT);
            if (writtenContent != null) {
                return new BookViewScreen.BookAccess(writtenContent.getPages(filterEnabled));
            } else {
                WritableBookContent writableContent = itemStack.get(DataComponents.WRITABLE_BOOK_CONTENT);
                return writableContent != null ? new BookViewScreen.BookAccess(writableContent.getPages(filterEnabled).<Component>map(Component::literal).toList()) : null;
            }
        }
    }
}

引用的其他类

  • Minecraft

    • 引用位置: 方法调用
    • 关联成员: Minecraft.getInstance()
  • ActiveTextCollector

    • 引用位置: 参数/方法调用/构造调用
    • 关联成员: ActiveTextCollector.ClickableStyleFinder(), ClickableStyleFinder()
  • GuiGraphicsExtractor

    • 引用位置: 参数
  • Button

    • 引用位置: 方法调用
    • 关联成员: Button.builder()
  • Screen

    • 引用位置: 继承
  • PageButton

    • 引用位置: 字段/构造调用
    • 关联成员: PageButton()
  • KeyEvent

    • 引用位置: 参数
  • MouseButtonEvent

    • 引用位置: 参数
  • ClickEvent

    • 引用位置: 参数/方法调用
    • 关联成员: ClickEvent.ChangePage(), ClickEvent.RunCommand()
  • CommonComponents

    • 引用位置: 方法调用
    • 关联成员: CommonComponents.joinLines()
  • Component

    • 引用位置: 字段/方法调用/返回值
    • 关联成员: Component.translatable()
  • ComponentUtils

    • 引用位置: 方法调用
    • 关联成员: ComponentUtils.mergeStyles()
  • Style

    • 引用位置: 字段
  • Identifier

    • 引用位置: 字段/方法调用
    • 关联成员: Identifier.withDefaultNamespace()
  • FormattedCharSequence

    • 引用位置: 字段
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.clamp()