diff --git a/core/src/main/java/org/piccolo2d/PNode.java b/core/src/main/java/org/piccolo2d/PNode.java index 4a6c2e4..8f6b250 100644 --- a/core/src/main/java/org/piccolo2d/PNode.java +++ b/core/src/main/java/org/piccolo2d/PNode.java @@ -3234,7 +3234,7 @@ /** * Return true if this node descends from the root. - * + * * @return whether this node descends from root node */ public boolean isDescendentOfRoot() { @@ -3242,10 +3242,56 @@ } /** - * Change the order of this node in its parent's children list so that it - * will draw in back of all of its other sibling nodes. + * Raise this node within the Z-order of its parent. + * + * @since 3.0 */ - public void moveToBack() { + public void raise() { + final PNode p = parent; + if (p != null) { + final int index = parent.indexOfChild(this); + final int siblingIndex = Math.min(parent.getChildrenCount() - 1, index + 1); + if (siblingIndex != index) { + raiseAbove(parent.getChild(siblingIndex)); + } + } + } + + /** + * Lower this node within the Z-order of its parent. + * + * @since 3.0 + */ + public void lower() { + final PNode p = parent; + if (p != null) { + final int index = parent.indexOfChild(this); + final int siblingIndex = Math.max(0, index - 1); + if (siblingIndex != index) { + lowerBelow(parent.getChild(siblingIndex)); + } + } + } + + /** + * Raise this node within the Z-order of its parent to the top. + * + * @since 3.0 + */ + public void raiseToTop() { + final PNode p = parent; + if (p != null) { + p.removeChild(this); + p.addChild(this); + } + } + + /** + * Lower this node within the Z-order of its parent to the bottom. + * + * @since 3.0 + */ + public void lowerToBottom() { final PNode p = parent; if (p != null) { p.removeChild(this); @@ -3254,12 +3300,27 @@ } /** - * Change the order of this node in its parent's children list so that it - * will draw in back of the specified sibling node. - * - * @param sibling sibling to move in back of + * Raise this node within the Z-order of its parent above the specified sibling node. + * + * @since 3.0 + * @param sibling sibling node to raise this node above */ - public void moveInBackOf(final PNode sibling) { + public void raiseAbove(final PNode sibling) { + final PNode p = parent; + if (p != null && p == sibling.getParent()) { + p.removeChild(this); + final int index = p.indexOfChild(sibling); + p.addChild(index + 1, this); + } + } + + /** + * Lower this node within the Z-order of its parent below the specified sibling node. + * + * @since 3.0 + * @param sibling sibling node to lower this node below + */ + public void lowerBelow(final PNode sibling) { final PNode p = parent; if (p != null && p == sibling.getParent()) { p.removeChild(this); @@ -3269,29 +3330,50 @@ } /** - * Change the order of this node in its parent's children list so that it - * will draw in front of all of its other sibling nodes. + * Raise the specified child node within the Z-order of this. + * + * @since 3.0 + * @param child child node to raise */ - public void moveToFront() { - final PNode p = parent; - if (p != null) { - p.removeChild(this); - p.addChild(this); + public void raise(final PNode child) { + if (children != null && children.contains(child) && this.equals(child.getParent())) { + child.raise(); } } /** - * Change the order of this node in its parent's children list so that it - * will draw in front of the specified sibling node. - * - * @param sibling sibling to move in front of + * Lower the specified child node within the Z-order of this. + * + * @since 3.0 + * @param child child node to lower */ - public void moveInFrontOf(final PNode sibling) { - final PNode p = parent; - if (p != null && p == sibling.getParent()) { - p.removeChild(this); - final int index = p.indexOfChild(sibling); - p.addChild(index + 1, this); + public void lower(final PNode child) { + if (children != null && children.contains(child) && this.equals(child.getParent())) { + child.lower(); + } + } + + /** + * Raise the specified child node within the Z-order of this to the top. + * + * @since 3.0 + * @param child child node to raise to the top + */ + public void raiseToTop(final PNode child) { + if (children != null && children.contains(child) && this.equals(child.getParent())) { + child.raiseToTop(); + } + } + + /** + * Lower the specified child node within the Z-order of this to the bottom. + * + * @since 3.0 + * @param child child node to lower to the bottom + */ + public void lowerToBottom(final PNode child) { + if (children != null && children.contains(child) && this.equals(child.getParent())) { + child.lowerToBottom(); } } diff --git a/core/src/main/java/org/piccolo2d/event/PDragEventHandler.java b/core/src/main/java/org/piccolo2d/event/PDragEventHandler.java index 81fd50e..efbff83 100644 --- a/core/src/main/java/org/piccolo2d/event/PDragEventHandler.java +++ b/core/src/main/java/org/piccolo2d/event/PDragEventHandler.java @@ -44,15 +44,15 @@ public class PDragEventHandler extends PDragSequenceEventHandler { private PNode draggedNode; - private boolean moveToFrontOnPress; + private boolean raiseToTopOnPress; /** - * Constructs a drag event handler which defaults to not moving the node to - * the front on drag. + * Constructs a drag event handler which defaults to not raising the node to + * the top on drag. */ public PDragEventHandler() { draggedNode = null; - moveToFrontOnPress = false; + raiseToTopOnPress = false; setEventFilter(new PInputEventFilter(InputEvent.BUTTON1_MASK)); } @@ -88,15 +88,15 @@ /** * Starts a drag event and moves the dragged node to the front if this - * handler has been directed to do so with a call to setMoveToFrontOnDrag. + * handler has been directed to do so with a call to setRaiseToTopOnDrag. * * @param event The Event responsible for the start of the drag */ protected void startDrag(final PInputEvent event) { super.startDrag(event); draggedNode = event.getPickedNode(); - if (moveToFrontOnPress) { - draggedNode.moveToFront(); + if (raiseToTopOnPress) { + draggedNode.raiseToTop(); } } @@ -124,23 +124,23 @@ } /** - * Returns whether this drag event handler has been informed to move nodes - * to the front of all other on drag. + * Returns whether this drag event handler has been informed to raise nodes + * to the top of all other on drag. * - * @return true if dragging a node will move it to the front + * @return true if dragging a node will raise it to the top */ - public boolean getMoveToFrontOnPress() { - return moveToFrontOnPress; + public boolean getRaiseToTopOnPress() { + return raiseToTopOnPress; } /** - * Informs this drag event handler whether it should move nodes to the front + * Informs this drag event handler whether it should raise nodes to the top * when they are dragged. Default is false. * - * @param moveToFrontOnPress true if dragging a node should move it to the - * front + * @param raiseToTopOnPress true if dragging a node should raise it to the + * top */ - public void setMoveToFrontOnPress(final boolean moveToFrontOnPress) { - this.moveToFrontOnPress = moveToFrontOnPress; + public void setRaiseToTopOnPress(final boolean raiseToTopOnPress) { + this.raiseToTopOnPress = raiseToTopOnPress; } } diff --git a/core/src/test/java/org/piccolo2d/PNodeTest.java b/core/src/test/java/org/piccolo2d/PNodeTest.java index cc9ebc9..07f877b 100644 --- a/core/src/test/java/org/piccolo2d/PNodeTest.java +++ b/core/src/test/java/org/piccolo2d/PNodeTest.java @@ -1315,25 +1315,153 @@ assertFalse(grandChild.isDescendentOf(unrelated)); } - public void testMoveToBackMovesNodeToBeFirstChild() { + public void testRaise() { final PNode parent = new PNode(); - parent.addChild(new PNode()); - parent.addChild(new PNode()); parent.addChild(node); - node.moveToBack(); + parent.addChild(new PNode()); + parent.addChild(new PNode()); + node.raise(); + assertEquals(1, parent.indexOfChild(node)); + } + + public void testRaiseOnly() { + final PNode parent = new PNode(); + parent.addChild(node); + node.raise(); assertEquals(0, parent.indexOfChild(node)); } - public void testMoveToFrontMovesNodeToBeLastChild() { + public void testLower() { + final PNode parent = new PNode(); + parent.addChild(new PNode()); + parent.addChild(new PNode()); + parent.addChild(node); + node.lower(); + assertEquals(1, parent.indexOfChild(node)); + } + + public void testLowerOnly() { + final PNode parent = new PNode(); + parent.addChild(node); + node.lower(); + assertEquals(0, parent.indexOfChild(node)); + } + + public void testRaiseToTop() { final PNode parent = new PNode(); parent.addChild(node); parent.addChild(new PNode()); parent.addChild(new PNode()); - node.moveToFront(); + node.raiseToTop(); assertEquals(2, parent.indexOfChild(node)); } - public void testMoveInBackOfMovesNodeToBeforeSibling() { + public void testRaiseToTopOnly() { + final PNode parent = new PNode(); + parent.addChild(node); + node.raiseToTop(); + assertEquals(0, parent.indexOfChild(node)); + } + + public void testLowerToBottom() { + final PNode parent = new PNode(); + parent.addChild(new PNode()); + parent.addChild(new PNode()); + parent.addChild(node); + node.lowerToBottom(); + assertEquals(0, parent.indexOfChild(node)); + } + + public void testLowerToBottomOnly() { + final PNode parent = new PNode(); + parent.addChild(node); + node.lowerToBottom(); + assertEquals(0, parent.indexOfChild(node)); + } + + public void testRaiseAbove() { + final PNode parent = new PNode(); + parent.addChild(node); + final PNode sibling = new PNode(); + parent.addChild(sibling); + parent.addChild(new PNode()); + node.raiseAbove(sibling); + assertEquals(1, parent.indexOfChild(node)); + } + + public void testLowerBelow() { + final PNode parent = new PNode(); + parent.addChild(new PNode()); + final PNode sibling = new PNode(); + parent.addChild(sibling); + parent.addChild(node); + node.lowerBelow(sibling); + assertEquals(1, parent.indexOfChild(node)); + } + + public void testRaiseChild() { + final PNode child0 = new PNode(); + final PNode child1 = new PNode(); + final PNode child2 = new PNode(); + node.addChild(child0); + node.addChild(child1); + node.addChild(child2); + node.raise(child0); + assertEquals(1, node.indexOfChild(child0)); + } + + public void testLowerChild() { + final PNode child0 = new PNode(); + final PNode child1 = new PNode(); + final PNode child2 = new PNode(); + node.addChild(child0); + node.addChild(child1); + node.addChild(child2); + node.lower(child2); + assertEquals(1, node.indexOfChild(child2)); + } + + public void testRaiseChildToTop() { + final PNode child0 = new PNode(); + final PNode child1 = new PNode(); + final PNode child2 = new PNode(); + node.addChild(child0); + node.addChild(child1); + node.addChild(child2); + node.raiseToTop(child0); + assertEquals(2, node.indexOfChild(child0)); + } + + public void testLowerChildToBottom() { + final PNode child0 = new PNode(); + final PNode child1 = new PNode(); + final PNode child2 = new PNode(); + node.addChild(child0); + node.addChild(child1); + node.addChild(child2); + node.lowerToBottom(child2); + assertEquals(0, node.indexOfChild(child2)); + } + + public void testLowerToBottomMovesNodeToBeFirstChild() { + final PNode parent = new PNode(); + parent.addChild(new PNode()); + parent.addChild(new PNode()); + parent.addChild(node); + node.lowerToBottom(); + assertEquals(0, parent.indexOfChild(node)); + } + + public void testRaiseToTopMovesNodeToBeLastChild() { + final PNode parent = new PNode(); + parent.addChild(node); + parent.addChild(new PNode()); + parent.addChild(new PNode()); + node.raiseToTop(); + assertEquals(2, parent.indexOfChild(node)); + } + + public void testLowerBelowMovesNodeToBeforeSibling() { final PNode parent = new PNode(); final PNode sibling = new PNode(); @@ -1342,11 +1470,11 @@ parent.addChild(new PNode()); parent.addChild(sibling); - node.moveInBackOf(sibling); + node.lowerBelow(sibling); assertEquals(2, parent.indexOfChild(node)); } - public void testMoveInFrontOfMovesNodeToAfterSibling() { + public void testRaiseAboveMovesNodeToAfterSibling() { final PNode parent = new PNode(); final PNode sibling = new PNode(); @@ -1355,11 +1483,11 @@ parent.addChild(new PNode()); parent.addChild(sibling); - node.moveInFrontOf(sibling); + node.raiseAbove(sibling); assertEquals(3, parent.indexOfChild(node)); } - public void testMoveInFrontOfDoesNothingIfNotSibling() { + public void testRaiseAboveDoesNothingIfNotSibling() { final PNode parent = new PNode(); final PNode stranger = new PNode(); @@ -1367,11 +1495,11 @@ parent.addChild(new PNode()); parent.addChild(new PNode()); - node.moveInFrontOf(stranger); + node.raiseAbove(stranger); assertEquals(0, parent.indexOfChild(node)); } - public void testMoveInBackOfDoesNothingIfNotSibling() { + public void testLowerBelowDoesNothingIfNotSibling() { final PNode parent = new PNode(); final PNode stranger = new PNode(); @@ -1379,7 +1507,7 @@ parent.addChild(new PNode()); parent.addChild(new PNode()); - node.moveInBackOf(stranger); + node.lowerBelow(stranger); assertEquals(0, parent.indexOfChild(node)); } diff --git a/core/src/test/java/org/piccolo2d/event/PDragEventHandlerTest.java b/core/src/test/java/org/piccolo2d/event/PDragEventHandlerTest.java index 9092d94..c4ac377 100644 --- a/core/src/test/java/org/piccolo2d/event/PDragEventHandlerTest.java +++ b/core/src/test/java/org/piccolo2d/event/PDragEventHandlerTest.java @@ -42,13 +42,13 @@ handler = new PDragEventHandler(); } - public void testMoveToFrontOnPressDefaultToFalse() { - assertFalse(handler.getMoveToFrontOnPress()); + public void testRaiseToTopOnPressDefaultToFalse() { + assertFalse(handler.getRaiseToTopOnPress()); } - public void testMoveToFrontOnPressPersists() { - handler.setMoveToFrontOnPress(true); - assertTrue(handler.getMoveToFrontOnPress()); + public void testRaiseToTopOnPressPersists() { + handler.setRaiseToTopOnPress(true); + assertTrue(handler.getRaiseToTopOnPress()); } } diff --git a/examples/src/main/java/org/piccolo2d/examples/GridExample.java b/examples/src/main/java/org/piccolo2d/examples/GridExample.java index b8900ce..b243956 100644 --- a/examples/src/main/java/org/piccolo2d/examples/GridExample.java +++ b/examples/src/main/java/org/piccolo2d/examples/GridExample.java @@ -155,7 +155,7 @@ protected void startDrag(final PInputEvent event) { super.startDrag(event); draggedNode = event.getPickedNode(); - draggedNode.moveToFront(); + draggedNode.raiseToTop(); nodeStartPosition = draggedNode.getOffset(); } diff --git a/examples/src/main/java/org/piccolo2d/tutorial/PiccoloPresentation.java b/examples/src/main/java/org/piccolo2d/tutorial/PiccoloPresentation.java index 652c4af..2ff7062 100644 --- a/examples/src/main/java/org/piccolo2d/tutorial/PiccoloPresentation.java +++ b/examples/src/main/java/org/piccolo2d/tutorial/PiccoloPresentation.java @@ -74,7 +74,7 @@ final PNode picked = event.getPickedNode(); if (picked.getParent() == slideBar) { - picked.moveToFront(); + picked.raiseToTop(); if (picked.getScale() == 1) { goToSlide(null); } @@ -100,7 +100,7 @@ currentSlide = slide; if (currentSlide != null) { - currentSlide.moveToFront(); + currentSlide.raiseToTop(); currentSlide.animateToTransform((AffineTransform) currentSlide.getAttribute("large"), 1000); } }