aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuuz <6596629+Juuxel@users.noreply.github.com>2021-12-06 21:24:53 +0200
committerJuuz <6596629+Juuxel@users.noreply.github.com>2021-12-06 21:24:53 +0200
commit9bdbedd46a01f2ce6336ab055b7bebceb3ddeb5f (patch)
tree274d9c02e4bb6276ece40bd57387338ef14b54b3 /src
parent1b1b50a4bca47b88c7cb10a5a80c5d3e52e87091 (diff)
downloadLibGui-9bdbedd46a01f2ce6336ab055b7bebceb3ddeb5f.tar.gz
LibGui-9bdbedd46a01f2ce6336ab055b7bebceb3ddeb5f.tar.bz2
LibGui-9bdbedd46a01f2ce6336ab055b7bebceb3ddeb5f.zip
Add WAbstractSlider.updateValueCoordRatios, bump to 5.1.0
Fixes #145.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java
index 2af6092..0f057ca 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java
@@ -51,11 +51,17 @@ public abstract class WAbstractSlider extends WWidget {
/**
* A value:coordinate ratio. Used for converting user input into values.
+ *
+ * @see #coordToValueRatio
+ * @see #updateValueCoordRatios()
*/
protected float valueToCoordRatio;
/**
* A coordinate:value ratio. Used for rendering the thumb.
+ *
+ * @see #valueToCoordRatio
+ * @see #updateValueCoordRatios()n
*/
protected float coordToValueRatio;
@@ -80,7 +86,7 @@ public abstract class WAbstractSlider extends WWidget {
}
/**
- * @return the thumb size along the slider axis
+ * {@return the thumb size along the slider axis}
*/
protected abstract int getThumbWidth();
@@ -93,12 +99,22 @@ public abstract class WAbstractSlider extends WWidget {
*/
protected abstract boolean isMouseInsideBounds(int x, int y);
+ /**
+ * Updates {@link #coordToValueRatio} and {@link #valueToCoordRatio}.
+ * This method should be called whenever this widget resizes or changes it min/max value boundaries.
+ *
+ * @since 5.1.0
+ */
+ protected void updateValueCoordRatios() {
+ int trackHeight = (axis == Axis.HORIZONTAL ? getWidth() : getHeight()) - getThumbWidth();
+ valueToCoordRatio = (float) (max - min) / trackHeight;
+ coordToValueRatio = 1 / valueToCoordRatio;
+ }
+
@Override
public void setSize(int x, int y) {
super.setSize(x, y);
- int trackHeight = (axis == Axis.HORIZONTAL ? x : y) - getThumbWidth();
- valueToCoordRatio = (float) (max - min) / trackHeight;
- coordToValueRatio = 1 / valueToCoordRatio;
+ updateValueCoordRatios();
}
@Override
@@ -252,6 +268,7 @@ public abstract class WAbstractSlider extends WWidget {
public void setMinValue(int min) {
this.min = min;
+ updateValueCoordRatios();
if (this.value < min) {
this.value = min;
onValueChanged(this.value);
@@ -260,6 +277,7 @@ public abstract class WAbstractSlider extends WWidget {
public void setMaxValue(int max) {
this.max = max;
+ updateValueCoordRatios();
if (this.value > max) {
this.value = max;
onValueChanged(this.value);