|
@@ -1,127 +1,39 @@
|
|
package me.shedaniel.clothconfig2.gui.entries;
|
|
package me.shedaniel.clothconfig2.gui.entries;
|
|
|
|
|
|
-import com.google.common.collect.ImmutableList;
|
|
|
|
-import com.google.common.collect.Lists;
|
|
|
|
-import net.minecraft.client.MinecraftClient;
|
|
|
|
-import net.minecraft.client.gui.Element;
|
|
|
|
-import net.minecraft.client.gui.widget.ButtonWidget;
|
|
|
|
import net.minecraft.client.resource.language.I18n;
|
|
import net.minecraft.client.resource.language.I18n;
|
|
-import net.minecraft.client.util.Window;
|
|
|
|
|
|
|
|
-import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
-import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.Function;
|
|
import java.util.function.Function;
|
|
import java.util.function.Supplier;
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
-public class EnumListEntry<T extends Enum<?>> extends TooltipListEntry<T> {
|
|
|
|
|
|
+public class EnumListEntry<T extends Enum<?>> extends SelectionListEntry<T> {
|
|
|
|
|
|
public static final Function<Enum, String> DEFAULT_NAME_PROVIDER = t -> I18n.translate(t instanceof Translatable ? ((Translatable) t).getKey() : t.toString());
|
|
public static final Function<Enum, String> DEFAULT_NAME_PROVIDER = t -> I18n.translate(t instanceof Translatable ? ((Translatable) t).getKey() : t.toString());
|
|
- private ImmutableList<T> values;
|
|
|
|
- private AtomicInteger index;
|
|
|
|
- private ButtonWidget buttonWidget, resetButton;
|
|
|
|
- private Consumer<T> saveConsumer;
|
|
|
|
- private Supplier<T> defaultValue;
|
|
|
|
- private List<Element> widgets;
|
|
|
|
- private Function<Enum, String> enumNameProvider;
|
|
|
|
|
|
|
|
@Deprecated
|
|
@Deprecated
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, Consumer<T> saveConsumer) {
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, Consumer<T> saveConsumer) {
|
|
- this(fieldName, clazz, value, "text.cloth-config.reset_value", null, saveConsumer);
|
|
|
|
|
|
+ super(fieldName, clazz.getEnumConstants(), value, "text.cloth-config.reset_value", null, saveConsumer);
|
|
}
|
|
}
|
|
|
|
|
|
@Deprecated
|
|
@Deprecated
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, String resetButtonKey, Supplier<T> defaultValue, Consumer<T> saveConsumer) {
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, String resetButtonKey, Supplier<T> defaultValue, Consumer<T> saveConsumer) {
|
|
- this(fieldName, clazz, value, resetButtonKey, defaultValue, saveConsumer, DEFAULT_NAME_PROVIDER);
|
|
|
|
|
|
+ super(fieldName, clazz.getEnumConstants(), value, resetButtonKey, defaultValue, saveConsumer, v -> DEFAULT_NAME_PROVIDER.apply(v));
|
|
}
|
|
}
|
|
|
|
|
|
@Deprecated
|
|
@Deprecated
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, String resetButtonKey, Supplier<T> defaultValue, Consumer<T> saveConsumer, Function<Enum, String> enumNameProvider) {
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, String resetButtonKey, Supplier<T> defaultValue, Consumer<T> saveConsumer, Function<Enum, String> enumNameProvider) {
|
|
- this(fieldName, clazz, value, resetButtonKey, defaultValue, saveConsumer, enumNameProvider, null);
|
|
|
|
|
|
+ super(fieldName, clazz.getEnumConstants(), value, resetButtonKey, defaultValue, saveConsumer, v -> enumNameProvider.apply(v), null);
|
|
}
|
|
}
|
|
|
|
|
|
@Deprecated
|
|
@Deprecated
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, String resetButtonKey, Supplier<T> defaultValue, Consumer<T> saveConsumer, Function<Enum, String> enumNameProvider, Supplier<Optional<String[]>> tooltipSupplier) {
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, String resetButtonKey, Supplier<T> defaultValue, Consumer<T> saveConsumer, Function<Enum, String> enumNameProvider, Supplier<Optional<String[]>> tooltipSupplier) {
|
|
- this(fieldName, clazz, value, resetButtonKey, defaultValue, saveConsumer, enumNameProvider, tooltipSupplier, false);
|
|
|
|
|
|
+ super(fieldName, clazz.getEnumConstants(), value, resetButtonKey, defaultValue, saveConsumer, v -> enumNameProvider.apply(v), tooltipSupplier, false);
|
|
}
|
|
}
|
|
|
|
|
|
@Deprecated
|
|
@Deprecated
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, String resetButtonKey, Supplier<T> defaultValue, Consumer<T> saveConsumer, Function<Enum, String> enumNameProvider, Supplier<Optional<String[]>> tooltipSupplier, boolean requiresRestart) {
|
|
public EnumListEntry(String fieldName, Class<T> clazz, T value, String resetButtonKey, Supplier<T> defaultValue, Consumer<T> saveConsumer, Function<Enum, String> enumNameProvider, Supplier<Optional<String[]>> tooltipSupplier, boolean requiresRestart) {
|
|
- super(fieldName, tooltipSupplier, requiresRestart);
|
|
|
|
- T[] valuesArray = clazz.getEnumConstants();
|
|
|
|
- if (valuesArray != null)
|
|
|
|
- this.values = ImmutableList.copyOf(valuesArray);
|
|
|
|
- else
|
|
|
|
- this.values = ImmutableList.of(value);
|
|
|
|
- this.defaultValue = defaultValue;
|
|
|
|
- this.index = new AtomicInteger(this.values.indexOf(value));
|
|
|
|
- this.index.compareAndSet(-1, 0);
|
|
|
|
- this.buttonWidget = new ButtonWidget(0, 0, 150, 20, "", widget -> {
|
|
|
|
- EnumListEntry.this.index.incrementAndGet();
|
|
|
|
- EnumListEntry.this.index.compareAndSet(EnumListEntry.this.values.size(), 0);
|
|
|
|
- getScreen().setEdited(true, isRequiresRestart());
|
|
|
|
- });
|
|
|
|
- this.resetButton = new ButtonWidget(0, 0, MinecraftClient.getInstance().textRenderer.getStringWidth(I18n.translate(resetButtonKey)) + 6, 20, I18n.translate(resetButtonKey), widget -> {
|
|
|
|
- EnumListEntry.this.index.set(getDefaultIndex());
|
|
|
|
- getScreen().setEdited(true, isRequiresRestart());
|
|
|
|
- });
|
|
|
|
- this.saveConsumer = saveConsumer;
|
|
|
|
- this.widgets = Lists.newArrayList(buttonWidget, resetButton);
|
|
|
|
- this.enumNameProvider = enumNameProvider;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void save() {
|
|
|
|
- if (saveConsumer != null)
|
|
|
|
- saveConsumer.accept(getValue());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public T getValue() {
|
|
|
|
- return this.values.get(this.index.get());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public Optional<T> getDefaultValue() {
|
|
|
|
- return defaultValue == null ? Optional.empty() : Optional.ofNullable(defaultValue.get());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void render(int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
|
|
|
|
- super.render(index, y, x, entryWidth, entryHeight, mouseX, mouseY, isSelected, delta);
|
|
|
|
- Window window = MinecraftClient.getInstance().getWindow();
|
|
|
|
- this.resetButton.active = isEditable() && getDefaultValue().isPresent() && getDefaultIndex() != this.index.get();
|
|
|
|
- this.resetButton.y = y;
|
|
|
|
- this.buttonWidget.active = isEditable();
|
|
|
|
- this.buttonWidget.y = y;
|
|
|
|
- this.buttonWidget.setMessage(enumNameProvider.apply(getValue()));
|
|
|
|
- if (MinecraftClient.getInstance().textRenderer.isRightToLeft()) {
|
|
|
|
- MinecraftClient.getInstance().textRenderer.drawWithShadow(I18n.translate(getFieldName()), window.getScaledWidth() - x - MinecraftClient.getInstance().textRenderer.getStringWidth(I18n.translate(getFieldName())), y + 5, getPreferredTextColor());
|
|
|
|
- this.resetButton.x = x;
|
|
|
|
- this.buttonWidget.x = x + resetButton.getWidth() + 2;
|
|
|
|
- this.buttonWidget.setWidth(150 - resetButton.getWidth() - 2);
|
|
|
|
- } else {
|
|
|
|
- MinecraftClient.getInstance().textRenderer.drawWithShadow(I18n.translate(getFieldName()), x, y + 5, getPreferredTextColor());
|
|
|
|
- this.resetButton.x = x + entryWidth - resetButton.getWidth();
|
|
|
|
- this.buttonWidget.x = x + entryWidth - 150;
|
|
|
|
- this.buttonWidget.setWidth(150 - resetButton.getWidth() - 2);
|
|
|
|
- }
|
|
|
|
- resetButton.render(mouseX, mouseY, delta);
|
|
|
|
- buttonWidget.render(mouseX, mouseY, delta);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private int getDefaultIndex() {
|
|
|
|
- return Math.max(0, this.values.indexOf(this.defaultValue.get()));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public List<? extends Element> children() {
|
|
|
|
- return widgets;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- public static interface Translatable {
|
|
|
|
- String getKey();
|
|
|
|
|
|
+ super(fieldName, clazz.getEnumConstants(), value, resetButtonKey, defaultValue, saveConsumer, v -> enumNameProvider.apply(v), tooltipSupplier, requiresRestart);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|