CycleButton.java

net.minecraft.client.gui.components.CycleButton

信息

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

    TODO

字段/常量

  • DEFAULT_ALT_LIST_SELECTOR

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

      TODO

  • BOOLEAN_OPTIONS

    • 类型: List<Boolean>
    • 修饰符: private static final
    • 源码定位: L28
    • 说明:

      TODO

  • defaultValueSupplier

    • 类型: Supplier<T>
    • 修饰符: private final
    • 源码定位: L29
    • 说明:

      TODO

  • name

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

      TODO

  • index

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

      TODO

  • value

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

      TODO

  • values

    • 类型: CycleButton.ValueListSupplier<T>
    • 修饰符: private final
    • 源码定位: L33
    • 说明:

      TODO

  • valueStringifier

    • 类型: Function<T,Component>
    • 修饰符: private final
    • 源码定位: L34
    • 说明:

      TODO

  • narrationProvider

    • 类型: Function<CycleButton<T>,MutableComponent>
    • 修饰符: private final
    • 源码定位: L35
    • 说明:

      TODO

  • onValueChange

    • 类型: CycleButton.OnValueChange<T>
    • 修饰符: private final
    • 源码定位: L36
    • 说明:

      TODO

  • displayState

    • 类型: CycleButton.DisplayState
    • 修饰符: private final
    • 源码定位: L37
    • 说明:

      TODO

  • tooltipSupplier

    • 类型: OptionInstance.TooltipSupplier<T>
    • 修饰符: private final
    • 源码定位: L38
    • 说明:

      TODO

  • spriteSupplier

    • 类型: CycleButton.SpriteSupplier<T>
    • 修饰符: private final
    • 源码定位: L39
    • 说明:

      TODO

内部类/嵌套类型

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

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

      TODO

  • net.minecraft.client.gui.components.CycleButton.DisplayState

    • 类型: enum
    • 修饰符: public static
    • 源码定位: L304
    • 说明:

      TODO

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

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

      TODO

  • net.minecraft.client.gui.components.CycleButton.SpriteSupplier

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

      TODO

  • net.minecraft.client.gui.components.CycleButton.ValueListSupplier

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

      TODO

构造器

private CycleButton(int x, int y, int width, int height, Component message, Component name, int index, T value, Supplier<T> defaultValueSupplier, CycleButton.ValueListSupplier<T> values, Function<T,Component> valueStringifier, Function<CycleButton<T>,MutableComponent> narrationProvider, CycleButton.OnValueChange<T> onValueChange, OptionInstance.TooltipSupplier<T> tooltipSupplier, CycleButton.DisplayState displayState, CycleButton.SpriteSupplier<T> spriteSupplier) @ L41

  • 构造器名:CycleButton
  • 源码定位:L41
  • 修饰符:private

参数:

  • x: int
  • y: int
  • width: int
  • height: int
  • message: Component
  • name: Component
  • index: int
  • value: T
  • defaultValueSupplier: Supplier
  • values: CycleButton.ValueListSupplier
  • valueStringifier: Function<T,Component>
  • narrationProvider: Function<CycleButton,MutableComponent>
  • onValueChange: CycleButton.OnValueChange
  • tooltipSupplier: OptionInstance.TooltipSupplier
  • displayState: CycleButton.DisplayState
  • spriteSupplier: CycleButton.SpriteSupplier

说明:

TODO

方法

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

protected void extractContents(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) @ L74

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

参数:

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

说明:

TODO

private void updateTooltip() @ L88

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

参数:

说明:

TODO

public void onPress(InputWithModifiers input) @ L92

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

参数:

  • input: InputWithModifiers

说明:

TODO

private void cycleValue(int delta) @ L101

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

参数:

  • delta: int

说明:

TODO

private T getCycledValue(int delta) @ L109

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

参数:

  • delta: int

说明:

TODO

public boolean mouseScrolled(double x, double y, double scrollX, double scrollY) @ L114

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

参数:

  • x: double
  • y: double
  • scrollX: double
  • scrollY: double

说明:

TODO

public void setValue(T newValue) @ L125

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

参数:

  • newValue: T

说明:

TODO

public void resetValue() @ L135

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

参数:

说明:

TODO

private void updateValue(T newValue) @ L140

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

参数:

  • newValue: T

说明:

TODO

private Component createLabelForValue(T newValue) @ L147

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

参数:

  • newValue: T

说明:

TODO

private MutableComponent createFullName(T newValue) @ L151

  • 方法名:createFullName
  • 源码定位:L151
  • 返回类型:MutableComponent
  • 修饰符:private

参数:

  • newValue: T

说明:

TODO

public T getValue() @ L155

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

参数:

说明:

TODO

protected MutableComponent createNarrationMessage() @ L159

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

参数:

说明:

TODO

public void updateWidgetNarration(NarrationElementOutput output) @ L164

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

参数:

  • output: NarrationElementOutput

说明:

TODO

public MutableComponent createDefaultNarrationMessage() @ L178

  • 方法名:createDefaultNarrationMessage
  • 源码定位:L178
  • 返回类型:MutableComponent
  • 修饰符:public

参数:

说明:

TODO

public static <T> CycleButton.Builder<T> builder(Function<T,Component> valueStringifier, Supplier<T> defaultValueSupplier) @ L184

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

参数:

  • valueStringifier: Function<T,Component>
  • defaultValueSupplier: Supplier

说明:

TODO

public static <T> CycleButton.Builder<T> builder(Function<T,Component> valueStringifier, T defaultValue) @ L188

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

参数:

  • valueStringifier: Function<T,Component>
  • defaultValue: T

说明:

TODO

public static CycleButton.Builder<Boolean> booleanBuilder(Component trueText, Component falseText, boolean defaultValue) @ L192

  • 方法名:booleanBuilder
  • 源码定位:L192
  • 返回类型:CycleButton.Builder
  • 修饰符:public static

参数:

  • trueText: Component
  • falseText: Component
  • defaultValue: boolean

说明:

TODO

public static CycleButton.Builder<Boolean> onOffBuilder(boolean initialValue) @ L196

  • 方法名:onOffBuilder
  • 源码定位:L196
  • 返回类型:CycleButton.Builder
  • 修饰符:public static

参数:

  • initialValue: boolean

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class CycleButton<T> extends AbstractButton implements ResettableOptionWidget {
    public static final BooleanSupplier DEFAULT_ALT_LIST_SELECTOR = () -> Minecraft.getInstance().hasAltDown();
    private static final List<Boolean> BOOLEAN_OPTIONS = ImmutableList.of(Boolean.TRUE, Boolean.FALSE);
    private final Supplier<T> defaultValueSupplier;
    private final Component name;
    private int index;
    private T value;
    private final CycleButton.ValueListSupplier<T> values;
    private final Function<T, Component> valueStringifier;
    private final Function<CycleButton<T>, MutableComponent> narrationProvider;
    private final CycleButton.OnValueChange<T> onValueChange;
    private final CycleButton.DisplayState displayState;
    private final OptionInstance.TooltipSupplier<T> tooltipSupplier;
    private final CycleButton.SpriteSupplier<T> spriteSupplier;
 
    private CycleButton(
        int x,
        int y,
        int width,
        int height,
        Component message,
        Component name,
        int index,
        T value,
        Supplier<T> defaultValueSupplier,
        CycleButton.ValueListSupplier<T> values,
        Function<T, Component> valueStringifier,
        Function<CycleButton<T>, MutableComponent> narrationProvider,
        CycleButton.OnValueChange<T> onValueChange,
        OptionInstance.TooltipSupplier<T> tooltipSupplier,
        CycleButton.DisplayState displayState,
        CycleButton.SpriteSupplier<T> spriteSupplier
    ) {
        super(x, y, width, height, message);
        this.name = name;
        this.index = index;
        this.defaultValueSupplier = defaultValueSupplier;
        this.value = value;
        this.values = values;
        this.valueStringifier = valueStringifier;
        this.narrationProvider = narrationProvider;
        this.onValueChange = onValueChange;
        this.displayState = displayState;
        this.tooltipSupplier = tooltipSupplier;
        this.spriteSupplier = spriteSupplier;
        this.updateTooltip();
    }
 
    @Override
    protected void extractContents(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
        Identifier sprite = this.spriteSupplier.apply(this, this.getValue());
        if (sprite != null) {
            graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, this.getX(), this.getY(), this.getWidth(), this.getHeight());
        } else {
            this.extractDefaultSprite(graphics);
        }
 
        if (this.displayState != CycleButton.DisplayState.HIDE) {
            this.extractDefaultLabel(graphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.NONE));
        }
    }
 
    private void updateTooltip() {
        this.setTooltip(this.tooltipSupplier.apply(this.value));
    }
 
    @Override
    public void onPress(InputWithModifiers input) {
        if (input.hasShiftDown()) {
            this.cycleValue(-1);
        } else {
            this.cycleValue(1);
        }
    }
 
    private void cycleValue(int delta) {
        List<T> list = this.values.getSelectedList();
        this.index = Mth.positiveModulo(this.index + delta, list.size());
        T newValue = list.get(this.index);
        this.updateValue(newValue);
        this.onValueChange.onValueChange(this, newValue);
    }
 
    private T getCycledValue(int delta) {
        List<T> list = this.values.getSelectedList();
        return list.get(Mth.positiveModulo(this.index + delta, list.size()));
    }
 
    @Override
    public boolean mouseScrolled(double x, double y, double scrollX, double scrollY) {
        if (scrollY > 0.0) {
            this.cycleValue(-1);
        } else if (scrollY < 0.0) {
            this.cycleValue(1);
        }
 
        return true;
    }
 
    public void setValue(T newValue) {
        List<T> list = this.values.getSelectedList();
        int newIndex = list.indexOf(newValue);
        if (newIndex != -1) {
            this.index = newIndex;
        }
 
        this.updateValue(newValue);
    }
 
    @Override
    public void resetValue() {
        this.setValue(this.defaultValueSupplier.get());
    }
 
    private void updateValue(T newValue) {
        Component newMessage = this.createLabelForValue(newValue);
        this.setMessage(newMessage);
        this.value = newValue;
        this.updateTooltip();
    }
 
    private Component createLabelForValue(T newValue) {
        return (Component)(this.displayState == CycleButton.DisplayState.VALUE ? this.valueStringifier.apply(newValue) : this.createFullName(newValue));
    }
 
    private MutableComponent createFullName(T newValue) {
        return CommonComponents.optionNameValue(this.name, this.valueStringifier.apply(newValue));
    }
 
    public T getValue() {
        return this.value;
    }
 
    @Override
    protected MutableComponent createNarrationMessage() {
        return this.narrationProvider.apply(this);
    }
 
    @Override
    public void updateWidgetNarration(NarrationElementOutput output) {
        output.add(NarratedElementType.TITLE, this.createNarrationMessage());
        if (this.active) {
            T nextValue = this.getCycledValue(1);
            Component nextValueText = this.createLabelForValue(nextValue);
            if (this.isFocused()) {
                output.add(NarratedElementType.USAGE, Component.translatable("narration.cycle_button.usage.focused", nextValueText));
            } else {
                output.add(NarratedElementType.USAGE, Component.translatable("narration.cycle_button.usage.hovered", nextValueText));
            }
        }
    }
 
    public MutableComponent createDefaultNarrationMessage() {
        return wrapDefaultNarrationMessage(
            (Component)(this.displayState == CycleButton.DisplayState.VALUE ? this.createFullName(this.value) : this.getMessage())
        );
    }
 
    public static <T> CycleButton.Builder<T> builder(Function<T, Component> valueStringifier, Supplier<T> defaultValueSupplier) {
        return new CycleButton.Builder<>(valueStringifier, defaultValueSupplier);
    }
 
    public static <T> CycleButton.Builder<T> builder(Function<T, Component> valueStringifier, T defaultValue) {
        return new CycleButton.Builder<>(valueStringifier, () -> defaultValue);
    }
 
    public static CycleButton.Builder<Boolean> booleanBuilder(Component trueText, Component falseText, boolean defaultValue) {
        return new CycleButton.Builder<>(b -> b == Boolean.TRUE ? trueText : falseText, () -> defaultValue).withValues(BOOLEAN_OPTIONS);
    }
 
    public static CycleButton.Builder<Boolean> onOffBuilder(boolean initialValue) {
        return new CycleButton.Builder<>(b -> b == Boolean.TRUE ? CommonComponents.OPTION_ON : CommonComponents.OPTION_OFF, () -> initialValue)
            .withValues(BOOLEAN_OPTIONS);
    }
 
    @OnlyIn(Dist.CLIENT)
    public static class Builder<T> {
        private final Supplier<T> defaultValueSupplier;
        private final Function<T, Component> valueStringifier;
        private OptionInstance.TooltipSupplier<T> tooltipSupplier = value -> null;
        private CycleButton.SpriteSupplier<T> spriteSupplier = (button, value) -> null;
        private Function<CycleButton<T>, MutableComponent> narrationProvider = CycleButton::createDefaultNarrationMessage;
        private CycleButton.ValueListSupplier<T> values = CycleButton.ValueListSupplier.create(ImmutableList.of());
        private CycleButton.DisplayState displayState = CycleButton.DisplayState.NAME_AND_VALUE;
 
        public Builder(Function<T, Component> valueStringifier, Supplier<T> defaultValueSupplier) {
            this.valueStringifier = valueStringifier;
            this.defaultValueSupplier = defaultValueSupplier;
        }
 
        public CycleButton.Builder<T> withValues(Collection<T> values) {
            return this.withValues(CycleButton.ValueListSupplier.create(values));
        }
 
        @SafeVarargs
        public final CycleButton.Builder<T> withValues(T... values) {
            return this.withValues(ImmutableList.copyOf(values));
        }
 
        public CycleButton.Builder<T> withValues(List<T> values, List<T> altValues) {
            return this.withValues(CycleButton.ValueListSupplier.create(CycleButton.DEFAULT_ALT_LIST_SELECTOR, values, altValues));
        }
 
        public CycleButton.Builder<T> withValues(BooleanSupplier altCondition, List<T> values, List<T> altValues) {
            return this.withValues(CycleButton.ValueListSupplier.create(altCondition, values, altValues));
        }
 
        public CycleButton.Builder<T> withValues(CycleButton.ValueListSupplier<T> valueListSupplier) {
            this.values = valueListSupplier;
            return this;
        }
 
        public CycleButton.Builder<T> withTooltip(OptionInstance.TooltipSupplier<T> tooltipSupplier) {
            this.tooltipSupplier = tooltipSupplier;
            return this;
        }
 
        public CycleButton.Builder<T> withCustomNarration(Function<CycleButton<T>, MutableComponent> narrationProvider) {
            this.narrationProvider = narrationProvider;
            return this;
        }
 
        public CycleButton.Builder<T> withSprite(CycleButton.SpriteSupplier<T> spriteSupplier) {
            this.spriteSupplier = spriteSupplier;
            return this;
        }
 
        public CycleButton.Builder<T> displayState(CycleButton.DisplayState state) {
            this.displayState = state;
            return this;
        }
 
        public CycleButton.Builder<T> displayOnlyValue() {
            return this.displayState(CycleButton.DisplayState.VALUE);
        }
 
        public CycleButton<T> create(Component name, CycleButton.OnValueChange<T> valueChangeListener) {
            return this.create(0, 0, 150, 20, name, valueChangeListener);
        }
 
        public CycleButton<T> create(int x, int y, int width, int height, Component name) {
            return this.create(x, y, width, height, name, (button, value) -> {});
        }
 
        public CycleButton<T> create(int x, int y, int width, int height, Component name, CycleButton.OnValueChange<T> valueChangeListener) {
            List<T> values = this.values.getDefaultList();
            if (values.isEmpty()) {
                throw new IllegalStateException("No values for cycle button");
            } else {
                T initialValue = this.defaultValueSupplier.get();
                int initialIndex = values.indexOf(initialValue);
                Component valueText = this.valueStringifier.apply(initialValue);
                Component initialTitle = (Component)(this.displayState == CycleButton.DisplayState.VALUE
                    ? valueText
                    : CommonComponents.optionNameValue(name, valueText));
                return new CycleButton<>(
                    x,
                    y,
                    width,
                    height,
                    initialTitle,
                    name,
                    initialIndex,
                    initialValue,
                    this.defaultValueSupplier,
                    this.values,
                    this.valueStringifier,
                    this.narrationProvider,
                    valueChangeListener,
                    this.tooltipSupplier,
                    this.displayState,
                    this.spriteSupplier
                );
            }
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public static enum DisplayState {
        NAME_AND_VALUE,
        VALUE,
        HIDE;
    }
 
    @FunctionalInterface
    @OnlyIn(Dist.CLIENT)
    public interface OnValueChange<T> {
        void onValueChange(CycleButton<T> button, T value);
    }
 
    @FunctionalInterface
    @OnlyIn(Dist.CLIENT)
    public interface SpriteSupplier<T> {
        @Nullable Identifier apply(CycleButton<T> button, T value);
    }
 
    @OnlyIn(Dist.CLIENT)
    public interface ValueListSupplier<T> {
        List<T> getSelectedList();
 
        List<T> getDefaultList();
 
        static <T> CycleButton.ValueListSupplier<T> create(Collection<T> values) {
            final List<T> copy = ImmutableList.copyOf(values);
            return new CycleButton.ValueListSupplier<T>() {
                @Override
                public List<T> getSelectedList() {
                    return copy;
                }
 
                @Override
                public List<T> getDefaultList() {
                    return copy;
                }
            };
        }
 
        static <T> CycleButton.ValueListSupplier<T> create(BooleanSupplier altSelector, List<T> defaultList, List<T> altList) {
            final List<T> defaultCopy = ImmutableList.copyOf(defaultList);
            final List<T> altCopy = ImmutableList.copyOf(altList);
            return new CycleButton.ValueListSupplier<T>() {
                @Override
                public List<T> getSelectedList() {
                    return altSelector.getAsBoolean() ? altCopy : defaultCopy;
                }
 
                @Override
                public List<T> getDefaultList() {
                    return defaultCopy;
                }
            };
        }
    }
}

引用的其他类