diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java index add86883..ac5ddcbe 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollBar.java @@ -15,7 +15,8 @@ public class WScrollBar extends WWidget { private static final Identifier FOCUS_TEXTURE = new Identifier(LibGuiCommon.MOD_ID, "widget/scroll_bar/focus"); - private static final int SCROLLING_SPEED = 4; + public static final int DEFAULT_SCROLLING_SPEED = 4; + private int scrollingSpeed = 4; protected Axis axis = Axis.HORIZONTAL; protected int value; @@ -203,7 +204,7 @@ public InputResult onKeyPressed(int ch, int key, int modifiers) { @Environment(EnvType.CLIENT) @Override public InputResult onMouseScroll(int x, int y, double horizontalAmount, double verticalAmount) { - setValue(getValue() + (int) (horizontalAmount - verticalAmount) * SCROLLING_SPEED); + setValue(getValue() + (int) (horizontalAmount - verticalAmount) * scrollingSpeed); return InputResult.PROCESSED; } @@ -227,6 +228,30 @@ public WScrollBar setMaxValue(int max) { return this; } + /** + * Sets the mouse scroll speed; + *
+ * Default value: {@value #DEFAULT_SCROLLING_SPEED} + * @param scrollingSpeed the scroll speed + * @return this scroll bar + */ + public WScrollBar setScrollingSpeed(int scrollingSpeed) { + if(scrollingSpeed < 0) throw new IllegalArgumentException("Negative value for scrolling speed"); + if(scrollingSpeed == 0) throw new IllegalArgumentException("Zero value for scrolling speed"); + + this.scrollingSpeed = scrollingSpeed; + return this; + } + + /** + * Gets the mouse scroll speed; + *
+ * Default value: {@value #DEFAULT_SCROLLING_SPEED} + */ + public int getScrollingSpeed() { + return scrollingSpeed; + } + public int getWindow() { return window; } diff --git a/src/testMod/java/io/github/cottonmc/test/client/ScrollBarTestGui.java b/src/testMod/java/io/github/cottonmc/test/client/ScrollBarTestGui.java index efac9d5b..7c94a535 100644 --- a/src/testMod/java/io/github/cottonmc/test/client/ScrollBarTestGui.java +++ b/src/testMod/java/io/github/cottonmc/test/client/ScrollBarTestGui.java @@ -1,13 +1,18 @@ package io.github.cottonmc.test.client; import net.fabricmc.fabric.api.util.TriState; +import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; +import io.github.cottonmc.cotton.gui.widget.WLabel; import io.github.cottonmc.cotton.gui.widget.WPlainPanel; import io.github.cottonmc.cotton.gui.widget.WScrollBar; +import io.github.cottonmc.cotton.gui.widget.WSlider; import io.github.cottonmc.cotton.gui.widget.WToggleButton; import io.github.cottonmc.cotton.gui.widget.data.Axis; +import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment; import io.github.cottonmc.cotton.gui.widget.data.Insets; +import io.github.cottonmc.cotton.gui.widget.data.VerticalAlignment; public class ScrollBarTestGui extends LightweightGuiDescription { private boolean darkMode = false; @@ -18,21 +23,37 @@ public ScrollBarTestGui() { root.setSize(256, 240); root.setInsets(Insets.ROOT_PANEL); - WScrollBar scrollBar1 = new WScrollBar(Axis.HORIZONTAL); - root.add(scrollBar1, 0, 0, 256, 16); + WScrollBar scrollBarTop = new WScrollBar(Axis.HORIZONTAL); + root.add(scrollBarTop, 0, 0, 256, 16); - WScrollBar scrollBar2 = new WScrollBar(Axis.HORIZONTAL); - root.add(scrollBar2, 0, 240 - scrollBar2.getHeight(), 256, 8); + WScrollBar scrollBarDown = new WScrollBar(Axis.HORIZONTAL); + root.add(scrollBarDown, 0, 240 - scrollBarDown.getHeight(), 256, 8); - WScrollBar scrollBar3 = new WScrollBar(Axis.VERTICAL); - root.add(scrollBar3, 0, 18, 16, 202); + WScrollBar scrollBarLeft = new WScrollBar(Axis.VERTICAL); + root.add(scrollBarLeft, 0, 18, 16, 202); - WScrollBar scrollBar4 = new WScrollBar(Axis.VERTICAL); - root.add(scrollBar4, 248, 18, 8, 202); + WScrollBar scrollBarRight = new WScrollBar(Axis.VERTICAL); + root.add(scrollBarRight, 248, 18, 8, 202); + + WLabel label = new WLabel(Text.of("Scrolling Speed: 4")); + label.setHorizontalAlignment(HorizontalAlignment.CENTER); + label.setVerticalAlignment(VerticalAlignment.CENTER); + root.add(label, 32, 112, 192, 16); + + WSlider slider = new WSlider(1, 100, Axis.HORIZONTAL); + slider.setDraggingFinishedListener(i -> { + label.setText(Text.of("Scrolling Speed: " + i)); + + scrollBarTop.setScrollingSpeed(i); + scrollBarDown.setScrollingSpeed(i); + scrollBarLeft.setScrollingSpeed(i); + scrollBarRight.setScrollingSpeed(i); + }); + root.add(slider, 78, 128, 100, 16); WToggleButton toggleButton = new WToggleButton(); toggleButton.setOnToggle(on -> darkMode = on); - root.add(toggleButton, 128 - (toggleButton.getWidth() / 2), 120 - (toggleButton.getHeight() / 2)); + root.add(toggleButton, 128 - (toggleButton.getWidth() / 2), 104 - (toggleButton.getHeight() / 2)); root.validate(this); }