Give dragged widgets real mouse capture #37
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<Renderable> drawable = new ArrayList<>();
|
||||||
protected final List<ControlElement<?>> controlElements = new ArrayList<>();
|
protected final List<ControlElement<?>> controlElements = new ArrayList<>();
|
||||||
protected boolean renderOutline;
|
protected boolean renderOutline;
|
||||||
|
private Interactable capturedChild;
|
||||||
|
|
||||||
public AbstractFrame(Dim2i dim, boolean renderOutline) {
|
public AbstractFrame(Dim2i dim, boolean renderOutline) {
|
||||||
this.dim = dim;
|
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);
|
drawContext.drawBorder(this.dim.x(), this.dim.y(), this.dim.getLimitX(), this.dim.getLimitY(), 0xFFAAAAAA);
|
||||||
}
|
}
|
||||||
for (Renderable drawable : this.drawable) {
|
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();
|
return this.children.stream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Interactable getCapturedChild() {
|
||||||
|
return this.capturedChild;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCapturedChild(Interactable child) {
|
||||||
|
this.capturedChild = child;
|
||||||
|
}
|
||||||
|
|
||||||
public Dim2i getDimensions() {
|
public Dim2i getDimensions() {
|
||||||
return this.dim;
|
return this.dim;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ public class OptionPageFrame extends AbstractFrame {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) {
|
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))
|
.filter(c -> c.isMouseOver(mouseX, mouseY))
|
||||||
.findFirst().orElse(null) : null;
|
.findFirst().orElse(null) : null;
|
||||||
super.render(drawContext, mouseX, mouseY, delta);
|
super.render(drawContext, mouseX, mouseY, delta);
|
||||||
|
|||||||
+27
-1
@@ -6,6 +6,15 @@ import java.util.stream.Stream;
|
|||||||
public interface InteractableContainer extends Interactable {
|
public interface InteractableContainer extends Interactable {
|
||||||
Stream<? extends Interactable> interactableChildren();
|
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) {
|
private boolean runSingleChildAction(Predicate<Interactable> action) {
|
||||||
// Defensive copy to handle mutation
|
// Defensive copy to handle mutation
|
||||||
return interactableChildren().toList().stream().anyMatch(action);
|
return interactableChildren().toList().stream().anyMatch(action);
|
||||||
@@ -17,16 +26,33 @@ public interface InteractableContainer extends Interactable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
default boolean mouseClicked(InteractionContext context, double mouseX, double mouseY, int button) {
|
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
|
@Override
|
||||||
default boolean mouseReleased(InteractionContext context, double mouseX, double mouseY, int button) {
|
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));
|
return runSingleChildAction(i -> i.mouseReleased(context, mouseX, mouseY, button));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
default boolean mouseDragged(InteractionContext context, double mouseX, double mouseY, int button, double deltaX, double deltaY) {
|
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));
|
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) {
|
public void render(DrawContext drawContext, int mouseX, int mouseY, float delta) {
|
||||||
super.render(drawContext, mouseX, mouseY, 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);
|
this.renderSlider(drawContext);
|
||||||
} else {
|
} else {
|
||||||
this.renderStandaloneValue(drawContext);
|
this.renderStandaloneValue(drawContext);
|
||||||
@@ -165,11 +165,8 @@ public class SliderControl implements Control<Integer> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean mouseDragged(InteractionContext context, double mouseX, double mouseY, int button, double deltaX, double deltaY) {
|
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.option.isAvailable() && button == 0 && this.sliderHeld) {
|
||||||
if (this.sliderHeld) {
|
this.setValueFromMouse(mouseX);
|
||||||
this.setValueFromMouse(mouseX);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user