From 9c655343bb91e2c7e117e2f99c2053d55f30e842 Mon Sep 17 00:00:00 2001 From: Juuxel Date: Sat, 31 Aug 2019 23:56:17 +0300 Subject: Some slider updates - WIP dark mode textures for simple sliders - Vertical/rotated labeled sliders - Text alignment for slider labels - Add missing getters for nicer Kotlin usage --- .../cotton/gui/widget/WAbstractSlider.java | 10 ++++ .../cottonmc/cotton/gui/widget/WLabeledSlider.java | 60 ++++++++++++++++++---- .../github/cottonmc/cotton/gui/widget/WSlider.java | 31 +++++++---- 3 files changed, 81 insertions(+), 20 deletions(-) (limited to 'src/main/java') 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..aa5e78d 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 @@ -143,10 +143,20 @@ public abstract class WAbstractSlider extends WWidget { onValueChanged(value); } + @Nullable + public IntConsumer getValueChangeListener() { + return valueChangeListener; + } + public void setValueChangeListener(@Nullable IntConsumer valueChangeListener) { this.valueChangeListener = valueChangeListener; } + @Nullable + public Runnable getDraggingFinishedListener() { + return draggingFinishedListener; + } + public void setDraggingFinishedListener(@Nullable Runnable draggingFinishedListener) { this.draggingFinishedListener = draggingFinishedListener; } 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..b921945 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.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; } } -- cgit