Checkbox.java

net.minecraft.client.gui.components.Checkbox

信息

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

    TODO

字段/常量

  • CHECKBOX_SELECTED_HIGHLIGHTED_SPRITE

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

      TODO

  • CHECKBOX_SELECTED_SPRITE

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

      TODO

  • CHECKBOX_HIGHLIGHTED_SPRITE

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

      TODO

  • CHECKBOX_SPRITE

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

      TODO

  • SPACING

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

      TODO

  • BOX_PADDING

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

      TODO

  • selected

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

      TODO

  • onValueChange

    • 类型: Checkbox.OnValueChange
    • 修饰符: private final
    • 源码定位: L27
    • 说明:

      TODO

  • textWidget

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

      TODO

内部类/嵌套类型

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

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

      TODO

  • net.minecraft.client.gui.components.Checkbox.OnValueChange

    • 类型: interface
    • 修饰符: public
    • 源码定位: L176
    • 说明:

      TODO

构造器

private Checkbox(int x, int y, int maxWidth, Component message, Font font, boolean selected, Checkbox.OnValueChange onValueChange) @ L30

  • 构造器名:Checkbox
  • 源码定位:L30
  • 修饰符:private

参数:

  • x: int
  • y: int
  • maxWidth: int
  • message: Component
  • font: Font
  • selected: boolean
  • onValueChange: Checkbox.OnValueChange

说明:

TODO

方法

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

public int adjustWidth(int maxWidth, Font font) @ L40

  • 方法名:adjustWidth
  • 源码定位:L40
  • 返回类型:int
  • 修饰符:public

参数:

  • maxWidth: int
  • font: Font

说明:

TODO

private int getAdjustedWidth(int maxWidth, Component message, Font font) @ L46

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

参数:

  • maxWidth: int
  • message: Component
  • font: Font

说明:

TODO

private int getAdjustedHeight(Font font) @ L50

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

参数:

  • font: Font

说明:

TODO

private static int getDefaultWidth(Component message, Font font) @ L54

  • 方法名:getDefaultWidth
  • 源码定位:L54
  • 返回类型:int
  • 修饰符:private static

参数:

  • message: Component
  • font: Font

说明:

TODO

public static Checkbox.Builder builder(Component message, Font font) @ L58

  • 方法名:builder
  • 源码定位:L58
  • 返回类型:Checkbox.Builder
  • 修饰符:public static

参数:

  • message: Component
  • font: Font

说明:

TODO

public static int getBoxSize(Font font) @ L62

  • 方法名:getBoxSize
  • 源码定位:L62
  • 返回类型:int
  • 修饰符:public static

参数:

  • font: Font

说明:

TODO

public void onPress(InputWithModifiers input) @ L66

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

参数:

  • input: InputWithModifiers

说明:

TODO

public boolean selected() @ L72

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

参数:

说明:

TODO

public void updateWidgetNarration(NarrationElementOutput output) @ L76

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

参数:

  • output: NarrationElementOutput

说明:

TODO

public void extractContents(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L94

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

参数:

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

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class Checkbox extends AbstractButton {
    private static final Identifier CHECKBOX_SELECTED_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("widget/checkbox_selected_highlighted");
    private static final Identifier CHECKBOX_SELECTED_SPRITE = Identifier.withDefaultNamespace("widget/checkbox_selected");
    private static final Identifier CHECKBOX_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("widget/checkbox_highlighted");
    private static final Identifier CHECKBOX_SPRITE = Identifier.withDefaultNamespace("widget/checkbox");
    private static final int SPACING = 4;
    private static final int BOX_PADDING = 8;
    private boolean selected;
    private final Checkbox.OnValueChange onValueChange;
    private final MultiLineTextWidget textWidget;
 
    private Checkbox(int x, int y, int maxWidth, Component message, Font font, boolean selected, Checkbox.OnValueChange onValueChange) {
        super(x, y, 0, 0, message);
        this.textWidget = new MultiLineTextWidget(message, font);
        this.textWidget.setMaxRows(2);
        this.width = this.adjustWidth(maxWidth, font);
        this.height = this.getAdjustedHeight(font);
        this.selected = selected;
        this.onValueChange = onValueChange;
    }
 
    public int adjustWidth(int maxWidth, Font font) {
        this.width = this.getAdjustedWidth(maxWidth, this.getMessage(), font);
        this.textWidget.setMaxWidth(this.width);
        return this.width;
    }
 
    private int getAdjustedWidth(int maxWidth, Component message, Font font) {
        return Math.min(getDefaultWidth(message, font), maxWidth);
    }
 
    private int getAdjustedHeight(Font font) {
        return Math.max(getBoxSize(font), this.textWidget.getHeight());
    }
 
    private static int getDefaultWidth(Component message, Font font) {
        return getBoxSize(font) + 4 + font.width(message);
    }
 
    public static Checkbox.Builder builder(Component message, Font font) {
        return new Checkbox.Builder(message, font);
    }
 
    public static int getBoxSize(Font font) {
        return 9 + 8;
    }
 
    @Override
    public void onPress(InputWithModifiers input) {
        this.selected = !this.selected;
        this.onValueChange.onValueChange(this, this.selected);
    }
 
    public boolean selected() {
        return this.selected;
    }
 
    @Override
    public void updateWidgetNarration(NarrationElementOutput output) {
        output.add(NarratedElementType.TITLE, this.createNarrationMessage());
        if (this.active) {
            if (this.isFocused()) {
                output.add(
                    NarratedElementType.USAGE,
                    Component.translatable(this.selected ? "narration.checkbox.usage.focused.uncheck" : "narration.checkbox.usage.focused.check")
                );
            } else {
                output.add(
                    NarratedElementType.USAGE,
                    Component.translatable(this.selected ? "narration.checkbox.usage.hovered.uncheck" : "narration.checkbox.usage.hovered.check")
                );
            }
        }
    }
 
    @Override
    public void extractContents(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        Minecraft minecraft = Minecraft.getInstance();
        Font font = minecraft.font;
        Identifier sprite;
        if (this.selected) {
            sprite = this.isFocused() ? CHECKBOX_SELECTED_HIGHLIGHTED_SPRITE : CHECKBOX_SELECTED_SPRITE;
        } else {
            sprite = this.isFocused() ? CHECKBOX_HIGHLIGHTED_SPRITE : CHECKBOX_SPRITE;
        }
 
        int boxSize = getBoxSize(font);
        graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, this.getX(), this.getY(), boxSize, boxSize, ARGB.white(this.alpha));
        int textX = this.getX() + boxSize + 4;
        int textY = this.getY() + boxSize / 2 - this.textWidget.getHeight() / 2;
        this.textWidget.setPosition(textX, textY);
        this.textWidget.visitLines(graphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.notClickable(this.isHovered())));
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class Builder {
        private final Component message;
        private final Font font;
        private int maxWidth;
        private int x = 0;
        private int y = 0;
        private Checkbox.OnValueChange onValueChange = Checkbox.OnValueChange.NOP;
        private boolean selected = false;
        private @Nullable OptionInstance<Boolean> option = null;
        private @Nullable Tooltip tooltip = null;
 
        private Builder(Component message, Font font) {
            this.message = message;
            this.font = font;
            this.maxWidth = Checkbox.getDefaultWidth(message, font);
        }
 
        public Checkbox.Builder pos(int x, int y) {
            this.x = x;
            this.y = y;
            return this;
        }
 
        public Checkbox.Builder onValueChange(Checkbox.OnValueChange onValueChange) {
            this.onValueChange = onValueChange;
            return this;
        }
 
        public Checkbox.Builder selected(boolean selected) {
            this.selected = selected;
            this.option = null;
            return this;
        }
 
        public Checkbox.Builder selected(OptionInstance<Boolean> option) {
            this.option = option;
            this.selected = option.get();
            return this;
        }
 
        public Checkbox.Builder tooltip(Tooltip tooltip) {
            this.tooltip = tooltip;
            return this;
        }
 
        public Checkbox.Builder maxWidth(int maxWidth) {
            this.maxWidth = maxWidth;
            return this;
        }
 
        public Checkbox build() {
            Checkbox.OnValueChange onChange = this.option == null ? this.onValueChange : (checkbox, value) -> {
                this.option.set(value);
                this.onValueChange.onValueChange(checkbox, value);
            };
            Checkbox box = new Checkbox(this.x, this.y, this.maxWidth, this.message, this.font, this.selected, onChange);
            box.setTooltip(this.tooltip);
            return box;
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public interface OnValueChange {
        Checkbox.OnValueChange NOP = (checkbox, value) -> {};
 
        void onValueChange(Checkbox checkbox, boolean value);
    }
}

引用的其他类

  • Minecraft

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

    • 引用位置: 参数
  • GuiGraphicsExtractor

    • 引用位置: 参数/方法调用
    • 关联成员: GuiGraphicsExtractor.HoveredTextEffects.notClickable()
  • AbstractButton

    • 引用位置: 继承
  • MultiLineTextWidget

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

    • 引用位置: 参数
  • InputWithModifiers

    • 引用位置: 参数
  • Component

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

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

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