| 1 | package net.minecraft.client.gui.screens.worldselection;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.function.BooleanSupplier;
|
| 7 | import java.util.function.Consumer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.ChatFormatting;
|
| 11 | import net.minecraft.client.Minecraft;
|
| 12 | import net.minecraft.client.gui.Font;
|
| 13 | import net.minecraft.client.gui.components.CycleButton;
|
| 14 | import net.minecraft.client.gui.components.MultiLineTextWidget;
|
| 15 | import net.minecraft.client.gui.components.StringWidget;
|
| 16 | import net.minecraft.client.gui.components.Tooltip;
|
| 17 | import net.minecraft.client.gui.layouts.GridLayout;
|
| 18 | import net.minecraft.client.gui.layouts.Layout;
|
| 19 | import net.minecraft.client.gui.layouts.SpacerElement;
|
| 20 | import net.minecraft.network.chat.CommonComponents;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | class SwitchGrid {
|
| 26 | private static final int DEFAULT_SWITCH_BUTTON_WIDTH = 44;
|
| 27 | private final List<SwitchGrid.LabeledSwitch> switches;
|
| 28 | private final Layout layout;
|
| 29 |
|
| 30 | private SwitchGrid(final List<SwitchGrid.LabeledSwitch> switches, final Layout layout) {
|
| 31 | this.switches = switches;
|
| 32 | this.layout = layout;
|
| 33 | }
|
| 34 |
|
| 35 | public Layout layout() {
|
| 36 | return this.layout;
|
| 37 | }
|
| 38 |
|
| 39 | public void refreshStates() {
|
| 40 | this.switches.forEach(SwitchGrid.LabeledSwitch::refreshState);
|
| 41 | }
|
| 42 |
|
| 43 | public static SwitchGrid.Builder builder(final int width) {
|
| 44 | return new SwitchGrid.Builder(width);
|
| 45 | }
|
| 46 |
|
| 47 | @Environment(EnvType.CLIENT)
|
| 48 | public static class Builder {
|
| 49 | private final int width;
|
| 50 | private final List<SwitchGrid.SwitchBuilder> switchBuilders = new ArrayList<>();
|
| 51 | private int paddingLeft;
|
| 52 | private int rowSpacing = 4;
|
| 53 | private int rowCount;
|
| 54 | private Optional<SwitchGrid.InfoUnderneathSettings> infoUnderneath = Optional.empty();
|
| 55 |
|
| 56 | public Builder(final int width) {
|
| 57 | this.width = width;
|
| 58 | }
|
| 59 |
|
| 60 | private void increaseRow() {
|
| 61 | this.rowCount++;
|
| 62 | }
|
| 63 |
|
| 64 | public SwitchGrid.SwitchBuilder addSwitch(final Component label, final BooleanSupplier stateSupplier, final Consumer<Boolean> onClicked) {
|
| 65 | SwitchGrid.SwitchBuilder switchBuilder = new SwitchGrid.SwitchBuilder(label, stateSupplier, onClicked, 44);
|
| 66 | this.switchBuilders.add(switchBuilder);
|
| 67 | return switchBuilder;
|
| 68 | }
|
| 69 |
|
| 70 | public SwitchGrid.Builder withPaddingLeft(final int paddingLeft) {
|
| 71 | this.paddingLeft = paddingLeft;
|
| 72 | return this;
|
| 73 | }
|
| 74 |
|
| 75 | public SwitchGrid.Builder withRowSpacing(final int rowSpacing) {
|
| 76 | this.rowSpacing = rowSpacing;
|
| 77 | return this;
|
| 78 | }
|
| 79 |
|
| 80 | public SwitchGrid build() {
|
| 81 | GridLayout switchGrid = new GridLayout().rowSpacing(this.rowSpacing);
|
| 82 | switchGrid.addChild(SpacerElement.width(this.width - 44), 0, 0);
|
| 83 | switchGrid.addChild(SpacerElement.width(44), 0, 1);
|
| 84 | List<SwitchGrid.LabeledSwitch> switches = new ArrayList<>();
|
| 85 | this.rowCount = 0;
|
| 86 |
|
| 87 | for (SwitchGrid.SwitchBuilder switchBuilder : this.switchBuilders) {
|
| 88 | switches.add(switchBuilder.build(this, switchGrid, 0));
|
| 89 | }
|
| 90 |
|
| 91 | switchGrid.arrangeElements();
|
| 92 | SwitchGrid result = new SwitchGrid(switches, switchGrid);
|
| 93 | result.refreshStates();
|
| 94 | return result;
|
| 95 | }
|
| 96 |
|
| 97 | public SwitchGrid.Builder withInfoUnderneath(final int maxRows, final boolean alwaysMaxHeight) {
|
| 98 | this.infoUnderneath = Optional.of(new SwitchGrid.InfoUnderneathSettings(maxRows, alwaysMaxHeight));
|
| 99 | return this;
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | @Environment(EnvType.CLIENT)
|
| 104 | private record InfoUnderneathSettings(int maxInfoRows, boolean alwaysMaxHeight) {
|
| 105 | }
|
| 106 |
|
| 107 | @Environment(EnvType.CLIENT)
|
| 108 | private record LabeledSwitch(CycleButton<Boolean> button, BooleanSupplier stateSupplier, @Nullable BooleanSupplier isActiveCondition) {
|
| 109 | public void refreshState() {
|
| 110 | this.button.setValue(this.stateSupplier.getAsBoolean());
|
| 111 | if (this.isActiveCondition != null) {
|
| 112 | this.button.active = this.isActiveCondition.getAsBoolean();
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Environment(EnvType.CLIENT)
|
| 118 | public static class SwitchBuilder {
|
| 119 | private final Component label;
|
| 120 | private final BooleanSupplier stateSupplier;
|
| 121 | private final Consumer<Boolean> onClicked;
|
| 122 | private @Nullable Component info;
|
| 123 | private @Nullable BooleanSupplier isActiveCondition;
|
| 124 | private final int buttonWidth;
|
| 125 |
|
| 126 | private SwitchBuilder(final Component label, final BooleanSupplier stateSupplier, final Consumer<Boolean> onClicked, final int buttonWidth) {
|
| 127 | this.label = label;
|
| 128 | this.stateSupplier = stateSupplier;
|
| 129 | this.onClicked = onClicked;
|
| 130 | this.buttonWidth = buttonWidth;
|
| 131 | }
|
| 132 |
|
| 133 | public SwitchGrid.SwitchBuilder withIsActiveCondition(final BooleanSupplier isActiveCondition) {
|
| 134 | this.isActiveCondition = isActiveCondition;
|
| 135 | return this;
|
| 136 | }
|
| 137 |
|
| 138 | public SwitchGrid.SwitchBuilder withInfo(final Component info) {
|
| 139 | this.info = info;
|
| 140 | return this;
|
| 141 | }
|
| 142 |
|
| 143 | private SwitchGrid.LabeledSwitch build(final SwitchGrid.Builder switchGridBuilder, final GridLayout gridLayout, final int startColumn) {
|
| 144 | switchGridBuilder.increaseRow();
|
| 145 | StringWidget labelWidget = new StringWidget(this.label, Minecraft.getInstance().font);
|
| 146 | gridLayout.addChild(
|
| 147 | labelWidget, switchGridBuilder.rowCount, startColumn, gridLayout.newCellSettings().align(0.0F, 0.5F).paddingLeft(switchGridBuilder.paddingLeft)
|
| 148 | );
|
| 149 | Optional<SwitchGrid.InfoUnderneathSettings> infoUnderneath = switchGridBuilder.infoUnderneath;
|
| 150 | CycleButton.Builder<Boolean> buttonBuilder = CycleButton.onOffBuilder(this.stateSupplier.getAsBoolean());
|
| 151 | buttonBuilder.displayOnlyValue();
|
| 152 | boolean hasTooltip = this.info != null && infoUnderneath.isEmpty();
|
| 153 | if (hasTooltip) {
|
| 154 | Tooltip tooltip = Tooltip.create(this.info);
|
| 155 | buttonBuilder.withTooltip(value -> tooltip);
|
| 156 | }
|
| 157 |
|
| 158 | if (this.info != null && !hasTooltip) {
|
| 159 | buttonBuilder.withCustomNarration(buttonx -> CommonComponents.joinForNarration(this.label, buttonx.createDefaultNarrationMessage(), this.info));
|
| 160 | } else {
|
| 161 | buttonBuilder.withCustomNarration(buttonx -> CommonComponents.joinForNarration(this.label, buttonx.createDefaultNarrationMessage()));
|
| 162 | }
|
| 163 |
|
| 164 | CycleButton<Boolean> button = buttonBuilder.create(0, 0, this.buttonWidth, 20, Component.empty(), (b, value) -> this.onClicked.accept(value));
|
| 165 | if (this.isActiveCondition != null) {
|
| 166 | button.active = this.isActiveCondition.getAsBoolean();
|
| 167 | }
|
| 168 |
|
| 169 | gridLayout.addChild(button, switchGridBuilder.rowCount, startColumn + 1, gridLayout.newCellSettings().alignHorizontallyRight());
|
| 170 | if (this.info != null) {
|
| 171 | infoUnderneath.ifPresent(
|
| 172 | infoUnderneathSettings -> {
|
| 173 | Component styledInfo = this.info.copy().withStyle(ChatFormatting.GRAY);
|
| 174 | Font font = Minecraft.getInstance().font;
|
| 175 | MultiLineTextWidget infoWidget = new MultiLineTextWidget(styledInfo, font);
|
| 176 | infoWidget.setMaxWidth(switchGridBuilder.width - switchGridBuilder.paddingLeft - this.buttonWidth);
|
| 177 | infoWidget.setMaxRows(infoUnderneathSettings.maxInfoRows());
|
| 178 | switchGridBuilder.increaseRow();
|
| 179 | int extraBottomPadding = infoUnderneathSettings.alwaysMaxHeight ? 9 * infoUnderneathSettings.maxInfoRows - infoWidget.getHeight() : 0;
|
| 180 | gridLayout.addChild(
|
| 181 | infoWidget,
|
| 182 | switchGridBuilder.rowCount,
|
| 183 | startColumn,
|
| 184 | gridLayout.newCellSettings().paddingTop(-switchGridBuilder.rowSpacing).paddingBottom(extraBottomPadding)
|
| 185 | );
|
| 186 | }
|
| 187 | );
|
| 188 | }
|
| 189 |
|
| 190 | return new SwitchGrid.LabeledSwitch(button, this.stateSupplier, this.isActiveCondition);
|
| 191 | }
|
| 192 | }
|
| 193 | }
|
| 194 |
|