AbstractSliderButton.java

net.minecraft.client.gui.components.AbstractSliderButton

信息

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

    TODO

字段/常量

  • SLIDER_SPRITE

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

      TODO

  • HIGHLIGHTED_SPRITE

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

      TODO

  • SLIDER_HANDLE_SPRITE

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

      TODO

  • SLIDER_HANDLE_HIGHLIGHTED_SPRITE

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

      TODO

  • TEXT_MARGIN

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

      TODO

  • DEFAULT_HEIGHT

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

      TODO

  • HANDLE_WIDTH

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

      TODO

  • HANDLE_HALF_WIDTH

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

      TODO

  • value

    • 类型: double
    • 修饰符: protected
    • 源码定位: L31
    • 说明:

      TODO

  • canChangeValue

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

      TODO

  • dragging

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

      TODO

内部类/嵌套类型

构造器

public AbstractSliderButton(int x, int y, int width, int height, Component message, double initialValue) @ L35

  • 构造器名:AbstractSliderButton
  • 源码定位:L35
  • 修饰符:public

参数:

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

说明:

TODO

方法

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

private Identifier getSprite() @ L40

  • 方法名:getSprite
  • 源码定位:L40
  • 返回类型:Identifier
  • 修饰符:private

参数:

说明:

TODO

private Identifier getHandleSprite() @ L44

  • 方法名:getHandleSprite
  • 源码定位:L44
  • 返回类型:Identifier
  • 修饰符:private

参数:

说明:

TODO

protected MutableComponent createNarrationMessage() @ L48

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

参数:

说明:

TODO

public void updateWidgetNarration(NarrationElementOutput output) @ L53

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

参数:

  • output: NarrationElementOutput

说明:

TODO

public void extractWidgetRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L69

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

参数:

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

说明:

TODO

protected void handleCursor(GuiGraphicsExtractor graphics) @ L85

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

参数:

  • graphics: GuiGraphicsExtractor

说明:

TODO

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

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

参数:

  • event: MouseButtonEvent
  • doubleClick: boolean

说明:

TODO

public void setFocused(boolean focused) @ L98

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

参数:

  • focused: boolean

说明:

TODO

public boolean keyPressed(KeyEvent event) @ L111

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

参数:

  • event: KeyEvent

说明:

TODO

private void setValueFromMouse(MouseButtonEvent event) @ L131

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

参数:

  • event: MouseButtonEvent

说明:

TODO

protected void setValue(double newValue) @ L135

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

参数:

  • newValue: double

说明:

TODO

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

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

参数:

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

说明:

TODO

public void playDownSound(SoundManager soundManager) @ L151

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

参数:

  • soundManager: SoundManager

说明:

TODO

public void onRelease(MouseButtonEvent event) @ L155

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

参数:

  • event: MouseButtonEvent

说明:

TODO

protected abstract void updateMessage() @ L161

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

参数:

说明:

TODO

protected abstract void applyValue() @ L163

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

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public abstract class AbstractSliderButton extends AbstractWidget.WithInactiveMessage {
    private static final Identifier SLIDER_SPRITE = Identifier.withDefaultNamespace("widget/slider");
    private static final Identifier HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("widget/slider_highlighted");
    private static final Identifier SLIDER_HANDLE_SPRITE = Identifier.withDefaultNamespace("widget/slider_handle");
    private static final Identifier SLIDER_HANDLE_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("widget/slider_handle_highlighted");
    protected static final int TEXT_MARGIN = 2;
    public static final int DEFAULT_HEIGHT = 20;
    protected static final int HANDLE_WIDTH = 8;
    private static final int HANDLE_HALF_WIDTH = 4;
    protected double value;
    protected boolean canChangeValue;
    private boolean dragging;
 
    public AbstractSliderButton(int x, int y, int width, int height, Component message, double initialValue) {
        super(x, y, width, height, message);
        this.value = initialValue;
    }
 
    private Identifier getSprite() {
        return this.isActive() && this.isFocused() && !this.canChangeValue ? HIGHLIGHTED_SPRITE : SLIDER_SPRITE;
    }
 
    private Identifier getHandleSprite() {
        return !this.isActive() || !this.isHovered && !this.canChangeValue ? SLIDER_HANDLE_SPRITE : SLIDER_HANDLE_HIGHLIGHTED_SPRITE;
    }
 
    @Override
    protected MutableComponent createNarrationMessage() {
        return Component.translatable("gui.narrate.slider", this.getMessage());
    }
 
    @Override
    public void updateWidgetNarration(NarrationElementOutput output) {
        output.add(NarratedElementType.TITLE, this.createNarrationMessage());
        if (this.active) {
            if (this.isFocused()) {
                if (this.canChangeValue) {
                    output.add(NarratedElementType.USAGE, Component.translatable("narration.slider.usage.focused"));
                } else {
                    output.add(NarratedElementType.USAGE, Component.translatable("narration.slider.usage.focused.keyboard_cannot_change_value"));
                }
            } else {
                output.add(NarratedElementType.USAGE, Component.translatable("narration.slider.usage.hovered"));
            }
        }
    }
 
    @Override
    public void extractWidgetRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        graphics.blitSprite(RenderPipelines.GUI_TEXTURED, this.getSprite(), this.getX(), this.getY(), this.getWidth(), this.getHeight(), ARGB.white(this.alpha));
        graphics.blitSprite(
            RenderPipelines.GUI_TEXTURED,
            this.getHandleSprite(),
            this.getX() + (int)(this.value * (this.width - 8)),
            this.getY(),
            8,
            this.getHeight(),
            ARGB.white(this.alpha)
        );
        this.extractScrollingStringOverContents(graphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.NONE), this.getMessage(), 2);
        this.handleCursor(graphics);
    }
 
    @Override
    protected void handleCursor(GuiGraphicsExtractor graphics) {
        if (this.isHovered()) {
            graphics.requestCursor(this.isActive() ? (this.dragging ? CursorTypes.RESIZE_EW : CursorTypes.POINTING_HAND) : CursorTypes.NOT_ALLOWED);
        }
    }
 
    @Override
    public void onClick(MouseButtonEvent event, boolean doubleClick) {
        this.dragging = this.active;
        this.setValueFromMouse(event);
    }
 
    @Override
    public void setFocused(boolean focused) {
        super.setFocused(focused);
        if (!focused) {
            this.canChangeValue = false;
        } else {
            InputType lastInputType = Minecraft.getInstance().getLastInputType();
            if (lastInputType == InputType.MOUSE || lastInputType == InputType.KEYBOARD_TAB) {
                this.canChangeValue = true;
            }
        }
    }
 
    @Override
    public boolean keyPressed(KeyEvent event) {
        if (event.isSelection()) {
            this.canChangeValue = !this.canChangeValue;
            return true;
        } else {
            if (this.canChangeValue) {
                boolean left = event.isLeft();
                boolean right = event.isRight();
                if (left || right) {
                    float direction = left ? -1.0F : 1.0F;
                    this.setValue(this.value + direction / (this.width - 8));
                    return true;
                }
            }
 
            return false;
        }
    }
 
    private void setValueFromMouse(MouseButtonEvent event) {
        this.setValue((event.x() - (this.getX() + 4)) / (this.width - 8));
    }
 
    protected void setValue(double newValue) {
        double oldValue = this.value;
        this.value = Mth.clamp(newValue, 0.0, 1.0);
        if (oldValue != this.value) {
            this.applyValue();
        }
 
        this.updateMessage();
    }
 
    @Override
    protected void onDrag(MouseButtonEvent event, double dx, double dy) {
        this.setValueFromMouse(event);
        super.onDrag(event, dx, dy);
    }
 
    @Override
    public void playDownSound(SoundManager soundManager) {
    }
 
    @Override
    public void onRelease(MouseButtonEvent event) {
        this.dragging = false;
        super.playDownSound(Minecraft.getInstance().getSoundManager());
    }
 
    protected abstract void updateMessage();
 
    protected abstract void applyValue();
}

引用的其他类