diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2021-02-27 16:53:46 +0200 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2021-02-27 16:53:46 +0200 |
commit | eca69cd75a986af22a18e48cd51861e92c70ca1e (patch) | |
tree | f7d638193e48de7dfecf5651fdca898529130443 | |
parent | 9987bf932829a495a9671677c4bdc3f53256ef5b (diff) | |
download | LibGui-eca69cd75a986af22a18e48cd51861e92c70ca1e.tar.gz LibGui-eca69cd75a986af22a18e48cd51861e92c70ca1e.tar.bz2 LibGui-eca69cd75a986af22a18e48cd51861e92c70ca1e.zip |
WScrollPanel: Add mouse scrolling support
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java | 30 |
1 files 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; + } } |