AbstractWidget.java

net.minecraft.client.gui.components.AbstractWidget

信息

  • 全限定名:net.minecraft.client.gui.components.AbstractWidget
  • 类型:public abstract class
  • 包:net.minecraft.client.gui.components
  • 源码路径:src/main/java/net/minecraft/client/gui/components/AbstractWidget.java
  • 起始行号:L31
  • 实现:LayoutElement, Renderable, GuiEventListener, NarratableEntry
  • 职责:

    TODO

字段/常量

  • width

    • 类型: int
    • 修饰符: protected
    • 源码定位: L32
    • 说明:

      TODO

  • height

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

      TODO

  • x

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

      TODO

  • y

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

      TODO

  • message

    • 类型: Component
    • 修饰符: protected
    • 源码定位: L36
    • 说明:

      TODO

  • isHovered

    • 类型: boolean
    • 修饰符: protected
    • 源码定位: L37
    • 说明:

      TODO

  • active

    • 类型: boolean
    • 修饰符: public
    • 源码定位: L38
    • 说明:

      TODO

  • visible

    • 类型: boolean
    • 修饰符: public
    • 源码定位: L39
    • 说明:

      TODO

  • alpha

    • 类型: float
    • 修饰符: protected
    • 源码定位: L40
    • 说明:

      TODO

  • tabOrderGroup

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

      TODO

  • focused

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

      TODO

  • tooltip

    • 类型: WidgetTooltipHolder
    • 修饰符: private final
    • 源码定位: L43
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.gui.components.AbstractWidget.WithInactiveMessage
    • 类型: class
    • 修饰符: public abstract static
    • 源码定位: L314
    • 说明:

      TODO

构造器

public AbstractWidget(int x, int y, int width, int height, Component message) @ L45

  • 构造器名:AbstractWidget
  • 源码定位:L45
  • 修饰符:public

参数:

  • x: int
  • y: int
  • width: int
  • height: int
  • message: Component

说明:

TODO

方法

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

public int getHeight() @ L53

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

参数:

说明:

TODO

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

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

参数:

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

说明:

TODO

protected void handleCursor(GuiGraphicsExtractor graphics) @ L67

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

参数:

  • graphics: GuiGraphicsExtractor

说明:

TODO

public void setTooltip(Tooltip tooltip) @ L73

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

参数:

  • tooltip: Tooltip

说明:

TODO

public void setTooltipDelay(Duration delay) @ L77

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

参数:

  • delay: Duration

说明:

TODO

protected MutableComponent createNarrationMessage() @ L81

  • 方法名:createNarrationMessage
  • 源码定位:L81
  • 返回类型:MutableComponent
  • 修饰符:protected

参数:

说明:

TODO

public static MutableComponent wrapDefaultNarrationMessage(Component message) @ L85

  • 方法名:wrapDefaultNarrationMessage
  • 源码定位:L85
  • 返回类型:MutableComponent
  • 修饰符:public static

参数:

  • message: Component

说明:

TODO

protected abstract void extractWidgetRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L89

  • 方法名:extractWidgetRenderState
  • 源码定位:L89
  • 返回类型:void
  • 修饰符:protected abstract

参数:

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

说明:

TODO

protected void extractScrollingStringOverContents(ActiveTextCollector output, Component message, int margin) @ L91

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

参数:

  • output: ActiveTextCollector
  • message: Component
  • margin: int

说明:

TODO

public void onClick(MouseButtonEvent event, boolean doubleClick) @ L99

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

参数:

  • event: MouseButtonEvent
  • doubleClick: boolean

说明:

TODO

public void onRelease(MouseButtonEvent event) @ L102

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

参数:

  • event: MouseButtonEvent

说明:

TODO

protected void onDrag(MouseButtonEvent event, double dx, double dy) @ L105

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

参数:

  • event: MouseButtonEvent
  • dx: double
  • dy: double

说明:

TODO

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

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

参数:

  • event: MouseButtonEvent
  • doubleClick: boolean

说明:

TODO

public boolean mouseReleased(MouseButtonEvent event) @ L126

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

参数:

  • event: MouseButtonEvent

说明:

TODO

protected boolean isValidClickButton(MouseButtonInfo buttonInfo) @ L136

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

参数:

  • buttonInfo: MouseButtonInfo

说明:

TODO

public boolean mouseDragged(MouseButtonEvent event, double dx, double dy) @ L140

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

参数:

  • event: MouseButtonEvent
  • dx: double
  • dy: double

说明:

TODO

public ComponentPath nextFocusPath(FocusNavigationEvent navigationEvent) @ L150

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

参数:

  • navigationEvent: FocusNavigationEvent

说明:

TODO

public boolean isMouseOver(double mouseX, double mouseY) @ L159

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

参数:

  • mouseX: double
  • mouseY: double

说明:

TODO

public void playDownSound(SoundManager soundManager) @ L164

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

参数:

  • soundManager: SoundManager

说明:

TODO

public static void playButtonClickSound(SoundManager soundManager) @ L168

  • 方法名:playButtonClickSound
  • 源码定位:L168
  • 返回类型:void
  • 修饰符:public static

参数:

  • soundManager: SoundManager

说明:

TODO

public int getWidth() @ L172

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

参数:

说明:

TODO

public void setWidth(int width) @ L177

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

参数:

  • width: int

说明:

TODO

public void setHeight(int height) @ L181

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

参数:

  • height: int

说明:

TODO

public void setAlpha(float alpha) @ L185

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

参数:

  • alpha: float

说明:

TODO

public float getAlpha() @ L189

  • 方法名:getAlpha
  • 源码定位:L189
  • 返回类型:float
  • 修饰符:public

参数:

说明:

TODO

public void setMessage(Component message) @ L193

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

参数:

  • message: Component

说明:

TODO

public Component getMessage() @ L197

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

参数:

说明:

TODO

public boolean isFocused() @ L201

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

参数:

说明:

TODO

public boolean isHovered() @ L206

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

参数:

说明:

TODO

public boolean isHoveredOrFocused() @ L210

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

参数:

说明:

TODO

public boolean isActive() @ L214

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

参数:

说明:

TODO

public void setFocused(boolean focused) @ L219

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

参数:

  • focused: boolean

说明:

TODO

public NarratableEntry.NarrationPriority narrationPriority() @ L224

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

参数:

说明:

TODO

public final void updateNarration(NarrationElementOutput output) @ L233

  • 方法名:updateNarration
  • 源码定位:L233
  • 返回类型:void
  • 修饰符:public final

参数:

  • output: NarrationElementOutput

说明:

TODO

protected abstract void updateWidgetNarration(NarrationElementOutput output) @ L239

  • 方法名:updateWidgetNarration
  • 源码定位:L239
  • 返回类型:void
  • 修饰符:protected abstract

参数:

  • output: NarrationElementOutput

说明:

TODO

protected void defaultButtonNarrationText(NarrationElementOutput output) @ L241

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

参数:

  • output: NarrationElementOutput

说明:

TODO

public int getX() @ L252

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

参数:

说明:

TODO

public void setX(int x) @ L257

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

参数:

  • x: int

说明:

TODO

public int getY() @ L262

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

参数:

说明:

TODO

public void setY(int y) @ L267

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

参数:

  • y: int

说明:

TODO

public int getRight() @ L272

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

参数:

说明:

TODO

public int getBottom() @ L276

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

参数:

说明:

TODO

public void visitWidgets(Consumer<AbstractWidget> widgetVisitor) @ L280

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

参数:

  • widgetVisitor: Consumer

说明:

TODO

public void setSize(int width, int height) @ L285

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

参数:

  • width: int
  • height: int

说明:

TODO

public ScreenRectangle getRectangle() @ L290

  • 方法名:getRectangle
  • 源码定位:L290
  • 返回类型:ScreenRectangle
  • 修饰符:public

参数:

说明:

TODO

private boolean areCoordinatesInRectangle(double x, double y) @ L295

  • 方法名:areCoordinatesInRectangle
  • 源码定位:L295
  • 返回类型:boolean
  • 修饰符:private

参数:

  • x: double
  • y: double

说明:

TODO

public void setRectangle(int width, int height, int x, int y) @ L299

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

参数:

  • width: int
  • height: int
  • x: int
  • y: int

说明:

TODO

public int getTabOrderGroup() @ L304

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

参数:

说明:

TODO

public void setTabOrderGroup(int tabOrderGroup) @ L309

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

参数:

  • tabOrderGroup: int

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public abstract class AbstractWidget implements LayoutElement, Renderable, GuiEventListener, NarratableEntry {
    protected int width;
    protected int height;
    private int x;
    private int y;
    protected Component message;
    protected boolean isHovered;
    public boolean active = true;
    public boolean visible = true;
    protected float alpha = 1.0F;
    private int tabOrderGroup;
    private boolean focused;
    private final WidgetTooltipHolder tooltip = new WidgetTooltipHolder();
 
    public AbstractWidget(int x, int y, int width, int height, Component message) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.message = message;
    }
 
    @Override
    public int getHeight() {
        return this.height;
    }
 
    @Override
    public final void extractRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        if (this.visible) {
            this.isHovered = graphics.containsPointInScissor(mouseX, mouseY) && this.areCoordinatesInRectangle(mouseX, mouseY);
            this.extractWidgetRenderState(graphics, mouseX, mouseY, a);
            this.tooltip.refreshTooltipForNextRenderPass(graphics, mouseX, mouseY, this.isHovered(), this.isFocused(), this.getRectangle());
        }
    }
 
    protected void handleCursor(GuiGraphicsExtractor graphics) {
        if (this.isHovered()) {
            graphics.requestCursor(this.isActive() ? CursorTypes.POINTING_HAND : CursorTypes.NOT_ALLOWED);
        }
    }
 
    public void setTooltip(@Nullable Tooltip tooltip) {
        this.tooltip.set(tooltip);
    }
 
    public void setTooltipDelay(Duration delay) {
        this.tooltip.setDelay(delay);
    }
 
    protected MutableComponent createNarrationMessage() {
        return wrapDefaultNarrationMessage(this.getMessage());
    }
 
    public static MutableComponent wrapDefaultNarrationMessage(Component message) {
        return Component.translatable("gui.narrate.button", message);
    }
 
    protected abstract void extractWidgetRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a);
 
    protected void extractScrollingStringOverContents(ActiveTextCollector output, Component message, int margin) {
        int left = this.getX() + margin;
        int right = this.getX() + this.getWidth() - margin;
        int top = this.getY();
        int bottom = this.getY() + this.getHeight();
        output.acceptScrollingWithDefaultCenter(message, left, right, top, bottom);
    }
 
    public void onClick(MouseButtonEvent event, boolean doubleClick) {
    }
 
    public void onRelease(MouseButtonEvent event) {
    }
 
    protected void onDrag(MouseButtonEvent event, double dx, double dy) {
    }
 
    @Override
    public boolean mouseClicked(MouseButtonEvent event, boolean doubleClick) {
        if (!this.isActive()) {
            return false;
        } else {
            if (this.isValidClickButton(event.buttonInfo())) {
                boolean isMouseOver = this.isMouseOver(event.x(), event.y());
                if (isMouseOver) {
                    this.playDownSound(Minecraft.getInstance().getSoundManager());
                    this.onClick(event, doubleClick);
                    return true;
                }
            }
 
            return false;
        }
    }
 
    @Override
    public boolean mouseReleased(MouseButtonEvent event) {
        if (this.isValidClickButton(event.buttonInfo())) {
            this.onRelease(event);
            return true;
        } else {
            return false;
        }
    }
 
    protected boolean isValidClickButton(MouseButtonInfo buttonInfo) {
        return buttonInfo.button() == 0;
    }
 
    @Override
    public boolean mouseDragged(MouseButtonEvent event, double dx, double dy) {
        if (this.isValidClickButton(event.buttonInfo())) {
            this.onDrag(event, dx, dy);
            return true;
        } else {
            return false;
        }
    }
 
    @Override
    public @Nullable ComponentPath nextFocusPath(FocusNavigationEvent navigationEvent) {
        if (!this.isActive()) {
            return null;
        } else {
            return !this.isFocused() ? ComponentPath.leaf(this) : null;
        }
    }
 
    @Override
    public boolean isMouseOver(double mouseX, double mouseY) {
        return this.isActive() && this.areCoordinatesInRectangle(mouseX, mouseY);
    }
 
    public void playDownSound(SoundManager soundManager) {
        playButtonClickSound(soundManager);
    }
 
    public static void playButtonClickSound(SoundManager soundManager) {
        soundManager.play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
    }
 
    @Override
    public int getWidth() {
        return this.width;
    }
 
    public void setWidth(int width) {
        this.width = width;
    }
 
    public void setHeight(int height) {
        this.height = height;
    }
 
    public void setAlpha(float alpha) {
        this.alpha = alpha;
    }
 
    public float getAlpha() {
        return this.alpha;
    }
 
    public void setMessage(Component message) {
        this.message = message;
    }
 
    public Component getMessage() {
        return this.message;
    }
 
    @Override
    public boolean isFocused() {
        return this.focused;
    }
 
    public boolean isHovered() {
        return this.isHovered;
    }
 
    public boolean isHoveredOrFocused() {
        return this.isHovered() || this.isFocused();
    }
 
    @Override
    public boolean isActive() {
        return this.visible && this.active;
    }
 
    @Override
    public void setFocused(boolean focused) {
        this.focused = focused;
    }
 
    @Override
    public NarratableEntry.NarrationPriority narrationPriority() {
        if (this.isFocused()) {
            return NarratableEntry.NarrationPriority.FOCUSED;
        } else {
            return this.isHovered ? NarratableEntry.NarrationPriority.HOVERED : NarratableEntry.NarrationPriority.NONE;
        }
    }
 
    @Override
    public final void updateNarration(NarrationElementOutput output) {
        this.updateWidgetNarration(output);
        this.tooltip.updateNarration(output);
    }
 
    protected abstract void updateWidgetNarration(final NarrationElementOutput output);
 
    protected void defaultButtonNarrationText(NarrationElementOutput output) {
        output.add(NarratedElementType.TITLE, this.createNarrationMessage());
        if (this.active) {
            if (this.isFocused()) {
                output.add(NarratedElementType.USAGE, Component.translatable("narration.button.usage.focused"));
            } else {
                output.add(NarratedElementType.USAGE, Component.translatable("narration.button.usage.hovered"));
            }
        }
    }
 
    @Override
    public int getX() {
        return this.x;
    }
 
    @Override
    public void setX(int x) {
        this.x = x;
    }
 
    @Override
    public int getY() {
        return this.y;
    }
 
    @Override
    public void setY(int y) {
        this.y = y;
    }
 
    public int getRight() {
        return this.getX() + this.getWidth();
    }
 
    public int getBottom() {
        return this.getY() + this.getHeight();
    }
 
    @Override
    public void visitWidgets(Consumer<AbstractWidget> widgetVisitor) {
        widgetVisitor.accept(this);
    }
 
    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
    }
 
    @Override
    public ScreenRectangle getRectangle() {
        return LayoutElement.super.getRectangle();
    }
 
    private boolean areCoordinatesInRectangle(double x, double y) {
        return x >= this.getX() && y >= this.getY() && x < this.getRight() && y < this.getBottom();
    }
 
    public void setRectangle(int width, int height, int x, int y) {
        this.setSize(width, height);
        this.setPosition(x, y);
    }
 
    @Override
    public int getTabOrderGroup() {
        return this.tabOrderGroup;
    }
 
    public void setTabOrderGroup(int tabOrderGroup) {
        this.tabOrderGroup = tabOrderGroup;
    }
 
    @OnlyIn(Dist.CLIENT)
    public abstract static class WithInactiveMessage extends AbstractWidget {
        private Component inactiveMessage;
 
        public static Component defaultInactiveMessage(Component activeMessage) {
            return ComponentUtils.mergeStyles(activeMessage, Style.EMPTY.withColor(-6250336));
        }
 
        public WithInactiveMessage(int x, int y, int width, int height, Component message) {
            super(x, y, width, height, message);
            this.inactiveMessage = defaultInactiveMessage(message);
        }
 
        @Override
        public Component getMessage() {
            return this.active ? super.getMessage() : this.inactiveMessage;
        }
 
        @Override
        public void setMessage(Component message) {
            super.setMessage(message);
            this.inactiveMessage = defaultInactiveMessage(message);
        }
    }
}

引用的其他类