feat(renderer): improve container dragging logic and add z-index management
This commit is contained in:
@@ -16,6 +16,7 @@ import de.extio.game_engine.renderer.model.RenderingBo;
|
|||||||
import de.extio.game_engine.renderer.model.bo.HorizontalAlignment;
|
import de.extio.game_engine.renderer.model.bo.HorizontalAlignment;
|
||||||
import de.extio.game_engine.renderer.model.bo.VerticalAlignment;
|
import de.extio.game_engine.renderer.model.bo.VerticalAlignment;
|
||||||
import de.extio.game_engine.renderer.model.event.MouseClickEvent;
|
import de.extio.game_engine.renderer.model.event.MouseClickEvent;
|
||||||
|
import de.extio.game_engine.renderer.model.event.MouseEvent;
|
||||||
import de.extio.game_engine.renderer.model.event.MouseMoveEvent;
|
import de.extio.game_engine.renderer.model.event.MouseMoveEvent;
|
||||||
import de.extio.game_engine.renderer.model.event.ViewportResizeEvent;
|
import de.extio.game_engine.renderer.model.event.ViewportResizeEvent;
|
||||||
import de.extio.game_engine.renderer.work.RendererWorkingSet;
|
import de.extio.game_engine.renderer.work.RendererWorkingSet;
|
||||||
@@ -162,26 +163,12 @@ public class Container extends AbstractClientModule implements InitializingBean
|
|||||||
|
|
||||||
protected void onMouseMoveEvent(final MouseMoveEvent event) {
|
protected void onMouseMoveEvent(final MouseMoveEvent event) {
|
||||||
if (this.draggable && event.isDrag()) {
|
if (this.draggable && event.isDrag()) {
|
||||||
Container foregroundContainer = null;
|
final Container containerUnderCursor = getContainerUnderCursor(event);
|
||||||
synchronized (DISPLAYED_CONTAINERS) {
|
if (containerUnderCursor == this) {
|
||||||
short zIndexHighest = -1;
|
|
||||||
for (final var container : DISPLAYED_CONTAINERS) {
|
|
||||||
if (container.dragging) {
|
|
||||||
foregroundContainer = container;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (SpatialUtils2.intersects(container.area.getPosition(), container.area.getDimension(), event.getScaledCoord(), ImmutableCoordI2.one())) {
|
|
||||||
if (container.zIndex > zIndexHighest) {
|
|
||||||
zIndexHighest = container.zIndex;
|
|
||||||
foregroundContainer = container;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (foregroundContainer == this) {
|
|
||||||
if (!this.dragging) {
|
if (!this.dragging) {
|
||||||
this.dragging = true;
|
this.dragging = true;
|
||||||
this.dragPrevPosition = null;
|
this.dragPrevPosition = null;
|
||||||
|
this.zIndexToTop();
|
||||||
this.LOGGER.debug("Started dragging container {}", this.id);
|
this.LOGGER.debug("Started dragging container {}", this.id);
|
||||||
}
|
}
|
||||||
if (this.dragPrevPosition != null) {
|
if (this.dragPrevPosition != null) {
|
||||||
@@ -196,6 +183,13 @@ public class Container extends AbstractClientModule implements InitializingBean
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void onMouseClickEvent(final MouseClickEvent event) {
|
protected void onMouseClickEvent(final MouseClickEvent event) {
|
||||||
|
final Container containerUnderCursor = this.getContainerUnderCursor(event);
|
||||||
|
if (containerUnderCursor == this) {
|
||||||
|
if (event.isPressed() && !this.dragging) {
|
||||||
|
this.zIndexToTop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!event.isPressed() && this.dragging) {
|
if (!event.isPressed() && this.dragging) {
|
||||||
this.dragging = false;
|
this.dragging = false;
|
||||||
this.dragPrevPosition = null;
|
this.dragPrevPosition = null;
|
||||||
@@ -264,8 +258,28 @@ public class Container extends AbstractClientModule implements InitializingBean
|
|||||||
this.draw();
|
this.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Container getContainerUnderCursor(final MouseEvent event) {
|
||||||
|
Container containerUnderCursor = null;
|
||||||
|
synchronized (DISPLAYED_CONTAINERS) {
|
||||||
|
short zIndexHighest = -1;
|
||||||
|
for (final var container : DISPLAYED_CONTAINERS) {
|
||||||
|
if (container.dragging) {
|
||||||
|
containerUnderCursor = container;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (SpatialUtils2.intersects(container.area.getPosition(), container.area.getDimension(), event.getScaledCoord(), ImmutableCoordI2.one())) {
|
||||||
|
if (container.zIndex > zIndexHighest) {
|
||||||
|
zIndexHighest = container.zIndex;
|
||||||
|
containerUnderCursor = container;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return containerUnderCursor;
|
||||||
|
}
|
||||||
|
|
||||||
private void zIndexToTop() {
|
private void zIndexToTop() {
|
||||||
if (! this.draggable) {
|
if (!this.draggable) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,10 +289,18 @@ public class Container extends AbstractClientModule implements InitializingBean
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (container.getzIndex() > this.getzIndex()) {
|
if (container.getzIndex() > this.getzIndex()) {
|
||||||
container.setzIndex((short) (container.getzIndex() - 1));
|
final var newZIndex = (short) (container.getzIndex() - 1);
|
||||||
|
if (container.getzIndex() != newZIndex) {
|
||||||
|
container.setzIndex(newZIndex);
|
||||||
|
container.draw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.setzIndex((short) (DISPLAYED_CONTAINERS.size() - 1));
|
}
|
||||||
|
final var newZIndex = (short) (DISPLAYED_CONTAINERS.size() - 1);
|
||||||
|
if (this.getzIndex() != newZIndex) {
|
||||||
|
this.setzIndex(newZIndex);
|
||||||
|
this.draw();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user