diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-05-06 20:07:44 +0200 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-05-06 20:07:44 +0200 |
commit | 1cfdef78d7f51f1d6e50dec4a16ac9dfe5f61a58 (patch) | |
tree | fede402dbca2277aef2d81c703265f3e8b8f83a0 /src/main/java/cc/polyfrost/oneconfig/gui | |
parent | a2c26edde80c87c4d2a45cd315c3753e303044b1 (diff) | |
download | OneConfig-1cfdef78d7f51f1d6e50dec4a16ac9dfe5f61a58.tar.gz OneConfig-1cfdef78d7f51f1d6e50dec4a16ac9dfe5f61a58.tar.bz2 OneConfig-1cfdef78d7f51f1d6e50dec4a16ac9dfe5f61a58.zip |
slider rewrite
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/gui')
3 files changed, 61 insertions, 52 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigSlider.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigSlider.java index 2e6baae..abae368 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigSlider.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigSlider.java @@ -2,8 +2,7 @@ package cc.polyfrost.oneconfig.gui.elements.config; import cc.polyfrost.oneconfig.config.OneConfigConfig; import cc.polyfrost.oneconfig.config.interfaces.BasicOption; -import cc.polyfrost.oneconfig.gui.elements.BasicElement; -import cc.polyfrost.oneconfig.gui.elements.text.TextInputField; +import cc.polyfrost.oneconfig.gui.elements.text.NumberInputField; import cc.polyfrost.oneconfig.lwjgl.RenderManager; import cc.polyfrost.oneconfig.lwjgl.font.Fonts; import cc.polyfrost.oneconfig.utils.InputUtils; @@ -13,36 +12,43 @@ import org.lwjgl.input.Mouse; import java.lang.reflect.Field; public class ConfigSlider extends BasicOption { - private final TextInputField inputField = new TextInputField(84, 24, "", false, false); + private final NumberInputField inputField; private final float min, max; private boolean isFloat = true; private final int step; private boolean dragging = false; - private int colorTop, colorBottom; public ConfigSlider(Field field, String name, int size, float min, float max, int step) { super(field, name, size); this.min = min; this.max = max; this.step = step; - inputField.onlyAcceptNumbers(true); - inputField.setCentered(true); + inputField = new NumberInputField(84, 24, 0, min, max, step); } @Override public void draw(long vg, int x, int y) { int xCoordinate = 0; + float value = 0; boolean hovered = InputUtils.isAreaHovered(x + 352, y, 512, 32); if (hovered && Mouse.isButtonDown(0)) dragging = true; - if (dragging) xCoordinate = (int) MathUtils.clamp(InputUtils.mouseX(), x + 352, x + 864); - if (dragging && InputUtils.isClicked()) { + if (dragging) { + xCoordinate = (int) MathUtils.clamp(InputUtils.mouseX(), x + 352, x + 864); + value = MathUtils.map(xCoordinate, x + 352, x + 864, min, max); + } else if (inputField.isToggled()) { + value = inputField.getCurrentValue(); + xCoordinate = (int) MathUtils.map(value, min, max, x + 352, x + 864); + } + if (dragging && InputUtils.isClicked() || inputField.isToggled()) { dragging = false; - if (step > 0) xCoordinate = getStepCoordinate(xCoordinate, x); - setValue(MathUtils.map(xCoordinate, x + 352, x + 864, min, max)); + if (step > 0) { + xCoordinate = getStepCoordinate(xCoordinate, x); + value = MathUtils.map(xCoordinate, x + 352, x + 864, min, max); + } + setValue(value); } - float value = 0; - if (!dragging) { + if (!dragging && !inputField.isToggled()) { try { Object object = get(); if (object instanceof Integer) @@ -53,6 +59,7 @@ public class ConfigSlider extends BasicOption { } catch (IllegalAccessException ignored) { } } + if (!inputField.isToggled()) inputField.setCurrentValue(value); RenderManager.drawString(vg, name, x, y + 17, OneConfigConfig.WHITE_90, 14f, Fonts.INTER_MEDIUM); RenderManager.drawRoundedRect(vg, x + 352, y + 13, 512, 6, OneConfigConfig.GRAY_300, 4f); diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/NumberInputField.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/NumberInputField.java index 0e28a90..1eefe74 100644 --- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/NumberInputField.java +++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/text/NumberInputField.java @@ -33,49 +33,50 @@ public class NumberInputField extends TextInputField { downArrow.update(x + width + 4, y + 14); try { current = Float.parseFloat(input); - if (current < min || current > max) { - super.errored = true; - } else { - upArrow.disable(false); - downArrow.disable(false); - } - if (current == max) colorTop = OneConfigConfig.GRAY_500_80; - if (current == min) colorBottom = OneConfigConfig.GRAY_500_80; - - colorTop = ColorUtils.getColor(colorTop, 2, upArrow.isHovered(), upArrow.isClicked()); - colorBottom = ColorUtils.getColor(colorBottom, 2, downArrow.isHovered(), downArrow.isClicked()); - if (upArrow.isClicked()) { - current += step; - if (current > max) current = max; - setCurrentValue(current); - } - if (downArrow.isClicked()) { - current -= step; - if(current < min) current = min; - setCurrentValue(current); - } - if (current >= max) { - NanoVG.nvgGlobalAlpha(vg, 0.3f); - upArrow.disable(true); - } - RenderManager.drawRoundedRectVaried(vg, x + width + 4, y, 12, 14, colorTop, 6f, 6f, 0f, 0f); - RenderManager.drawImage(vg, Images.UP_ARROW, x + width + 5, y + 2, 10, 10); - if (current >= max) NanoVG.nvgGlobalAlpha(vg, 1f); - - if (current <= min) { - NanoVG.nvgGlobalAlpha(vg, 0.3f); - downArrow.disable(true); - } - RenderManager.drawRoundedRectVaried(vg, x + width + 4, y + 14, 12, 14, colorBottom, 0f, 0f, 6f, 6f); - NanoVG.nvgTranslate(vg, x + width + 15, y + 25); - NanoVG.nvgRotate(vg, (float) Math.toRadians(180)); - RenderManager.drawImage(vg, Images.UP_ARROW, 0, 0, 10, 10); - NanoVG.nvgResetTransform(vg); - NanoVG.nvgGlobalAlpha(vg, 1f); + } catch (NumberFormatException e) { + super.errored = true; + } - } catch (Exception e) { + if (current < min || current > max) { super.errored = true; + } else { + upArrow.disable(false); + downArrow.disable(false); + } + if (current == max) colorTop = OneConfigConfig.GRAY_500_80; + if (current == min) colorBottom = OneConfigConfig.GRAY_500_80; + + colorTop = ColorUtils.getColor(colorTop, 2, upArrow.isHovered(), upArrow.isClicked()); + colorBottom = ColorUtils.getColor(colorBottom, 2, downArrow.isHovered(), downArrow.isClicked()); + if (upArrow.isClicked()) { + current += step; + if (current > max) current = max; + setCurrentValue(current); } + if (downArrow.isClicked()) { + current -= step; + if (current < min) current = min; + setCurrentValue(current); + } + if (current >= max) { + NanoVG.nvgGlobalAlpha(vg, 0.3f); + upArrow.disable(true); + } + RenderManager.drawRoundedRectVaried(vg, x + width + 4, y, 12, 14, colorTop, 6f, 6f, 0f, 0f); + RenderManager.drawImage(vg, Images.UP_ARROW, x + width + 5, y + 2, 10, 10); + if (current >= max) NanoVG.nvgGlobalAlpha(vg, 1f); + + if (current <= min) { + NanoVG.nvgGlobalAlpha(vg, 0.3f); + downArrow.disable(true); + } + RenderManager.drawRoundedRectVaried(vg, x + width + 4, y + 14, 12, 14, colorBottom, 0f, 0f, 6f, 6f); + NanoVG.nvgTranslate(vg, x + width + 15, y + 25); + NanoVG.nvgRotate(vg, (float) Math.toRadians(180)); + RenderManager.drawImage(vg, Images.UP_ARROW, 0, 0, 10, 10); + NanoVG.nvgResetTransform(vg); + NanoVG.nvgGlobalAlpha(vg, 1f); + try { super.draw(vg, x, y - 2); } catch (Exception e) { 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 6983ae8..2ad851b 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 @@ -96,6 +96,7 @@ public class TextInputField extends BasicElement { } int color = toggled ? OneConfigConfig.WHITE : OneConfigConfig.WHITE_60; if (!toggled) caretPos = input.length(); + if (caretPos > input.length()) caretPos = input.length(); float width; StringBuilder s = new StringBuilder(); if (!password) { |