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