aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorJuuxel <kasperi.kauppi@gmail.com>2019-08-27 11:10:30 +0300
committerJuuxel <kasperi.kauppi@gmail.com>2019-08-27 11:10:30 +0300
commitcd052a3d42fae2c80c58b28741dd66e9da1a6fcd (patch)
tree1754f0c9502b078ac48a548ac0b2dfad6b3d19f0 /src/main
parentc4fe40d1daee84662c6c269f53dd68a2c2a136f1 (diff)
downloadLibGui-cd052a3d42fae2c80c58b28741dd66e9da1a6fcd.tar.gz
LibGui-cd052a3d42fae2c80c58b28741dd66e9da1a6fcd.tar.bz2
LibGui-cd052a3d42fae2c80c58b28741dd66e9da1a6fcd.zip
Add sliders, WIP
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java122
-rw-r--r--src/main/resources/assets/libgui/textures/widget/slider.pngbin0 -> 668 bytes
2 files changed, 122 insertions, 0 deletions
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
new file mode 100644
index 0000000..ee0ce3a
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java
@@ -0,0 +1,122 @@
+package io.github.cottonmc.cotton.gui.widget;
+
+import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
+import net.minecraft.util.Identifier;
+
+import javax.annotation.Nullable;
+import java.util.function.IntConsumer;
+
+public class WSlider extends WWidget {
+ private static final int TRACK_WIDTH = 6;
+ private static final int THUMB_SIZE = 8;
+ // TODO: Horizontal textures
+ private static final Identifier TEXTURE = new Identifier("libgui", "textures/widget/slider.png");
+
+ private final int min, max;
+ private final int valueRange;
+ private final Axis axis;
+
+ private int value;
+ private float valueToCoordRatio, coordToValueRatio;
+ @Nullable private IntConsumer valueChangeListener = null;
+ @Nullable private Runnable mouseReleaseListener = null;
+ private boolean dragging = false;
+
+ public WSlider(int min, int max, Axis axis) {
+ this.min = min;
+ this.max = max;
+ this.valueRange = max - min + 1;
+ this.axis = axis;
+ this.value = min;
+ }
+
+ public WSlider(int max, Axis axis) {
+ this(0, max, axis);
+ }
+
+ @Override
+ public void setSize(int x, int y) {
+ super.setSize(x, y);
+ // FIXME: that - and + stuff fixes the rendering but causes range issues (too large for X and too small for Y)
+ int trackHeight = (axis == Axis.HORIZONTAL ? (x - THUMB_SIZE) : (y + THUMB_SIZE));
+ valueToCoordRatio = (float) valueRange / trackHeight;
+ coordToValueRatio = 1 / valueToCoordRatio;
+ }
+
+ @Override
+ public boolean canResize() {
+ return true;
+ }
+
+ @Override
+ public void onMouseDrag(int x, int y, int button) {
+ // a = mouse coordinate on slider axis, axisWidth = width of slider axis
+ // ao = axis-opposite mouse coordinate, aoCenter = center of ao's axis
+ int a = axis == Axis.HORIZONTAL ? x : y;
+ int axisWidth = axis == Axis.HORIZONTAL ? width : height;
+ int ao = axis == Axis.HORIZONTAL ? y : x;
+ int aoCenter = (axis == Axis.HORIZONTAL ? height : width) / 2;
+ if (dragging || ao >= aoCenter - TRACK_WIDTH / 2 && ao <= aoCenter + TRACK_WIDTH / 2) {
+ dragging = true;
+ int pos = axis == Axis.VERTICAL ? (axisWidth - a + THUMB_SIZE / 2) : (a - THUMB_SIZE / 2);
+ value = min + (int) (valueToCoordRatio * pos);
+ if (valueChangeListener != null) valueChangeListener.accept(value);
+ }
+ }
+
+ @Override
+ public void onClick(int x, int y, int button) {
+ onMouseDrag(x, y, button);
+ onMouseUp(x, y, button);
+ }
+
+ @Override
+ public WWidget onMouseUp(int x, int y, int button) {
+ dragging = false;
+ if (mouseReleaseListener != null) mouseReleaseListener.run();
+
+ return super.onMouseUp(x, y, button);
+ }
+
+ @Override
+ public void paintBackground(int x, int y) {
+ float px = 1 / 16f;
+ if (axis == Axis.VERTICAL) {
+ int trackX = x + width / 2 - TRACK_WIDTH / 2;
+ int thumbY = y + height - (int) (coordToValueRatio * (value - min));
+
+ ScreenDrawing.rect(TEXTURE, trackX, y + 1, TRACK_WIDTH, 1, 0*px, 8*px, 6*px, 9*px, 0xFFFFFFFF);
+ ScreenDrawing.rect(TEXTURE, trackX, y + 2, TRACK_WIDTH, height - 2, 0*px, 9*px, 6*px, 10*px, 0xFFFFFFFF);
+ ScreenDrawing.rect(TEXTURE, trackX, y + height, TRACK_WIDTH, 1, 0*px, 10*px, 6*px, 11*px, 0xFFFFFFFF);
+
+ ScreenDrawing.rect(TEXTURE, x + width / 2 - THUMB_SIZE / 2, thumbY, THUMB_SIZE, THUMB_SIZE, 0*px, 0*px, 8*px, 8*px, 0xFFFFFFFF);
+ } else {
+ int trackY = y + height / 2 - TRACK_WIDTH / 2;
+ int thumbX = x + (int) (coordToValueRatio * (value - min));
+
+ ScreenDrawing.rect(x, trackY, 1, TRACK_WIDTH, 0xFFFF0000);
+ ScreenDrawing.rect(x + 1, trackY, width - 2, TRACK_WIDTH, 0xFFFF0000);
+ ScreenDrawing.rect(x + width - 1, trackY, 1, TRACK_WIDTH, 0xFFFF0000);
+
+ ScreenDrawing.rect(thumbX, y + height / 2 - THUMB_SIZE / 2, THUMB_SIZE, THUMB_SIZE, 0xFF00FFFF);
+ }
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+ public WSlider setValueChangeListener(@Nullable IntConsumer valueChangeListener) {
+ this.valueChangeListener = valueChangeListener;
+ return this;
+ }
+
+ public WSlider setMouseReleaseListener(@Nullable Runnable mouseReleaseListener) {
+ this.mouseReleaseListener = mouseReleaseListener;
+ return this;
+ }
+}
diff --git a/src/main/resources/assets/libgui/textures/widget/slider.png b/src/main/resources/assets/libgui/textures/widget/slider.png
new file mode 100644
index 0000000..8fbf16f
--- /dev/null
+++ b/src/main/resources/assets/libgui/textures/widget/slider.png
Binary files differ