diff options
author | Juuxel <kasperi.kauppi@gmail.com> | 2019-08-27 17:10:06 +0300 |
---|---|---|
committer | Juuxel <kasperi.kauppi@gmail.com> | 2019-08-27 17:10:06 +0300 |
commit | 5f3e03cba9fe46791c717071b2b209129099d35b (patch) | |
tree | 6d5ebc59a6a898eb16d196f3f5a60ac99251736c /src/main | |
parent | cd052a3d42fae2c80c58b28741dd66e9da1a6fcd (diff) | |
download | LibGui-5f3e03cba9fe46791c717071b2b209129099d35b.tar.gz LibGui-5f3e03cba9fe46791c717071b2b209129099d35b.tar.bz2 LibGui-5f3e03cba9fe46791c717071b2b209129099d35b.zip |
More work on sliders
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java | 9 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java | 2 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WSlider.java | 25 | ||||
-rw-r--r-- | src/main/resources/assets/libgui/textures/widget/slider.png | bin | 668 -> 1831 bytes |
4 files changed, 21 insertions, 15 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java index 23c5b6f..454df9d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java @@ -3,10 +3,7 @@ package io.github.cottonmc.cotton.gui.client.modmenu; import io.github.cottonmc.cotton.gui.client.BackgroundPainter; import io.github.cottonmc.cotton.gui.client.LibGuiClient; import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; -import io.github.cottonmc.cotton.gui.widget.WButton; -import io.github.cottonmc.cotton.gui.widget.WGridPanel; -import io.github.cottonmc.cotton.gui.widget.WTextField; -import io.github.cottonmc.cotton.gui.widget.WToggleButton; +import io.github.cottonmc.cotton.gui.widget.*; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.TranslatableText; @@ -30,7 +27,9 @@ public class ConfigGui extends LightweightGuiDescription { WTextField testField = new WTextField(); testField.setSuggestion("test"); root.add(testField, 0, 3, 4, 1); - + root.add(new WSlider(100, 0, Axis.VERTICAL).setValueChangeListener(System.out::println), 0, 4, 1, 4); + root.add(new WSlider(0, 123, Axis.HORIZONTAL).setValueChangeListener(System.out::println), 1, 4, 4, 1); + root.add(new WKirbSprite(), 5, 4); WButton doneButton = new WButton(new TranslatableText("gui.done")); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java index 0f5159c..16a8d71 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java @@ -31,7 +31,7 @@ public class WButton extends WWidget { @Override public void paintForeground(int x, int y, int mouseX, int mouseY) { - System.out.println("Mouse: { "+mouseX+", "+mouseY+" }"); + //System.out.println("Mouse: { "+mouseX+", "+mouseY+" }"); boolean hovered = (mouseX>=x && mouseY>=y && mouseX<x+getWidth() && mouseY<y+getHeight()); int state = 1; //1=regular. 2=hovered. 0=disabled. 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 ee0ce3a..c08d1bc 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 @@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui.widget; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import net.minecraft.util.Identifier; +import net.minecraft.util.math.MathHelper; import javax.annotation.Nullable; import java.util.function.IntConsumer; @@ -20,9 +21,15 @@ public class WSlider extends WWidget { private float valueToCoordRatio, coordToValueRatio; @Nullable private IntConsumer valueChangeListener = null; @Nullable private Runnable mouseReleaseListener = null; + + // Used for detecting dragging after the user starts dragging + // on top of the slider, but then moves the mouse out but still within the widget's boundary. private boolean dragging = false; public WSlider(int min, int max, Axis axis) { + if (max >= min) + throw new IllegalArgumentException("Minimum value must be smaller than the maximum!"); + this.min = min; this.max = max; this.valueRange = max - min + 1; @@ -37,8 +44,7 @@ public class WSlider extends WWidget { @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)); + int trackHeight = (axis == Axis.HORIZONTAL ? x : y) - THUMB_SIZE + 1; valueToCoordRatio = (float) valueRange / trackHeight; coordToValueRatio = 1 / valueToCoordRatio; } @@ -58,8 +64,9 @@ public class WSlider extends WWidget { 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); + int pos = (axis == Axis.VERTICAL ? (axisWidth - a) : a) - THUMB_SIZE / 2; + int futureValue = min + (int) (valueToCoordRatio * pos); + value = MathHelper.clamp(futureValue, min, max); if (valueChangeListener != null) valueChangeListener.accept(value); } } @@ -83,7 +90,7 @@ public class WSlider extends WWidget { float px = 1 / 16f; if (axis == Axis.VERTICAL) { int trackX = x + width / 2 - TRACK_WIDTH / 2; - int thumbY = y + height - (int) (coordToValueRatio * (value - min)); + int thumbY = y + height - THUMB_SIZE + 1 - (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); @@ -94,11 +101,11 @@ public class WSlider extends WWidget { 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(TEXTURE, x, trackY, 1, TRACK_WIDTH, 8*px, 0*px, 9*px, 6*px, 0xFFFFFFFF); + ScreenDrawing.rect(TEXTURE, x + 1, trackY, width - 2, TRACK_WIDTH, 9*px, 0*px, 10*px, 6*px, 0xFFFFFFFF); + ScreenDrawing.rect(TEXTURE, x + width - 1, trackY, 1, TRACK_WIDTH, 10*px, 0*px, 11*px, 6*px, 0xFFFFFFFF); - ScreenDrawing.rect(thumbX, y + height / 2 - THUMB_SIZE / 2, THUMB_SIZE, THUMB_SIZE, 0xFF00FFFF); + ScreenDrawing.rect(TEXTURE, thumbX, y + height / 2 - THUMB_SIZE / 2, THUMB_SIZE, THUMB_SIZE, 8*px, 8*px, 16*px, 16*px, 0xFFFFFFFF); } } diff --git a/src/main/resources/assets/libgui/textures/widget/slider.png b/src/main/resources/assets/libgui/textures/widget/slider.png Binary files differindex 8fbf16f..d4e2eda 100644 --- a/src/main/resources/assets/libgui/textures/widget/slider.png +++ b/src/main/resources/assets/libgui/textures/widget/slider.png |