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:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user