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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.scenegraph;
\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
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
27 public class ModelNode extends AbstractGraphicsNode implements Animatable, IGeometryNode{
\r
28 protected List<ShapeNode> shapes = new ArrayList<ShapeNode>();
\r
29 private boolean highlighted;
\r
31 protected Resource modelResource;
\r
33 private boolean geometryCreated = false;
\r
35 public ModelNode(ThreeDimensionalEditorBase editor,IGraphicsNode parent, Graph graph, Resource shapeResource) {
\r
36 super(editor,parent, graph, shapeResource);
\r
40 protected void createGeometry(Graph graph) {
\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
49 for (G3DNode node: nodes) {
\r
50 ShapeNode shape = new ShapeNode(editor,this,graph,node.getResource());
\r
53 shape.setID(getID());
\r
54 shape.setVisible(true);
\r
55 shape.updateGeometry(graph);
\r
56 createRecursive(graph,shape);
\r
58 geometryCreated = true;
\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
69 shape.setID(getID());
\r
70 shape.setVisible(true);
\r
71 shape.updateGeometry(graph);
\r
72 createRecursive(graph,shape);
\r
77 public void updateGeometry(Graph graph) {
\r
78 if (!geometryCreated) {
\r
79 createGeometry(graph);
\r
82 updateTransform(graph);
\r
83 // for (IGraphicsNode node : getChildren()) {
\r
84 // ((IGeometryNode)node).updateGeometry();
\r
86 for (ShapeNode node : shapes)
\r
87 node.updateGeometry(graph);
\r
90 public boolean isVisible() {
\r
91 for (IGraphicsNode n : getChildren())
\r
92 if (n instanceof ISelectableNode)
\r
93 if (!((ISelectableNode)n).isVisible())
\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
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
116 public void setPickable(boolean pickable) {
\r
117 for(ShapeNode n : shapes) {
\r
118 n.setPickable(pickable);
\r
122 public boolean isHighlighted() {
\r
123 return highlighted;
\r
126 public void setHighlighted(boolean selected) {
\r
127 if (this.highlighted == selected)
\r
129 this.highlighted = selected;
\r
130 for (ShapeNode n : shapes)
\r
131 n.setHighlighted(selected);
\r
136 public void setSelected(boolean selected) {
\r
137 if (this.selected == selected)
\r
139 this.selected = selected;
\r
140 for (ShapeNode n : shapes)
\r
141 n.setSelected(selected);
\r
144 public void animate(double delta, double frameTime) {
\r
145 for (ShapeNode n : shapes)
\r
146 n.animate(delta,frameTime);
\r
149 public G3DModel getG3DModel(Graph graph) {
\r
150 return new G3DModel(graph, modelResource);
\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
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
164 boolean set = false;
\r
165 for (ShapeNode n : shapes) {
\r
166 if (n.setAnimation(graph,animation))
\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
177 G3DModel model = getG3DModel(graph);
\r
178 Collection<org.simantics.g2d.stubs.anim.Animation> animations = model.getAnimation();
\r
179 int num = animations.size();
\r
181 ErrorLogger.getDefault().logWarning("Cannot set animation for " + shapeResource + " since it has no animations", null);
\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
190 return setAnimation(graph,i.next().getResource());
\r