Skip to content

Commit

Permalink
[ui] only set 'BaseComponent.hovered' to true when the hovered compon…
Browse files Browse the repository at this point in the history
…ent is not obscured by a different one
  • Loading branch information
gliscowo committed Jan 9, 2024
1 parent bf43ac6 commit 57ca576
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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();
}
}
19 changes: 14 additions & 5 deletions src/main/java/io/wispforest/owo/ui/base/BaseComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down

0 comments on commit 57ca576

Please sign in to comment.