diff options
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/interfaces')
3 files changed, 149 insertions, 12 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java b/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java index 48abf1c..3549456 100644 --- a/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java +++ b/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java @@ -1,49 +1,112 @@ package io.polyfrost.oneconfig.interfaces; import com.google.gson.*; +import io.polyfrost.oneconfig.annotations.*; +import io.polyfrost.oneconfig.core.ConfigCore; +import io.polyfrost.oneconfig.data.ModData; +import io.polyfrost.oneconfig.gui.elements.config.*; import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Map; public class Config { private final File configFile; - public Config(File configFile) { + Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting() + .registerTypeAdapterFactory(OneConfigTypeAdapterFactory.getStaticTypeAdapterFactory()).create(); + + /** + * @param modData information about the mod + * @param configFile file where config is stored + */ + public Config(ModData modData, File configFile) { this.configFile = configFile; if (configFile.exists()) load(); else save(); + modData.config = this; + ConfigCore.settings.put(modData, generateOptionList(this.getClass())); } - Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting() - .registerTypeAdapterFactory(OneConfigTypeAdapterFactory.getStaticTypeAdapterFactory()).create(); - + /** + * Save current config to file + */ public void save() { try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8))) { writer.write(gson.toJson(this.getClass())); - } catch (IOException ignored) { + } catch (IOException e) { + e.printStackTrace(); } } + /** + * Load file and overwrite current values + */ public void load() { try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8))) { - processPart(new JsonParser().parse(reader).getAsJsonObject(), this.getClass()); - } catch (IOException ignored) { + deserializePart(new JsonParser().parse(reader).getAsJsonObject(), this.getClass()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Generate the option list for internal use only + * + * @param clazz target class + * @return list of options + */ + private ArrayList<Option> generateOptionList(Class<?> clazz) { + ArrayList<Option> options = new ArrayList<>(); + for (Class<?> innerClass : clazz.getClasses()) { + if (innerClass.isAnnotationPresent(Category.class)) { + Category category = innerClass.getAnnotation(Category.class); + options.add(new OConfigCategory(category.name(), category.description(), generateOptionList(innerClass))); + } + } + for (Field field : clazz.getFields()) { + if (field.isAnnotationPresent(Button.class)) { + Button button = field.getAnnotation(Button.class); + options.add(new OConfigButton(field, button.name(), button.description(), button.text())); + } else if (field.isAnnotationPresent(ColorPicker.class)) { + ColorPicker colorPicker = field.getAnnotation(ColorPicker.class); + options.add(new OConfigColor(field, colorPicker.name(), colorPicker.description(), colorPicker.allowAlpha())); + } else if (field.isAnnotationPresent(Selector.class)) { + Selector selector = field.getAnnotation(Selector.class); + options.add(new OConfigSelector(field, selector.name(), selector.description(), selector.options(), selector.defaultSelection())); + } else if (field.isAnnotationPresent(Slider.class)) { + Slider slider = field.getAnnotation(Slider.class); + options.add(new OConfigSlider(field, slider.name(), slider.description(), slider.min(), slider.max(), slider.precision())); + } else if (field.isAnnotationPresent(Switch.class)) { + Switch aSwitch = field.getAnnotation(Switch.class); + options.add(new OConfigSwitch(field, aSwitch.name(), aSwitch.description())); + } else if (field.isAnnotationPresent(TextField.class)) { + TextField textField = field.getAnnotation(TextField.class); + options.add(new OConfigText(field, textField.name(), textField.description(), textField.placeholder(), textField.hideText())); + } } + return options; } - private void processPart(JsonObject json, Class<?> clazz) { + /** + * Deserialize part of config and load values + * + * @param json json to deserialize + * @param clazz target class + */ + private void deserializePart(JsonObject json, Class<?> clazz) { for (Map.Entry<String, JsonElement> element : json.entrySet()) { String name = element.getKey(); JsonElement value = element.getValue(); if (value.isJsonObject()) { for (Class<?> innerClass : clazz.getClasses()) { if (innerClass.getSimpleName().equals(name)) { - processPart(value.getAsJsonObject(), innerClass); + deserializePart(value.getAsJsonObject(), innerClass); break; } } diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java index 95a558d..df6ee87 100644 --- a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java +++ b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java @@ -30,12 +30,10 @@ final class OneConfigTypeAdapter<T> extends TypeAdapter<Class<T>> { for (Field field : value.getFields()) { out.name(field.getName()); field.setAccessible(true); - final Object fieldValue = field.get(null); final TypeAdapter<Object> adapter = (TypeAdapter) gson.getAdapter(field.getType()); - adapter.write(out, fieldValue); + adapter.write(out, field.get(null)); } for (Class<?> clazz : value.getClasses()) { - System.out.println(clazz.toString()); out.name(clazz.getSimpleName()); final TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class); adapter.write(out, parser.parse(gson.toJson(clazz))); diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/Option.java b/src/main/java/io/polyfrost/oneconfig/interfaces/Option.java new file mode 100644 index 0000000..d996b60 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/interfaces/Option.java @@ -0,0 +1,76 @@ +package io.polyfrost.oneconfig.interfaces; + +import java.lang.reflect.Field; + +@SuppressWarnings({"unused"}) +public abstract class Option { + protected final Field field; + protected final String name; + protected final String description; + + /** + * Initialize option + * + * @param field variable attached to option (null for category) + * @param name name of option + * @param description description of option + */ + public Option(Field field, String name, String description) { + this.field = field; + this.name = name; + this.description = description; + if (field != null) + field.setAccessible(true); + } + + /** + * @param object Java object to set the variable to + */ + protected void set(Object object) throws IllegalAccessException { + if (field == null) return; + field.set(null, object); + } + + /** + * @return value of variable as Java object + */ + protected Object get() throws IllegalAccessException { + if (field == null) return null; + return field.get(null); + } + + /** + * @return height of option to align other options accordingly + */ + public abstract int getHeight(); + + /** + * Function that gets called when drawing option + * + * @param x x position + * @param y y position + * @param width width of menu + * @param mouseX x position of mouse + * @param mouseY y position of mouse + */ + public abstract void draw(int x, int y, int width, int mouseX, int mouseY); + + /** + * Function that gets called when mouse is clicked + * + * @param mouseX x position of mouse + * @param mouseY y position of mouse + * @param mouseButton button that got pressed + */ + protected void onMouseClicked(int mouseX, int mouseY, int mouseButton) { + } + + /** + * Function that gets called when a key is typed + * + * @param typedChar char that has been typed + * @param keyCode code of key + */ + protected void keyTyped(char typedChar, int keyCode) { + } +} |