PopupScreen.java

net.minecraft.client.gui.components.PopupScreen

信息

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

    TODO

字段/常量

  • BACKGROUND_SPRITE

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

      TODO

  • SPACING

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

      TODO

  • BG_BORDER_WITH_SPACING

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

      TODO

  • BUTTON_SPACING

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

      TODO

  • IMAGE_SIZE_X

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

      TODO

  • IMAGE_SIZE_Y

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

      TODO

  • POPUP_DEFAULT_WIDTH

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

      TODO

  • backgroundScreen

    • 类型: Screen
    • 修饰符: private final
    • 源码定位: L28
    • 说明:

      TODO

  • image

    • 类型: Identifier
    • 修饰符: private final
    • 源码定位: L29
    • 说明:

      TODO

  • messages

    • 类型: List<Component>
    • 修饰符: private final
    • 源码定位: L30
    • 说明:

      TODO

  • buttons

    • 类型: List<PopupScreen.ButtonOption>
    • 修饰符: private final
    • 源码定位: L31
    • 说明:

      TODO

  • onClose

    • 类型: Runnable
    • 修饰符: private final
    • 源码定位: L32
    • 说明:

      TODO

  • contentWidth

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

      TODO

  • layout

    • 类型: LinearLayout
    • 修饰符: private final
    • 源码定位: L34
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.gui.components.PopupScreen.Builder

    • 类型: class
    • 修饰符: public static
    • 源码定位: L141
    • 说明:

      TODO

  • net.minecraft.client.gui.components.PopupScreen.ButtonOption

    • 类型: record
    • 修饰符: private
    • 源码定位: L190
    • 说明:

      TODO

构造器

private PopupScreen(Screen backgroundScreen, int backgroundWidth, Identifier image, Component title, List<Component> messages, List<PopupScreen.ButtonOption> buttons, Runnable onClose) @ L36

  • 构造器名:PopupScreen
  • 源码定位:L36
  • 修饰符:private

参数:

  • backgroundScreen: Screen
  • backgroundWidth: int
  • image: Identifier
  • title: Component
  • messages: List
  • buttons: List<PopupScreen.ButtonOption>
  • onClose: Runnable

说明:

TODO

方法

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

public void added() @ L54

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

参数:

说明:

TODO

protected void init() @ L62

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

参数:

说明:

TODO

private LinearLayout buildButtonRow() @ L81

  • 方法名:buildButtonRow
  • 源码定位:L81
  • 返回类型:LinearLayout
  • 修饰符:private

参数:

说明:

TODO

protected void repositionElements() @ L94

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

参数:

说明:

TODO

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

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

参数:

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

说明:

TODO

public Component getNarrationMessage() @ L126

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

参数:

说明:

TODO

public void onClose() @ L131

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

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class PopupScreen extends Screen {
    private static final Identifier BACKGROUND_SPRITE = Identifier.withDefaultNamespace("popup/background");
    private static final int SPACING = 12;
    private static final int BG_BORDER_WITH_SPACING = 18;
    private static final int BUTTON_SPACING = 6;
    private static final int IMAGE_SIZE_X = 130;
    private static final int IMAGE_SIZE_Y = 64;
    private static final int POPUP_DEFAULT_WIDTH = 250;
    private final @Nullable Screen backgroundScreen;
    private final @Nullable Identifier image;
    private final List<Component> messages;
    private final List<PopupScreen.ButtonOption> buttons;
    private final @Nullable Runnable onClose;
    private final int contentWidth;
    private final LinearLayout layout = LinearLayout.vertical();
 
    private PopupScreen(
        @Nullable Screen backgroundScreen,
        int backgroundWidth,
        @Nullable Identifier image,
        Component title,
        List<Component> messages,
        List<PopupScreen.ButtonOption> buttons,
        @Nullable Runnable onClose
    ) {
        super(title);
        this.backgroundScreen = backgroundScreen;
        this.image = image;
        this.messages = messages;
        this.buttons = buttons;
        this.onClose = onClose;
        this.contentWidth = backgroundWidth - 36;
    }
 
    @Override
    public void added() {
        super.added();
        if (this.backgroundScreen != null) {
            this.backgroundScreen.clearFocus();
        }
    }
 
    @Override
    protected void init() {
        if (this.backgroundScreen != null) {
            this.backgroundScreen.init(this.width, this.height);
        }
 
        this.layout.spacing(12).defaultCellSetting().alignHorizontallyCenter();
        this.layout
            .addChild(new MultiLineTextWidget(this.title.copy().withStyle(ChatFormatting.BOLD), this.font).setMaxWidth(this.contentWidth).setCentered(true));
        if (this.image != null) {
            this.layout.addChild(ImageWidget.texture(130, 64, this.image, 130, 64));
        }
 
        this.messages.forEach(message -> this.layout.addChild(new MultiLineTextWidget(message, this.font).setMaxWidth(this.contentWidth).setCentered(true)));
        this.layout.addChild(this.buildButtonRow());
        this.layout.visitWidgets(x$0 -> this.addRenderableWidget(x$0));
        this.repositionElements();
    }
 
    private LinearLayout buildButtonRow() {
        int totalSpacing = 6 * (this.buttons.size() - 1);
        int buttonWidth = Math.min((this.contentWidth - totalSpacing) / this.buttons.size(), 150);
        LinearLayout row = LinearLayout.horizontal();
        row.spacing(6);
 
        for (PopupScreen.ButtonOption button : this.buttons) {
            row.addChild(Button.builder(button.message(), b -> button.action().accept(this)).width(buttonWidth).build());
        }
 
        return row;
    }
 
    @Override
    protected void repositionElements() {
        if (this.backgroundScreen != null) {
            this.backgroundScreen.resize(this.width, this.height);
        }
 
        this.layout.arrangeElements();
        FrameLayout.centerInRectangle(this.layout, this.getRectangle());
    }
 
    @Override
    public void extractBackground(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        if (this.backgroundScreen != null) {
            this.backgroundScreen.extractBackground(graphics, mouseX, mouseY, a);
            graphics.nextStratum();
            this.backgroundScreen.extractRenderState(graphics, -1, -1, a);
            graphics.nextStratum();
            this.extractTransparentBackground(graphics);
        } else {
            super.extractBackground(graphics, mouseX, mouseY, a);
        }
 
        graphics.blitSprite(
            RenderPipelines.GUI_TEXTURED,
            BACKGROUND_SPRITE,
            this.layout.getX() - 18,
            this.layout.getY() - 18,
            this.layout.getWidth() + 36,
            this.layout.getHeight() + 36
        );
    }
 
    @Override
    public Component getNarrationMessage() {
        return CommonComponents.joinForNarration(this.title, CommonComponents.joinLines(this.messages));
    }
 
    @Override
    public void onClose() {
        if (this.onClose != null) {
            this.onClose.run();
        }
 
        this.minecraft.setScreen(this.backgroundScreen);
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class Builder {
        private final @Nullable Screen backgroundScreen;
        private final Component title;
        private final List<Component> messages = new ArrayList<>();
        private int width = 250;
        private @Nullable Identifier image;
        private final List<PopupScreen.ButtonOption> buttons = new ArrayList<>();
        private @Nullable Runnable onClose = null;
 
        public Builder(@Nullable Screen backgroundScreen, Component title) {
            this.backgroundScreen = backgroundScreen;
            this.title = title;
        }
 
        public PopupScreen.Builder setWidth(int width) {
            this.width = width;
            return this;
        }
 
        public PopupScreen.Builder setImage(Identifier image) {
            this.image = image;
            return this;
        }
 
        public PopupScreen.Builder addMessage(Component message) {
            this.messages.add(message);
            return this;
        }
 
        public PopupScreen.Builder addButton(Component message, Consumer<PopupScreen> action) {
            this.buttons.add(new PopupScreen.ButtonOption(message, action));
            return this;
        }
 
        public PopupScreen.Builder onClose(Runnable onClose) {
            this.onClose = onClose;
            return this;
        }
 
        public PopupScreen build() {
            if (this.buttons.isEmpty()) {
                throw new IllegalStateException("Popup must have at least one button");
            } else {
                return new PopupScreen(this.backgroundScreen, this.width, this.image, this.title, this.messages, List.copyOf(this.buttons), this.onClose);
            }
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    private record ButtonOption(Component message, Consumer<PopupScreen> action) {
    }
}

引用的其他类

  • GuiGraphicsExtractor

    • 引用位置: 参数
  • Button

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

    • 引用位置: 方法调用
    • 关联成员: ImageWidget.texture()
  • MultiLineTextWidget

    • 引用位置: 构造调用
    • 关联成员: MultiLineTextWidget()
  • FrameLayout

    • 引用位置: 方法调用
    • 关联成员: FrameLayout.centerInRectangle()
  • LinearLayout

    • 引用位置: 字段/方法调用/返回值
    • 关联成员: LinearLayout.horizontal(), LinearLayout.vertical()
  • Screen

    • 引用位置: 参数/字段/继承
  • CommonComponents

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

    • 引用位置: 参数/字段/返回值
  • Identifier

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