modularization, part 1
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package draylar.omegaconfiggui;
|
||||
|
||||
import draylar.omegaconfig.api.Config;
|
||||
import draylar.omegaconfiggui.api.screen.ModMenuHelper;
|
||||
import draylar.omegaconfiggui.api.screen.OmegaConfigScreen;
|
||||
import draylar.omegaconfiggui.api.screen.OmegaConfigScreenSupplier;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class OmegaConfigGui implements ClientModInitializer {
|
||||
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
|
||||
}
|
||||
|
||||
public static <T extends Config> void registerConfigScreen(T config) {
|
||||
registerConfigScreen(config, parent -> new OmegaConfigScreen<>(config, parent));
|
||||
}
|
||||
|
||||
public static <T extends Config> void registerConfigScreen(T config, OmegaConfigScreenSupplier<T> screenFactory) {
|
||||
if(FabricLoader.getInstance().isModLoaded("modmenu")) {
|
||||
ModMenuHelper.injectScreen(config, screenFactory);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package draylar.omegaconfiggui.api.screen;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
|
||||
import draylar.omegaconfig.api.Config;
|
||||
import draylar.omegaconfiggui.mixin.ModMenuAccessor;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class ModMenuHelper {
|
||||
|
||||
public static <T extends Config> void injectScreen(T config, OmegaConfigScreenSupplier<T> factory) {
|
||||
// collect existing configurations
|
||||
ImmutableMap<String, ConfigScreenFactory<?>> configScreenFactories = ModMenuAccessor.getConfigScreenFactories();
|
||||
Map<String, ConfigScreenFactory<?>> nMap = new HashMap<>();
|
||||
nMap.putAll(configScreenFactories);
|
||||
|
||||
// add our factory
|
||||
nMap.put(config.getModid(), factory::get);
|
||||
|
||||
// they will suspect nothing
|
||||
ModMenuAccessor.setConfigScreenFactories(ImmutableMap.copyOf(nMap));
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
package draylar.omegaconfiggui.api.screen;
|
||||
|
||||
import draylar.omegaconfig.api.Config;
|
||||
import draylar.omegaconfiggui.api.screen.widget.LabelWidget;
|
||||
import draylar.omegaconfiggui.api.screen.widget.TypeWidgets;
|
||||
import draylar.omegaconfiggui.api.screen.widget.WidgetSupplier;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.Drawable;
|
||||
import net.minecraft.client.gui.Element;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.AbstractButtonWidget;
|
||||
import net.minecraft.client.gui.widget.ButtonWidget;
|
||||
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.Pair;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class OmegaConfigScreen<T extends Config> extends Screen {
|
||||
|
||||
private static final List<Class<?>> validClasses = Arrays.asList(Double.class, String.class, Boolean.class);
|
||||
private final T config;
|
||||
private final Screen parent;
|
||||
private final Map<Field, Pair<WidgetSupplier<Object, AbstractButtonWidget>, AbstractButtonWidget>> fieldWidgets = new HashMap<>();
|
||||
|
||||
public OmegaConfigScreen(T config, Screen parent) {
|
||||
super(new LiteralText(""));
|
||||
this.config = config;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
super.init();
|
||||
|
||||
try {
|
||||
// add label & button for each config entry
|
||||
// TODO: SUB-CLASS SUPPORT
|
||||
List<Field> collect = Arrays.stream(config.getClass().getDeclaredFields()).filter(field -> validClasses.contains(TypeWidgets.unbox(field.getType()))).collect(Collectors.toList());
|
||||
int over = 0;
|
||||
|
||||
for (Field field : collect) {
|
||||
Class<?> unbox = TypeWidgets.unbox(field.getType());
|
||||
|
||||
// label
|
||||
addChild(new LabelWidget(150, 5 + 20 * over, new LiteralText(field.getName())));
|
||||
|
||||
// get value
|
||||
field.setAccessible(true);
|
||||
Object value = field.get(config);
|
||||
|
||||
// button / interact
|
||||
AbstractButtonWidget button;
|
||||
WidgetSupplier<Object, AbstractButtonWidget> widgetSupplier = (WidgetSupplier<Object, AbstractButtonWidget>) TypeWidgets.get(unbox);
|
||||
if(widgetSupplier != null) {
|
||||
button = widgetSupplier.create(250, 5 + 20 * over, 50, 50, new LiteralText(field.getName()), value);
|
||||
} else {
|
||||
button = new TextFieldWidget(client.textRenderer, 250, 5 + 20 * over, 50, 20, new LiteralText(field.getName()));
|
||||
}
|
||||
|
||||
fieldWidgets.put(field, new Pair<>(widgetSupplier, button));
|
||||
|
||||
addButton(button);
|
||||
|
||||
over++;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// save and exit
|
||||
ButtonWidget exit = new ButtonWidget(width - 60, height - 30, 50, 20, new LiteralText("Exit"), widget -> onClose());
|
||||
ButtonWidget save = new ButtonWidget(10, height - 30, 50, 20, new LiteralText("Save"), widget -> {
|
||||
fieldWidgets.forEach((field, pair) -> {
|
||||
try {
|
||||
field.set(config, pair.getLeft().get(pair.getRight()));
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
|
||||
config.save();
|
||||
});
|
||||
addButton(save);
|
||||
addButton(exit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
|
||||
renderBackground(matrices);
|
||||
|
||||
for (AbstractButtonWidget button : this.buttons) {
|
||||
button.render(matrices, mouseX, mouseY, delta);
|
||||
}
|
||||
|
||||
for (Element element : this.children) {
|
||||
if (element instanceof Drawable && !buttons.contains(element)) {
|
||||
((Drawable) element).render(matrices, mouseX, mouseY, delta);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose() {
|
||||
client.openScreen(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void renderBackground(MatrixStack matrices) {
|
||||
super.renderBackground(matrices);
|
||||
|
||||
MinecraftClient.getInstance().getTextureManager().bindTexture(new Identifier("textures/block/dirt.png"));
|
||||
drawTexture(matrices, 0, 0, 0, 0, 16, 16);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package draylar.omegaconfiggui.api.screen;
|
||||
|
||||
import draylar.omegaconfig.api.Config;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
|
||||
public interface OmegaConfigScreenSupplier<T extends Config> {
|
||||
OmegaConfigScreen<T> get(Screen parent);
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.Drawable;
|
||||
import net.minecraft.client.gui.DrawableHelper;
|
||||
import net.minecraft.client.gui.Element;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
public class LabelWidget extends DrawableHelper implements Drawable, Element {
|
||||
|
||||
private final int x;
|
||||
private final int y;
|
||||
private final Text message;
|
||||
|
||||
public LabelWidget(int x, int y, Text message) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
|
||||
drawTextWithShadow(matrices, MinecraftClient.getInstance().textRenderer, message, x, y, 0xffffff);
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget;
|
||||
|
||||
import draylar.omegaconfiggui.api.screen.widget.supplier.BooleanWidgetSupplier;
|
||||
import draylar.omegaconfiggui.api.screen.widget.supplier.DoubleWidgetSupplier;
|
||||
import draylar.omegaconfiggui.api.screen.widget.supplier.StringWidgetSupplier;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TypeWidgets {
|
||||
|
||||
public static final Map<Class<?>, WidgetSupplier<?, ?>> CLASS_WIDGETS = new HashMap<>();
|
||||
public static final Map<Class<?>, Class<?>> PRIMITIVE_TO_BOXED = new HashMap<>();
|
||||
|
||||
static {
|
||||
CLASS_WIDGETS.put(Double.class, new DoubleWidgetSupplier());
|
||||
CLASS_WIDGETS.put(Boolean.class, new BooleanWidgetSupplier());
|
||||
CLASS_WIDGETS.put(String.class, new StringWidgetSupplier());
|
||||
|
||||
// primitive -> boxed
|
||||
PRIMITIVE_TO_BOXED.put(boolean.class, Boolean.class);
|
||||
PRIMITIVE_TO_BOXED.put(byte.class, Byte.class);
|
||||
PRIMITIVE_TO_BOXED.put(char.class, Character.class);
|
||||
PRIMITIVE_TO_BOXED.put(double.class, Double.class);
|
||||
PRIMITIVE_TO_BOXED.put(float.class, Float.class);
|
||||
PRIMITIVE_TO_BOXED.put(int.class, Integer.class);
|
||||
PRIMITIVE_TO_BOXED.put(long.class, Long.class);
|
||||
PRIMITIVE_TO_BOXED.put(short.class, Short.class);
|
||||
PRIMITIVE_TO_BOXED.put(void.class, Void.class);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static <T> WidgetSupplier<T, ?> get(Class<T> typeClass) {
|
||||
return (WidgetSupplier<T, ?>) CLASS_WIDGETS.get(unbox(typeClass));
|
||||
}
|
||||
|
||||
public static Class<?> unbox(Class<?> c) {
|
||||
return PRIMITIVE_TO_BOXED.getOrDefault(c, c);
|
||||
}
|
||||
|
||||
private TypeWidgets() {
|
||||
// NO-OP
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget;
|
||||
|
||||
import net.minecraft.client.gui.widget.AbstractButtonWidget;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public interface WidgetSupplier<T, A extends AbstractButtonWidget> {
|
||||
AbstractButtonWidget create(int x, int y, int width, int height, LiteralText prompt, T value);
|
||||
T get(A widget);
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget.supplier;
|
||||
|
||||
import draylar.omegaconfiggui.api.screen.widget.WidgetSupplier;
|
||||
import net.minecraft.client.gui.widget.CheckboxWidget;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public class BooleanWidgetSupplier implements WidgetSupplier<Boolean, CheckboxWidget> {
|
||||
|
||||
@Override
|
||||
public CheckboxWidget create(int x, int y, int width, int height, LiteralText prompt, Boolean value) {
|
||||
return new CheckboxWidget(x, y, 20, 20, new LiteralText(""), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean get(CheckboxWidget widget) {
|
||||
return widget.isChecked();
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget.supplier;
|
||||
|
||||
import draylar.omegaconfiggui.api.screen.widget.WidgetSupplier;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.widget.AbstractButtonWidget;
|
||||
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public class DoubleWidgetSupplier implements WidgetSupplier<Double, AbstractButtonWidget> {
|
||||
|
||||
@Override
|
||||
public AbstractButtonWidget create(int x, int y, int width, int height, LiteralText prompt, Double value) {
|
||||
return new TextFieldWidget(MinecraftClient.getInstance().textRenderer, x, y, width, height, new LiteralText(String.valueOf(value)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double get(AbstractButtonWidget widget) {
|
||||
return Double.parseDouble(widget.getMessage().asString());
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget.supplier;
|
||||
|
||||
import draylar.omegaconfiggui.api.screen.widget.WidgetSupplier;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.widget.AbstractButtonWidget;
|
||||
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public class StringWidgetSupplier implements WidgetSupplier<String, AbstractButtonWidget> {
|
||||
|
||||
@Override
|
||||
public AbstractButtonWidget create(int x, int y, int width, int height, LiteralText prompt, String value) {
|
||||
return new TextFieldWidget(MinecraftClient.getInstance().textRenderer, x, y, width, height, new LiteralText(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(AbstractButtonWidget widget) {
|
||||
return widget.getMessage().asString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package draylar.omegaconfiggui.mixin;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.terraformersmc.modmenu.ModMenu;
|
||||
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.gen.Accessor;
|
||||
|
||||
@Mixin(ModMenu.class)
|
||||
public interface ModMenuAccessor {
|
||||
@Accessor
|
||||
static ImmutableMap<String, ConfigScreenFactory<?>> getConfigScreenFactories() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Accessor
|
||||
static void setConfigScreenFactories(ImmutableMap<String, ConfigScreenFactory<?>> configScreenFactories) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user