From 2aebb0c27604561119c387cb4d68bfa4ace0a04c Mon Sep 17 00:00:00 2001 From: rdh Date: Mon, 31 Aug 2026 01:27:32 -0400 Subject: [PATCH] Give dragged widgets real mouse capture --- .../impl/gui/frame/AbstractFrame.java | 15 +++++++++- .../impl/gui/frame/OptionPageFrame.java | 2 +- .../gui/framework/InteractableContainer.java | 28 ++++++++++++++++++- .../api/options/control/SliderControl.java | 9 ++---- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gui/frame/AbstractFrame.java b/common/src/main/java/org/embeddedt/embeddium/impl/gui/frame/AbstractFrame.java index 574321134..c420c628a 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gui/frame/AbstractFrame.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gui/frame/AbstractFrame.java @@ -18,6 +18,7 @@ public abstract class AbstractFrame extends AbstractWidget implements Interactab protected final List drawable = new ArrayList<>(); protected final List> controlElements = new ArrayList<>(); protected boolean renderOutline; + private Interactable capturedChild; public AbstractFrame(Dim2i dim, boolean renderOutline) { this.dim = dim; @@ -44,7 +45,9 @@ public abstract class AbstractFrame extends AbstractWidget implements Interactab drawContext.drawBorder(this.dim.x(), this.dim.y(), this.dim.getLimitX(), this.dim.getLimitY(), 0xFFAAAAAA); } for (Renderable drawable : this.drawable) { - drawable.render(drawContext, mouseX, mouseY, delta); + // While a child is captured (mid-drag), don't let the cursor also "hover" its siblings. + boolean isOtherWhileCapturing = this.capturedChild != null && drawable != this.capturedChild; + drawable.render(drawContext, isOtherWhileCapturing ? -1 : mouseX, isOtherWhileCapturing ? -1 : mouseY, delta); } } @@ -53,6 +56,16 @@ public abstract class AbstractFrame extends AbstractWidget implements Interactab return this.children.stream(); } + @Override + public Interactable getCapturedChild() { + return this.capturedChild; + } + + @Override + public void setCapturedChild(Interactable child) { + this.capturedChild = child; + } + public Dim2i getDimensions() { return this.dim; } diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gui/frame/OptionPageFrame.java b/common/src/main/java/org/embeddedt/embeddium/impl/gui/frame/OptionPageFrame.java index f7c546354..1e4c654de 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gui/frame/OptionPageFrame.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gui/frame/OptionPageFrame.java @@ -94,7 +94,7 @@ public class OptionPageFrame extends AbstractFrame { @Override public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) { - ControlElement hoveredElement = this.isMouseOver(mouseX, mouseY) ? this.controlElements.stream() + ControlElement hoveredElement = this.getCapturedChild() == null && this.isMouseOver(mouseX, mouseY) ? this.controlElements.stream() .filter(c -> c.isMouseOver(mouseX, mouseY)) .findFirst().orElse(null) : null; super.render(drawContext, mouseX, mouseY, delta); diff --git a/common/src/main/java/org/embeddedt/embeddium/impl/gui/framework/InteractableContainer.java b/common/src/main/java/org/embeddedt/embeddium/impl/gui/framework/InteractableContainer.java index 7dd59b486..694425dff 100644 --- a/common/src/main/java/org/embeddedt/embeddium/impl/gui/framework/InteractableContainer.java +++ b/common/src/main/java/org/embeddedt/embeddium/impl/gui/framework/InteractableContainer.java @@ -6,6 +6,15 @@ import java.util.stream.Stream; public interface InteractableContainer extends Interactable { Stream interactableChildren(); + /** + * the child that most recently claimed a mouseClicked. while set, drag events + * are routed straight to it instead of being re-checked against the cursor position each time, + * so a drag started on e.g. a slider isn't dropped the moment the cursor strays off its narrow + * hitbox. Cleared on mouseReleased. + */ + Interactable getCapturedChild(); + void setCapturedChild(Interactable child); + private boolean runSingleChildAction(Predicate action) { // Defensive copy to handle mutation return interactableChildren().toList().stream().anyMatch(action); @@ -17,16 +26,33 @@ public interface InteractableContainer extends Interactable { @Override default boolean mouseClicked(InteractionContext context, double mouseX, double mouseY, int button) { - return runSingleChildAction(i -> i.mouseClicked(context, mouseX, mouseY, button)); + setCapturedChild(null); + return runSingleChildAction(i -> { + if (i.mouseClicked(context, mouseX, mouseY, button)) { + setCapturedChild(i); + return true; + } + return false; + }); } @Override default boolean mouseReleased(InteractionContext context, double mouseX, double mouseY, int button) { + Interactable captured = getCapturedChild(); + setCapturedChild(null); + if (captured != null) { + captured.mouseReleased(context, mouseX, mouseY, button); + return true; + } return runSingleChildAction(i -> i.mouseReleased(context, mouseX, mouseY, button)); } @Override default boolean mouseDragged(InteractionContext context, double mouseX, double mouseY, int button, double deltaX, double deltaY) { + Interactable captured = getCapturedChild(); + if (captured != null) { + return captured.mouseDragged(context, mouseX, mouseY, button, deltaX, deltaY); + } return runSingleChildAction(i -> i.isMouseOver(mouseX, mouseY) && i.mouseDragged(context, mouseX, mouseY, button, deltaX, deltaY)); } diff --git a/common/src/main/java/org/taumc/celeritas/api/options/control/SliderControl.java b/common/src/main/java/org/taumc/celeritas/api/options/control/SliderControl.java index 4ad589cec..de32c93b7 100644 --- a/common/src/main/java/org/taumc/celeritas/api/options/control/SliderControl.java +++ b/common/src/main/java/org/taumc/celeritas/api/options/control/SliderControl.java @@ -79,7 +79,7 @@ public class SliderControl implements Control { public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) { super.render(drawContext, mouseX, mouseY, delta); - if (this.option.isAvailable() && (this.isMouseOver(mouseX, mouseY))) { + if (this.option.isAvailable() && (this.sliderHeld || this.isMouseOver(mouseX, mouseY))) { this.renderSlider(drawContext); } else { this.renderStandaloneValue(drawContext); @@ -165,11 +165,8 @@ public class SliderControl implements Control { @Override public boolean mouseDragged(InteractionContext context, double mouseX, double mouseY, int button, double deltaX, double deltaY) { - if (this.option.isAvailable() && button == 0 && this.sliderBounds.containsCursor(mouseX, mouseY)) { - if (this.sliderHeld) { - this.setValueFromMouse(mouseX); - } - + if (this.option.isAvailable() && button == 0 && this.sliderHeld) { + this.setValueFromMouse(mouseX); return true; } -- 2.54.1