aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java11
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/gui/elements/config/ConfigSlider.java168
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/utils/MathUtils.java13
3 files changed, 71 insertions, 121 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java
index 80eec9b..84fda67 100644
--- a/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java
+++ b/src/main/java/cc/polyfrost/oneconfig/gui/elements/BasicElement.java
@@ -40,7 +40,7 @@ public class BasicElement {
}
public void update(int x, int y) {
- if(disabled) {
+ if (disabled) {
hovered = false;
clicked = false;
return;
@@ -48,11 +48,9 @@ public class BasicElement {
hovered = InputUtils.isAreaHovered(x - hitBoxX, y - hitBoxY, width + hitBoxX, height + hitBoxY);
clicked = InputUtils.isClicked() && hovered;
- if (hovered) {
- if (clicked) {
- toggled = !toggled;
- onClick();
- }
+ if (clicked) {
+ toggled = !toggled;
+ onClick();
}
}
@@ -101,6 +99,7 @@ public class BasicElement {
public boolean isDisabled() {
return disabled;
}
+
public void disable(boolean state) {
disabled = state;
}
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 69cb887..9a0050a 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
@@ -16,169 +16,115 @@ import org.lwjgl.nanovg.NanoVG;
import java.lang.reflect.Field;
public class ConfigSlider extends BasicOption {
- private final BasicElement slideYBoi = new BasicElement(24, 24, false);
private final TextInputField inputField = new TextInputField(84, 24, "", false, false);
private final BasicElement upArrow = new BasicElement(12, 14, false);
private final BasicElement downArrow = new BasicElement(12, 14, false);
private final float min, max;
- private int steps = 0;
- private int colorTop, colorBottom;
private boolean isFloat = true;
- private Float prevAsNum = null;
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;
- if (step > 0) {
- steps = (int) ((max - min) / step);
- }
- slideYBoi.setCustomHitbox(28, 8);
inputField.onlyAcceptNumbers(true);
inputField.setCentered(true);
}
@Override
- public int getHeight() {
- return 32;
- }
-
- @Override
public void draw(long vg, int x, int y) {
- float value = 0;
- try {
- Object object = get();
- if (object instanceof Integer)
- isFloat = false;
- if (isFloat) value = (float) object;
- else value = (int) object;
- if (prevAsNum == null) prevAsNum = value;
- } catch (IllegalAccessException ignored) {
+ int xCoordinate = 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()) {
+ dragging = false;
+ if (step > 0) xCoordinate = getStepCoordinate(xCoordinate, x);
+ setValue(MathUtils.map(xCoordinate, x + 352, x + 864, min, max));
}
- float current = MathUtils.clamp((value - min) / (max - min));
- float currentAsNum = current * (max - min) + min;
- if (!inputField.isToggled()) inputField.setInput(String.format("%.01f", currentAsNum));
- inputField.setErrored(false);
- if (inputField.isToggled()) {
+ float value = 0;
+ if (!dragging) {
try {
- float input = Float.parseFloat(inputField.getInput());
- if (input < min) {
- inputField.setErrored(true);
- input = min;
- }
- if (input > max) {
- inputField.setErrored(true);
- input = max;
- }
- if (steps == 0) {
- current = MathUtils.clamp((input - min) / (max - min));
- } else {
- current = toNearestStep(MathUtils.clamp((input - min) / (max - min)));
- }
- } catch (NumberFormatException ignored) {
- inputField.setErrored(true);
+ Object object = get();
+ if (object instanceof Integer)
+ isFloat = false;
+ if (isFloat) value = (float) object;
+ else value = (int) object;
+ xCoordinate = (int) MathUtils.map(value, min, max, x + 352, x + 864);
+ } catch (IllegalAccessException ignored) {
}
}
- inputField.draw(vg, x + 892, y);
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);
- slideYBoi.update(x + 340 + (int) (current * 512), y + 4);
- if (steps != 0) {
- for (float i = 0; i <= 1.005f; i += 1f / steps) { // sometimes it's just more than 1, so we add a little
- int color = current > i ? OneConfigConfig.BLUE_500 : OneConfigConfig.GRAY_300;
- RenderManager.drawRoundedRect(vg, x + 351 + (int) (i * 512), y + 9, 4, 14, color, 2f);
+ RenderManager.drawRoundedRect(vg, x + 352, y + 13, xCoordinate - x - 352, 6, OneConfigConfig.BLUE_500, 4f);
+ if (step > 0) {
+ for (float i = x + 352; i <= x + 864; i += 512 / ((max - min) / step)) {
+ int color = xCoordinate > i - 2 ? OneConfigConfig.BLUE_500 : OneConfigConfig.GRAY_300;
+ RenderManager.drawRoundedRect(vg, i - 2, y + 9, 4, 14, color, 2f);
}
}
- RenderManager.drawRoundedRect(vg, x + 352, y + 13, (int) (current * 512), 6, OneConfigConfig.BLUE_500, 4f);
- if (steps == 0)
- RenderManager.drawRoundedRect(vg, x + 340 + (int) (current * 512), y + 4, 24, 24, OneConfigConfig.WHITE, 12f);
- else
- RenderManager.drawRoundedRect(vg, x + 346 + (int) (current * 512), y + 4, 8, 24, OneConfigConfig.WHITE, 4f);
-
- int mouseX = InputUtils.mouseX() - (x + 352);
- if (InputUtils.isAreaClicked(x + 332, y + 9, 542, 10) && !slideYBoi.isHovered()) {
- if (steps == 0) {
- current = MathUtils.clamp(mouseX / 512f);
- } else current = MathUtils.clamp(toNearestStep(mouseX / 512f));
- }
- if (slideYBoi.isHovered() && Mouse.isButtonDown(0)) {
- if (steps == 0) {
- current = MathUtils.clamp(mouseX / 512f);
- } else current = MathUtils.clamp(toNearestStep(mouseX / 512f));
- }
- currentAsNum = current * (max - min) + min;
+ if (step == 0)
+ RenderManager.drawRoundedRect(vg, xCoordinate - 12, y + 4, 24, 24, OneConfigConfig.WHITE, 12f);
+ else RenderManager.drawRoundedRect(vg, xCoordinate - 4, y + 4, 8, 24, OneConfigConfig.WHITE, 4f);
+ upArrow.update(x + 980, y);
+ downArrow.update(x + 980, y + 14);
+ inputField.draw(vg, x + 892, y);
- RenderManager.drawRoundedRect(vg, x + 980, y, 12, 28, OneConfigConfig.GRAY_500, 6f);
upArrow.update(x + 980, y);
downArrow.update(x + 980, y + 14);
- if (current == 1f) colorTop = OneConfigConfig.GRAY_500_80;
- if (current == 0f) colorBottom = OneConfigConfig.GRAY_500_80;
+ if (xCoordinate == x + 864) colorTop = OneConfigConfig.GRAY_500_80;
+ if (xCoordinate == x + 452) 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()) {
- currentAsNum += step == 0 ? 1 : step;
- current = MathUtils.clamp((currentAsNum - min) / (max - min));
- }
- if (downArrow.isClicked()) {
- currentAsNum -= step == 0 ? 1 : step;
- current = MathUtils.clamp((currentAsNum - min) / (max - min));
- }
- if (current == 1f) NanoVG.nvgGlobalAlpha(vg, 0.3f);
+ if (upArrow.isClicked()) setValue(MathUtils.clamp(value + (step > 0 ? 1 : step), min, max));
+ if (downArrow.isClicked()) setValue(MathUtils.clamp(value - (step > 0 ? 1 : step), min, max));
+ //if (current == 1f) NanoVG.nvgGlobalAlpha(vg, 0.3f);
RenderManager.drawRoundedRectVaried(vg, x + 980, y, 12, 14, colorTop, 6f, 6f, 0f, 0f);
RenderManager.drawImage(vg, Images.UP_ARROW, x + 981, y + 2, 10, 10);
- if (current == 1f) NanoVG.nvgGlobalAlpha(vg, 1f);
+ //if (current == 1f) NanoVG.nvgGlobalAlpha(vg, 1f);
- if (current == 0f) NanoVG.nvgGlobalAlpha(vg, 0.3f);
+ //if (current == 0f) NanoVG.nvgGlobalAlpha(vg, 0.3f);
RenderManager.drawRoundedRectVaried(vg, x + 980, y + 14, 12, 14, colorBottom, 0f, 0f, 6f, 6f);
NanoVG.nvgTranslate(vg, x + 991, 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);
+ }
- if (currentAsNum != prevAsNum) {
- try {
- if (isFloat) set(currentAsNum);
- else set(Math.round(currentAsNum));
- } catch (IllegalAccessException ignored) {
- }
- prevAsNum = currentAsNum;
+ private int getStepCoordinate(int xCoordinate, int x) {
+ Integer nearest = null;
+ for (float i = x + 352; i <= x + 864; i += 512 / ((max - min) / step)) {
+ if (nearest == null || Math.abs(xCoordinate - i) < Math.abs(xCoordinate - nearest)) nearest = (int) i;
}
+ return nearest == null ? 0 : nearest;
}
- private float toNearestStep(float input) {
- float stepF = 1f / steps;
- float stepAbove = 1f, stepBelow = 0f;
- for (float a = 0f; a <= 1f; a += stepF) {
- if (a > input) {
- stepAbove = a;
- break;
- }
- }
- for (float a = 1f; a >= 0f; a -= stepF) {
- if (a <= input) {
- stepBelow = a;
- break;
- }
- }
- if (stepAbove - input > input - stepBelow) {
- return stepBelow;
- } else {
- return stepAbove;
+ private void setValue(float value) {
+ try {
+ if (isFloat) set(value);
+ else set(Math.round(value));
+ } catch (IllegalAccessException ignored) {
}
}
@Override
- public boolean hasHalfSize() {
- return false;
+ public void keyTyped(char key, int keyCode) {
+ inputField.keyTyped(key, keyCode);
}
@Override
- public void keyTyped(char key, int keyCode) {
- inputField.keyTyped(key, keyCode);
+ public int getHeight() {
+ return 32;
+ }
+
+ @Override
+ public boolean hasHalfSize() {
+ return false;
}
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/utils/MathUtils.java b/src/main/java/cc/polyfrost/oneconfig/utils/MathUtils.java
index 7eb4944..e30c227 100644
--- a/src/main/java/cc/polyfrost/oneconfig/utils/MathUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/utils/MathUtils.java
@@ -2,18 +2,21 @@ package cc.polyfrost.oneconfig.utils;
public class MathUtils {
public static float clamp(float number) {
- return number < (float) 0.0 ? (float) 0.0 : Math.min(number, (float) 1.0);
+ return number < 0f ? 0f : Math.min(number, 1f);
+ }
+
+ public static float clamp(float number, float min, float max) {
+ return number < min ? min : Math.min(number, max);
}
public static float easeOut(float current, float goal, float speed) {
- if (Math.floor(Math.abs(goal - current) * Math.abs(current - goal) * 3) > 0) {
+ if (Math.round(Math.abs(goal - current) * 25) > 0) {
return current + (goal - current) / speed;
} else {
return goal;
}
}
-
public static float easeInQuad(float current) {
return current * current;
}
@@ -26,5 +29,7 @@ public class MathUtils {
return c / 2 * ((float) Math.sqrt(1 - (t -= 2) * t) + 1) + b;
}
-
+ public static float map(float value, float start1, float stop1, float start2, float stop2) {
+ return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
+ }
}