From 15707f388229ed62cddc6e95a3b0a9c653afc7a0 Mon Sep 17 00:00:00 2001
From: Juuxel <kasperi.kauppi@gmail.com>
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')

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<T extends CottonCraftingController> 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