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
This commit is contained in:
Andrzej Kapolka
2006-08-29 20:12:11 +00:00
parent bb05bd3f3f
commit 164e646a16
2 changed files with 57 additions and 18 deletions
@@ -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<ParticleGeometry> _respawner =
new SpatialVisitor<ParticleGeometry>(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;
@@ -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<T extends Spatial>
{
public SpatialVisitor (Class<T> 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<T> _type;
}