Give dragged widgets real mouse capture #37

Merged
embeddedt merged 1 commits from rdh/celeritas:gui-dragging-better into stonecutter 2026-08-31 14:31:01 -04:00
4 changed files with 44 additions and 8 deletions

No files matched your search

@@ -18,6 +18,7 @@ public abstract class AbstractFrame extends AbstractWidget implements Interactab
protected final List<Renderable> drawable = new ArrayList<>();
protected final List<ControlElement<?>> 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;
}
@@ -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);
@@ -6,6 +6,15 @@ import java.util.stream.Stream;
public interface InteractableContainer extends Interactable {
Stream<? extends Interactable> 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<Interactable> 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));
}
@@ -79,7 +79,7 @@ public class SliderControl implements Control<Integer> {
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<Integer> {
@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) {
if (this.option.isAvailable() && button == 0 && this.sliderHeld) {
this.setValueFromMouse(mouseX);
}
return true;
}