]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/scenegraph/ModelNode.java
40e845a10b8d7a0ec901931d253d846053bb69fa
[simantics/3d.git] / org.simantics.proconf.g3d / src / org / simantics / proconf / g3d / scenegraph / ModelNode.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.scenegraph;\r
12 \r
13 import java.util.ArrayList;\r
14 import java.util.Collection;\r
15 import java.util.Iterator;\r
16 import java.util.List;\r
17 \r
18 import org.simantics.db.Graph;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.proconf.g3d.Resources;\r
21 import org.simantics.proconf.g3d.animation.Animatable;\r
22 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
23 import org.simantics.proconf.g3d.stubs.G3DModel;\r
24 import org.simantics.proconf.g3d.stubs.G3DNode;\r
25 import org.simantics.utils.ErrorLogger;\r
26 \r
27 public class ModelNode extends AbstractGraphicsNode implements Animatable, IGeometryNode{\r
28         protected List<ShapeNode> shapes = new ArrayList<ShapeNode>();\r
29     private boolean highlighted;\r
30    \r
31     protected Resource modelResource;\r
32     \r
33     private boolean geometryCreated = false;\r
34     \r
35     public ModelNode(ThreeDimensionalEditorBase editor,IGraphicsNode parent, Graph graph, Resource shapeResource) {\r
36         super(editor,parent, graph, shapeResource);\r
37         \r
38     }\r
39     \r
40     protected void createGeometry(Graph graph) {\r
41         \r
42         if (modelResource != null) {\r
43                 G3DModel model = getG3DModel(graph);\r
44             Collection<G3DNode> nodes = model.getChild();\r
45             if (nodes.size() == 0) {\r
46                 ErrorLogger.defaultLogError("ModelNode " + model.getResource() + " has no shapes", null);\r
47                 return;\r
48             }\r
49             for (G3DNode node: nodes) {\r
50                 ShapeNode shape = new ShapeNode(editor,this,graph,node.getResource());\r
51                 shapes.add(shape);\r
52 \r
53                 shape.setID(getID());\r
54                 shape.setVisible(true);\r
55                 shape.updateGeometry(graph);\r
56                 createRecursive(graph,shape);\r
57             } \r
58             geometryCreated = true;\r
59         }\r
60     }\r
61     \r
62     private void createRecursive(Graph graph,IGraphicsNode parentNode) {\r
63         Collection<G3DNode> nodes = parentNode.getG3DNode(graph).getChild();\r
64          for (G3DNode node: nodes) {\r
65                  if (node.getRelatedObjects(Resources.g3dResource.GeometryDefinitionOf).size() == 0) {\r
66                          ShapeNode shape = new ShapeNode(editor,parentNode,graph,node.getResource());\r
67                          shapes.add(shape);\r
68 \r
69                          shape.setID(getID());\r
70                          shape.setVisible(true);\r
71                          shape.updateGeometry(graph);\r
72                          createRecursive(graph,shape);\r
73                  }\r
74          } \r
75     }\r
76     \r
77     public void updateGeometry(Graph graph) {\r
78         if (!geometryCreated) {\r
79                 createGeometry(graph);\r
80                 return;\r
81         }\r
82         updateTransform(graph);\r
83 //      for (IGraphicsNode node : getChildren()) {\r
84 //                      ((IGeometryNode)node).updateGeometry();\r
85 //              }\r
86         for (ShapeNode node : shapes)\r
87             node.updateGeometry(graph);\r
88     }\r
89     \r
90     public boolean isVisible() {\r
91          for (IGraphicsNode n : getChildren())\r
92                         if (n instanceof ISelectableNode)\r
93                 if (!((ISelectableNode)n).isVisible())\r
94                         return false;\r
95         return true;\r
96    }\r
97     \r
98     public void setVisible(boolean visible) {\r
99          for (IGraphicsNode node : getChildren()) {\r
100                 if (node instanceof ISelectableNode)\r
101                         ((ISelectableNode)node).setVisible(visible);\r
102          }\r
103     }\r
104     \r
105     @Override\r
106     public void dispose() {\r
107         // shapes must be removed reverse order (leafs first)\r
108         for (int i = shapes.size() - 1; i >= 0; i--) {\r
109                 shapes.get(i).dispose();\r
110         }\r
111         super.dispose();\r
112     }\r
113     \r
114     \r
115     @Override\r
116     public void setPickable(boolean pickable) {\r
117         for(ShapeNode n : shapes) {\r
118             n.setPickable(pickable);\r
119         }\r
120     }\r
121     \r
122     public boolean isHighlighted() {\r
123         return highlighted;\r
124     }\r
125     \r
126     public void setHighlighted(boolean selected) {\r
127         if (this.highlighted == selected)\r
128             return;\r
129         this.highlighted = selected;\r
130         for (ShapeNode n : shapes)\r
131             n.setHighlighted(selected);\r
132         \r
133     }\r
134     \r
135     @Override\r
136     public void setSelected(boolean selected) {\r
137         if (this.selected == selected)\r
138             return;\r
139         this.selected = selected;\r
140         for (ShapeNode n : shapes)\r
141             n.setSelected(selected);\r
142     }\r
143     \r
144     public void animate(double delta, double frameTime) {\r
145         for (ShapeNode n : shapes)\r
146             n.animate(delta,frameTime);\r
147     }\r
148     \r
149     public G3DModel getG3DModel(Graph graph) {\r
150         return new G3DModel(graph, modelResource);\r
151     }\r
152     \r
153     public boolean setAnimation(Graph graph, Resource animation) {\r
154         if (modelResource == null) {\r
155             ErrorLogger.getDefault().logWarning("Cannot set animation for " + shapeResource + " since it has no graphics", null);\r
156             return false;    \r
157         }\r
158         Collection<Resource> animations = graph.getObjects(modelResource, Resources.animationResource.HasAnimation);\r
159         if (!animations.contains(animation)) {\r
160             ErrorLogger.getDefault().logWarning("Cannot set animation for " + shapeResource + " since it doesn't have requested animation " + animation.getResource(), null);\r
161             return false;\r
162         }\r
163   \r
164         boolean set = false;\r
165         for (ShapeNode n : shapes) {\r
166             if (n.setAnimation(graph,animation))\r
167                 set = true;\r
168         }\r
169         return set;\r
170     }\r
171     \r
172     public boolean setRandomAnimation(Graph graph) {\r
173         if (modelResource == null) {\r
174             ErrorLogger.getDefault().logWarning("Cannot set animation for " + shapeResource + " since it has no graphics", null);\r
175             return false;    \r
176         }\r
177         G3DModel model = getG3DModel(graph);\r
178         Collection<org.simantics.g2d.stubs.anim.Animation> animations = model.getAnimation();\r
179         int num = animations.size();\r
180         if (num == 0) {\r
181             ErrorLogger.getDefault().logWarning("Cannot set animation for " + shapeResource + " since it has no animations", null);\r
182             return false;            \r
183         }\r
184         int random = (int)Math.round(Math.random() * (num-1));\r
185         Iterator<org.simantics.g2d.stubs.anim.Animation> i = animations.iterator();\r
186         while(random > 0) {\r
187             i.next();\r
188             random--;\r
189         }\r
190         return setAnimation(graph,i.next().getResource());\r
191         \r
192     }\r
193     \r
194     \r
195 }\r