diff options
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/gui')
3 files changed, 99 insertions, 6 deletions
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; + } +} |