diff options
author | Juuxel <kasperi.kauppi@gmail.com> | 2019-09-01 00:28:21 +0300 |
---|---|---|
committer | Juuxel <kasperi.kauppi@gmail.com> | 2019-09-01 00:28:21 +0300 |
commit | 15707f388229ed62cddc6e95a3b0a9c653afc7a0 (patch) | |
tree | 8ca3895d0d93dd1c28cf24741d67fe3e94abf5ca | |
parent | f0fba38f40ad2beda06a638356d4ef47e62cd03b (diff) | |
download | LibGui-15707f388229ed62cddc6e95a3b0a9c653afc7a0.tar.gz LibGui-15707f388229ed62cddc6e95a3b0a9c653afc7a0.tar.bz2 LibGui-15707f388229ed62cddc6e95a3b0a9c653afc7a0.zip |
Add scrolling support for widgets
4 files changed, 30 insertions, 0 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java index ea388d1..d862b29 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java @@ -169,6 +169,12 @@ public class CottonClientScreen extends Screen { } @Override + public boolean mouseScrolled(double mouseX, double mouseY, double amount) { + if (description.getRootPanel()==null) return super.mouseScrolled(mouseX, mouseY, amount); + return description.getRootPanel().onMouseScroll((int) mouseX - left, (int) mouseY - top, amount); + } + + @Override public boolean charTyped(char ch, int keyCode) { if (description.getFocus()==null) return false; description.getFocus().onCharTyped(ch); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java index ad00784..b2d16a7 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java @@ -142,6 +142,12 @@ public class CottonInventoryScreen<T extends CottonCraftingController> extends A } @Override + public boolean mouseScrolled(double mouseX, double mouseY, double amount) { + if (description.getRootPanel()==null) return super.mouseScrolled(mouseX, mouseY, amount); + return description.getRootPanel().onMouseScroll((int) mouseX - left, (int) mouseY - top, amount); + } + + @Override protected void drawBackground(float partialTicks, int mouseX, int mouseY) {} //This is just an AbstractContainerScreen thing; most Screens don't work this way. public void paint(int mouseX, int mouseY) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java index 33ca28c..8648ceb 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java @@ -119,6 +119,13 @@ public abstract class WPanel extends WWidget { } }*/ + @Override + public boolean onMouseScroll(int x, int y, double amount) { + if (children.isEmpty()) return false; + WWidget child = hit(x, y); + return child != this && child.onMouseScroll(x - child.getX(), y - child.getY(), amount); + } + /** * Finds the most specific child node at this location. */ diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java index 1bbbf85..8362573 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java @@ -106,6 +106,17 @@ public class WWidget { } /** + * Notifies this widget that the mouse has been scrolled inside its bounds. + * @param x The X coordinate of the event, in widget-space (0 is the left edge of this widget) + * @param y The Y coordinate of the event, in widget-space (0 is the top edge of this widget) + * @param amount The scrolled amount. Positive values are up and negative values are down. + * @return true if the scrolling was handled + */ + public boolean onMouseScroll(int x, int y, double amount) { + return false; + } + + /** * Notifies this widget that a character has been typed. This method is subject to key repeat, * and may be called for characters that do not directly have a corresponding keyboard key. * @param ch the character typed |