Revision: 808 Author: heuermh Date: Fri Oct 23 13:36:43 2009 Log: making PCamera internally consistent with regards to layer references, best we can do without making related methods final http://code.google.com/p/piccolo2d/source/detail?r=808
Modified: /piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCamera.java /piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/PCameraTest.java ======================================= --- /piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCamera.java Tue Oct 20 14:02:49 2009 +++ /piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/PCamera.java Fri Oct 23 13:36:43 2009 @@ -336,12 +336,9 @@ * by this camera is empty */ public PBounds getUnionOfLayerFullBounds() { - // todo: this method is implemented inconsistently with regards to non-final methods - // a subclass might override getLayerCount and/or getLayer, thus this method should either - // use layers.size() and layers.get(index) or getLayerCount() and getLayer(index) final PBounds result = new PBounds(); - final int count = getLayerCount(); - for (int i = 0; i < count; i++) { + final int size = layers.size(); + for (int i = 0; i < size; i++) { final PLayer each = (PLayer) layers.get(i); result.add(each.getFullBoundsReference()); } @@ -375,11 +372,8 @@ * @param paintContext context in which painting occurs */ protected void paintCameraView(final PPaintContext paintContext) { - // todo: this method is implemented inconsistently with regards to non-final methods - // a subclass might override getLayerCount and/or getLayer, thus this method should either - // use layers.size() and layers.get(index) or getLayerCount() and getLayer(index) - final int count = getLayerCount(); - for (int i = 0; i < count; i++) { + final int size = layers.size(); + for (int i = 0; i < size; i++) { final PLayer each = (PLayer) layers.get(i); each.fullPaint(paintContext); } @@ -402,8 +396,9 @@ final Color boundsColor = Color.red; final Color fullBoundsColor = new Color(1.0f, 0f, 0f, 0.2f); - for (int i = 0; i < getLayerCount(); i++) { - getLayer(i).getAllNodes(null, nodes); + final int size = layers.size(); + for (int i = 0; i < size; i++) { + ((PLayer) layers.get(i)).getAllNodes(null, nodes); } final Iterator i = getAllNodes(null, nodes).iterator(); @@ -521,11 +516,8 @@ * camera were picked */ protected boolean pickCameraView(final PPickPath pickPath) { - // todo: this method is implemented inconsistently with regards to non-final methods - // a subclass might override getLayerCount and/or getLayer, thus this method should either - // use layers.size() and layers.get(index) or getLayerCount() and getLayer(index) - final int count = getLayerCount(); - for (int i = count - 1; i >= 0; i--) { + final int size = layers.size(); + for (int i = size - 1; i >= 0; i--) { final PLayer each = (PLayer) layers.get(i); if (each.fullPick(pickPath)) { return true; ======================================= --- /piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/PCameraTest.java Fri Oct 23 12:36:22 2009 +++ /piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/PCameraTest.java Fri Oct 23 13:36:43 2009 @@ -35,6 +35,7 @@ import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.io.IOException; +import java.util.Collection; import junit.framework.TestCase; import edu.umd.cs.piccolo.activities.PActivity; @@ -42,6 +43,7 @@ import edu.umd.cs.piccolo.util.PAffineTransform; import edu.umd.cs.piccolo.util.PBounds; import edu.umd.cs.piccolo.util.PDebug; +import edu.umd.cs.piccolo.util.PNodeFilter; import edu.umd.cs.piccolo.util.PPaintContext; import edu.umd.cs.piccolo.util.PPickPath; @@ -345,6 +347,90 @@ // expected } } + + public void testTooFewLayersCamera() { + PCamera tooFew = new TooFewLayersCamera(); + MockPLayer layer0 = new MockPLayer(); + MockPLayer layer1 = new MockPLayer(); + tooFew.addLayer(layer0); + tooFew.addLayer(layer1); + assertEquals(layer0, tooFew.getLayer(0)); + assertEquals(layer1, tooFew.getLayer(1)); + assertEquals(layer0, tooFew.getLayersReference().get(0)); + assertEquals(layer1, tooFew.getLayersReference().get(1)); + assertEquals(0, tooFew.indexOfLayer(layer0)); + assertEquals(0, tooFew.indexOfLayer(layer0)); + + // pickCameraView + PPickPath pickPath = new PPickPath(tooFew, new PBounds(0, 0, 400, 400)); + tooFew.pickCameraView(pickPath); + assertTrue(layer0.fullPickCalled()); + assertTrue(layer1.fullPickCalled()); + + // paintCameraView + BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); + Graphics2D graphics = image.createGraphics(); + PPaintContext paintContext = new PPaintContext(graphics); + tooFew.paintCameraView(paintContext); + assertTrue(layer0.fullPaintCalled()); + assertTrue(layer1.fullPaintCalled()); + + // getUnionOfLayerFullBounds + tooFew.getUnionOfLayerFullBounds(); + assertTrue(layer0.fullBoundsReferenceCalled()); + assertTrue(layer1.fullBoundsReferenceCalled()); + + // paintDebugInfo + PDebug.debugBounds = true; + tooFew.paintDebugInfo(paintContext); + assertTrue(layer0.getAllNodesCalled()); + assertTrue(layer1.getAllNodesCalled()); + PDebug.debugBounds = false; + + graphics.dispose(); + } + + public void testTooManyLayersCamera() { + PCamera tooMany = new TooManyLayersCamera(); + MockPLayer layer0 = new MockPLayer(); + MockPLayer layer1 = new MockPLayer(); + tooMany.addLayer(layer0); + tooMany.addLayer(layer1); + assertEquals(layer0, tooMany.getLayer(0)); + assertEquals(layer1, tooMany.getLayer(1)); + assertEquals(layer0, tooMany.getLayersReference().get(0)); + assertEquals(layer1, tooMany.getLayersReference().get(1)); + assertEquals(0, tooMany.indexOfLayer(layer0)); + assertEquals(0, tooMany.indexOfLayer(layer0)); + + // pickCameraView + PPickPath pickPath = new PPickPath(tooMany, new PBounds(0, 0, 400, 400)); + tooMany.pickCameraView(pickPath); + assertTrue(layer0.fullPickCalled()); + assertTrue(layer1.fullPickCalled()); + + // paintCameraView + BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); + Graphics2D graphics = image.createGraphics(); + PPaintContext paintContext = new PPaintContext(graphics); + tooMany.paintCameraView(paintContext); + assertTrue(layer0.fullPaintCalled()); + assertTrue(layer1.fullPaintCalled()); + + // getUnionOfLayerFullBounds + tooMany.getUnionOfLayerFullBounds(); + assertTrue(layer0.fullBoundsReferenceCalled()); + assertTrue(layer1.fullBoundsReferenceCalled()); + + // paintDebugInfo + PDebug.debugBounds = true; + tooMany.paintDebugInfo(paintContext); + assertTrue(layer0.getAllNodesCalled()); + assertTrue(layer1.getAllNodesCalled()); + PDebug.debugBounds = false; + + graphics.dispose(); + } static class MockPComponent implements PComponent { @@ -363,4 +449,79 @@ public void setInteracting(final boolean interacting) { } } -} + + /** + * Mock PLayer. Should consider using mock library in version 2.0. + */ + private static final class MockPLayer extends PLayer { + private static final long serialVersionUID = 1L; + private boolean fullBoundsReferenceCalled = false; + private boolean fullPaintCalled = false; + private boolean getAllNodesCalled = false; + private boolean fullPickCalled = false; + + /** {...@inheritdoc} */ + public PBounds getFullBoundsReference() { + fullBoundsReferenceCalled = true; + return super.getFullBoundsReference(); + } + + /** {...@inheritdoc} */ + public void fullPaint(final PPaintContext paintContext) { + fullPaintCalled = true; + super.fullPaint(paintContext); + } + + /** {...@inheritdoc} */ + public Collection getAllNodes(final PNodeFilter filter, final Collection nodes) { + getAllNodesCalled = true; + return super.getAllNodes(filter, nodes); + } + + /** {...@inheritdoc} */ + public boolean fullPick(final PPickPath pickPath) { + fullPickCalled = true; + return super.fullPick(pickPath); + } + + private boolean fullBoundsReferenceCalled() { + return fullBoundsReferenceCalled; + } + + private boolean fullPaintCalled() { + return fullPaintCalled; + } + + private boolean getAllNodesCalled() { + return getAllNodesCalled; + } + + private boolean fullPickCalled() { + return fullPickCalled; + } + } + + /** + * Subclass of PCamera that advertises too few layers. + */ + private static final class TooFewLayersCamera extends PCamera { + private static final long serialVersionUID = 1L; + + /** {...@inheritdoc} */ + public int getLayerCount() { + return Math.max(0, super.getLayerCount() - 1); + } + } + + /** + * Subclass of PCamera that advertises too many layers. + */ + private static final class TooManyLayersCamera extends PCamera { + private static final long serialVersionUID = 1L; + + /** {...@inheritdoc} */ + public int getLayerCount() { + return super.getLayerCount() + 1; + } + } +} --~--~---------~--~----~------------~-------~--~----~ Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en -~----------~----~----~----~------~----~------~--~---