From 15707f388229ed62cddc6e95a3b0a9c653afc7a0 Mon Sep 17 00:00:00 2001 From: Juuxel Date: Sun, 1 Sep 2019 00:28:21 +0300 Subject: Add scrolling support for widgets --- .../github/cottonmc/cotton/gui/client/CottonClientScreen.java | 6 ++++++ .../cottonmc/cotton/gui/client/CottonInventoryScreen.java | 6 ++++++ .../java/io/github/cottonmc/cotton/gui/widget/WPanel.java | 7 +++++++ .../java/io/github/cottonmc/cotton/gui/widget/WWidget.java | 11 +++++++++++ 4 files changed, 30 insertions(+) (limited to 'src/main') 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 @@ -168,6 +168,12 @@ public class CottonClientScreen extends Screen { return result; } + @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; 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 @@ -141,6 +141,12 @@ public class CottonInventoryScreen extends A return result; } + @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. 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 @@ -105,6 +105,17 @@ public class WWidget { public void onClick(int x, int y, int button) { } + /** + * 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. -- cgit From 60ec3fbc34d8266c15aeb1d0be7795efd821d73d Mon Sep 17 00:00:00 2001 From: Juuxel Date: Sun, 1 Sep 2019 00:48:19 +0300 Subject: Improve scrolling --- .../io/github/cottonmc/cotton/gui/client/CottonClientScreen.java | 8 +++++++- .../github/cottonmc/cotton/gui/client/CottonInventoryScreen.java | 8 +++++++- src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java | 5 ++--- src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java | 4 +--- 4 files changed, 17 insertions(+), 8 deletions(-) (limited to 'src/main') 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 d862b29..26176e7 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 @@ -171,7 +171,13 @@ 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); + WPanel root = description.getRootPanel(); + WWidget child = root.hit((int) mouseX - left, (int) mouseY - top); + if (child == root) return false; + // Use root.onMouseScroll to work in cases where the hit result is in a nested panel + // and the mouse position can't be moved to the widget space by subtracting the widget's position + root.onMouseScroll((int) mouseX - left, (int) mouseY - top, amount); + return true; } @Override 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 b2d16a7..961d6f9 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 @@ -144,7 +144,13 @@ public class CottonInventoryScreen 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); + WPanel root = description.getRootPanel(); + WWidget child = root.hit((int) mouseX - left, (int) mouseY - top); + if (child == root) return false; + // Use root.onMouseScroll to work in cases where the hit result is in a nested panel + // and the mouse position can't be moved to the widget space by subtracting the widget's position + root.onMouseScroll((int) mouseX - left, (int) mouseY - top, amount); + return true; } @Override 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 8648ceb..14faa2d 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 @@ -120,10 +120,9 @@ public abstract class WPanel extends WWidget { }*/ @Override - public boolean onMouseScroll(int x, int y, double amount) { - if (children.isEmpty()) return false; + public void onMouseScroll(int x, int y, double amount) { WWidget child = hit(x, y); - return child != this && child.onMouseScroll(x - child.getX(), y - child.getY(), amount); + if (child != this) child.onMouseScroll(x - child.getX(), y - child.getY(), amount); } /** 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 8362573..e4c5746 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 @@ -110,10 +110,8 @@ public class WWidget { * @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; + public void onMouseScroll(int x, int y, double amount) { } /** -- cgit From 82908f7397658b12d045df103db0f32779f33089 Mon Sep 17 00:00:00 2001 From: Juuxel Date: Sun, 1 Sep 2019 00:58:49 +0300 Subject: Improve scrolling again --- .../github/cottonmc/cotton/gui/client/CottonClientScreen.java | 11 +++++++---- .../cottonmc/cotton/gui/client/CottonInventoryScreen.java | 11 +++++++---- .../java/io/github/cottonmc/cotton/gui/widget/WPanel.java | 6 ------ 3 files changed, 14 insertions(+), 14 deletions(-) (limited to 'src/main') 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 26176e7..11d7dbd 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 @@ -171,12 +171,15 @@ 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); + WPanel root = description.getRootPanel(); - WWidget child = root.hit((int) mouseX - left, (int) mouseY - top); + int containerX = (int)mouseX-left; + int containerY = (int)mouseY-top; + + WWidget child = root.hit(containerX, containerY); if (child == root) return false; - // Use root.onMouseScroll to work in cases where the hit result is in a nested panel - // and the mouse position can't be moved to the widget space by subtracting the widget's position - root.onMouseScroll((int) mouseX - left, (int) mouseY - top, amount); + + child.onMouseScroll(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY(), amount); return true; } 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 961d6f9..e3df6d6 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 @@ -144,12 +144,15 @@ public class CottonInventoryScreen extends A @Override public boolean mouseScrolled(double mouseX, double mouseY, double amount) { if (description.getRootPanel()==null) return super.mouseScrolled(mouseX, mouseY, amount); + WPanel root = description.getRootPanel(); - WWidget child = root.hit((int) mouseX - left, (int) mouseY - top); + int containerX = (int)mouseX-left; + int containerY = (int)mouseY-top; + + WWidget child = root.hit(containerX, containerY); if (child == root) return false; - // Use root.onMouseScroll to work in cases where the hit result is in a nested panel - // and the mouse position can't be moved to the widget space by subtracting the widget's position - root.onMouseScroll((int) mouseX - left, (int) mouseY - top, amount); + + child.onMouseScroll(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY(), amount); return true; } 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 14faa2d..33ca28c 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,12 +119,6 @@ public abstract class WPanel extends WWidget { } }*/ - @Override - public void onMouseScroll(int x, int y, double amount) { - WWidget child = hit(x, y); - if (child != this) child.onMouseScroll(x - child.getX(), y - child.getY(), amount); - } - /** * Finds the most specific child node at this location. */ -- cgit From e17a411ca512b6b08a577941f04659d7d007ec3d Mon Sep 17 00:00:00 2001 From: Juuxel Date: Sun, 1 Sep 2019 09:24:35 +0300 Subject: Deliver mouse scrolling events to the root panel as well --- .../java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java | 2 -- .../io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java | 2 -- 2 files changed, 4 deletions(-) (limited to 'src/main') 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 11d7dbd..91cf7b5 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 @@ -177,8 +177,6 @@ public class CottonClientScreen extends Screen { int containerY = (int)mouseY-top; WWidget child = root.hit(containerX, containerY); - if (child == root) return false; - child.onMouseScroll(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY(), amount); return true; } 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 e3df6d6..07cd84b 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 @@ -150,8 +150,6 @@ public class CottonInventoryScreen extends A int containerY = (int)mouseY-top; WWidget child = root.hit(containerX, containerY); - if (child == root) return false; - child.onMouseScroll(containerX - child.getAbsoluteX(), containerY - child.getAbsoluteY(), amount); return true; } -- cgit