From 5f3e03cba9fe46791c717071b2b209129099d35b Mon Sep 17 00:00:00 2001 From: Juuxel Date: Tue, 27 Aug 2019 17:10:06 +0300 Subject: More work on sliders --- .../cotton/gui/client/modmenu/ConfigGui.java | 9 ++++---- .../github/cottonmc/cotton/gui/widget/WButton.java | 2 +- .../github/cottonmc/cotton/gui/widget/WSlider.java | 25 ++++++++++++++-------- 3 files changed, 21 insertions(+), 15 deletions(-) (limited to 'src/main/java/io') 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= 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); } } -- cgit