From 164e646a1600066a4db2a2af5822101930c0bd95 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 29 Aug 2006 20:12:11 +0000 Subject: [PATCH] Added a visitor class for performing operations on all instances of Spatial-derived classes in a scene graph. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@40 ed5b42cb-e716-0410-a449-f6a68f950b19 --- .../com/threerings/jme/tools/ModelViewer.java | 30 +++++-------- .../threerings/jme/util/SpatialVisitor.java | 45 +++++++++++++++++++ 2 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 src/java/com/threerings/jme/util/SpatialVisitor.java diff --git a/src/java/com/threerings/jme/tools/ModelViewer.java b/src/java/com/threerings/jme/tools/ModelViewer.java index 7b3228c3..44af1f24 100644 --- a/src/java/com/threerings/jme/tools/ModelViewer.java +++ b/src/java/com/threerings/jme/tools/ModelViewer.java @@ -95,7 +95,7 @@ import com.jme.util.TextureKey; import com.jme.util.TextureManager; import com.jme.util.export.binary.BinaryImporter; import com.jme.util.geom.Debugger; -import com.jmex.effects.particles.ParticleMesh; +import com.jmex.effects.particles.ParticleGeometry; import com.samskivert.swing.GroupLayout; import com.samskivert.swing.Spacer; @@ -111,6 +111,7 @@ import com.threerings.jme.Log; import com.threerings.jme.camera.CameraHandler; import com.threerings.jme.model.Model; import com.threerings.jme.model.TextureProvider; +import com.threerings.jme.util.SpatialVisitor; /** * A simple viewer application that allows users to examine models and their @@ -832,8 +833,8 @@ public class ModelViewer extends JmeCanvasApp JPanel bpanel = new JPanel(); bpanel.add(new JButton(new AbstractAction( _msg.get("m.respawn_particles")) { - public void actionPerformed (ActionEvent e) { - forceRespawn(_spatial); + public void actionPerformed (ActionEvent e) { + _respawner.traverse(_spatial); } })); bpanel.add(new JButton(new AbstractAction(_msg.get("m.close")) { @@ -863,21 +864,6 @@ public class ModelViewer extends JmeCanvasApp } } - /** - * Recursively forces all particles to respawn. - */ - protected void forceRespawn (Spatial spatial) - { - if (spatial instanceof ParticleMesh) { - ((ParticleMesh)spatial).forceRespawn(); - } else if (spatial instanceof Node) { - Node node = (Node)spatial; - for (int ii = 0, nn = node.getQuantity(); ii < nn; ii++) { - forceRespawn(node.getChild(ii)); - } - } - } - /** The imported scene. */ protected Spatial _spatial; @@ -1025,6 +1011,14 @@ public class ModelViewer extends JmeCanvasApp protected static Config _config = new Config("com/threerings/jme/tools/ModelViewer"); + /** Forces all particle systems to respawn. */ + protected static SpatialVisitor _respawner = + new SpatialVisitor(ParticleGeometry.class) { + protected void visit (ParticleGeometry geom) { + geom.forceRespawn(); + } + }; + /** The number of lines on the grid in each direction. */ protected static final int GRID_SIZE = 32; diff --git a/src/java/com/threerings/jme/util/SpatialVisitor.java b/src/java/com/threerings/jme/util/SpatialVisitor.java new file mode 100644 index 00000000..dccaf8d5 --- /dev/null +++ b/src/java/com/threerings/jme/util/SpatialVisitor.java @@ -0,0 +1,45 @@ +// +// $Id$ + +package com.threerings.jme.util; + +import com.jme.scene.Node; +import com.jme.scene.Spatial; + +/** + * Performs a depth-first scene graph traversal looking for {@link Spatial}s + * of a given class. + */ +public abstract class SpatialVisitor +{ + public SpatialVisitor (Class type) + { + _type = type; + } + + /** + * Traverses the given node in depth-first order, calling {@link #visit} + * for each {@link Spatial} of the configured class encountered. + */ + public void traverse (Spatial spatial) + { + if (_type.isInstance(spatial)) { + visit(_type.cast(spatial)); + } + if (spatial instanceof Node) { + Node node = (Node)spatial; + for (int ii = 0, nn = node.getQuantity(); ii < nn; ii++) { + traverse(node.getChild(ii)); + } + } + } + + /** + * Called once for each {@link Spatial} of the configured class in the + * scene graph. + */ + protected abstract void visit (T child); + + /** The type of spatial of interest. */ + protected Class _type; +}