aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFalkreon <falkreon@gmail.com>2019-09-22 10:46:28 -0500
committerGitHub <noreply@github.com>2019-09-22 10:46:28 -0500
commit4a0360b555821cf4d2d3342540608f3361f6a0e7 (patch)
treec2b5dd14fea2c0ecc5fa4aa45a347cf58f6c3463 /src
parente7cd64eb26f59ed287801b1cea705319c4c97dc3 (diff)
parent5d26c800f398c549fd6128833852d494ce2e8f1a (diff)
downloadLibGui-4a0360b555821cf4d2d3342540608f3361f6a0e7.tar.gz
LibGui-4a0360b555821cf4d2d3342540608f3361f6a0e7.tar.bz2
LibGui-4a0360b555821cf4d2d3342540608f3361f6a0e7.zip
Merge pull request #13 from Juuxel/slider-updates
Slider updates
Diffstat (limited to 'src')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java83
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WLabeledSlider.java60
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java31
-rw-r--r--src/main/resources/assets/libgui/textures/widget/slider_dark.pngbin0 -> 2919 bytes
-rw-r--r--src/main/resources/assets/libgui/textures/widget/slider_light.png (renamed from src/main/resources/assets/libgui/textures/widget/slider.png)bin2978 -> 2978 bytes
5 files changed, 142 insertions, 32 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 641b396..9aa0782 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
@@ -14,7 +14,7 @@ import java.util.function.IntConsumer;
* <p>You can set two listeners on a slider:
* <ul>
* <li>
- * A value change listener that gets all value changes (including direct setValue calls).
+ * A value change listener that gets all value changes.
* </li>
* <li>
* A dragging finished listener that gets called when the player stops dragging the slider
@@ -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 DRAGGING_FINISHED_RATE_LIMIT_FOR_SCROLLING = 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,24 +137,76 @@ 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 = DRAGGING_FINISHED_RATE_LIMIT_FOR_SCROLLING;
+ }
+ }
+
public int getValue() {
return value;
}
+ /**
+ * Sets the slider value without calling listeners.
+ * @param value the new value
+ */
public void setValue(int value) {
- this.value = value;
- onValueChanged(value);
+ setValue(value, false);
+ }
+
+ /**
+ * Sets the slider value.
+ *
+ * @param value the new value
+ * @param callListeners if true, call all slider listeners
+ */
+ public void setValue(int value, boolean callListeners) {
+ int previous = this.value;
+ this.value = MathHelper.clamp(value, min, max);
+ if (callListeners && previous != this.value) {
+ onValueChanged(this.value);
+ if (draggingFinishedListener != null) draggingFinishedListener.accept(value);
+ }
+ }
+
+ @Nullable
+ public IntConsumer getValueChangeListener() {
+ return valueChangeListener;
}
public void setValueChangeListener(@Nullable IntConsumer valueChangeListener) {
this.valueChangeListener = valueChangeListener;
}
- public void setDraggingFinishedListener(@Nullable Runnable draggingFinishedListener) {
+ @Nullable
+ public IntConsumer getDraggingFinishedListener() {
+ return draggingFinishedListener;
+ }
+
+ public void setDraggingFinishedListener(@Nullable IntConsumer draggingFinishedListener) {
this.draggingFinishedListener = draggingFinishedListener;
}
@@ -190,15 +249,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;
}
}
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 a959485..4541427 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
@@ -1,6 +1,8 @@
package io.github.cottonmc.cotton.gui.widget;
+import com.mojang.blaze3d.systems.RenderSystem;
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
+import io.github.cottonmc.cotton.gui.widget.data.Alignment;
import io.github.cottonmc.cotton.gui.widget.data.Axis;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@@ -21,20 +23,33 @@ import javax.annotation.Nullable;
public class WLabeledSlider extends WAbstractSlider {
@Nullable private Text label = null;
@Nullable private LabelUpdater labelUpdater = null;
+ private Alignment labelAlignment = Alignment.CENTER;
public WLabeledSlider(int min, int max) {
- super(min, max, Axis.HORIZONTAL);
+ this(min, max, Axis.HORIZONTAL);
}
- public WLabeledSlider(int min, int max, Text label) {
- this(min, max);
+ public WLabeledSlider(int min, int max, Axis axis) {
+ super(min, max, axis);
+ }
+
+ public WLabeledSlider(int min, int max, Axis axis, @Nullable Text label) {
+ this(min, max, axis);
this.label = label;
}
+ public WLabeledSlider(int min, int max, @Nullable Text label) {
+ this(min, max);
+ this.label = label;
+ }
@Override
public void setSize(int x, int y) {
- super.setSize(x, 20);
+ if (axis == Axis.HORIZONTAL) {
+ super.setSize(x, 20);
+ } else {
+ super.setSize(20, y);
+ }
}
@Nullable
@@ -54,6 +69,19 @@ public class WLabeledSlider extends WAbstractSlider {
}
}
+ public Alignment getLabelAlignment() {
+ return labelAlignment;
+ }
+
+ public void setLabelAlignment(Alignment labelAlignment) {
+ this.labelAlignment = labelAlignment;
+ }
+
+ @Nullable
+ public LabelUpdater getLabelUpdater() {
+ return labelUpdater;
+ }
+
public void setLabelUpdater(@Nullable LabelUpdater labelUpdater) {
this.labelUpdater = labelUpdater;
}
@@ -71,27 +99,39 @@ public class WLabeledSlider extends WAbstractSlider {
@Environment(EnvType.CLIENT)
@Override
public void paintBackground(int x, int y, int mouseX, int mouseY) {
- drawButton(x, y, 0, width);
+ int aWidth = axis == Axis.HORIZONTAL ? width : height;
+ int aHeight = axis == Axis.HORIZONTAL ? height : width;
+ int rotMouseX = axis == Axis.HORIZONTAL ? mouseX : (height - mouseY);
+ int rotMouseY = axis == Axis.HORIZONTAL ? mouseY : mouseX;
+
+ RenderSystem.pushMatrix();
+ RenderSystem.translatef(x, y, 0);
+ if (axis == Axis.VERTICAL) {
+ RenderSystem.translatef(0, height, 0);
+ RenderSystem.rotatef(270, 0, 0, 1);
+ }
+ drawButton(0, 0, 0, aWidth);
// 1: regular, 2: hovered, 0: disabled/dragging
int thumbX = Math.round(coordToValueRatio * (value - min));
int thumbY = 0;
int thumbWidth = getThumbWidth();
- int thumbHeight = height;
- boolean hovering = mouseX >= thumbX && mouseX <= thumbX + thumbWidth && mouseY >= thumbY && mouseY <= thumbY + thumbHeight;
+ int thumbHeight = aHeight;
+ boolean hovering = rotMouseX >= thumbX && rotMouseX <= thumbX + thumbWidth && rotMouseY >= thumbY && rotMouseY <= thumbY + thumbHeight;
int thumbState = dragging || hovering ? 2 : 1;
- drawButton(x + thumbX, y + thumbY, thumbState, thumbWidth);
+ drawButton(thumbX, thumbY, thumbState, thumbWidth);
if (thumbState == 1 && isFocused()) {
float px = 1 / 32f;
- ScreenDrawing.texturedRect(x + thumbX, y + thumbY, thumbWidth, thumbHeight, WSlider.TEXTURE, 24*px, 0*px, 32*px, 20*px, 0xFFFFFFFF);
+ ScreenDrawing.texturedRect(thumbX, thumbY, thumbWidth, thumbHeight, WSlider.LIGHT_TEXTURE, 24*px, 0*px, 32*px, 20*px, 0xFFFFFFFF);
}
if (label != null) {
int color = isMouseInsideBounds(mouseX, mouseY) ? 0xFFFFA0 : 0xE0E0E0;
- ScreenDrawing.drawCenteredWithShadow(label.asFormattedString(), x + width / 2, y + height / 2 - 4, color);
+ ScreenDrawing.drawStringWithShadow(label.asFormattedString(), labelAlignment, 2, aHeight / 2 - 4, aWidth - 4, color);
}
+ RenderSystem.popMatrix();
}
// state = 1: regular, 2: hovered, 0: disabled/dragging
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 3bd7c20..2dd3a8e 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
@@ -1,6 +1,7 @@
package io.github.cottonmc.cotton.gui.widget;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
+import io.github.cottonmc.cotton.gui.client.LibGuiClient;
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
import io.github.cottonmc.cotton.gui.widget.data.Axis;
import net.fabricmc.api.EnvType;
@@ -17,7 +18,8 @@ import javax.annotation.Nullable;
public class WSlider extends WAbstractSlider {
public static final int TRACK_WIDTH = 6;
public static final int THUMB_SIZE = 8;
- public static final Identifier TEXTURE = new Identifier("libgui", "textures/widget/slider.png");
+ public static final Identifier LIGHT_TEXTURE = new Identifier("libgui", "textures/widget/slider_light.png");
+ public static final Identifier DARK_TEXTURE = new Identifier("libgui", "textures/widget/slider_dark.png");
@Environment(EnvType.CLIENT)
@Nullable
@@ -27,6 +29,7 @@ public class WSlider extends WAbstractSlider {
super(min, max, axis);
}
+ @Deprecated
public WSlider(int max, Axis axis) {
this(0, max, axis);
}
@@ -46,6 +49,7 @@ public class WSlider extends WAbstractSlider {
return ao >= aoCenter - TRACK_WIDTH / 2 - 2 && ao <= aoCenter + TRACK_WIDTH / 2 + 2;
}
+ @SuppressWarnings("SuspiciousNameCombination")
@Environment(EnvType.CLIENT)
@Override
public void paintBackground(int x, int y, int mouseX, int mouseY) {
@@ -57,6 +61,7 @@ public class WSlider extends WAbstractSlider {
int thumbX, thumbY;
// thumbXOffset: thumb texture x offset in pixels
int thumbXOffset;
+ Identifier texture = LibGuiClient.config.darkMode ? DARK_TEXTURE : LIGHT_TEXTURE;
if (axis == Axis.VERTICAL) {
int trackX = x + width / 2 - TRACK_WIDTH / 2;
@@ -64,33 +69,39 @@ public class WSlider extends WAbstractSlider {
thumbY = height - THUMB_SIZE + 1 - (int) (coordToValueRatio * (value - min));
thumbXOffset = 0;
- ScreenDrawing.texturedRect(trackX, y + 1, TRACK_WIDTH, 1, TEXTURE, 16*px, 0*px, 22*px, 1*px, 0xFFFFFFFF);
- ScreenDrawing.texturedRect(trackX, y + 2, TRACK_WIDTH, height - 2, TEXTURE, 16*px, 1*px, 22*px, 2*px, 0xFFFFFFFF);
- ScreenDrawing.texturedRect(trackX, y + height, TRACK_WIDTH, 1, TEXTURE, 16*px, 2*px, 22*px, 3*px, 0xFFFFFFFF);
+ ScreenDrawing.texturedRect(trackX, y + 1, TRACK_WIDTH, 1, texture, 16*px, 0*px, 22*px, 1*px, 0xFFFFFFFF);
+ ScreenDrawing.texturedRect(trackX, y + 2, TRACK_WIDTH, height - 2, texture, 16*px, 1*px, 22*px, 2*px, 0xFFFFFFFF);
+ 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));
thumbY = height / 2 - THUMB_SIZE / 2;
thumbXOffset = 8;
- ScreenDrawing.texturedRect(x, trackY, 1, TRACK_WIDTH, TEXTURE, 16*px, 3*px, 17*px, 9*px, 0xFFFFFFFF);
- ScreenDrawing.texturedRect(x + 1, trackY, width - 2, TRACK_WIDTH, TEXTURE, 17*px, 3*px, 18*px, 9*px, 0xFFFFFFFF);
- ScreenDrawing.texturedRect(x + width - 1, trackY, 1, TRACK_WIDTH, TEXTURE, 18*px, 3*px, 19*px, 9*px, 0xFFFFFFFF);
+ ScreenDrawing.texturedRect(x, trackY, 1, TRACK_WIDTH, texture, 16*px, 3*px, 17*px, 9*px, 0xFFFFFFFF);
+ ScreenDrawing.texturedRect(x + 1, trackY, width - 2, TRACK_WIDTH, texture, 17*px, 3*px, 18*px, 9*px, 0xFFFFFFFF);
+ ScreenDrawing.texturedRect(x + width - 1, trackY, 1, TRACK_WIDTH, texture, 18*px, 3*px, 19*px, 9*px, 0xFFFFFFFF);
}
// thumbState values:
// 0: default, 1: dragging, 2: hovered
int thumbState = dragging ? 1 : (mouseX >= thumbX && mouseX <= thumbX + THUMB_SIZE && mouseY >= thumbY && mouseY <= thumbY + THUMB_SIZE ? 2 : 0);
- ScreenDrawing.texturedRect(x + thumbX, y + thumbY, THUMB_SIZE, THUMB_SIZE, TEXTURE, thumbXOffset*px, 0*px + thumbState * 8*px, (thumbXOffset + 8)*px, 8*px + thumbState * 8*px, 0xFFFFFFFF);
+ ScreenDrawing.texturedRect(x + thumbX, y + thumbY, THUMB_SIZE, THUMB_SIZE, texture, thumbXOffset*px, 0*px + thumbState * 8*px, (thumbXOffset + 8)*px, 8*px + thumbState * 8*px, 0xFFFFFFFF);
if (thumbState == 0 && isFocused()) {
- ScreenDrawing.texturedRect(x + thumbX, y + thumbY, THUMB_SIZE, THUMB_SIZE, TEXTURE, 0*px, 24*px, 8*px, 32*px, 0xFFFFFFFF);
+ ScreenDrawing.texturedRect(x + thumbX, y + thumbY, THUMB_SIZE, THUMB_SIZE, texture, 0*px, 24*px, 8*px, 32*px, 0xFFFFFFFF);
}
}
}
@Environment(EnvType.CLIENT)
- public void setBackgroundPainter(BackgroundPainter backgroundPainter) {
+ @Nullable
+ public BackgroundPainter getBackgroundPainter() {
+ return backgroundPainter;
+ }
+
+ @Environment(EnvType.CLIENT)
+ public void setBackgroundPainter(@Nullable BackgroundPainter backgroundPainter) {
this.backgroundPainter = backgroundPainter;
}
}
diff --git a/src/main/resources/assets/libgui/textures/widget/slider_dark.png b/src/main/resources/assets/libgui/textures/widget/slider_dark.png
new file mode 100644
index 0000000..9e83925
--- /dev/null
+++ b/src/main/resources/assets/libgui/textures/widget/slider_dark.png
Binary files differ
diff --git a/src/main/resources/assets/libgui/textures/widget/slider.png b/src/main/resources/assets/libgui/textures/widget/slider_light.png
index e41250e..e41250e 100644
--- a/src/main/resources/assets/libgui/textures/widget/slider.png
+++ b/src/main/resources/assets/libgui/textures/widget/slider_light.png
Binary files differ