diff options
Diffstat (limited to 'src')
5 files changed, 24 insertions, 14 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java index 62609d4..d4b6022 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -4,6 +4,7 @@ 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.config.core.OneKeyBind; import cc.polyfrost.oneconfig.config.data.Mod; import cc.polyfrost.oneconfig.config.data.PageLocation; import cc.polyfrost.oneconfig.config.elements.BasicOption; @@ -16,6 +17,7 @@ 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.internal.config.core.KeyBindHandler; import cc.polyfrost.oneconfig.utils.gui.GuiUtils; import com.google.gson.*; @@ -215,4 +217,15 @@ public class Config { if (!optionNames.containsKey(option)) return; optionNames.get(option).addListener(runnable); } + + /** + * Register an action to a keybind + * + * @param keyBind The keybind + * @param runnable The code to be executed + */ + protected void registerKeyBind(OneKeyBind keyBind, Runnable runnable) { + keyBind.setRunnable(runnable); + KeyBindHandler.INSTANCE.addKeyBind(keyBind); + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/core/OneKeyBind.java b/src/main/java/cc/polyfrost/oneconfig/config/core/OneKeyBind.java index 50a26e0..791bc4a 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/core/OneKeyBind.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/core/OneKeyBind.java @@ -6,20 +6,15 @@ import java.util.ArrayList; public class OneKeyBind { protected final ArrayList<Integer> keyBinds = new ArrayList<>(); - protected transient final Runnable runnable; + protected transient Runnable runnable; protected transient boolean hasRun; - public OneKeyBind(Runnable runnable, int... keys) { - this.runnable = runnable; + public OneKeyBind(int... keys) { for (int key : keys) { keyBinds.add(key); } } - public OneKeyBind(int... keys) { - this(null, keys); - } - public boolean isActive() { if (keyBinds.size() == 0) return false; for (int keyBind : keyBinds) { @@ -57,4 +52,8 @@ public class OneKeyBind { public int getSize() { return keyBinds.size(); } + + public void setRunnable(Runnable runnable) { + this.runnable = runnable; + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigKeyBind.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigKeyBind.java index aa215a4..94224f1 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigKeyBind.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigKeyBind.java @@ -1,17 +1,16 @@ package cc.polyfrost.oneconfig.gui.elements.config; import cc.polyfrost.oneconfig.config.annotations.KeyBind; -import cc.polyfrost.oneconfig.internal.assets.Colors; import cc.polyfrost.oneconfig.config.core.OneKeyBind; import cc.polyfrost.oneconfig.config.elements.BasicOption; import cc.polyfrost.oneconfig.gui.OneConfigGui; import cc.polyfrost.oneconfig.gui.elements.BasicButton; -import cc.polyfrost.oneconfig.internal.config.core.KeyBindHandler; +import cc.polyfrost.oneconfig.internal.assets.Colors; +import cc.polyfrost.oneconfig.internal.assets.SVGs; +import cc.polyfrost.oneconfig.libs.universal.UKeyboard; import cc.polyfrost.oneconfig.renderer.RenderManager; import cc.polyfrost.oneconfig.renderer.font.Fonts; -import cc.polyfrost.oneconfig.internal.assets.SVGs; import cc.polyfrost.oneconfig.utils.color.ColorPalette; -import cc.polyfrost.oneconfig.libs.universal.UKeyboard; import java.lang.reflect.Field; @@ -23,7 +22,6 @@ public class ConfigKeyBind extends BasicOption { super(field, parent, name, category, subcategory, size); button = new BasicButton(256, 32, "", SVGs.KEYSTROKE, null, BasicButton.ALIGNMENT_JUSTIFIED, ColorPalette.SECONDARY); button.setToggleable(true); - KeyBindHandler.INSTANCE.addKeyBind(getKeyBind()); } public static ConfigKeyBind create(Field field, Object parent) { diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/config/Preferences.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/Preferences.java index 10c14a8..ffb4b62 100644 --- a/src/main/java/cc/polyfrost/oneconfig/internal/config/Preferences.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/Preferences.java @@ -17,9 +17,10 @@ public class Preferences extends InternalConfig { name = "OneConfig Keybind", size = 2 ) - public static OneKeyBind oneConfigKeyBind = new OneKeyBind(() -> GuiUtils.displayScreen(OneConfigGui.create()), UKeyboard.KEY_RSHIFT); + public static OneKeyBind oneConfigKeyBind = new OneKeyBind(UKeyboard.KEY_RSHIFT); public Preferences() { super("Preferences", "Preferences.json"); + registerKeyBind(oneConfigKeyBind, () -> GuiUtils.displayScreen(OneConfigGui.create())); } } diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java index 6829ae4..e285b51 100644 --- a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java +++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/KeyBindHandler.java @@ -18,7 +18,6 @@ public class KeyBindHandler { } public void addKeyBind(OneKeyBind keyBind) { - if (keyBind == null) return; keyBinds.add(keyBind); } |