SwitchGrid.java
net.minecraft.client.gui.screens.worldselection.SwitchGrid
信息
- 全限定名:net.minecraft.client.gui.screens.worldselection.SwitchGrid
- 类型:package-private class
- 包:net.minecraft.client.gui.screens.worldselection
- 源码路径:src/main/java/net/minecraft/client/gui/screens/worldselection/SwitchGrid.java
- 起始行号:L25
- 职责:
TODO
字段/常量
-
DEFAULT_SWITCH_BUTTON_WIDTH- 类型:
int - 修饰符:
private static final - 源码定位:
L26 - 说明:
TODO
- 类型:
-
switches- 类型:
List<SwitchGrid.LabeledSwitch> - 修饰符:
private final - 源码定位:
L27 - 说明:
TODO
- 类型:
-
layout- 类型:
Layout - 修饰符:
private final - 源码定位:
L28 - 说明:
TODO
- 类型:
内部类/嵌套类型
-
net.minecraft.client.gui.screens.worldselection.SwitchGrid.Builder- 类型:
class - 修饰符:
public static - 源码定位:
L48 - 说明:
TODO
- 类型:
-
net.minecraft.client.gui.screens.worldselection.SwitchGrid.InfoUnderneathSettings- 类型:
record - 修饰符:
private - 源码定位:
L104 - 说明:
TODO
- 类型:
-
net.minecraft.client.gui.screens.worldselection.SwitchGrid.LabeledSwitch- 类型:
record - 修饰符:
private - 源码定位:
L108 - 说明:
TODO
- 类型:
-
net.minecraft.client.gui.screens.worldselection.SwitchGrid.SwitchBuilder- 类型:
class - 修饰符:
public static - 源码定位:
L118 - 说明:
TODO
- 类型:
构造器
private SwitchGrid(List<SwitchGrid.LabeledSwitch> switches, Layout layout) @ L30
- 构造器名:SwitchGrid
- 源码定位:L30
- 修饰符:private
参数:
- switches: List<SwitchGrid.LabeledSwitch>
- layout: Layout
说明:
TODO
方法
下面的方法块按源码顺序生成。
public Layout layout() @ L35
- 方法名:layout
- 源码定位:L35
- 返回类型:Layout
- 修饰符:public
参数:
- 无
说明:
TODO
public void refreshStates() @ L39
- 方法名:refreshStates
- 源码定位:L39
- 返回类型:void
- 修饰符:public
参数:
- 无
说明:
TODO
public static SwitchGrid.Builder builder(int width) @ L43
- 方法名:builder
- 源码定位:L43
- 返回类型:SwitchGrid.Builder
- 修饰符:public static
参数:
- width: int
说明:
TODO
代码
@OnlyIn(Dist.CLIENT)
class SwitchGrid {
private static final int DEFAULT_SWITCH_BUTTON_WIDTH = 44;
private final List<SwitchGrid.LabeledSwitch> switches;
private final Layout layout;
private SwitchGrid(List<SwitchGrid.LabeledSwitch> switches, Layout layout) {
this.switches = switches;
this.layout = layout;
}
public Layout layout() {
return this.layout;
}
public void refreshStates() {
this.switches.forEach(SwitchGrid.LabeledSwitch::refreshState);
}
public static SwitchGrid.Builder builder(int width) {
return new SwitchGrid.Builder(width);
}
@OnlyIn(Dist.CLIENT)
public static class Builder {
private final int width;
private final List<SwitchGrid.SwitchBuilder> switchBuilders = new ArrayList<>();
private int paddingLeft;
private int rowSpacing = 4;
private int rowCount;
private Optional<SwitchGrid.InfoUnderneathSettings> infoUnderneath = Optional.empty();
public Builder(int width) {
this.width = width;
}
private void increaseRow() {
this.rowCount++;
}
public SwitchGrid.SwitchBuilder addSwitch(Component label, BooleanSupplier stateSupplier, Consumer<Boolean> onClicked) {
SwitchGrid.SwitchBuilder switchBuilder = new SwitchGrid.SwitchBuilder(label, stateSupplier, onClicked, 44);
this.switchBuilders.add(switchBuilder);
return switchBuilder;
}
public SwitchGrid.Builder withPaddingLeft(int paddingLeft) {
this.paddingLeft = paddingLeft;
return this;
}
public SwitchGrid.Builder withRowSpacing(int rowSpacing) {
this.rowSpacing = rowSpacing;
return this;
}
public SwitchGrid build() {
GridLayout switchGrid = new GridLayout().rowSpacing(this.rowSpacing);
switchGrid.addChild(SpacerElement.width(this.width - 44), 0, 0);
switchGrid.addChild(SpacerElement.width(44), 0, 1);
List<SwitchGrid.LabeledSwitch> switches = new ArrayList<>();
this.rowCount = 0;
for (SwitchGrid.SwitchBuilder switchBuilder : this.switchBuilders) {
switches.add(switchBuilder.build(this, switchGrid, 0));
}
switchGrid.arrangeElements();
SwitchGrid result = new SwitchGrid(switches, switchGrid);
result.refreshStates();
return result;
}
public SwitchGrid.Builder withInfoUnderneath(int maxRows, boolean alwaysMaxHeight) {
this.infoUnderneath = Optional.of(new SwitchGrid.InfoUnderneathSettings(maxRows, alwaysMaxHeight));
return this;
}
}
@OnlyIn(Dist.CLIENT)
private record InfoUnderneathSettings(int maxInfoRows, boolean alwaysMaxHeight) {
}
@OnlyIn(Dist.CLIENT)
private record LabeledSwitch(CycleButton<Boolean> button, BooleanSupplier stateSupplier, @Nullable BooleanSupplier isActiveCondition) {
public void refreshState() {
this.button.setValue(this.stateSupplier.getAsBoolean());
if (this.isActiveCondition != null) {
this.button.active = this.isActiveCondition.getAsBoolean();
}
}
}
@OnlyIn(Dist.CLIENT)
public static class SwitchBuilder {
private final Component label;
private final BooleanSupplier stateSupplier;
private final Consumer<Boolean> onClicked;
private @Nullable Component info;
private @Nullable BooleanSupplier isActiveCondition;
private final int buttonWidth;
private SwitchBuilder(Component label, BooleanSupplier stateSupplier, Consumer<Boolean> onClicked, int buttonWidth) {
this.label = label;
this.stateSupplier = stateSupplier;
this.onClicked = onClicked;
this.buttonWidth = buttonWidth;
}
public SwitchGrid.SwitchBuilder withIsActiveCondition(BooleanSupplier isActiveCondition) {
this.isActiveCondition = isActiveCondition;
return this;
}
public SwitchGrid.SwitchBuilder withInfo(Component info) {
this.info = info;
return this;
}
private SwitchGrid.LabeledSwitch build(SwitchGrid.Builder switchGridBuilder, GridLayout gridLayout, int startColumn) {
switchGridBuilder.increaseRow();
StringWidget labelWidget = new StringWidget(this.label, Minecraft.getInstance().font);
gridLayout.addChild(
labelWidget, switchGridBuilder.rowCount, startColumn, gridLayout.newCellSettings().align(0.0F, 0.5F).paddingLeft(switchGridBuilder.paddingLeft)
);
Optional<SwitchGrid.InfoUnderneathSettings> infoUnderneath = switchGridBuilder.infoUnderneath;
CycleButton.Builder<Boolean> buttonBuilder = CycleButton.onOffBuilder(this.stateSupplier.getAsBoolean());
buttonBuilder.displayOnlyValue();
boolean hasTooltip = this.info != null && infoUnderneath.isEmpty();
if (hasTooltip) {
Tooltip tooltip = Tooltip.create(this.info);
buttonBuilder.withTooltip(value -> tooltip);
}
if (this.info != null && !hasTooltip) {
buttonBuilder.withCustomNarration(buttonx -> CommonComponents.joinForNarration(this.label, buttonx.createDefaultNarrationMessage(), this.info));
} else {
buttonBuilder.withCustomNarration(buttonx -> CommonComponents.joinForNarration(this.label, buttonx.createDefaultNarrationMessage()));
}
CycleButton<Boolean> button = buttonBuilder.create(0, 0, this.buttonWidth, 20, Component.empty(), (b, value) -> this.onClicked.accept(value));
if (this.isActiveCondition != null) {
button.active = this.isActiveCondition.getAsBoolean();
}
gridLayout.addChild(button, switchGridBuilder.rowCount, startColumn + 1, gridLayout.newCellSettings().alignHorizontallyRight());
if (this.info != null) {
infoUnderneath.ifPresent(
infoUnderneathSettings -> {
Component styledInfo = this.info.copy().withStyle(ChatFormatting.GRAY);
Font font = Minecraft.getInstance().font;
MultiLineTextWidget infoWidget = new MultiLineTextWidget(styledInfo, font);
infoWidget.setMaxWidth(switchGridBuilder.width - switchGridBuilder.paddingLeft - this.buttonWidth);
infoWidget.setMaxRows(infoUnderneathSettings.maxInfoRows());
switchGridBuilder.increaseRow();
int extraBottomPadding = infoUnderneathSettings.alwaysMaxHeight ? 9 * infoUnderneathSettings.maxInfoRows - infoWidget.getHeight() : 0;
gridLayout.addChild(
infoWidget,
switchGridBuilder.rowCount,
startColumn,
gridLayout.newCellSettings().paddingTop(-switchGridBuilder.rowSpacing).paddingBottom(extraBottomPadding)
);
}
);
}
return new SwitchGrid.LabeledSwitch(button, this.stateSupplier, this.isActiveCondition);
}
}
}引用的其他类
-
- 引用位置:
方法调用 - 关联成员:
Minecraft.getInstance()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
CycleButton.onOffBuilder()
- 引用位置:
-
- 引用位置:
构造调用 - 关联成员:
MultiLineTextWidget()
- 引用位置:
-
- 引用位置:
构造调用 - 关联成员:
StringWidget()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Tooltip.create()
- 引用位置:
-
- 引用位置:
构造调用 - 关联成员:
GridLayout()
- 引用位置:
-
- 引用位置:
参数/字段/返回值
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
SpacerElement.width()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
CommonComponents.joinForNarration()
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Component.empty()
- 引用位置: