Hi guys, I was thinking about to use W3C DOM implementation to manage the graph scene. Then I decided to test, and check if it's a good approach. I don't think I've imagined all possibilities, problems, etc. So I want your opinion… pros, cons, etc.
I'm not suggesting to change the current JME graph scene.
The following code can explain the idea:
public class TestScene {
public static void main(String[] args) {
new TestScene();
}
public TestScene() {
// create the nodes (meshes, geometries, etc) -- it's just a test
SceneNode node1 = new SceneNode();
SceneNode node2 = new SceneNode();
SceneNode node3 = new SceneNode();
SceneNode node4 = new SceneNode();
SceneNode node5 = new SceneNode();
SceneNode node6 = new SceneNode();
SceneNode node7 = new SceneNode();
SceneNode node8 = new SceneNode();
SceneNode node9 = new SceneNode();
// change the localPosition in node5, it will be used later for debugging ;p
node5.getLocalPosition().set(-5f, 10f, 1.333f);
SceneNodeManager nodeMgr = new SceneNodeManager();
// insert them into the node manager, giving a name
nodeMgr.insert("node1", node1);
nodeMgr.insert("node2", node2);
nodeMgr.insert("node3", node3);
nodeMgr.insert("node4", node4);
nodeMgr.insert("node5", node5);
nodeMgr.insert("node6", node6);
nodeMgr.insert("node7", node7);
nodeMgr.insert("node8", node8);
nodeMgr.insert("node9", node9);
// create the graph scene passing the node manager (node *database*)
SceneGraph graph = new SceneGraph(nodeMgr);
// build the scene's hierarchy using the created nodes
graph.attachNode(null, "node1", AttachMethod.ROOT);
graph.attachNode("node1", "node2", AttachMethod.CHILD);
graph.attachNode("node2", "node3", AttachMethod.CHILD);
graph.attachNode("node3", "node4", AttachMethod.BEFORE);
graph.attachNode("node1", "node5", AttachMethod.CHILD);
graph.attachNode("node4", "node6", AttachMethod.CHILD);
graph.attachNode("node5", "node7", AttachMethod.CHILD);
graph.attachNode("node3", "node8", AttachMethod.CHILD);
graph.attachNode("node2", "node9", AttachMethod.CHILD);
// create the scene debugger and dump the scene
SceneDebugger debugger = new SceneDebugger(graph);
debugger.dumpSceneTree();
// remove the node2, and dump the scene again
graph.detachNode("node2");
debugger.dumpSceneTree();
// retrieve the node5 from the scene, and print out it's localPosition
SceneNode gotNode = graph.getNode("node5");
System.out.println("node5.localPosition = "+gotNode.getLocalPosition().toString());
}
}
The output is:
*** scene dump begin ***
-- node1
| -- node2
| | -- node4
| | | -- node6
| | -- node3
| | | -- node8
| | -- node9
| -- node5
| | -- node7
*** scene dump end ***
*** scene dump begin ***
-- node1
| -- node5
| | -- node7
*** scene dump end ***
node5.localPosition = -5.000000, 10.000000, 1.333000
Basically:
- SceneNodeManager has a Hashtable, used to store every SceneNode created. They're tied to a given name, that works as key;
- SceneNode doesn't store the name inside itself;
- SceneGraph has a org.w3c.dom.Document, where SceneNodes can be attached/inserted/whatever, and organized hierarchically.
- SceneDebugger has the methods to dump the whole tree;