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
@@ -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;
}