| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.OptionInstance;
|
| 9 | import net.minecraft.client.Options;
|
| 10 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 11 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 12 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 13 | import net.minecraft.client.gui.screens.Screen;
|
| 14 | import net.minecraft.client.gui.screens.options.OptionsSubScreen;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class OptionsList extends ContainerObjectSelectionList<OptionsList.AbstractEntry> {
|
| 20 | private static final int BIG_BUTTON_WIDTH = 310;
|
| 21 | private static final int DEFAULT_ITEM_HEIGHT = 25;
|
| 22 | private final OptionsSubScreen screen;
|
| 23 |
|
| 24 | public OptionsList(final Minecraft minecraft, final int width, final OptionsSubScreen screen) {
|
| 25 | super(minecraft, width, screen.layout.getContentHeight(), screen.layout.getHeaderHeight(), 25);
|
| 26 | this.centerListVertically = false;
|
| 27 | this.screen = screen;
|
| 28 | }
|
| 29 |
|
| 30 | public void addBig(final OptionInstance<?> option) {
|
| 31 | this.addEntry(OptionsList.Entry.big(this.minecraft.options, option, this.screen));
|
| 32 | }
|
| 33 |
|
| 34 | public void addBig(final AbstractWidget widget) {
|
| 35 | this.addEntry(OptionsList.Entry.big(widget, this.screen));
|
| 36 | }
|
| 37 |
|
| 38 | public void addSmall(final OptionInstance<?>... options) {
|
| 39 | for (int i = 0; i < options.length; i += 2) {
|
| 40 | OptionInstance<?> secondOption = i < options.length - 1 ? options[i + 1] : null;
|
| 41 | this.addEntry(OptionsList.Entry.small(this.minecraft.options, options[i], secondOption, this.screen));
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | public void addSmall(final List<AbstractWidget> widgets) {
|
| 46 | for (int i = 0; i < widgets.size(); i += 2) {
|
| 47 | this.addSmall(widgets.get(i), i < widgets.size() - 1 ? widgets.get(i + 1) : null);
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | public void addSmall(final AbstractWidget firstOption, final @Nullable AbstractWidget secondOption) {
|
| 52 | this.addEntry(OptionsList.Entry.small(firstOption, secondOption, this.screen));
|
| 53 | }
|
| 54 |
|
| 55 | public void addSmall(final AbstractWidget firstOption, final OptionInstance<?> firstOptionInstance, final @Nullable AbstractWidget secondOption) {
|
| 56 | this.addEntry(OptionsList.Entry.small(firstOption, firstOptionInstance, secondOption, this.screen));
|
| 57 | }
|
| 58 |
|
| 59 | public void addHeader(final Component text) {
|
| 60 | int lineHeight = 9;
|
| 61 | int paddingTop = this.children().isEmpty() ? 0 : lineHeight * 2;
|
| 62 | this.addEntry(new OptionsList.HeaderEntry(this.screen, text, paddingTop), paddingTop + lineHeight * 2);
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public int getRowWidth() {
|
| 67 | return 310;
|
| 68 | }
|
| 69 |
|
| 70 | public @Nullable AbstractWidget findOption(final OptionInstance<?> option) {
|
| 71 | for (OptionsList.AbstractEntry child : this.children()) {
|
| 72 | if (child instanceof OptionsList.Entry entry) {
|
| 73 | AbstractWidget widgetForOption = entry.findOption(option);
|
| 74 | if (widgetForOption != null) {
|
| 75 | return widgetForOption;
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return null;
|
| 81 | }
|
| 82 |
|
| 83 | public void applyUnsavedChanges() {
|
| 84 | for (OptionsList.AbstractEntry child : this.children()) {
|
| 85 | if (child instanceof OptionsList.Entry entry) {
|
| 86 | for (OptionsList.OptionInstanceWidget optionInstanceWidget : entry.children) {
|
| 87 | if (optionInstanceWidget.optionInstance() != null && optionInstanceWidget.widget() instanceof OptionInstance.OptionInstanceSliderButton<?> optionSlider) {
|
| 88 | optionSlider.applyUnsavedValue();
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | public void resetOption(final OptionInstance<?> option) {
|
| 96 | for (OptionsList.AbstractEntry child : this.children()) {
|
| 97 | if (child instanceof OptionsList.Entry entry) {
|
| 98 | for (OptionsList.OptionInstanceWidget optionInstanceWidget : entry.children) {
|
| 99 | if (optionInstanceWidget.optionInstance() == option && optionInstanceWidget.widget() instanceof ResettableOptionWidget resettableOptionWidget) {
|
| 100 | resettableOptionWidget.resetValue();
|
| 101 | return;
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | @Environment(EnvType.CLIENT)
|
| 109 | protected abstract static class AbstractEntry extends ContainerObjectSelectionList.Entry<OptionsList.AbstractEntry> {
|
| 110 | }
|
| 111 |
|
| 112 | @Environment(EnvType.CLIENT)
|
| 113 | protected static class Entry extends OptionsList.AbstractEntry {
|
| 114 | private final List<OptionsList.OptionInstanceWidget> children;
|
| 115 | private final Screen screen;
|
| 116 | private static final int X_OFFSET = 160;
|
| 117 |
|
| 118 | private Entry(final List<OptionsList.OptionInstanceWidget> widgets, final Screen screen) {
|
| 119 | this.children = widgets;
|
| 120 | this.screen = screen;
|
| 121 | }
|
| 122 |
|
| 123 | public static OptionsList.Entry big(final Options options, final OptionInstance<?> optionInstance, final Screen screen) {
|
| 124 | return new OptionsList.Entry(List.of(new OptionsList.OptionInstanceWidget(optionInstance.createButton(options, 0, 0, 310), optionInstance)), screen);
|
| 125 | }
|
| 126 |
|
| 127 | public static OptionsList.Entry big(final AbstractWidget widget, final Screen screen) {
|
| 128 | widget.setWidth(310);
|
| 129 | return new OptionsList.Entry(List.of(new OptionsList.OptionInstanceWidget(widget, null)), screen);
|
| 130 | }
|
| 131 |
|
| 132 | public static OptionsList.Entry small(final AbstractWidget leftWidget, final @Nullable AbstractWidget rightWidget, final Screen screen) {
|
| 133 | return rightWidget == null
|
| 134 | ? new OptionsList.Entry(List.of(new OptionsList.OptionInstanceWidget(leftWidget)), screen)
|
| 135 | : new OptionsList.Entry(List.of(new OptionsList.OptionInstanceWidget(leftWidget), new OptionsList.OptionInstanceWidget(rightWidget)), screen);
|
| 136 | }
|
| 137 |
|
| 138 | public static OptionsList.Entry small(
|
| 139 | final AbstractWidget leftWidget, final OptionInstance<?> leftWidgetOptionInstance, final @Nullable AbstractWidget rightWidget, final Screen screen
|
| 140 | ) {
|
| 141 | return rightWidget == null
|
| 142 | ? new OptionsList.Entry(List.of(new OptionsList.OptionInstanceWidget(leftWidget, leftWidgetOptionInstance)), screen)
|
| 143 | : new OptionsList.Entry(
|
| 144 | List.of(new OptionsList.OptionInstanceWidget(leftWidget, leftWidgetOptionInstance), new OptionsList.OptionInstanceWidget(rightWidget)), screen
|
| 145 | );
|
| 146 | }
|
| 147 |
|
| 148 | public static OptionsList.Entry small(
|
| 149 | final Options options, final OptionInstance<?> optionA, final @Nullable OptionInstance<?> optionB, final OptionsSubScreen screen
|
| 150 | ) {
|
| 151 | AbstractWidget buttonA = optionA.createButton(options);
|
| 152 | return optionB == null
|
| 153 | ? new OptionsList.Entry(List.of(new OptionsList.OptionInstanceWidget(buttonA, optionA)), screen)
|
| 154 | : new OptionsList.Entry(
|
| 155 | List.of(new OptionsList.OptionInstanceWidget(buttonA, optionA), new OptionsList.OptionInstanceWidget(optionB.createButton(options), optionB)), screen
|
| 156 | );
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 161 | int xOffset = 0;
|
| 162 | int x = this.screen.width / 2 - 155;
|
| 163 |
|
| 164 | for (OptionsList.OptionInstanceWidget optionInstanceWidget : this.children) {
|
| 165 | optionInstanceWidget.widget().setPosition(x + xOffset, this.getContentY());
|
| 166 | optionInstanceWidget.widget().extractRenderState(graphics, mouseX, mouseY, a);
|
| 167 | xOffset += 160;
|
| 168 | }
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public List<? extends GuiEventListener> children() {
|
| 173 | return Lists.transform(this.children, OptionsList.OptionInstanceWidget::widget);
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public List<? extends NarratableEntry> narratables() {
|
| 178 | return Lists.transform(this.children, OptionsList.OptionInstanceWidget::widget);
|
| 179 | }
|
| 180 |
|
| 181 | public @Nullable AbstractWidget findOption(final OptionInstance<?> option) {
|
| 182 | for (OptionsList.OptionInstanceWidget child : this.children) {
|
| 183 | if (child.optionInstance == option) {
|
| 184 | return child.widget();
|
| 185 | }
|
| 186 | }
|
| 187 |
|
| 188 | return null;
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | @Environment(EnvType.CLIENT)
|
| 193 | protected static class HeaderEntry extends OptionsList.AbstractEntry {
|
| 194 | private final Screen screen;
|
| 195 | private final int paddingTop;
|
| 196 | private final FocusableTextWidget widget;
|
| 197 |
|
| 198 | protected HeaderEntry(final Screen screen, final Component text, final int paddingTop) {
|
| 199 | this.screen = screen;
|
| 200 | this.paddingTop = paddingTop;
|
| 201 | this.widget = FocusableTextWidget.builder(text, screen.getFont())
|
| 202 | .alwaysShowBorder(false)
|
| 203 | .backgroundFill(FocusableTextWidget.BackgroundFill.ON_FOCUS)
|
| 204 | .build();
|
| 205 | }
|
| 206 |
|
| 207 | @Override
|
| 208 | public List<? extends NarratableEntry> narratables() {
|
| 209 | return List.of(this.widget);
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public void extractContent(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final boolean hovered, final float a) {
|
| 214 | this.widget.setPosition(this.screen.width / 2 - this.widget.getWidth() / 2, this.getContentY() + this.paddingTop);
|
| 215 | this.widget.extractRenderState(graphics, mouseX, mouseY, a);
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | public List<? extends GuiEventListener> children() {
|
| 220 | return List.of(this.widget);
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | @Environment(EnvType.CLIENT)
|
| 225 | public record OptionInstanceWidget(AbstractWidget widget, @Nullable OptionInstance<?> optionInstance) {
|
| 226 | public OptionInstanceWidget(final AbstractWidget widget) {
|
| 227 | this(widget, null);
|
| 228 | }
|
| 229 | }
|
| 230 | }
|
| 231 |
|