Add multiple choice settings keys
This commit is contained in:
@@ -5,10 +5,7 @@ import net.minecraftforge.common.config.Configuration;
|
|||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class SettingsRegistry {
|
public class SettingsRegistry {
|
||||||
@@ -97,6 +94,10 @@ public class SettingsRegistry {
|
|||||||
T getDefault();
|
T getDefault();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface MultipleChoiceSettingKey<T> extends SettingKey<T> {
|
||||||
|
List<T> getChoices();
|
||||||
|
}
|
||||||
|
|
||||||
public static class SettingKeys<T> implements SettingKey<T> {
|
public static class SettingKeys<T> implements SettingKey<T> {
|
||||||
private final String category;
|
private final String category;
|
||||||
private final String key;
|
private final String key;
|
||||||
@@ -130,4 +131,21 @@ public class SettingsRegistry {
|
|||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class MultipleChoiceSettingKeys<T> extends SettingKeys<T> implements MultipleChoiceSettingKey<T> {
|
||||||
|
private List<T> choices = Collections.emptyList();
|
||||||
|
|
||||||
|
public MultipleChoiceSettingKeys(String category, String key, String displayString, T defaultValue) {
|
||||||
|
super(category, key, displayString, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChoices(List<T> choices) {
|
||||||
|
this.choices = Collections.unmodifiableList(choices);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<T> getChoices() {
|
||||||
|
return choices;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,22 @@
|
|||||||
package com.replaymod.core.gui;
|
package com.replaymod.core.gui;
|
||||||
|
|
||||||
|
import com.replaymod.core.SettingsRegistry;
|
||||||
import de.johni0702.minecraft.gui.container.AbstractGuiScreen;
|
import de.johni0702.minecraft.gui.container.AbstractGuiScreen;
|
||||||
import de.johni0702.minecraft.gui.container.GuiPanel;
|
import de.johni0702.minecraft.gui.container.GuiPanel;
|
||||||
import de.johni0702.minecraft.gui.element.GuiButton;
|
import de.johni0702.minecraft.gui.element.GuiButton;
|
||||||
import de.johni0702.minecraft.gui.element.GuiElement;
|
import de.johni0702.minecraft.gui.element.GuiElement;
|
||||||
import de.johni0702.minecraft.gui.element.GuiLabel;
|
import de.johni0702.minecraft.gui.element.GuiLabel;
|
||||||
import de.johni0702.minecraft.gui.element.GuiToggleButton;
|
import de.johni0702.minecraft.gui.element.GuiToggleButton;
|
||||||
|
import de.johni0702.minecraft.gui.element.advanced.GuiDropdownMenu;
|
||||||
import de.johni0702.minecraft.gui.layout.CustomLayout;
|
import de.johni0702.minecraft.gui.layout.CustomLayout;
|
||||||
import de.johni0702.minecraft.gui.layout.HorizontalLayout;
|
import de.johni0702.minecraft.gui.layout.HorizontalLayout;
|
||||||
import de.johni0702.minecraft.gui.layout.VerticalLayout;
|
import de.johni0702.minecraft.gui.layout.VerticalLayout;
|
||||||
import com.replaymod.core.SettingsRegistry;
|
import de.johni0702.minecraft.gui.utils.Consumer;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import net.minecraft.client.resources.I18n;
|
import net.minecraft.client.resources.I18n;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class GuiReplaySettings extends AbstractGuiScreen<GuiReplaySettings> {
|
public class GuiReplaySettings extends AbstractGuiScreen<GuiReplaySettings> {
|
||||||
|
|
||||||
public GuiReplaySettings(final net.minecraft.client.gui.GuiScreen parent, final SettingsRegistry settingsRegistry) {
|
public GuiReplaySettings(final net.minecraft.client.gui.GuiScreen parent, final SettingsRegistry settingsRegistry) {
|
||||||
@@ -45,6 +50,32 @@ public class GuiReplaySettings extends AbstractGuiScreen<GuiReplaySettings> {
|
|||||||
settingsRegistry.save();
|
settingsRegistry.save();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
} else if (key instanceof SettingsRegistry.MultipleChoiceSettingKey) {
|
||||||
|
final SettingsRegistry.MultipleChoiceSettingKey<?> multipleChoiceKey =
|
||||||
|
(SettingsRegistry.MultipleChoiceSettingKey<?>) key;
|
||||||
|
List<?> values = multipleChoiceKey.getChoices();
|
||||||
|
MultipleChoiceDropdownEntry[] entries = new MultipleChoiceDropdownEntry[values.size()];
|
||||||
|
int selected = 0;
|
||||||
|
Object currentValue = settingsRegistry.get(multipleChoiceKey);
|
||||||
|
for (int j = 0; j < entries.length; j++) {
|
||||||
|
Object value = values.get(j);
|
||||||
|
entries[j] = new MultipleChoiceDropdownEntry(value,
|
||||||
|
multipleChoiceKey.getDisplayString() + ": " + I18n.format(value.toString()));
|
||||||
|
if (currentValue.equals(value)) {
|
||||||
|
selected = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final GuiDropdownMenu<MultipleChoiceDropdownEntry> menu =
|
||||||
|
new GuiDropdownMenu<MultipleChoiceDropdownEntry>().setSize(150, 20).setValues(entries);
|
||||||
|
menu.setSelected(selected).onSelection(new Consumer<Integer>() {
|
||||||
|
@Override
|
||||||
|
public void consume(Integer obj) {
|
||||||
|
settingsRegistry.set((SettingsRegistry.SettingKey) multipleChoiceKey,
|
||||||
|
menu.getSelectedValue().value);
|
||||||
|
settingsRegistry.save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
element = menu;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Type " + key.getDefault().getClass() + " not supported.");
|
throw new IllegalArgumentException("Type " + key.getDefault().getClass() + " not supported.");
|
||||||
}
|
}
|
||||||
@@ -72,4 +103,15 @@ public class GuiReplaySettings extends AbstractGuiScreen<GuiReplaySettings> {
|
|||||||
protected GuiReplaySettings getThis() {
|
protected GuiReplaySettings getThis() {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
private static class MultipleChoiceDropdownEntry {
|
||||||
|
private final Object value;
|
||||||
|
private final String text;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user