diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-05-11 17:54:19 +0200 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-05-11 17:54:19 +0200 |
commit | b239915ac1e4b2b51b42d2c2fe163dffcdd7ae4f (patch) | |
tree | 8a1d31cb79da3a1626596f70e014b395b45b6178 /src | |
parent | 333b4099972260d0e348ec20f8a00a430471aac8 (diff) | |
download | OneConfig-b239915ac1e4b2b51b42d2c2fe163dffcdd7ae4f.tar.gz OneConfig-b239915ac1e4b2b51b42d2c2fe163dffcdd7ae4f.tar.bz2 OneConfig-b239915ac1e4b2b51b42d2c2fe163dffcdd7ae4f.zip |
keybinds
Diffstat (limited to 'src')
10 files changed, 173 insertions, 9 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/core/OneKeyBind.java b/src/main/java/cc/polyfrost/oneconfig/config/core/OneKeyBind.java new file mode 100644 index 0000000..f39e1c7 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/core/OneKeyBind.java @@ -0,0 +1,47 @@ +package cc.polyfrost.oneconfig.config.core; + +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.InputEvent; +import org.lwjgl.input.Keyboard; + +import java.util.ArrayList; + +public class OneKeyBind { + private final ArrayList<Integer> keyBinds = new ArrayList<>(); + + public OneKeyBind(int... keys) { + for (int key : keys) { + keyBinds.add(key); + } + } + + public boolean isActive() { + if (keyBinds.size() == 0) return false; + for (int keyBind : keyBinds) { + if (!Keyboard.isKeyDown(keyBind)) return false; + } + return true; + } + + public String getDisplay() { + StringBuilder sb = new StringBuilder(); + for (int keyBind : keyBinds) { + if (sb.length() != 0) sb.append(" + "); + sb.append(Keyboard.getKeyName(keyBind)); + } + return sb.toString().trim(); + } + + public void addKey(int key) { + if (!keyBinds.contains(key)) keyBinds.add(key); + } + + public void clearKeys() { + keyBinds.clear(); + } + + public int getSize() { + return keyBinds.size(); + } +} 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 6fe5733..36351f6 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/data/OptionType.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/data/OptionType.java @@ -34,7 +34,6 @@ public enum OptionType { * Type: int */ DROPDOWN, - //MULTI_DROPDOWN, /** * Type: doesn't matter */ @@ -46,5 +45,9 @@ public enum OptionType { /** * Type: runnable */ - BUTTON + BUTTON, + /** + * Type: OneKeyBind + */ + KEYBIND, } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/interfaces/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/interfaces/Config.java index 9eecc52..32853f8 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/interfaces/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/interfaces/Config.java @@ -148,6 +148,9 @@ public class Config { case BUTTON: options.add(new ConfigButton(field, option.name(), option.size(), option.buttonText())); break; + case KEYBIND: + options.add(new ConfigKeyBind(field, option.name(), option.size())); + break; } } } diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java index ae70758..940c4cb 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/OneConfigGui.java @@ -41,6 +41,7 @@ public class OneConfigGui extends GuiScreen { public boolean mouseDown; private float scale = 1f; public static OneConfigGui instanceToRestore = null; + public boolean allowClose = true; public OneConfigGui() { INSTANCE = this; @@ -174,7 +175,7 @@ public class OneConfigGui extends GuiScreen { protected void keyTyped(char key, int keyCode) { Keyboard.enableRepeatEvents(true); try { - super.keyTyped(key, keyCode); + if (allowClose) super.keyTyped(key, keyCode); textInputField.keyTyped(key, keyCode); if (currentColorSelector != null) currentColorSelector.keyTyped(key, keyCode); currentPage.keyTyped(key, keyCode); diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java index bd2521a..f705d0b 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicButton.java @@ -10,8 +10,6 @@ import cc.polyfrost.oneconfig.utils.ColorUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.awt.*; - public class BasicButton extends BasicElement { protected String text; @@ -25,6 +23,7 @@ public class BasicButton extends BasicElement { private boolean toggleable; private Page page; private Runnable runnable; + private boolean alignIconLeft = false; /** * Create a new basic button. Used mostly on the homepage and the sidebar. Note: The button will not be drawn until you call {@link #draw(long, int, int)}. @@ -79,7 +78,7 @@ public class BasicButton extends BasicElement { int textColor = -1; RenderManager.drawRectangle(vg, x, y, this.width, this.height, this.currentColor); float contentWidth = RenderManager.getTextWidth(vg, text, fontSize, Fonts.MEDIUM); - if (fileNameLeftIco != null) { + if (fileNameLeftIco != null && !alignIconLeft) { contentWidth += 28; } if (fileNameRightIco != null) { @@ -95,9 +94,13 @@ public class BasicButton extends BasicElement { if (thisAlignment == ALIGNMENT_CENTER) { int middle = x + this.width / 2; - RenderManager.drawString(vg, text, middle - contentWidth / 2 + (fileNameLeftIco != null ? 28 : 0), y + ((float) height / 2) + 1, textColor, fontSize, Fonts.MEDIUM); + if (alignIconLeft) + RenderManager.drawString(vg, text, middle - contentWidth / 2, y + ((float) height / 2) + 1, textColor, fontSize, Fonts.MEDIUM); + else + RenderManager.drawString(vg, text, middle - contentWidth / 2 + (fileNameLeftIco != null ? 28 : 0), y + ((float) height / 2) + 1, textColor, fontSize, Fonts.MEDIUM); if (fileNameLeftIco != null) { - RenderManager.drawImage(vg, fileNameLeftIco, middle - contentWidth / 2, y + 8, 20, 20); + if (alignIconLeft) RenderManager.drawImage(vg, fileNameLeftIco, x + 12, y + height / 2f - 10, 20, 20); + else RenderManager.drawImage(vg, fileNameLeftIco, middle - contentWidth / 2, y + 8, 20, 20); } if (fileNameRightIco != null) { RenderManager.drawImage(vg, fileNameRightIco, middle + contentWidth / 2 - (fileNameLeftIco != null ? 20 : 24), y + 8, 20, 20); @@ -153,4 +156,12 @@ public class BasicButton extends BasicElement { public String getText() { return text; } + + public void setText(String text) { + this.text = text; + } + + public void alignIconLeft(boolean value) { + alignIconLeft = value; + } } 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 new file mode 100644 index 0000000..809143f --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigKeyBind.java @@ -0,0 +1,81 @@ +package cc.polyfrost.oneconfig.gui.elements.config; + +import cc.polyfrost.oneconfig.config.OneConfigConfig; +import cc.polyfrost.oneconfig.config.core.OneKeyBind; +import cc.polyfrost.oneconfig.config.interfaces.BasicOption; +import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.gui.elements.BasicButton; +import cc.polyfrost.oneconfig.lwjgl.RenderManager; +import cc.polyfrost.oneconfig.lwjgl.font.Fonts; +import cc.polyfrost.oneconfig.lwjgl.image.Images; +import org.lwjgl.input.Keyboard; + +import java.lang.reflect.Field; + +public class ConfigKeyBind extends BasicOption { + private final BasicButton button; + private boolean clicked = false; + + public ConfigKeyBind(Field field, String name, int size) { + super(field, name, size); + button = new BasicButton(256, 32, "", Images.KEYSTROKE, null, 0, BasicButton.ALIGNMENT_CENTER, true); + button.alignIconLeft(true); + } + + @Override + public void draw(long vg, int x, int y) { + RenderManager.drawString(vg, name, x, y + 17, OneConfigConfig.WHITE, 14f, Fonts.MEDIUM); + OneKeyBind keyBind = getKeyBind(); + String text = keyBind.getDisplay(); + if (button.isToggled()) { + if (text.equals("")) text = "Recording... (ESC to clear)"; + if (!clicked) { + keyBind.clearKeys(); + setKeyBind(keyBind); + clicked = true; + } else if (keyBind.getSize() == 0 || keyBind.isActive()) { + OneConfigGui.INSTANCE.allowClose = false; + } else { + button.setToggled(false); + clicked = false; + OneConfigGui.INSTANCE.allowClose = true; + } + } else if (text.equals("")) text = "None"; + button.setText(text); + button.draw(vg, x + (size == 1 ? 224 : 736), y); + } + + @Override + public void keyTyped(char key, int keyCode) { + if (!button.isToggled()) return; + OneKeyBind keyBind = getKeyBind(); + if (keyCode == Keyboard.KEY_ESCAPE) { + keyBind.clearKeys(); + button.setToggled(false); + OneConfigGui.INSTANCE.allowClose = true; + clicked = false; + } else keyBind.addKey(keyCode); + setKeyBind(keyBind); + } + + private OneKeyBind getKeyBind() { + OneKeyBind keyBind = new OneKeyBind(); + try { + keyBind = (OneKeyBind) get(); + } catch (IllegalAccessException ignored) { + } + return keyBind; + } + + private void setKeyBind(OneKeyBind keyBind) { + try { + set(keyBind); + } catch (IllegalAccessException ignored) { + } + } + + @Override + public int getHeight() { + return 32; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/Images.java b/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/Images.java index fd2fc3c..8cb2804 100644 --- a/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/Images.java +++ b/src/main/java/cc/polyfrost/oneconfig/lwjgl/image/Images.java @@ -11,6 +11,7 @@ public enum Images { FAVORITE_OFF("/assets/oneconfig/icons/favorite_inactive.png"), HIDE_EYE("/assets/oneconfig/icons/hide_eye.png"), HIDE_EYE_OFF("/assets/oneconfig/icons/hide_eye_off.png"), + KEYSTROKE("/assets/oneconfig/icons/keystroke.png"), // TODO color picker ones COLOR_BASE("/assets/oneconfig/colorui/color_base.png"), diff --git a/src/main/java/cc/polyfrost/oneconfig/test/TestConfig.java b/src/main/java/cc/polyfrost/oneconfig/test/TestConfig.java index d314354..439c51d 100644 --- a/src/main/java/cc/polyfrost/oneconfig/test/TestConfig.java +++ b/src/main/java/cc/polyfrost/oneconfig/test/TestConfig.java @@ -2,6 +2,7 @@ package cc.polyfrost.oneconfig.test; import cc.polyfrost.oneconfig.config.annotations.ConfigPage; import cc.polyfrost.oneconfig.config.annotations.Option; +import cc.polyfrost.oneconfig.config.core.OneKeyBind; import cc.polyfrost.oneconfig.config.data.*; import cc.polyfrost.oneconfig.config.interfaces.Config; import cc.polyfrost.oneconfig.lwjgl.OneColor; @@ -19,6 +20,21 @@ public class TestConfig extends Config { public static boolean ignored; @Option( + name = "Keybind (1x)", + subcategory = "Test", + type = OptionType.KEYBIND + ) + public static OneKeyBind oneKeyBind = new OneKeyBind( 18, 80); + + @Option( + name = "Keybind (2x)", + subcategory = "Test", + type = OptionType.KEYBIND, + size = 2 + ) + public static OneKeyBind oneKeyBind2 = new OneKeyBind(27, 80); + + @Option( name = "Crash game", subcategory = "Test", type = OptionType.BUTTON, diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java index 30f393e..d17d716 100644 --- a/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/utils/ColorUtils.java @@ -37,7 +37,8 @@ public class ColorUtils { return getColorComponents(color, splitColor(OneConfigConfig.BLUE_600), splitColor(OneConfigConfig.BLUE_500), hover, 150f); case 2: return getColorComponents(color, splitColor(OneConfigConfig.GRAY_500), splitColor(OneConfigConfig.GRAY_300), hover, 50f); - + case 3: + return getColorComponents(color, splitColor(OneConfigConfig.GRAY_500), splitColor(OneConfigConfig.GRAY_300), hover, 25f); } } diff --git a/src/main/resources/assets/oneconfig/icons/keystroke.png b/src/main/resources/assets/oneconfig/icons/keystroke.png Binary files differnew file mode 100644 index 0000000..6556550 --- /dev/null +++ b/src/main/resources/assets/oneconfig/icons/keystroke.png |