--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.proconf.g3d.scenegraph;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+import java.util.List;\r
+\r
+import org.simantics.db.Graph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.proconf.g3d.Resources;\r
+import org.simantics.proconf.g3d.animation.Animatable;\r
+import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
+import org.simantics.proconf.g3d.stubs.G3DModel;\r
+import org.simantics.proconf.g3d.stubs.G3DNode;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+public class ModelNode extends AbstractGraphicsNode implements Animatable, IGeometryNode{\r
+ protected List<ShapeNode> shapes = new ArrayList<ShapeNode>();\r
+ private boolean highlighted;\r
+ \r
+ protected Resource modelResource;\r
+ \r
+ private boolean geometryCreated = false;\r
+ \r
+ public ModelNode(ThreeDimensionalEditorBase editor,IGraphicsNode parent, Graph graph, Resource shapeResource) {\r
+ super(editor,parent, graph, shapeResource);\r
+ \r
+ }\r
+ \r
+ protected void createGeometry(Graph graph) {\r
+ \r
+ if (modelResource != null) {\r
+ G3DModel model = getG3DModel(graph);\r
+ Collection<G3DNode> nodes = model.getChild();\r
+ if (nodes.size() == 0) {\r
+ ErrorLogger.defaultLogError("ModelNode " + model.getResource().getResourceId() + " has no shapes", null);\r
+ return;\r
+ }\r
+ for (G3DNode node: nodes) {\r
+ ShapeNode shape = new ShapeNode(editor,this,graph,node.getResource());\r
+ shapes.add(shape);\r
+\r
+ shape.setID(getID());\r
+ shape.setVisible(true);\r
+ shape.updateGeometry(graph);\r
+ createRecursive(graph,shape);\r
+ } \r
+ geometryCreated = true;\r
+ }\r
+ }\r
+ \r
+ private void createRecursive(Graph graph,IGraphicsNode parentNode) {\r
+ Collection<G3DNode> nodes = parentNode.getG3DNode(graph).getChild();\r
+ for (G3DNode node: nodes) {\r
+ if (node.getRelatedObjects(Resources.g3dResource.GeometryDefinitionOf).size() == 0) {\r
+ ShapeNode shape = new ShapeNode(editor,parentNode,graph,node.getResource());\r
+ shapes.add(shape);\r
+\r
+ shape.setID(getID());\r
+ shape.setVisible(true);\r
+ shape.updateGeometry(graph);\r
+ createRecursive(graph,shape);\r
+ }\r
+ } \r
+ }\r
+ \r
+ public void updateGeometry(Graph graph) {\r
+ if (!geometryCreated) {\r
+ createGeometry(graph);\r
+ return;\r
+ }\r
+ updateTransform(graph);\r
+// for (IGraphicsNode node : getChildren()) {\r
+// ((IGeometryNode)node).updateGeometry();\r
+// }\r
+ for (ShapeNode node : shapes)\r
+ node.updateGeometry(graph);\r
+ }\r
+ \r
+ public boolean isVisible() {\r
+ for (IGraphicsNode n : getChildren())\r
+ if (n instanceof ISelectableNode)\r
+ if (!((ISelectableNode)n).isVisible())\r
+ return false;\r
+ return true;\r
+ }\r
+ \r
+ public void setVisible(boolean visible) {\r
+ for (IGraphicsNode node : getChildren()) {\r
+ if (node instanceof ISelectableNode)\r
+ ((ISelectableNode)node).setVisible(visible);\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void dispose() {\r
+ // shapes must be removed reverse order (leafs first)\r
+ for (int i = shapes.size() - 1; i >= 0; i--) {\r
+ shapes.get(i).dispose();\r
+ }\r
+ super.dispose();\r
+ }\r
+ \r
+ \r
+ @Override\r
+ public void setPickable(boolean pickable) {\r
+ for(ShapeNode n : shapes) {\r
+ n.setPickable(pickable);\r
+ }\r
+ }\r
+ \r
+ public boolean isHighlighted() {\r
+ return highlighted;\r
+ }\r
+ \r
+ public void setHighlighted(boolean selected) {\r
+ if (this.highlighted == selected)\r
+ return;\r
+ this.highlighted = selected;\r
+ for (ShapeNode n : shapes)\r
+ n.setHighlighted(selected);\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public void setSelected(boolean selected) {\r
+ if (this.selected == selected)\r
+ return;\r
+ this.selected = selected;\r
+ for (ShapeNode n : shapes)\r
+ n.setSelected(selected);\r
+ }\r
+ \r
+ public void animate(double delta, double frameTime) {\r
+ for (ShapeNode n : shapes)\r
+ n.animate(delta,frameTime);\r
+ }\r
+ \r
+ public G3DModel getG3DModel(Graph graph) {\r
+ return new G3DModel(graph, modelResource);\r
+ }\r
+ \r
+ public boolean setAnimation(Graph graph, Resource animation) {\r
+ if (modelResource == null) {\r
+ ErrorLogger.getDefault().logWarning("Cannot set animation for " + shapeResource + " since it has no graphics", null);\r
+ return false; \r
+ }\r
+ Collection<Resource> animations = graph.getObjects(modelResource, Resources.animationResource.HasAnimation);\r
+ if (!animations.contains(animation)) {\r
+ ErrorLogger.getDefault().logWarning("Cannot set animation for " + shapeResource + " since it doesn't have requested animation " + animation.getResource(), null);\r
+ return false;\r
+ }\r
+ \r
+ boolean set = false;\r
+ for (ShapeNode n : shapes) {\r
+ if (n.setAnimation(graph,animation))\r
+ set = true;\r
+ }\r
+ return set;\r
+ }\r
+ \r
+ public boolean setRandomAnimation(Graph graph) {\r
+ if (modelResource == null) {\r
+ ErrorLogger.getDefault().logWarning("Cannot set animation for " + shapeResource + " since it has no graphics", null);\r
+ return false; \r
+ }\r
+ G3DModel model = getG3DModel(graph);\r
+ Collection<org.simantics.animation.stubs.Animation> animations = model.getAnimation();\r
+ int num = animations.size();\r
+ if (num == 0) {\r
+ ErrorLogger.getDefault().logWarning("Cannot set animation for " + shapeResource + " since it has no animations", null);\r
+ return false; \r
+ }\r
+ int random = (int)Math.round(Math.random() * (num-1));\r
+ Iterator<org.simantics.animation.stubs.Animation> i = animations.iterator();\r
+ while(random > 0) {\r
+ i.next();\r
+ random--;\r
+ }\r
+ return setAnimation(graph,i.next().getResource());\r
+ \r
+ }\r
+ \r
+ \r
+}\r