diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-09-11 14:39:50 +0300 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-09-11 14:39:50 +0300 |
commit | e1e6697da8b9ada8f36607f5837f915d0d328a66 (patch) | |
tree | bb2933f700071bf5e0569ba2c242182d037fe4a0 /src/main/java | |
parent | d493eb273dd98bf7063146af1a8b4a71f4e93381 (diff) | |
download | LibGui-e1e6697da8b9ada8f36607f5837f915d0d328a66.tar.gz LibGui-e1e6697da8b9ada8f36607f5837f915d0d328a66.tar.bz2 LibGui-e1e6697da8b9ada8f36607f5837f915d0d328a66.zip |
Make hovering take WWidget.isWithinBounds into account
Also improved docs and added WWidget.canHover.
Diffstat (limited to 'src/main/java')
3 files changed, 30 insertions, 1 deletions
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<S extends Screen & CottonScreenImpl> { 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 @@ -464,12 +464,33 @@ public class WWidget { } /** + * 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 <i>observable property</i> that can be modified and listened to. * + * <p>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. + * + * <p>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<Boolean> hoveredProperty() { @@ -491,6 +512,7 @@ public class WWidget { /** * Sets the {@link #hoveredProperty() hovered} property. + * This is equivalent to calling <code>{@link #hoveredProperty()}.set(<i>hovered</i>)</code>. * * @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<T> implements ObservableView<T> { private static final String DEFAULT_NAME = "<unnamed>"; private boolean hasValue; |