diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-08 20:46:20 +0300 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-08 20:46:20 +0300 |
commit | 5f415d444efd70fb5747395fe913ace3d89e33cb (patch) | |
tree | dbb0d412de159013469ffc5ce1a18df4fa479467 | |
parent | 6134cc78ea2452e9d133258dfcf5bc634148d576 (diff) | |
download | LibGui-5f415d444efd70fb5747395fe913ace3d89e33cb.tar.gz LibGui-5f415d444efd70fb5747395fe913ace3d89e33cb.tar.bz2 LibGui-5f415d444efd70fb5747395fe913ace3d89e33cb.zip |
Add slider directions, fix scroll bars not scrolling properly
4 files changed, 76 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 b5e1344..7a2ccec 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 @@ -34,6 +34,7 @@ public abstract class WAbstractSlider extends WWidget { protected int min, max; protected final Axis axis; + protected Direction direction; protected int value; @@ -71,6 +72,7 @@ public abstract class WAbstractSlider extends WWidget { this.max = max; this.axis = axis; this.value = min; + this.direction = (axis == Axis.HORIZONTAL) ? Direction.RIGHT : Direction.UP; } /** @@ -132,7 +134,25 @@ public abstract class WAbstractSlider extends WWidget { } private void moveSlider(int x, int y) { - int pos = (axis == Axis.VERTICAL ? (height - y) : x) - getThumbWidth() / 2; + int axisPos; + + switch (direction) { + case UP: + axisPos = height - y; + break; + case DOWN: + axisPos = y; + break; + case LEFT: + axisPos = x; + break; + case RIGHT: + default: + axisPos = width - x; + break; + } + + int pos = axisPos - getThumbWidth() / 2; int rawValue = min + Math.round(valueToCoordRatio * pos); int previousValue = value; value = MathHelper.clamp(rawValue, min, max); @@ -246,6 +266,31 @@ public abstract class WAbstractSlider extends WWidget { return axis; } + /** + * Gets the direction of this slider. + * + * @return the direction + * @since 2.0.0 + */ + public Direction getDirection() { + return direction; + } + + /** + * Sets the direction of this slider. + * + * @param direction the new direction + * @throws IllegalArgumentException if the {@linkplain Direction#getAxis() direction axis} is not equal to {@link #axis}. + * @since 2.0.0 + */ + public void setDirection(Direction direction) { + if (direction.getAxis() != axis) { + throw new IllegalArgumentException("Incorrect axis: " + axis); + } + + this.direction = direction; + } + protected void onValueChanged(int value) { if (valueChangeListener != null) valueChangeListener.accept(value); } @@ -294,4 +339,21 @@ public abstract class WAbstractSlider extends WWidget { private static boolean isIncreasingKey(int ch) { return ch == GLFW.GLFW_KEY_RIGHT || ch == GLFW.GLFW_KEY_UP; } + + public enum Direction { + UP(Axis.VERTICAL), + DOWN(Axis.VERTICAL), + LEFT(Axis.HORIZONTAL), + RIGHT(Axis.HORIZONTAL); + + private final Axis axis; + + Direction(Axis axis) { + this.axis = axis; + } + + public Axis getAxis() { + return axis; + } + } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java index 3d14674..2ff9f68 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java @@ -163,7 +163,9 @@ public class WLabeledSlider extends WAbstractSlider { public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { int aWidth = axis == Axis.HORIZONTAL ? width : height; int aHeight = axis == Axis.HORIZONTAL ? height : width; - int rotMouseX = axis == Axis.HORIZONTAL ? mouseX : (height - mouseY); + int rotMouseX = axis == Axis.HORIZONTAL + ? (direction == Direction.RIGHT ? width - mouseX : mouseX) + : (direction == Direction.UP ? height - mouseY : mouseY); int rotMouseY = axis == Axis.HORIZONTAL ? mouseY : mouseX; matrices.push(); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java index 27ee960..2010a08 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java @@ -22,6 +22,10 @@ public class WScrollBar extends WAbstractSlider { */ public WScrollBar(Axis axis) { super(0, 100, axis); + + if (axis == Axis.VERTICAL) { + setDirection(Direction.DOWN); + } } @Override diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java index f43736f..630f23a 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java @@ -62,7 +62,9 @@ public class WSlider extends WAbstractSlider { if (axis == Axis.VERTICAL) { int trackX = x + width / 2 - TRACK_WIDTH / 2; thumbX = width / 2 - THUMB_SIZE / 2; - thumbY = height - THUMB_SIZE + 1 - (int) (coordToValueRatio * (value - min)); + thumbY = direction == Direction.UP + ? (height - THUMB_SIZE) + 1 - (int) (coordToValueRatio * (value - min)) + : Math.round(coordToValueRatio * (value - min)); thumbXOffset = 0; ScreenDrawing.texturedRect(trackX, y + 1, TRACK_WIDTH, 1, texture, 16*px, 0*px, 22*px, 1*px, 0xFFFFFFFF); @@ -70,7 +72,9 @@ public class WSlider extends WAbstractSlider { ScreenDrawing.texturedRect(trackX, y + height, TRACK_WIDTH, 1, texture, 16*px, 2*px, 22*px, 3*px, 0xFFFFFFFF); } else { int trackY = y + height / 2 - TRACK_WIDTH / 2; - thumbX = Math.round(coordToValueRatio * (value - min)); + thumbX = direction == Direction.RIGHT + ? (width - THUMB_SIZE) + 1 - (int) (coordToValueRatio * (value - min)) + : Math.round(coordToValueRatio * (value - min)); thumbY = height / 2 - THUMB_SIZE / 2; thumbXOffset = 8; |