diff options
Diffstat (limited to 'src/main/java/cc')
13 files changed, 190 insertions, 35 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java index de6c575..c3cbe8b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -1,24 +1,33 @@ package cc.polyfrost.oneconfig.config; -import cc.polyfrost.oneconfig.internal.config.annotations.Option; +import cc.polyfrost.oneconfig.config.annotations.CustomOption; +import cc.polyfrost.oneconfig.config.annotations.HUD; +import cc.polyfrost.oneconfig.config.annotations.Page; import cc.polyfrost.oneconfig.config.core.ConfigUtils; -import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; -import cc.polyfrost.oneconfig.config.data.*; +import cc.polyfrost.oneconfig.config.data.Mod; +import cc.polyfrost.oneconfig.config.data.PageLocation; import cc.polyfrost.oneconfig.config.elements.BasicOption; import cc.polyfrost.oneconfig.config.elements.OptionPage; +import cc.polyfrost.oneconfig.config.elements.OptionSubcategory; import cc.polyfrost.oneconfig.config.profiles.Profiles; import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigPageButton; import cc.polyfrost.oneconfig.gui.pages.ModConfigPage; +import cc.polyfrost.oneconfig.hud.HUDUtils; +import cc.polyfrost.oneconfig.internal.config.annotations.Option; +import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; import cc.polyfrost.oneconfig.utils.gui.GuiUtils; import com.google.gson.*; import java.io.*; -import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.util.*; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; import java.util.function.Supplier; public class Config { @@ -84,19 +93,39 @@ public class Config { protected void generateOptionList(Object instance, OptionPage page, Mod mod, boolean migrate) { for (Field field : instance.getClass().getDeclaredFields()) { Option option = ConfigUtils.findAnnotation(field, Option.class); - if (option != null) - ConfigUtils.addOptionToPage(page, option, field, instance, migrate ? mod.migrator : null); - // TODO: Make dependencies work, pages, hud + if (option != null) { + BasicOption configOption = ConfigUtils.addOptionToPage(page, option, field, instance, migrate ? mod.migrator : null); + optionNames.put(page.equals(mod.defaultPage) ? "" : page.name + "." + field.getName(), configOption); + } else if (field.isAnnotationPresent(CustomOption.class)) { + BasicOption configOption = getCustomOption(field, page, mod, migrate); + if (configOption == null) continue; + optionNames.put(page.equals(mod.defaultPage) ? "" : page.name + "." + field.getName(), configOption); + } else if (field.isAnnotationPresent(Page.class)) { + Page optionPage = field.getAnnotation(Page.class); + OptionSubcategory subcategory = ConfigUtils.getSubCategory(page, optionPage.category(), optionPage.subcategory()); + Object pageInstance = ConfigUtils.getField(field, instance); + if (pageInstance == null) continue; + OptionPage newPage = new OptionPage(optionPage.name(), mod); + generateOptionList(pageInstance, newPage, mod, migrate); + ConfigPageButton button = new ConfigPageButton(field, instance, optionPage.name(), optionPage.description(), optionPage.category(), optionPage.subcategory(), newPage); + if (optionPage.location() == PageLocation.TOP) subcategory.topButtons.add(button); + else subcategory.bottomButtons.add(button); + } else if (field.isAnnotationPresent(HUD.class)) { + HUDUtils.addHudOptions(page, field, instance); + } } } /** - * Overwrite this method to add your own custom option types + * All fields with the CustomOption annotation are sent to this function * - * @param field target field - * @param page page to add options too + * @param field Target field + * @param page Page to add options too + * @param mod The data of the mod + * @param migrate If the data should be migrated */ - protected void processCustomOption(Field field, OptionPage page) { + protected BasicOption getCustomOption(Field field, OptionPage page, Mod mod, boolean migrate) { + return null; } /** diff --git a/src/main/java/cc/polyfrost/oneconfig/config/annotations/CustomOption.java b/src/main/java/cc/polyfrost/oneconfig/config/annotations/CustomOption.java new file mode 100644 index 0000000..6a1109b --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/annotations/CustomOption.java @@ -0,0 +1,14 @@ +package cc.polyfrost.oneconfig.config.annotations; + +import cc.polyfrost.oneconfig.config.data.OptionType; +import cc.polyfrost.oneconfig.internal.config.annotations.Option; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface CustomOption { +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/annotations/HUD.java b/src/main/java/cc/polyfrost/oneconfig/config/annotations/HUD.java new file mode 100644 index 0000000..7bb1903 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/annotations/HUD.java @@ -0,0 +1,16 @@ +package cc.polyfrost.oneconfig.config.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface HUD { + String name(); + + String category() default "General"; + + String subcategory() default ""; +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/annotations/ConfigPage.java b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Page.java index 432d65c..2e93a7d 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/annotations/ConfigPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Page.java @@ -9,7 +9,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) -public @interface ConfigPage { +public @interface Page { /** * The name of the page that will be displayed to the user */ diff --git a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java index 2002b01..5327dad 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java @@ -52,27 +52,23 @@ public class ConfigUtils { return options; } - public static void addOptionToPage(OptionPage page, Option option, Field field, Object instance, @Nullable Migrator migrator) { + public static BasicOption addOptionToPage(OptionPage page, Option option, Field field, Object instance, @Nullable Migrator migrator) { BasicOption configOption = getOption(option, field, instance); - if (configOption == null) return; + if (configOption == null) return null; if (migrator != null) { Object value = migrator.getValue(field, configOption.name, configOption.category, configOption.subcategory); - if (value != null) { - try { - field.set(instance, value); - } catch (IllegalAccessException ignored) { - } - } + if (value != null) setField(field, value, instance); } - getOptions(page, configOption.category, configOption.subcategory).add(configOption); + getSubCategory(page, configOption.category, configOption.subcategory).options.add(configOption); + return configOption; } - public static ArrayList<BasicOption> getOptions(OptionPage page, String categoryName, String subcategoryName) { + public static OptionSubcategory getSubCategory(OptionPage page, String categoryName, String subcategoryName) { if (!page.categories.containsKey(categoryName)) page.categories.put(categoryName, new OptionCategory()); OptionCategory category = page.categories.get(categoryName); if (category.subcategories.size() == 0 || !category.subcategories.get(category.subcategories.size() - 1).getName().equals(subcategoryName)) category.subcategories.add(new OptionSubcategory(subcategoryName)); - return category.subcategories.get(category.subcategories.size() - 1).options; + return category.subcategories.get(category.subcategories.size() - 1); } public static <T extends Annotation> T findAnnotation(Field field, Class<T> annotationType) { @@ -82,4 +78,21 @@ public class ConfigUtils { } return null; } + + public static Object getField(Field field, Object parent) { + try { + field.setAccessible(true); + return field.get(parent); + } catch (IllegalAccessException ignored) { + return null; + } + } + + public static void setField(Field field, Object value, Object parent) { + try { + field.setAccessible(true); + field.set(parent, value); + } catch (IllegalAccessException ignored) { + } + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/data/OptionType.java b/src/main/java/cc/polyfrost/oneconfig/config/data/OptionType.java index 4ae11da..e636ebb 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/data/OptionType.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/data/OptionType.java @@ -45,8 +45,4 @@ public enum OptionType { * Type: OneKeyBind */ KEYBIND, - /** - * Type: ? extends BasicHud - */ - HUD, } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java index 0ddf469..07a60ee 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java @@ -9,6 +9,7 @@ import cc.polyfrost.oneconfig.gui.elements.text.TextInputField; import cc.polyfrost.oneconfig.gui.pages.ModsPage; import cc.polyfrost.oneconfig.gui.pages.Page; import cc.polyfrost.oneconfig.internal.assets.Colors; +import cc.polyfrost.oneconfig.internal.config.OneConfigConfig; import cc.polyfrost.oneconfig.renderer.RenderManager; import cc.polyfrost.oneconfig.renderer.font.Fonts; import cc.polyfrost.oneconfig.internal.assets.SVGs; @@ -23,6 +24,7 @@ import gg.essential.universal.UScreen; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.lwjgl.input.Mouse; +import org.lwjgl.nanovg.NanoVG; import java.util.ArrayList; @@ -74,6 +76,10 @@ public class OneConfigGui extends UScreen { currentPage = new ModsPage(); parents.add(currentPage); } + if (OneConfigConfig.australia) { + NanoVG.nvgTranslate(vg, UResolution.getWindowWidth(), UResolution.getWindowHeight()); + NanoVG.nvgRotate(vg, (float) Math.toRadians(180)); + } scale = Math.min(UResolution.getWindowWidth() / 1920f, UResolution.getWindowHeight() / 1080f); if (scale < 1) scale = Math.min(Math.min(1f, UResolution.getWindowWidth() / 1280f), Math.min(1f, UResolution.getWindowHeight() / 800f)); diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigPageButton.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigPageButton.java index 5c7c2c4..0e6695f 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigPageButton.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigPageButton.java @@ -20,8 +20,8 @@ public class ConfigPageButton extends BasicOption { public final String description; private final ColorAnimation backgroundColor = new ColorAnimation(ColorPalette.SECONDARY); - public ConfigPageButton(Field field, Object parent, String name, String description, OptionPage page) { - super(field, parent, name, "", "", 2); + public ConfigPageButton(Field field, Object parent, String name, String description, String category, String subcategory, OptionPage page) { + super(field, parent, name, category, subcategory, 2); this.description = description; this.page = page; } diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java new file mode 100644 index 0000000..758249a --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java @@ -0,0 +1,45 @@ +package cc.polyfrost.oneconfig.hud; + +import cc.polyfrost.oneconfig.config.annotations.HUD; +import cc.polyfrost.oneconfig.config.core.ConfigUtils; +import cc.polyfrost.oneconfig.config.elements.BasicOption; +import cc.polyfrost.oneconfig.config.elements.OptionPage; +import cc.polyfrost.oneconfig.gui.elements.config.*; +import cc.polyfrost.oneconfig.internal.hud.HudCore; + +import java.lang.reflect.Field; +import java.util.ArrayList; + +public class HUDUtils { + public static void addHudOptions(OptionPage page, Field field, Object instance) { + HUD hudAnnotation = field.getAnnotation(HUD.class); + BasicHud hud = (BasicHud) ConfigUtils.getField(field, instance); + if (hud == null) return; + HudCore.huds.add(hud); + String category = hudAnnotation.category(); + String subcategory = hudAnnotation.subcategory(); + ArrayList<BasicOption> options = ConfigUtils.getSubCategory(page, hudAnnotation.category(), hudAnnotation.subcategory()).options; + try { + options.add(new ConfigHeader(field, hud, hudAnnotation.name(), category, subcategory, 2)); + options.add(new ConfigSwitch(hud.getClass().getField("enabled"), hud, "Enabled", category, subcategory, 2)); + options.addAll(ConfigUtils.getClassOptions(hud)); + options.add(new ConfigCheckbox(hud.getClass().getField("rounded"), hud, "Rounded corners", category, subcategory, 1)); + options.get(options.size() - 1).addDependency(() -> hud.enabled); + options.add(new ConfigCheckbox(hud.getClass().getField("border"), hud, "Outline/border", category, subcategory, 1)); + options.get(options.size() - 1).addDependency(() -> hud.enabled); + options.add(new ConfigColorElement(hud.getClass().getField("bgColor"), hud, "Background color:", category, subcategory, 1)); + options.get(options.size() - 1).addDependency(() -> hud.enabled); + options.add(new ConfigColorElement(hud.getClass().getField("borderColor"), hud, "Border color:", category, subcategory, 1)); + options.get(options.size() - 1).addDependency(() -> hud.enabled && hud.border); + options.add(new ConfigSlider(hud.getClass().getField("cornerRadius"), hud, "Corner radius:", category, subcategory, 2, 0, 10, 0)); + options.get(options.size() - 1).addDependency(() -> hud.enabled && hud.rounded); + options.add(new ConfigSlider(hud.getClass().getField("borderSize"), hud, "Border thickness:", category, subcategory, 2, 0, 10, 0)); + options.get(options.size() - 1).addDependency(() -> hud.enabled && hud.border); + options.add(new ConfigSlider(hud.getClass().getField("paddingX"), hud, "X-Padding", category, subcategory, 2, 0, 50, 0)); + options.get(options.size() - 1).addDependency(() -> hud.enabled); + options.add(new ConfigSlider(hud.getClass().getField("paddingY"), hud, "Y-Padding", category, subcategory, 2, 0, 50, 0)); + options.get(options.size() - 1).addDependency(() -> hud.enabled); + } catch (NoSuchFieldException ignored) { + } + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/config/OneConfigConfig.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/OneConfigConfig.java index 8f67ee6..09b21b2 100644 --- a/src/main/java/cc/polyfrost/oneconfig/internal/config/OneConfigConfig.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/OneConfigConfig.java @@ -17,6 +17,7 @@ public class OneConfigConfig extends Config { public static ArrayList<OneColor> favoriteColors = new ArrayList<>(); public static ArrayList<OneColor> recentColors = new ArrayList<>(); public static boolean allShowShortCut = false; + public static boolean australia = false; public OneConfigConfig() { super(null, "OneConfig.json"); diff --git a/src/main/java/cc/polyfrost/oneconfig/test/TestConfig.java b/src/main/java/cc/polyfrost/oneconfig/test/TestConfig.java index 8d14fb0..ab3b09f 100644 --- a/src/main/java/cc/polyfrost/oneconfig/test/TestConfig.java +++ b/src/main/java/cc/polyfrost/oneconfig/test/TestConfig.java @@ -16,13 +16,13 @@ public class TestConfig extends Config { name = "Test Switch", size = 2 ) - boolean testSwitch = false; + public boolean testSwitch = false; @Checkbox( name = "Check box", size = 2 ) - boolean testCheckBox = true; + public static boolean testCheckBox = true; @Info( text = "Test Info", @@ -42,11 +42,11 @@ public class TestConfig extends Config { options = {"option1", "option2", "option3"}, size = 2 ) - int testDropdown = 0; + private int testDropdown = 0; @Color( name = "Test Color", - size = 2 + size = 2 ) OneColor testColor = new OneColor(0, 255, 255); @@ -54,7 +54,7 @@ public class TestConfig extends Config { name = "Test Text", size = 2 ) - String testText = "Epic Text"; + private static String testText = "Epic Text"; @Button( name = "Test Button", @@ -83,6 +83,21 @@ public class TestConfig extends Config { ) boolean testDualOption = false; + @Page( + name = "Test Page", + location = PageLocation.TOP + + ) + public TestPage testPage = new TestPage(); + + @Page( + name = "Test Page", + description = "Test Description", + location = PageLocation.BOTTOM + + ) + public TestPage testPage2 = new TestPage(); + @Switch( name = "Test Switch", size = 2, @@ -98,6 +113,11 @@ public class TestConfig extends Config { ) boolean testSwitch2 = false; + @HUD( + name = "Test HUD", + category = "HUD" + ) + public TestHud hud = new TestHud(false, 0, 0); public TestConfig() { super(new Mod("Test Mod", ModType.UTIL_QOL, new VigilanceMigrator("./config/testConfig.toml")), "hacksConfig.json"); diff --git a/src/main/java/cc/polyfrost/oneconfig/test/TestHud.java b/src/main/java/cc/polyfrost/oneconfig/test/TestHud.java index 0a59c23..023ae43 100644 --- a/src/main/java/cc/polyfrost/oneconfig/test/TestHud.java +++ b/src/main/java/cc/polyfrost/oneconfig/test/TestHud.java @@ -1,5 +1,7 @@ package cc.polyfrost.oneconfig.test; +import cc.polyfrost.oneconfig.config.annotations.Switch; +import cc.polyfrost.oneconfig.config.annotations.Text; import cc.polyfrost.oneconfig.hud.TextHud; import net.minecraft.client.Minecraft; @@ -19,7 +21,13 @@ public class TestHud extends TextHud { return lines; } + @Switch( + name = "Has Second Line" + ) public boolean hasSecondLine = false; + @Text( + name = "Second Line Text" + ) public String secondLine = "Epic text"; } diff --git a/src/main/java/cc/polyfrost/oneconfig/test/TestPage.java b/src/main/java/cc/polyfrost/oneconfig/test/TestPage.java index abb4cc7..54a775a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/test/TestPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/test/TestPage.java @@ -1,4 +1,11 @@ package cc.polyfrost.oneconfig.test; +import cc.polyfrost.oneconfig.config.annotations.Switch; + public class TestPage { + + @Switch( + name = "Epic Test Switch" + ) + boolean test = false; } |