diff options
30 files changed, 391 insertions, 173 deletions
@@ -5,4 +5,13 @@ OneConfig is a next generation config library currently in development for all M For a guide of how to use OneConfig please see our documentation at https://docs.polyfrost.cc ![image](https://user-images.githubusercontent.com/67508414/172061081-7a32a139-d5c6-4f5a-907a-5af66deb799f.png) +<br><br><br> +### Supporting Projects +![YourKitLogo|50](https://www.yourkit.com/images/yklogo.png) + +YourKit supports open source projects with innovative and intelligent tools +for monitoring and profiling Java and .NET applications. <br> +YourKit is the creator of [YourKit Java Profiler](https://www.yourkit.com/java/profiler/), +[YourKit .NET Profiler](https://www.yourkit.com/.net/profiler/), +and [YourKit YouMonitor](https://www.yourkit.com/youmonitor/). diff --git a/api/OneConfig.api b/api/OneConfig.api index c047917..4f5a3bc 100644 --- a/api/OneConfig.api +++ b/api/OneConfig.api @@ -1,5 +1,6 @@ public class cc/polyfrost/oneconfig/config/Config { protected final field configFile Ljava/lang/String; + protected final field defaults Ljava/util/HashMap; public field enabled Z protected final field gson Lcom/google/gson/Gson; public field hasBeenInitialized Z @@ -15,6 +16,7 @@ public class cc/polyfrost/oneconfig/config/Config { protected fun deserializePart (Lcom/google/gson/JsonObject;Ljava/lang/Object;)V protected fun generateOptionList (Ljava/lang/Object;Lcc/polyfrost/oneconfig/config/elements/OptionPage;Lcc/polyfrost/oneconfig/config/data/Mod;Z)V protected fun getCustomOption (Ljava/lang/reflect/Field;Lcc/polyfrost/oneconfig/config/annotations/CustomOption;Lcc/polyfrost/oneconfig/config/elements/OptionPage;Lcc/polyfrost/oneconfig/config/data/Mod;Z)Lcc/polyfrost/oneconfig/config/elements/BasicOption; + public fun getDefault (Ljava/lang/reflect/Field;)Ljava/lang/Object; protected fun hideIf (Ljava/lang/String;Ljava/lang/String;)V protected fun hideIf (Ljava/lang/String;Ljava/util/function/Supplier;)V protected fun hideIf (Ljava/lang/String;Z)V @@ -22,6 +24,7 @@ public class cc/polyfrost/oneconfig/config/Config { public fun load ()V public fun openGui ()V protected fun registerKeyBind (Lcc/polyfrost/oneconfig/config/core/OneKeyBind;Ljava/lang/Runnable;)V + public fun reset ()V public fun save ()V } @@ -137,25 +140,6 @@ public abstract interface annotation class cc/polyfrost/oneconfig/config/annotat public abstract fun subcategory ()Ljava/lang/String; } -public class cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig : cc/polyfrost/oneconfig/config/Config { - public final field vigilant Lgg/essential/vigilance/Vigilant; - public fun <init> (Lcc/polyfrost/oneconfig/config/data/Mod;Ljava/lang/String;Lgg/essential/vigilance/Vigilant;)V - public fun addDependency (Lgg/essential/vigilance/data/PropertyData;Lgg/essential/vigilance/data/PropertyData;)V - public fun initialize ()V - public fun load ()V - public fun save ()V -} - -public abstract interface annotation class cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceName : java/lang/annotation/Annotation { - public abstract fun category ()Ljava/lang/String; - public abstract fun name ()Ljava/lang/String; - public abstract fun subcategory ()Ljava/lang/String; -} - -public abstract interface class cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilantAccessor { - public abstract fun getPropertyCollector ()Lgg/essential/vigilance/data/PropertyCollector; -} - public class cc/polyfrost/oneconfig/config/core/ConfigUtils { public fun <init> ()V public static fun addOptionToPage (Lcc/polyfrost/oneconfig/config/elements/OptionPage;Lcc/polyfrost/oneconfig/internal/config/annotations/Option;Ljava/lang/reflect/Field;Ljava/lang/Object;Lcc/polyfrost/oneconfig/config/migration/Migrator;)Lcc/polyfrost/oneconfig/config/elements/BasicOption; @@ -299,12 +283,14 @@ public abstract class cc/polyfrost/oneconfig/config/elements/BasicOption { public fun isEnabled ()Z public fun isHidden ()Z public fun keyTyped (CI)V + public fun reset (Lcc/polyfrost/oneconfig/config/Config;)V protected fun set (Ljava/lang/Object;)V } public class cc/polyfrost/oneconfig/config/elements/OptionCategory { public final field subcategories Ljava/util/ArrayList; public fun <init> ()V + public fun reset (Lcc/polyfrost/oneconfig/config/Config;)V } public class cc/polyfrost/oneconfig/config/elements/OptionPage { @@ -312,6 +298,7 @@ public class cc/polyfrost/oneconfig/config/elements/OptionPage { public final field mod Lcc/polyfrost/oneconfig/config/data/Mod; public final field name Ljava/lang/String; public fun <init> (Ljava/lang/String;Lcc/polyfrost/oneconfig/config/data/Mod;)V + public fun reset (Lcc/polyfrost/oneconfig/config/Config;)V } public class cc/polyfrost/oneconfig/config/elements/OptionSubcategory { @@ -322,6 +309,7 @@ public class cc/polyfrost/oneconfig/config/elements/OptionSubcategory { public fun draw (JII)I public fun drawLast (JI)V public fun getName ()Ljava/lang/String; + public fun reset (Lcc/polyfrost/oneconfig/config/Config;)V } public class cc/polyfrost/oneconfig/config/gson/ExclusionUtils { @@ -473,7 +461,7 @@ public abstract interface class cc/polyfrost/oneconfig/gui/GuiPause { } public class cc/polyfrost/oneconfig/gui/HudGui : cc/polyfrost/oneconfig/libs/universal/UScreen, cc/polyfrost/oneconfig/gui/GuiPause { - public fun <init> ()V + public fun <init> (Z)V public fun doesGuiPauseGame ()Z public fun initScreen (II)V public fun onDrawScreen (Lcc/polyfrost/oneconfig/libs/universal/UMatrixStack;IIF)V @@ -857,7 +845,7 @@ public abstract class cc/polyfrost/oneconfig/gui/pages/Page { public class cc/polyfrost/oneconfig/hud/HUDUtils { public fun <init> ()V - public static fun addHudOptions (Lcc/polyfrost/oneconfig/config/elements/OptionPage;Ljava/lang/reflect/Field;Ljava/lang/Object;)V + public static fun addHudOptions (Lcc/polyfrost/oneconfig/config/elements/OptionPage;Ljava/lang/reflect/Field;Ljava/lang/Object;Lcc/polyfrost/oneconfig/config/Config;)V } public abstract class cc/polyfrost/oneconfig/hud/Hud { @@ -891,6 +879,8 @@ public abstract class cc/polyfrost/oneconfig/hud/Hud { public abstract fun getWidth (F)I public fun getXScaled (I)F public fun getYScaled (I)F + public fun isEnabled ()Z + public fun setConfig (Lcc/polyfrost/oneconfig/config/Config;)V } public abstract class cc/polyfrost/oneconfig/hud/SingleTextHud : cc/polyfrost/oneconfig/hud/TextHud { @@ -1069,14 +1059,10 @@ public final class cc/polyfrost/oneconfig/renderer/RenderManager { public static fun drawSvg (JLjava/lang/String;FFFF)V public static fun drawSvg (JLjava/lang/String;FFFFI)V public static fun drawText (JLjava/lang/String;FFIFLcc/polyfrost/oneconfig/renderer/font/Font;)V - public static fun drawText (JLjava/lang/String;FFIFLcc/polyfrost/oneconfig/renderer/font/Fonts;)V public static fun drawURL (JLjava/lang/String;FFFLcc/polyfrost/oneconfig/renderer/font/Font;)V - public static fun drawURL (JLjava/lang/String;FFFLcc/polyfrost/oneconfig/renderer/font/Fonts;)V public static fun drawWrappedString (JLjava/lang/String;FFFIFLcc/polyfrost/oneconfig/renderer/font/Font;)V - public static fun drawWrappedString (JLjava/lang/String;FFFIFLcc/polyfrost/oneconfig/renderer/font/Fonts;)V public static fun fillNVGColorWithRGBA (FFFFLorg/lwjgl/nanovg/NVGColor;)V public static fun getTextWidth (JLjava/lang/String;FLcc/polyfrost/oneconfig/renderer/font/Font;)F - public static fun getTextWidth (JLjava/lang/String;FLcc/polyfrost/oneconfig/renderer/font/Fonts;)F public static fun scale (JFF)V public static fun setAlpha (JF)V public static fun setupAndDraw (Ljava/util/function/LongConsumer;)V @@ -1092,6 +1078,20 @@ public final class cc/polyfrost/oneconfig/renderer/RenderManager$TextType : java public static fun values ()[Lcc/polyfrost/oneconfig/renderer/RenderManager$TextType; } +public class cc/polyfrost/oneconfig/renderer/TextRenderer { + public fun <init> ()V + public static fun drawText (JLjava/lang/String;FFIFLcc/polyfrost/oneconfig/renderer/font/Font;)V + public static fun drawText (JLjava/lang/String;FFIFLcc/polyfrost/oneconfig/renderer/font/Font;F)V + public static fun drawText (JLjava/util/ArrayList;FFIFLcc/polyfrost/oneconfig/renderer/font/Font;F)V + public static fun drawText (JLjava/util/ArrayList;FFIFLcc/polyfrost/oneconfig/renderer/font/Font;FF)V + public static fun drawURL (JLjava/lang/String;FFFLcc/polyfrost/oneconfig/renderer/font/Font;)V + public static fun drawWrappedString (JLjava/lang/String;FFFIFLcc/polyfrost/oneconfig/renderer/font/Font;)V + public static fun getTextWidth (JLjava/lang/String;FLcc/polyfrost/oneconfig/renderer/font/Font;)F + public static fun getTextWidth (JLjava/lang/String;FLcc/polyfrost/oneconfig/renderer/font/Font;F)F + public static fun wrapText (JLjava/lang/String;FFLcc/polyfrost/oneconfig/renderer/font/Font;)Ljava/util/ArrayList; + public static fun wrapText (JLjava/lang/String;FFLcc/polyfrost/oneconfig/renderer/font/Font;F)Ljava/util/ArrayList; +} + public class cc/polyfrost/oneconfig/renderer/font/Font { public fun <init> (Ljava/lang/String;Ljava/lang/String;)V public fun getBuffer ()Ljava/nio/ByteBuffer; @@ -1107,16 +1107,14 @@ public class cc/polyfrost/oneconfig/renderer/font/FontManager { public fun loadFont (JLcc/polyfrost/oneconfig/renderer/font/Font;)V } -public final class cc/polyfrost/oneconfig/renderer/font/Fonts : java/lang/Enum { - public static final field BOLD Lcc/polyfrost/oneconfig/renderer/font/Fonts; - public static final field MEDIUM Lcc/polyfrost/oneconfig/renderer/font/Fonts; - public static final field MINECRAFT_BOLD Lcc/polyfrost/oneconfig/renderer/font/Fonts; - public static final field MINECRAFT_REGULAR Lcc/polyfrost/oneconfig/renderer/font/Fonts; - public static final field REGULAR Lcc/polyfrost/oneconfig/renderer/font/Fonts; - public static final field SEMIBOLD Lcc/polyfrost/oneconfig/renderer/font/Fonts; - public final field font Lcc/polyfrost/oneconfig/renderer/font/Font; - public static fun valueOf (Ljava/lang/String;)Lcc/polyfrost/oneconfig/renderer/font/Fonts; - public static fun values ()[Lcc/polyfrost/oneconfig/renderer/font/Fonts; +public class cc/polyfrost/oneconfig/renderer/font/Fonts { + public static final field BOLD Lcc/polyfrost/oneconfig/renderer/font/Font; + public static final field MEDIUM Lcc/polyfrost/oneconfig/renderer/font/Font; + public static final field MINECRAFT_BOLD Lcc/polyfrost/oneconfig/renderer/font/Font; + public static final field MINECRAFT_REGULAR Lcc/polyfrost/oneconfig/renderer/font/Font; + public static final field REGULAR Lcc/polyfrost/oneconfig/renderer/font/Font; + public static final field SEMIBOLD Lcc/polyfrost/oneconfig/renderer/font/Font; + public fun <init> ()V } public class cc/polyfrost/oneconfig/renderer/scissor/Scissor { @@ -1201,7 +1199,6 @@ public final class cc/polyfrost/oneconfig/utils/NetworkUtils { public final class cc/polyfrost/oneconfig/utils/TextUtils { public fun <init> ()V public static fun wrapText (JLjava/lang/String;FFLcc/polyfrost/oneconfig/renderer/font/Font;)Ljava/util/ArrayList; - public static fun wrapText (JLjava/lang/String;FFLcc/polyfrost/oneconfig/renderer/font/Fonts;)Ljava/util/ArrayList; } public class cc/polyfrost/oneconfig/utils/TickDelay { diff --git a/gradle.properties b/gradle.properties index de53850..3bedb89 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ mod_name = OneConfig mod_id = oneconfig -mod_version = 0.1.0-alpha43 +mod_version = 0.1.0-alpha50 essential.defaults.loom=0 diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java index 9557728..8a5608e 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -38,6 +38,7 @@ public class Config { transient protected final String configFile; transient protected final Gson gson = new GsonBuilder().setExclusionStrategies(new ProfileExclusionStrategy()).excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create(); transient protected final Gson nonProfileSpecificGson = new GsonBuilder().setExclusionStrategies(new NonProfileSpecificExclusionStrategy()).excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create(); + transient protected final HashMap<Field, Object> defaults = new HashMap<>(); transient public Mod mod; public transient boolean hasBeenInitialized = false; public boolean enabled; @@ -45,7 +46,7 @@ public class Config { /** * @param modData information about the mod * @param configFile file where config is stored - * @param enabled whether the mod is enabled or not + * @param enabled whether the mod is enabled or not */ public Config(Mod modData, String configFile, boolean enabled) { this.configFile = configFile; @@ -138,7 +139,7 @@ public class Config { 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); + HUDUtils.addHudOptions(page, field, instance, this); } } } @@ -306,18 +307,19 @@ public class Config { } /** + * @param field The field to get the default value from + * @return The default value of the given field + */ + public Object getDefault(Field field) { + return defaults.get(field); + } + + /** * Reset this config file to its defaults. - * @return true if successful, false if not. - * @deprecated <b>not implemented yet.</b> */ - @Deprecated - public boolean reset() { - try { - // TODO - } catch (Exception e) { - e.printStackTrace(); - return false; + public void reset() { + for (BasicOption option : optionNames.values()) { + option.reset(this); } - return true; } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java index 86b339b..6f9b47a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java @@ -1,5 +1,7 @@ package cc.polyfrost.oneconfig.config.elements; +import cc.polyfrost.oneconfig.config.Config; + import java.lang.reflect.Field; import java.util.ArrayList; import java.util.function.Supplier; @@ -86,6 +88,20 @@ public abstract class BasicOption { } /** + * Reset the field to its default value + * + * @param config The config the field is in + */ + public void reset(Config config) { + Object object = config.getDefault(field); + if (object == null) return; + try { + set(object); + } catch (IllegalAccessException ignored) { + } + } + + /** * @return If the option is enabled, based on the dependencies */ public boolean isEnabled() { diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java index 4a37bfc..f144f60 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java @@ -1,7 +1,15 @@ package cc.polyfrost.oneconfig.config.elements; +import cc.polyfrost.oneconfig.config.Config; + import java.util.ArrayList; public class OptionCategory { public final ArrayList<OptionSubcategory> subcategories = new ArrayList<>(); + + public void reset(Config config) { + for (OptionSubcategory subcategory : subcategories) { + subcategory.reset(config); + } + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java index 5eaac8b..916e1d9 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java @@ -1,5 +1,6 @@ package cc.polyfrost.oneconfig.config.elements; +import cc.polyfrost.oneconfig.config.Config; import cc.polyfrost.oneconfig.config.data.Mod; import java.util.LinkedHashMap; @@ -13,4 +14,10 @@ public class OptionPage { this.name = name; this.mod = mod; } + + public void reset(Config config) { + for (OptionCategory subcategory : categories.values()) { + subcategory.reset(config); + } + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java index 365ddb6..e1342d4 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java @@ -1,5 +1,6 @@ package cc.polyfrost.oneconfig.config.elements; +import cc.polyfrost.oneconfig.config.Config; import cc.polyfrost.oneconfig.internal.assets.Colors; import cc.polyfrost.oneconfig.gui.OneConfigGui; import cc.polyfrost.oneconfig.gui.elements.config.ConfigPageButton; @@ -104,4 +105,10 @@ public class OptionSubcategory { public String getName() { return name; } + + public void reset(Config config) { + for (BasicOption option : options) { + options.remove(config); + } + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java b/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java index f09d648..aed1919 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java @@ -1,6 +1,6 @@ package cc.polyfrost.oneconfig.config.migration; -import cc.polyfrost.oneconfig.config.compatibility.vigilance.VigilanceName; +import cc.polyfrost.oneconfig.internal.config.compatibility.vigilance.VigilanceName; import java.io.BufferedReader; import java.io.File; diff --git a/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java b/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java index 29a9daa..686fb69 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java @@ -60,7 +60,7 @@ public class Profiles { public static void loadProfile(String profile) { ConfigCore.saveAll(); OneConfigConfig.currentProfile = profile; - //OneConfig.config.save(); todo do we actually need this + OneConfigConfig.getInstance().save(); ConfigCore.reInitAll(); } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/HudGui.java b/src/main/java/cc/polyfrost/oneconfig/gui/HudGui.java index f5b1d3f..fc90c01 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/HudGui.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/HudGui.java @@ -1,13 +1,13 @@ package cc.polyfrost.oneconfig.gui; -import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; import cc.polyfrost.oneconfig.hud.Hud; +import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; import cc.polyfrost.oneconfig.internal.hud.HudCore; -import cc.polyfrost.oneconfig.libs.universal.UResolution; -import cc.polyfrost.oneconfig.renderer.RenderManager; import cc.polyfrost.oneconfig.libs.universal.UKeyboard; import cc.polyfrost.oneconfig.libs.universal.UMatrixStack; +import cc.polyfrost.oneconfig.libs.universal.UResolution; import cc.polyfrost.oneconfig.libs.universal.UScreen; +import cc.polyfrost.oneconfig.renderer.RenderManager; import cc.polyfrost.oneconfig.utils.gui.GuiUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -44,7 +44,7 @@ public class HudGui extends UScreen implements GuiPause { } for (Hud hud : HudCore.huds) { - if (hud.enabled) processHud(matrixStack, hud, mouseX); + if (hud.isEnabled()) processHud(matrixStack, hud, mouseX); } } @@ -128,7 +128,7 @@ public class HudGui extends UScreen implements GuiPause { float width = editingHud.getWidth(editingHud.scale) + editingHud.paddingX * editingHud.scale; ArrayList<Float> verticalLines = new ArrayList<>(); for (Hud hud : HudCore.huds) { - if (!hud.enabled) continue; + if (!hud.isEnabled()) continue; verticalLines.addAll(getXSnappingHud(hud)); } getSpaceSnapping(verticalLines); @@ -167,7 +167,7 @@ public class HudGui extends UScreen implements GuiPause { float height = editingHud.getHeight(editingHud.scale) + editingHud.paddingY * editingHud.scale; ArrayList<Float> horizontalLines = new ArrayList<>(); for (Hud hud : HudCore.huds) { - if (!hud.enabled) continue; + if (!hud.isEnabled()) continue; horizontalLines.addAll(getYSnappingHud(hud)); } getSpaceSnapping(horizontalLines); @@ -229,7 +229,7 @@ public class HudGui extends UScreen implements GuiPause { } editingHud = null; for (Hud hud : HudCore.huds) { - if (!hud.enabled) continue; + if (!hud.isEnabled()) continue; if (mouseClickedHud(hud, (int) mouseX, (int) mouseY)) break; } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java index 1095ae0..41d0b4f 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java @@ -109,7 +109,8 @@ public class OneConfigGui extends UScreen implements GuiPause { RenderManager.setAlpha(vg, 0.5f); } else { backArrow.disable(false); - if (!backArrow.isHovered() || Platform.getMousePlatform().isButtonDown(0)) RenderManager.setAlpha(vg, 0.8f); + if (!backArrow.isHovered() || Platform.getMousePlatform().isButtonDown(0)) + RenderManager.setAlpha(vg, 0.8f); } RenderManager.drawSvg(vg, SVGs.CARET_LEFT, x + 246, y + 22, 28, 28); RenderManager.setAlpha(vg, 1f); @@ -118,7 +119,8 @@ public class OneConfigGui extends UScreen implements GuiPause { RenderManager.setAlpha(vg, 0.5f); } else { forwardArrow.disable(false); - if (!forwardArrow.isHovered() || Platform.getMousePlatform().isButtonDown(0)) RenderManager.setAlpha(vg, 0.8f); + if (!forwardArrow.isHovered() || Platform.getMousePlatform().isButtonDown(0)) + RenderManager.setAlpha(vg, 0.8f); } RenderManager.drawSvg(vg, SVGs.CARET_RIGHT, x + 294, y + 22, 28, 28); RenderManager.setAlpha(vg, 1f); @@ -140,7 +142,7 @@ public class OneConfigGui extends UScreen implements GuiPause { } ScissorManager.scissor(vg, x + 224, y + 72, 1056, 728); - Scissor blockedClicks = InputUtils.blockInputArea(x + 224, y, 1056,72); + Scissor blockedClicks = InputUtils.blockInputArea(x + 224, y, 1056, 72); if (prevPage != null && animation != null) { float pageProgress = animation.get(GuiUtils.getDeltaTime()); if (!animation.isReversed()) { @@ -188,9 +190,7 @@ public class OneConfigGui extends UScreen implements GuiPause { public void onKeyPressed(int keyCode, char typedChar, @Nullable UKeyboard.Modifiers modifiers) { UKeyboard.allowRepeatEvents(true); try { - if (keyCode == 1 && currentPage.parents.size() > 1) { - openPage(currentPage.parents.get(currentPage.parents.size() - 2)); - } else if (allowClose) super.onKeyPressed(keyCode, typedChar, modifiers); + if (allowClose) super.onKeyPressed(keyCode, typedChar, modifiers); textInputField.keyTyped(typedChar, keyCode); if (currentColorSelector != null) currentColorSelector.keyTyped(typedChar, keyCode); currentPage.keyTyped(typedChar, keyCode); diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigDropdown.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigDropdown.java index ddcf6eb..fa48592 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigDropdown.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigDropdown.java @@ -1,13 +1,13 @@ package cc.polyfrost.oneconfig.gui.elements.config; import cc.polyfrost.oneconfig.config.annotations.Dropdown; -import cc.polyfrost.oneconfig.internal.assets.Colors; import cc.polyfrost.oneconfig.config.elements.BasicOption; import cc.polyfrost.oneconfig.gui.animations.ColorAnimation; +import cc.polyfrost.oneconfig.internal.assets.Colors; +import cc.polyfrost.oneconfig.internal.assets.SVGs; import cc.polyfrost.oneconfig.platform.Platform; import cc.polyfrost.oneconfig.renderer.RenderManager; import cc.polyfrost.oneconfig.renderer.font.Fonts; -import cc.polyfrost.oneconfig.internal.assets.SVGs; import cc.polyfrost.oneconfig.renderer.scissor.Scissor; import cc.polyfrost.oneconfig.utils.InputUtils; import cc.polyfrost.oneconfig.utils.color.ColorPalette; @@ -43,8 +43,8 @@ public class ConfigDropdown extends BasicOption { else hovered = InputUtils.isAreaHovered(x + 352, y, 640, 32) && isEnabled(); if (hovered && InputUtils.isClicked() || opened && InputUtils.isClicked(true) && - (size == 1 && !InputUtils.isAreaHovered(x + 224, y + 40, 256, options.length * 32) || - size == 2 && !InputUtils.isAreaHovered(x + 352, y + 40, 640, options.length * 32))) { + (size == 1 && !InputUtils.isAreaHovered(x + 224, y + 40, 256, options.length * 32, true) || + size == 2 && !InputUtils.isAreaHovered(x + 352, y + 40, 640, options.length * 32, true))) { opened = !opened; backgroundColor.setPalette(opened ? ColorPalette.PRIMARY : ColorPalette.SECONDARY); if (opened) inputScissor = InputUtils.blockAllInput(); diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java index 552051f..054915c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/TextInputField.java @@ -129,18 +129,18 @@ public class TextInputField extends BasicElement { float width; StringBuilder s = new StringBuilder(); if (multiLine) { - wrappedText = TextUtils.wrapText(vg, input, this.width - 24, 14f, Fonts.REGULAR.font); + wrappedText = TextUtils.wrapText(vg, input, this.width - 24, 14f, Fonts.REGULAR); lines = wrappedText.size(); if (!toggled) caretPos = wrappedText.get(wrappedText.size() - 1).length(); int caretLine = (int) MathUtils.clamp(getCaretLine(caretPos), 0, wrappedText.size() - 1); - width = RenderManager.getTextWidth(vg, wrappedText.get(caretLine).substring(0, getLineCaret(caretPos, caretLine)), 14f, Fonts.REGULAR.font); + width = RenderManager.getTextWidth(vg, wrappedText.get(caretLine).substring(0, getLineCaret(caretPos, caretLine)), 14f, Fonts.REGULAR); } else if (!password) { - width = RenderManager.getTextWidth(vg, input.substring(0, caretPos), 14f, Fonts.REGULAR.font); + width = RenderManager.getTextWidth(vg, input.substring(0, caretPos), 14f, Fonts.REGULAR); } else { for (int i = 0; i < input.length(); i++) { s.append("*"); } - width = RenderManager.getTextWidth(vg, s.substring(0, caretPos), 14f, Fonts.REGULAR.font); + width = RenderManager.getTextWidth(vg, s.substring(0, caretPos), 14f, Fonts.REGULAR); } if (hovered) { int state = Platform.getMousePlatform().getButtonState(0); //todo does this work diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java b/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java index 5f454f2..8a2e226 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/pages/ModsPage.java @@ -9,6 +9,7 @@ import cc.polyfrost.oneconfig.internal.assets.Colors; import cc.polyfrost.oneconfig.internal.config.OneConfigConfig; import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; import cc.polyfrost.oneconfig.renderer.RenderManager; +import cc.polyfrost.oneconfig.renderer.TextRenderer; import cc.polyfrost.oneconfig.renderer.font.Fonts; import cc.polyfrost.oneconfig.utils.color.ColorPalette; diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java index e91a7ce..a67177c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java @@ -1,5 +1,6 @@ package cc.polyfrost.oneconfig.hud; +import cc.polyfrost.oneconfig.config.Config; import cc.polyfrost.oneconfig.config.annotations.HUD; import cc.polyfrost.oneconfig.config.core.ConfigUtils; import cc.polyfrost.oneconfig.config.elements.BasicOption; @@ -11,10 +12,11 @@ import java.lang.reflect.Field; import java.util.ArrayList; public class HUDUtils { - public static void addHudOptions(OptionPage page, Field field, Object instance) { + public static void addHudOptions(OptionPage page, Field field, Object instance, Config config) { HUD hudAnnotation = field.getAnnotation(HUD.class); Hud hud = (Hud) ConfigUtils.getField(field, instance); if (hud == null) return; + hud.setConfig(config); HudCore.huds.add(hud); String category = hudAnnotation.category(); String subcategory = hudAnnotation.subcategory(); @@ -24,21 +26,21 @@ public class HUDUtils { 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.get(options.size() - 1).addDependency(hud::isEnabled); options.add(new ConfigCheckbox(hud.getClass().getField("border"), hud, "Outline/border", category, subcategory, 1)); - options.get(options.size() - 1).addDependency(() -> hud.enabled); + options.get(options.size() - 1).addDependency(hud::isEnabled); options.add(new ConfigColorElement(hud.getClass().getField("bgColor"), hud, "Background color:", category, subcategory, 1, true)); - options.get(options.size() - 1).addDependency(() -> hud.enabled); + options.get(options.size() - 1).addDependency(hud::isEnabled); options.add(new ConfigColorElement(hud.getClass().getField("borderColor"), hud, "Border color:", category, subcategory, 1, true)); - options.get(options.size() - 1).addDependency(() -> hud.enabled && hud.border); + options.get(options.size() - 1).addDependency(() -> hud.isEnabled() && hud.border); options.add(new ConfigSlider(hud.getClass().getField("cornerRadius"), hud, "Corner radius:", category, subcategory, 0, 10, 0)); - options.get(options.size() - 1).addDependency(() -> hud.enabled && hud.rounded); + options.get(options.size() - 1).addDependency(() -> hud.isEnabled() && hud.rounded); options.add(new ConfigSlider(hud.getClass().getField("borderSize"), hud, "Border thickness:", category, subcategory, 0, 10, 0)); - options.get(options.size() - 1).addDependency(() -> hud.enabled && hud.border); + options.get(options.size() - 1).addDependency(() -> hud.isEnabled() && hud.border); options.add(new ConfigSlider(hud.getClass().getField("paddingX"), hud, "X-Padding", category, subcategory, 0, 50, 0)); - options.get(options.size() - 1).addDependency(() -> hud.enabled); + options.get(options.size() - 1).addDependency(hud::isEnabled); options.add(new ConfigSlider(hud.getClass().getField("paddingY"), hud, "Y-Padding", category, subcategory, 0, 50, 0)); - options.get(options.size() - 1).addDependency(() -> hud.enabled); + options.get(options.size() - 1).addDependency(hud::isEnabled); } catch (NoSuchFieldException ignored) { } } diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java b/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java index 982bee9..05a4f76 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java @@ -40,6 +40,7 @@ import cc.polyfrost.oneconfig.renderer.RenderManager; */ public abstract class Hud { public boolean enabled; + transient private Config config; public boolean rounded; public boolean border; public OneColor bgColor; @@ -236,6 +237,17 @@ public abstract class Hud { return (float) (screenHeight - (1d - yUnscaled) * screenHeight - (getHeight(scale) + paddingY * scale)); } + /** + * @return If the hud is enabled + */ + public boolean isEnabled() { + return enabled && (config == null || config.enabled); + } + + public void setConfig(Config config) { + this.config = config; + } + @Switch( name = "Show in Chat" ) diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java index 381e0a8..8dd8ac3 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/TextHud.java @@ -103,7 +103,7 @@ public abstract class TextHud extends Hud { private class TickHandler { @Subscribe private void onTick(TickEvent event) { - if (event.stage != Stage.START) return; + if (event.stage != Stage.START || !isEnabled()) return; lines.clear(); if (!HudCore.editing) getLines(lines); else getExampleLines(lines); diff --git a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceConfig.java index 97dd0cb..689bf25 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceConfig.java @@ -1,4 +1,4 @@ -package cc.polyfrost.oneconfig.config.compatibility.vigilance; +package cc.polyfrost.oneconfig.internal.config.compatibility.vigilance; import cc.polyfrost.oneconfig.config.core.ConfigUtils; import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; diff --git a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceName.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceName.java index 8417ad8..ff84d3c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceName.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceName.java @@ -1,4 +1,4 @@ -package cc.polyfrost.oneconfig.config.compatibility.vigilance; +package cc.polyfrost.oneconfig.internal.config.compatibility.vigilance; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilantAccessor.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilantAccessor.java index c449bd9..f059dbc 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilantAccessor.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilantAccessor.java @@ -1,4 +1,4 @@ -package cc.polyfrost.oneconfig.config.compatibility.vigilance; +package cc.polyfrost.oneconfig.internal.config.compatibility.vigilance; import gg.essential.vigilance.data.PropertyCollector; diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java index ab796c7..c5724e5 100644 --- a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java @@ -40,7 +40,6 @@ public class ConfigCore { } public static void sortMods() { - ArrayList<Mod> mods = new ArrayList<>(ConfigCore.mods); ConfigCore.mods = mods.stream().filter((mod -> OneConfigConfig.favoriteMods.contains(mod.name))).sorted().collect(Collectors.toList()); mods.removeAll(ConfigCore.mods); diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/hud/HudCore.java b/src/main/java/cc/polyfrost/oneconfig/internal/hud/HudCore.java index 2bbd3b7..31604ed 100644 --- a/src/main/java/cc/polyfrost/oneconfig/internal/hud/HudCore.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/hud/HudCore.java @@ -15,8 +15,8 @@ public class HudCore { public void onRender(HudRenderEvent event) { if (editing) return; for (Hud hud : huds) { - if (hud.enabled) - hud.drawAll(event.matrices, hud.getXScaled(UResolution.getScaledWidth()), hud.getYScaled(UResolution.getScaledHeight()), hud.scale, true); + if (!hud.isEnabled()) continue; + hud.drawAll(event.matrices, hud.getXScaled(UResolution.getScaledWidth()), hud.getYScaled(UResolution.getScaledHeight()), hud.scale, true); } } } diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java b/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java index 5869ffb..b747088 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/RenderManager.java @@ -292,22 +292,6 @@ public final class RenderManager { * @param font The font. * @see cc.polyfrost.oneconfig.renderer.font.Font */ - public static void drawText(long vg, String text, float x, float y, int color, float size, Fonts font) { - drawText(vg, text, x, y, color, size, font.font); - } - - /** - * Draws a String with the given parameters. - * - * @param vg The NanoVG context. - * @param text The text. - * @param x The x position. - * @param y The y position. - * @param color The color. - * @param size The size. - * @param font The font. - * @see cc.polyfrost.oneconfig.renderer.font.Font - */ public static void drawText(long vg, String text, float x, float y, int color, float size, Font font) { nvgBeginPath(vg); nvgFontSize(vg, size); @@ -331,22 +315,6 @@ public final class RenderManager { * @param size The size. * @param font The font. */ - public static void drawWrappedString(long vg, String text, float x, float y, float width, int color, float size, Fonts font) { - drawWrappedString(vg, text, x, y, width, color, size, font.font); - } - - /** - * Draws a String wrapped at the given width, with the given parameters. - * - * @param vg The NanoVG context. - * @param text The text. - * @param x The x position. - * @param y The y position. - * @param width The width. - * @param color The color. - * @param size The size. - * @param font The font. - */ public static void drawWrappedString(long vg, String text, float x, float y, float width, int color, float size, Font font) { nvgBeginPath(vg); nvgFontSize(vg, size); @@ -366,18 +334,6 @@ public final class RenderManager { * @see RenderManager#drawText(long, String, float, float, int, float, Font) * @see InputUtils#isAreaClicked(int, int, int, int) */ - public static void drawURL(long vg, String url, float x, float y, float size, Fonts font) { - drawURL(vg, url, x, y, size, font.font); - } - - /** - * Draw a formatted URL (a string in blue with an underline) that when clicked, opens the given text. - * - * <p><b>This does NOT scale to Minecraft's GUI scale!</b></p> - * - * @see RenderManager#drawText(long, String, float, float, int, float, Font) - * @see InputUtils#isAreaClicked(int, int, int, int) - */ public static void drawURL(long vg, String url, float x, float y, float size, Font font) { drawText(vg, url, x, y, Colors.PRIMARY_500, size, font); float length = getTextWidth(vg, url, size, font); @@ -487,10 +443,6 @@ public final class RenderManager { drawRoundImage(vg, filePath.filePath, x, y, width, height, radius); } - public static float getTextWidth(long vg, String text, float fontSize, Fonts font) { - return getTextWidth(vg, text, fontSize, font.font); - } - /** * Get the width of the provided String. * diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/TextRenderer.java b/src/main/java/cc/polyfrost/oneconfig/renderer/TextRenderer.java new file mode 100644 index 0000000..95071d1 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/TextRenderer.java @@ -0,0 +1,217 @@ +package cc.polyfrost.oneconfig.renderer; + +import cc.polyfrost.oneconfig.internal.assets.Colors; +import cc.polyfrost.oneconfig.renderer.font.Font; +import cc.polyfrost.oneconfig.utils.InputUtils; +import cc.polyfrost.oneconfig.utils.NetworkUtils; +import com.google.common.annotations.Beta; +import org.lwjgl.nanovg.NVGColor; + +import java.util.ArrayList; + +import static cc.polyfrost.oneconfig.renderer.RenderManager.color; +import static org.lwjgl.nanovg.NanoVG.*; + +public class TextRenderer { + /** + * Draws a String with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param color The color. + * @param size The size. + * @param font The font. + * @param letterSpacing The letter spacing + * @see cc.polyfrost.oneconfig.renderer.font.Font + */ + public static void drawText(long vg, String text, float x, float y, int color, float size, Font font, float letterSpacing) { + nvgBeginPath(vg); + nvgFontSize(vg, size); + nvgTextLetterSpacing(vg, letterSpacing); + nvgFontFace(vg, font.getName()); + nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); + NVGColor nvgColor = color(vg, color); + nvgText(vg, x, y, text); + nvgFill(vg); + nvgColor.free(); + } + + /** + * Draws a String with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param color The color. + * @param size The size. + * @param font The font. + * @see cc.polyfrost.oneconfig.renderer.font.Font + */ + public static void drawText(long vg, String text, float x, float y, int color, float size, Font font) { + drawText(vg, text, x, y, color, size, font, 0); + } + + /** + * Draws a String with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param color The color. + * @param size The size. + * @param font The font. + * @param lineHeight The line height + * @param letterSpacing The letter spacing + * @see cc.polyfrost.oneconfig.renderer.font.Font + */ + public static void drawText(long vg, ArrayList<String> text, float x, float y, int color, float size, Font font, float lineHeight, float letterSpacing) { + float textY = y; + for (String line : text) { + drawText(vg, line, x, textY, color, size, font, letterSpacing); + textY += lineHeight; + } + } + + /** + * Draws a String with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param color The color. + * @param size The size. + * @param font The font. + * @param lineHeight The line height + * @see cc.polyfrost.oneconfig.renderer.font.Font + */ + public static void drawText(long vg, ArrayList<String> text, float x, float y, int color, float size, Font font, float lineHeight) { + drawText(vg, text, x, y, color, size, font, lineHeight, 0); + } + + /** + * Draws a String wrapped at the given width, with the given parameters. + * + * @param vg The NanoVG context. + * @param text The text. + * @param x The x position. + * @param y The y position. + * @param width The width. + * @param color The color. + * @param size The size. + * @param font The font. + */ + public static void drawWrappedString(long vg, String text, float x, float y, float width, int color, float size, Font font) { + nvgBeginPath(vg); + nvgFontSize(vg, size); + nvgFontFace(vg, font.getName()); + nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE); + NVGColor nvgColor = color(vg, color); + nvgTextBox(vg, x, y, width, text); + nvgFill(vg); + nvgColor.free(); + } + + /** + * Draw a formatted URL (a string in blue with an underline) that when clicked, opens the given text. + * + * <p><b>This does NOT scale to Minecraft's GUI scale!</b></p> + * + * @see RenderManager#drawText(long, String, float, float, int, float, Font) + * @see InputUtils#isAreaClicked(int, int, int, int) + */ + public static void drawURL(long vg, String url, float x, float y, float size, Font font) { + drawText(vg, url, x, y, Colors.PRIMARY_500, size, font); + float length = getTextWidth(vg, url, size, font); + RenderManager.drawRectangle(vg, x, y + size / 2, length, 1, Colors.PRIMARY_500); + if (InputUtils.isAreaClicked((int) (x - 2), (int) (y - 1), (int) (length + 4), (int) (size / 2 + 3))) { + NetworkUtils.browseLink(url); + } + } + + /** + * Get the width of the provided String. + * + * @param vg The NanoVG context. + * @param text The text. + * @param fontSize The font size. + * @param font The font. + * @param letterSpacing The letter spacing + * @return The width of the text. + */ + public static float getTextWidth(long vg, String text, float fontSize, Font font, float letterSpacing) { + float[] bounds = new float[4]; + nvgFontSize(vg, fontSize); + nvgTextLetterSpacing(vg, letterSpacing); + nvgFontFace(vg, font.getName()); + return nvgTextBounds(vg, 0, 0, text, bounds); + } + + /** + * Get the width of the provided String. + * + * @param vg The NanoVG context. + * @param text The text. + * @param fontSize The font size. + * @param font The font. + * @return The width of the text. + */ + public static float getTextWidth(long vg, String text, float fontSize, Font font) { + return getTextWidth(vg, text, fontSize, font, 0); + } + + /** + * Wraps a string into an array of lines. + * + * @param vg The NanoVG context. + * @param text The text to wrap. + * @param maxWidth The maximum width of each line. + * @param fontSize The font size. + * @param font The font to use. + * @param letterSpacing The letter spacing + * @return The array of lines. + */ + @Beta + public static ArrayList<String> wrapText(long vg, String text, float maxWidth, float fontSize, Font font, float letterSpacing) { + ArrayList<String> wrappedText = new ArrayList<>(); + text += " "; + int prevIndex = 0; + for (int i = text.indexOf(" "); i >= 0; i = text.indexOf(" ", i + 1)) { + String textPart = text.substring(0, i); + float textWidth = getTextWidth(vg, textPart, fontSize, font, letterSpacing); + if (textWidth < maxWidth) { + prevIndex = i; + continue; + } + wrappedText.add(text.substring(0, prevIndex) + " "); + wrappedText.addAll(wrapText(vg, text.substring(prevIndex + 1), maxWidth, fontSize, font, letterSpacing)); + break; + } + if (wrappedText.size() == 0) wrappedText.add(text); + String temp = wrappedText.get(wrappedText.size() - 1); + if (temp.length() != 0) { + wrappedText.remove(wrappedText.size() - 1); + wrappedText.add(temp.substring(0, temp.length() - 1)); + } + return wrappedText; + } + + /** + * Wraps a string into an array of lines. + * + * @param vg The NanoVG context. + * @param text The text to wrap. + * @param maxWidth The maximum width of each line. + * @param fontSize The font size. + * @param font The font to use. + * @return The array of lines. + */ + @Beta + public static ArrayList<String> wrapText(long vg, String text, float maxWidth, float fontSize, Font font) { + return wrapText(vg, text, maxWidth, fontSize, font, 0); + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java b/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java index 388911d..79ac518 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/font/FontManager.java @@ -3,6 +3,7 @@ package cc.polyfrost.oneconfig.renderer.font; import cc.polyfrost.oneconfig.utils.IOUtils; import java.io.IOException; +import java.lang.reflect.Field; import java.nio.ByteBuffer; import static org.lwjgl.nanovg.NanoVG.nvgCreateFontMem; @@ -17,8 +18,15 @@ public class FontManager { */ public void initialize(long vg) { - for (Fonts fonts : Fonts.values()) { - loadFont(vg, fonts.font); + for (Field field : Fonts.class.getDeclaredFields()) { + try { + field.setAccessible(true); + Object font = field.get(null); + if (!(font instanceof Font)) continue; + loadFont(vg, (Font) font); + } catch (Exception e) { + throw new RuntimeException("Could not initialize fonts"); + } } } diff --git a/src/main/java/cc/polyfrost/oneconfig/renderer/font/Fonts.java b/src/main/java/cc/polyfrost/oneconfig/renderer/font/Fonts.java index 9b6193f..3120c7b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/renderer/font/Fonts.java +++ b/src/main/java/cc/polyfrost/oneconfig/renderer/font/Fonts.java @@ -1,15 +1,10 @@ package cc.polyfrost.oneconfig.renderer.font; -public enum Fonts { - BOLD(new Font("inter-bold", "/assets/oneconfig/font/Bold.otf")), - SEMIBOLD(new Font("inter-semibold", "/assets/oneconfig/font/SemiBold.otf")), - MEDIUM(new Font("inter-medium", "/assets/oneconfig/font/Medium.otf")), - REGULAR(new Font("inter-regular", "/assets/oneconfig/font/Regular.otf")), - MINECRAFT_REGULAR(new Font("mc-regular", "/assets/oneconfig/font/Minecraft-Regular.otf")), - MINECRAFT_BOLD(new Font("mc-bold", "/assets/oneconfig/font/Minecraft-Bold.otf")); - public final Font font; - - Fonts(Font font) { - this.font = font; - } +public class Fonts { + public static final Font BOLD = new Font("inter-bold", "/assets/oneconfig/font/Bold.otf"); + public static final Font SEMIBOLD = new Font("inter-semibold", "/assets/oneconfig/font/SemiBold.otf"); + public static final Font MEDIUM = new Font("inter-medium", "/assets/oneconfig/font/Medium.otf"); + public static final Font REGULAR = new Font("inter-regular", "/assets/oneconfig/font/Regular.otf"); + public static final Font MINECRAFT_REGULAR = new Font("mc-regular", "/assets/oneconfig/font/Minecraft-Regular.otf"); + public static final Font MINECRAFT_BOLD = new Font("mc-bold", "/assets/oneconfig/font/Minecraft-Bold.otf"); } diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java index 51b42d8..265e2f4 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/TextUtils.java @@ -21,20 +21,6 @@ public final class TextUtils { * @param font The font to use. * @return The array of lines. */ - public static ArrayList<String> wrapText(long vg, String text, float maxWidth, float fontSize, Fonts font) { - return wrapText(vg, text, maxWidth, fontSize, font.font); - } - - /** - * Wraps a string into an array of lines. - * - * @param vg The NanoVG context. - * @param text The text to wrap. - * @param maxWidth The maximum width of each line. - * @param fontSize The font size. - * @param font The font to use. - * @return The array of lines. - */ public static ArrayList<String> wrapText(long vg, String text, float maxWidth, float fontSize, Font font) { ArrayList<String> wrappedText = new ArrayList<>(); text += " "; diff --git a/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/asm/tweakers/VigilantTransformer.java b/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/asm/tweakers/VigilantTransformer.java index 8dd60cf..b3fed37 100644 --- a/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/asm/tweakers/VigilantTransformer.java +++ b/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/asm/tweakers/VigilantTransformer.java @@ -19,11 +19,11 @@ public class VigilantTransformer implements ITransformer { */ @Override public void transform(String transformedName, ClassNode node) { - if (!node.interfaces.contains("cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilantAccessor")) { - node.fields.add(new FieldNode(Opcodes.ACC_PUBLIC, "oneconfig$config", "Lcc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig;", null, null)); + if (!node.interfaces.contains("cc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilantAccessor")) { + node.fields.add(new FieldNode(Opcodes.ACC_PUBLIC, "oneconfig$config", "Lcc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceConfig;", null, null)); node.fields.add(new FieldNode(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "oneconfig$file", Type.getDescriptor(File.class), null, null)); - node.interfaces.add("cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilantAccessor"); + node.interfaces.add("cc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilantAccessor"); MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, "getPropertyCollector", "()Lgg/essential/vigilance/data/PropertyCollector;", null, null); LabelNode labelNode = new LabelNode(); methodNode.instructions.add(labelNode); @@ -40,17 +40,17 @@ public class VigilantTransformer implements ITransformer { methodNode2.instructions.add(labelNode2); methodNode2.instructions.add(new LineNumberNode(15636436, labelNode2)); methodNode2.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0)); - methodNode2.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, "gg/essential/vigilance/Vigilant", "oneconfig$config", "Lcc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig;")); + methodNode2.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, "gg/essential/vigilance/Vigilant", "oneconfig$config", "Lcc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceConfig;")); methodNode2.instructions.add(new JumpInsnNode(Opcodes.IFNULL, labelNode4)); methodNode2.instructions.add(labelNode3); methodNode2.instructions.add(new LineNumberNode(15636437, labelNode3)); methodNode2.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0)); - methodNode2.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, "gg/essential/vigilance/Vigilant", "oneconfig$config", "Lcc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig;")); + methodNode2.instructions.add(new FieldInsnNode(Opcodes.GETFIELD, "gg/essential/vigilance/Vigilant", "oneconfig$config", "Lcc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceConfig;")); methodNode2.instructions.add(new VarInsnNode(Opcodes.ALOAD, 1)); methodNode2.instructions.add(new VarInsnNode(Opcodes.ALOAD, 2)); - methodNode2.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "cc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig", "addDependency", "(Lgg/essential/vigilance/data/PropertyData;Lgg/essential/vigilance/data/PropertyData;)V", false)); + methodNode2.instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "cc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceConfig", "addDependency", "(Lgg/essential/vigilance/data/PropertyData;Lgg/essential/vigilance/data/PropertyData;)V", false)); methodNode2.instructions.add(labelNode4); methodNode2.instructions.add(new LineNumberNode(15636438, labelNode4)); @@ -64,8 +64,8 @@ public class VigilantTransformer implements ITransformer { list.add(new VarInsnNode(Opcodes.ALOAD, 0)); list.add(new VarInsnNode(Opcodes.ALOAD, 0)); list.add(new FieldInsnNode(Opcodes.GETFIELD, "gg/essential/vigilance/Vigilant", "oneconfig$file", Type.getDescriptor(File.class))); - list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "cc/polyfrost/oneconfig/internal/plugin/hooks/VigilantHook", "returnNewConfig", "(Lgg/essential/vigilance/Vigilant;Ljava/io/File;)Lcc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig;", false)); - list.add(new FieldInsnNode(Opcodes.PUTFIELD, "gg/essential/vigilance/Vigilant", "oneconfig$config", "Lcc/polyfrost/oneconfig/config/compatibility/vigilance/VigilanceConfig;")); + list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "cc/polyfrost/oneconfig/internal/plugin/hooks/VigilantHook", "returnNewConfig", "(Lgg/essential/vigilance/Vigilant;Ljava/io/File;)Lcc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceConfig;", false)); + list.add(new FieldInsnNode(Opcodes.PUTFIELD, "gg/essential/vigilance/Vigilant", "oneconfig$config", "Lcc/polyfrost/oneconfig/internal/config/compatibility/vigilance/VigilanceConfig;")); method.instructions.insertBefore(method.instructions.getLast().getPrevious(), list); } else if (method.name.equals("addDependency") && method.desc.equals("(Lgg/essential/vigilance/data/PropertyData;Lgg/essential/vigilance/data/PropertyData;)V")) { InsnList list = new InsnList(); diff --git a/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/hooks/VigilantHook.java b/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/hooks/VigilantHook.java index a012773..fcd5ff4 100644 --- a/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/hooks/VigilantHook.java +++ b/versions/src/main/java/cc/polyfrost/oneconfig/internal/plugin/hooks/VigilantHook.java @@ -1,6 +1,6 @@ package cc.polyfrost.oneconfig.internal.plugin.hooks; -import cc.polyfrost.oneconfig.config.compatibility.vigilance.VigilanceConfig; +import cc.polyfrost.oneconfig.internal.config.compatibility.vigilance.VigilanceConfig; import cc.polyfrost.oneconfig.config.data.Mod; import cc.polyfrost.oneconfig.config.data.ModType; import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; |