aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
authorJuuxel <kasperi.kauppi@gmail.com>2019-09-01 10:17:21 +0300
committerJuuxel <kasperi.kauppi@gmail.com>2019-09-01 10:17:21 +0300
commit0d6f07bc2360ae83d4bc4348c3cad3ed10bac12b (patch)
tree754a43b13648581cc359a69b9019e596a2c00485 /src/main/java/io
parentadac0f68e48297cddabf9967e00166c03c201f5e (diff)
downloadLibGui-0d6f07bc2360ae83d4bc4348c3cad3ed10bac12b.tar.gz
LibGui-0d6f07bc2360ae83d4bc4348c3cad3ed10bac12b.tar.bz2
LibGui-0d6f07bc2360ae83d4bc4348c3cad3ed10bac12b.zip
Add scrolling support for sliders
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java51
1 files changed, 41 insertions, 10 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 aa5e78d..677686d 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
@@ -25,6 +25,11 @@ import java.util.function.IntConsumer;
* </ul>
*/
public abstract class WAbstractSlider extends WWidget {
+ /**
+ * The minimum time between two draggingFinished events caused by scrolling ({@link #onMouseScroll}).
+ */
+ private static final int SCROLLING_DRAGGING_FINISHED_RATE_LIMIT = 10;
+
protected final int min, max;
protected final Axis axis;
@@ -49,10 +54,12 @@ public abstract class WAbstractSlider extends WWidget {
/**
* True if there is a pending dragging finished event caused by the keyboard.
*/
- private boolean valueChangedWithKeys = false;
+ private boolean pendingDraggingFinishedFromKeyboard = false;
+ private int draggingFinishedFromScrollingTimer = 0;
+ private boolean pendingDraggingFinishedFromScrolling = false;
@Nullable private IntConsumer valueChangeListener = null;
- @Nullable private Runnable draggingFinishedListener = null;
+ @Nullable private IntConsumer draggingFinishedListener = null;
protected WAbstractSlider(int min, int max, Axis axis) {
if (max <= min)
@@ -116,7 +123,7 @@ public abstract class WAbstractSlider extends WWidget {
@Override
public void onClick(int x, int y, int button) {
moveSlider(x, y);
- if (draggingFinishedListener != null) draggingFinishedListener.run();
+ if (draggingFinishedListener != null) draggingFinishedListener.accept(value);
}
private void moveSlider(int x, int y) {
@@ -130,10 +137,34 @@ public abstract class WAbstractSlider extends WWidget {
@Override
public WWidget onMouseUp(int x, int y, int button) {
dragging = false;
- if (draggingFinishedListener != null) draggingFinishedListener.run();
+ if (draggingFinishedListener != null) draggingFinishedListener.accept(value);
return super.onMouseUp(x, y, button);
}
+ @Override
+ public void onMouseScroll(int x, int y, double amount) {
+ int previous = value;
+ value = MathHelper.clamp(value + (int) (valueToCoordRatio * amount * 2), min, max);
+
+ if (previous != value) {
+ onValueChanged(value);
+ pendingDraggingFinishedFromScrolling = true;
+ }
+ }
+
+ @Override
+ public void tick() {
+ if (draggingFinishedFromScrollingTimer > 0) {
+ draggingFinishedFromScrollingTimer--;
+ }
+
+ if (pendingDraggingFinishedFromScrolling && draggingFinishedFromScrollingTimer <= 0) {
+ if (draggingFinishedListener != null) draggingFinishedListener.accept(value);
+ pendingDraggingFinishedFromScrolling = false;
+ draggingFinishedFromScrollingTimer = SCROLLING_DRAGGING_FINISHED_RATE_LIMIT;
+ }
+ }
+
public int getValue() {
return value;
}
@@ -153,11 +184,11 @@ public abstract class WAbstractSlider extends WWidget {
}
@Nullable
- public Runnable getDraggingFinishedListener() {
+ public IntConsumer getDraggingFinishedListener() {
return draggingFinishedListener;
}
- public void setDraggingFinishedListener(@Nullable Runnable draggingFinishedListener) {
+ public void setDraggingFinishedListener(@Nullable IntConsumer draggingFinishedListener) {
this.draggingFinishedListener = draggingFinishedListener;
}
@@ -200,15 +231,15 @@ public abstract class WAbstractSlider extends WWidget {
if (valueChanged) {
onValueChanged(value);
- valueChangedWithKeys = true;
+ pendingDraggingFinishedFromKeyboard = true;
}
}
@Override
public void onKeyReleased(int ch, int key, int modifiers) {
- if (valueChangedWithKeys && (isDecreasingKey(ch) || isIncreasingKey(ch))) {
- if (draggingFinishedListener != null) draggingFinishedListener.run();
- valueChangedWithKeys = false;
+ if (pendingDraggingFinishedFromKeyboard && (isDecreasingKey(ch) || isIncreasingKey(ch))) {
+ if (draggingFinishedListener != null) draggingFinishedListener.accept(value);
+ pendingDraggingFinishedFromKeyboard = false;
}
}