From e1e6697da8b9ada8f36607f5837f915d0d328a66 Mon Sep 17 00:00:00 2001
From: Juuz <6596629+Juuxel@users.noreply.github.com>
Date: Sat, 11 Sep 2021 14:39:50 +0300
Subject: Make hovering take WWidget.isWithinBounds into account
Also improved docs and added WWidget.canHover.
---
.../cotton/gui/impl/client/MouseInputHandler.java | 8 +++++++-
.../github/cottonmc/cotton/gui/widget/WWidget.java | 22 ++++++++++++++++++++++
.../cotton/gui/widget/data/ObservableProperty.java | 1 +
3 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java
index 156f967..02b8d9a 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java
@@ -94,12 +94,18 @@ public final class MouseInputHandler {
public void onMouseMove(int containerX, int containerY) {
WWidget hit = screen.getDescription().getRootPanel().hit(containerX, containerY);
- hovered.set(hit);
runTree(
hit,
widget -> widget.onMouseMove(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY())
);
+
+ @Nullable
+ WWidget hoveredWidget = runTree(
+ hit,
+ widget -> InputResult.of(widget.canHover() && widget.isWithinBounds(containerX - widget.getAbsoluteX(), containerY - widget.getAbsoluteY()))
+ );
+ hovered.set(hoveredWidget);
}
/**
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 417c01d..9106473 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
@@ -463,13 +463,34 @@ public class WWidget {
public void addPainters() {
}
+ /**
+ * Tests whether this widget receives {@linkplain #hoveredProperty() mouse hovering status}.
+ *
+ * @return true if this widget receives hovering status, false otherwise
+ * @since 4.2.0
+ */
+ public boolean canHover() {
+ return true;
+ }
+
/**
* Returns whether the user is hovering over this widget.
* The result is an observable property that can be modified and listened to.
*
+ *
This property takes into account {@link #isWithinBounds(int, int)} to check + * if the cursor is within the bounds, as well as {@link #canHover()} to enable hovering at all. + * + *
Hovering is used by LibGui itself mostly for narration support.
+ * For rendering, it might be preferable that you check the mouse coordinates in
+ * {@link #paint(MatrixStack, int, int, int, int) paint()} directly.
+ * That lets you react to different parts of the widget being hovered over.
+ *
* @experimental
* @return the {@code hovered} property
* @since 4.2.0
+ * @see #canHover()
+ * @see #isHovered()
+ * @see #setHovered(boolean)
*/
@ApiStatus.Experimental
public ObservableProperty{@link #hoveredProperty()}.set(hovered)
.
*
* @experimental
* @param hovered the new value; true if hovered, false otherwise
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java
index 5f375c2..a287c43 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/data/ObservableProperty.java
@@ -20,6 +20,7 @@ import java.util.Objects;
* @since 4.2.0
*/
@ApiStatus.Experimental
+// TODO: Add filters
public final class ObservableProperty