diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-06-05 17:43:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-05 17:43:23 +0200 |
commit | ab7256dff5d6d37488081ba7a01b36d3ee9ef563 (patch) | |
tree | 8207341e6c402848cdbe7b2f2297f5f975e0e083 /src/main/java/cc/polyfrost/oneconfig/config/elements | |
parent | a903cfc4d3f76cf3db24749b65156d126fa714e7 (diff) | |
download | OneConfig-ab7256dff5d6d37488081ba7a01b36d3ee9ef563.tar.gz OneConfig-ab7256dff5d6d37488081ba7a01b36d3ee9ef563.tar.bz2 OneConfig-ab7256dff5d6d37488081ba7a01b36d3ee9ef563.zip |
refactor (#36)
* refactor
* fix vig compat
* fix nanovg thingy
* e
* finalize
* gui utils package thingy
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config/elements')
4 files changed, 243 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java new file mode 100644 index 0000000..d4efeaa --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java @@ -0,0 +1,98 @@ +package cc.polyfrost.oneconfig.config.elements; + +import java.lang.reflect.Field; +import java.util.function.Supplier; + +@SuppressWarnings({"unused"}) +public abstract class BasicOption { + public final int size; + protected final Field field; + protected final String name; + protected final Object parent; + private Supplier<Boolean> dependency; + + /** + * Initialize option + * + * @param field variable attached to option (null for category) + * @param parent the parent object of the field, used for getting and setting the variable + * @param name name of option + * @param size size of option, 0 for single column, 1 for double. + */ + public BasicOption(Field field, Object parent, String name, int size) { + this.field = field; + this.parent = parent; + this.name = name; + this.size = size; + 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(parent, object); + } + + /** + * @return value of variable as Java object + */ + protected Object get() throws IllegalAccessException { + if (field == null) return null; + return field.get(parent); + } + + /** + * @return height of option to align other options accordingly + */ + public abstract int getHeight(); + + /** + * Function that gets called when drawing option + * + * @param vg NanoVG context + * @param x x position + * @param y y position + */ + public abstract void draw(long vg, int x, int y); + + /** + * Function that gets called last drawing option, + * should be used for things that draw above other options + * + * @param vg NanoVG context + * @param x x position + * @param y y position + */ + public void drawLast(long vg, int x, int y) { + } + + /** + * Function that gets called when a key is typed + * + * @param key char that has been typed + * @param keyCode code of key + */ + public void keyTyped(char key, int keyCode) { + } + + /** + * @return If the component has an option to render at half size + */ + public boolean hasHalfSize() { + return true; + } + + public String getName() { + return name; + } + + public void setDependency(Supplier<Boolean> supplier) { + this.dependency = supplier; + } + + protected boolean isEnabled() { + return dependency == null || dependency.get(); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java new file mode 100644 index 0000000..4a37bfc --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java @@ -0,0 +1,7 @@ +package cc.polyfrost.oneconfig.config.elements; + +import java.util.ArrayList; + +public class OptionCategory { + public final ArrayList<OptionSubcategory> subcategories = new ArrayList<>(); +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java new file mode 100644 index 0000000..5eaac8b --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java @@ -0,0 +1,16 @@ +package cc.polyfrost.oneconfig.config.elements; + +import cc.polyfrost.oneconfig.config.data.Mod; + +import java.util.LinkedHashMap; + +public class OptionPage { + public final String name; + public final Mod mod; + public final LinkedHashMap<String, OptionCategory> categories = new LinkedHashMap<>(); + + public OptionPage(String name, Mod mod) { + this.name = name; + this.mod = mod; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java new file mode 100644 index 0000000..ff1d770 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java @@ -0,0 +1,122 @@ +package cc.polyfrost.oneconfig.config.elements; + +import cc.polyfrost.oneconfig.gui.Colors; +import cc.polyfrost.oneconfig.config.elements.BasicOption; +import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigPageButton; +import cc.polyfrost.oneconfig.renderer.RenderManager; +import cc.polyfrost.oneconfig.renderer.font.Fonts; + +import java.util.ArrayList; + +public class OptionSubcategory { + private final String name; + public ArrayList<BasicOption> options = new ArrayList<>(); + public ArrayList<ConfigPageButton> topButtons = new ArrayList<>(); + public ArrayList<ConfigPageButton> bottomButtons = new ArrayList<>(); + private ArrayList<BasicOption> filteredOptions = new ArrayList<>(); + private int drawLastY; + + public OptionSubcategory(String name) { + this.name = name; + } + + public int draw(long vg, int x, int y) { + String filter = OneConfigGui.INSTANCE == null ? "" : OneConfigGui.INSTANCE.getSearchValue().toLowerCase().trim(); + filteredOptions = new ArrayList<>(options); + ArrayList<ConfigPageButton> filteredTop = new ArrayList<>(topButtons); + ArrayList<ConfigPageButton> filteredBottom = new ArrayList<>(bottomButtons); + if (!filter.equals("") && !name.toLowerCase().contains(filter)) { + filteredOptions.clear(); + filteredTop.clear(); + filteredBottom.clear(); + for (BasicOption option : options) { + if (option.getName().toLowerCase().contains(filter)) filteredOptions.add(option); + } + for (ConfigPageButton page : topButtons) { + if (page.getName().toLowerCase().contains(filter) || page.description.toLowerCase().contains(filter)) + filteredTop.add(page); + } + for (ConfigPageButton page : bottomButtons) { + if (page.getName().toLowerCase().contains(filter) || page.description.toLowerCase().contains(filter)) + filteredBottom.add(page); + } + } + if (filteredOptions.size() == 0 && filteredTop.size() == 0 && filteredBottom.size() == 0) return 0; + int optionY = y; + if (!name.equals("")) { + RenderManager.drawText(vg, name, x, y + 12, Colors.WHITE_90, 24, Fonts.MEDIUM); + optionY += 36; + } + + for (ConfigPageButton page : filteredTop) { + page.draw(vg, x, optionY); + optionY += page.getHeight() + 16; + } + + if (filteredOptions.size() > 0) { + int backgroundSize = 16; + for (int i = 0; i < filteredOptions.size(); i++) { + BasicOption option = filteredOptions.get(i); + if (i + 1 < filteredOptions.size()) { + BasicOption nextOption = filteredOptions.get(i + 1); + if (option.size == 1 && option.hasHalfSize() && nextOption.size == 1 && nextOption.hasHalfSize()) { + backgroundSize += Math.max(option.getHeight(), nextOption.getHeight()) + 16; + i++; + continue; + } + } + backgroundSize += option.getHeight() + 16; + } + RenderManager.drawRoundedRect(vg, x - 16, optionY, 1024, backgroundSize, Colors.GRAY_900, 20); + optionY += 16; + } + + drawLastY = optionY; + if (filteredOptions.size() > 0) { + for (int i = 0; i < filteredOptions.size(); i++) { + BasicOption option = filteredOptions.get(i); + option.draw(vg, x, optionY); + if (i + 1 < filteredOptions.size()) { + BasicOption nextOption = filteredOptions.get(i + 1); + if (option.size == 1 && option.hasHalfSize() && nextOption.size == 1 && nextOption.hasHalfSize()) { + nextOption.draw(vg, x + 512, optionY); + optionY += Math.max(option.getHeight(), nextOption.getHeight()) + 16; + i++; + continue; + } + } + optionY += option.getHeight() + 16; + } + optionY += 16; + } + + for (ConfigPageButton page : filteredBottom) { + page.draw(vg, x, optionY); + optionY += page.getHeight() + 16; + } + + return optionY - y; + } + + public void drawLast(long vg, int x) { + for (int i = 0; i < filteredOptions.size(); i++) { + BasicOption option = filteredOptions.get(i); + option.drawLast(vg, x, drawLastY); + if (i + 1 < filteredOptions.size()) { + BasicOption nextOption = filteredOptions.get(i + 1); + if (option.size == 1 && option.hasHalfSize() && nextOption.size == 1 && nextOption.hasHalfSize()) { + nextOption.drawLast(vg, x + 512, drawLastY); + drawLastY += Math.max(option.getHeight(), nextOption.getHeight()) + 16; + i++; + continue; + } + } + drawLastY += option.getHeight() + 16; + } + } + + public String getName() { + return name; + } +} |