From 57ca5760eb43c8ff8eca03ab46d9ecc4b47374e4 Mon Sep 17 00:00:00 2001 From: glisco Date: Wed, 10 Jan 2024 00:25:48 +0100 Subject: [PATCH] [ui] only set 'BaseComponent.hovered' to true when the hovered component is not obscured by a different one --- .../owo/mixin/ui/ClickableWidgetMixin.java | 7 +++++++ .../wispforest/owo/ui/base/BaseComponent.java | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/wispforest/owo/mixin/ui/ClickableWidgetMixin.java b/src/main/java/io/wispforest/owo/mixin/ui/ClickableWidgetMixin.java index cfebc0cc..fa6ce587 100644 --- a/src/main/java/io/wispforest/owo/mixin/ui/ClickableWidgetMixin.java +++ b/src/main/java/io/wispforest/owo/mixin/ui/ClickableWidgetMixin.java @@ -9,6 +9,7 @@ import io.wispforest.owo.ui.parsing.UIParsing; import io.wispforest.owo.ui.util.FocusHandler; import io.wispforest.owo.util.EventSource; +import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.tooltip.TooltipComponent; import net.minecraft.client.gui.widget.ClickableWidget; import org.jetbrains.annotations.Nullable; @@ -30,6 +31,7 @@ public abstract class ClickableWidgetMixin implements ComponentStub, net.minecra @Shadow public boolean active; + @Shadow protected boolean hovered; @Unique protected VanillaWidgetComponent owo$wrapper = null; @@ -363,4 +365,9 @@ public void updateY(int y) { protected CursorStyle owo$preferredCursorStyle() { return CursorStyle.POINTER; } + + @Inject(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/widget/ClickableWidget;renderWidget(Lnet/minecraft/client/gui/DrawContext;IIF)V")) + private void setHovered(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) { + if (this.owo$wrapper != null) this.hovered = this.hovered && this.owo$wrapper.hovered(); + } } diff --git a/src/main/java/io/wispforest/owo/ui/base/BaseComponent.java b/src/main/java/io/wispforest/owo/ui/base/BaseComponent.java index b4947f54..bd820ef5 100644 --- a/src/main/java/io/wispforest/owo/ui/base/BaseComponent.java +++ b/src/main/java/io/wispforest/owo/ui/base/BaseComponent.java @@ -141,13 +141,22 @@ public void update(float delta, int mouseX, int mouseY) { boolean nowHovered = this.isInBoundingBox(mouseX, mouseY); if (this.hovered != nowHovered) { - this.hovered = nowHovered; + this.updateHoveredState(mouseX, mouseY, nowHovered); + } + } - if (nowHovered) { - this.mouseEnterEvents.sink().onMouseEnter(); - } else { - this.mouseLeaveEvents.sink().onMouseLeave(); + protected void updateHoveredState(int mouseX, int mouseY, boolean nowHovered) { + this.hovered = nowHovered; + + if (nowHovered) { + if (this.root() == null || this.root().childAt(mouseX, mouseY) != this) { + this.hovered = false; + return; } + + this.mouseEnterEvents.sink().onMouseEnter(); + } else { + this.mouseLeaveEvents.sink().onMouseLeave(); } }