From eca69cd75a986af22a18e48cd51861e92c70ca1e Mon Sep 17 00:00:00 2001 From: Juuxel <6596629+Juuxel@users.noreply.github.com> Date: Sat, 27 Feb 2021 16:53:46 +0200 Subject: WScrollPanel: Add mouse scrolling support --- .../cottonmc/cotton/gui/widget/WScrollPanel.java | 30 +++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java index e66d359..b3513cb 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java @@ -6,6 +6,7 @@ import net.fabricmc.fabric.api.util.TriState; import net.minecraft.client.util.math.MatrixStack; import io.github.cottonmc.cotton.gui.widget.data.Axis; +import io.github.cottonmc.cotton.gui.widget.data.InputResult; /** * Similar to the JScrollPane in Swing, this widget represents a scrollable widget. @@ -104,12 +105,8 @@ public class WScrollPanel extends WClippedPanel { public void layout() { children.clear(); - boolean horizontal = (scrollingHorizontally == TriState.DEFAULT) - ? (widget.width > this.width - SCROLL_BAR_SIZE) - : scrollingHorizontally.get(); - boolean vertical = (scrollingVertically == TriState.DEFAULT) - ? (widget.height > this.height - SCROLL_BAR_SIZE) - : scrollingVertically.get(); + boolean horizontal = hasHorizontalScrollbar(); + boolean vertical = hasVerticalScrollbar(); int offset = (horizontal && vertical) ? SCROLL_BAR_SIZE : 0; verticalScrollBar.setSize(SCROLL_BAR_SIZE, this.height - offset); @@ -131,4 +128,25 @@ public class WScrollPanel extends WClippedPanel { if (vertical) children.add(verticalScrollBar); if (horizontal) children.add(horizontalScrollBar); } + + private boolean hasHorizontalScrollbar() { + return (scrollingHorizontally == TriState.DEFAULT) + ? (widget.width > this.width - SCROLL_BAR_SIZE) + : scrollingHorizontally.get(); + } + + private boolean hasVerticalScrollbar() { + return (scrollingVertically == TriState.DEFAULT) + ? (widget.height > this.height - SCROLL_BAR_SIZE) + : scrollingVertically.get(); + } + + @Override + public InputResult onMouseScroll(int x, int y, double amount) { + if (hasVerticalScrollbar()) { + return verticalScrollBar.onMouseScroll(0, 0, amount); + } + + return InputResult.IGNORED; + } } -- cgit