]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/scenegraph/AbstractGraphicsNode.java
git-svn-id: https://www.simantics.org/svn/simantics/3d/branches/dev@9354 ac1ea38d...
[simantics/3d.git] / org.simantics.proconf.g3d / src / org / simantics / proconf / g3d / scenegraph / AbstractGraphicsNode.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 \r
16 import javax.vecmath.AxisAngle4d;\r
17 import javax.vecmath.AxisAngle4f;\r
18 import javax.vecmath.Matrix3d;\r
19 import javax.vecmath.Quat4d;\r
20 import javax.vecmath.Vector3f;\r
21 \r
22 import org.simantics.db.Graph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.proconf.g3d.base.G3DTools;\r
25 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
26 import org.simantics.proconf.g3d.base.VecmathJmeTools;\r
27 import org.simantics.proconf.g3d.stubs.G3DNode;\r
28 \r
29 import com.jme.scene.Node;\r
30 \r
31 \r
32 public abstract class AbstractGraphicsNode implements ISelectableNode {\r
33 \r
34         protected ThreeDimensionalEditorBase editor;\r
35         \r
36     protected IGraphicsNode parent = null;\r
37 \r
38     protected Resource shapeResource = null;\r
39   \r
40     protected boolean selected;\r
41 \r
42     protected Node parentGroup = null;\r
43     protected Node transform = null;\r
44    // protected Node center = null;\r
45     \r
46     protected String id;\r
47  \r
48     private ArrayList<IGraphicsNode> children = new ArrayList<IGraphicsNode>();\r
49 \r
50     public AbstractGraphicsNode(ThreeDimensionalEditorBase editor, IGraphicsNode parent, Graph graph, Resource shapeResource) {\r
51         assert (parent != null);\r
52         this.editor = editor;\r
53         this.parent = parent;\r
54         this.parentGroup = parent.getGroup();\r
55         this.shapeResource = shapeResource;\r
56         this.id = editor.getScenegraphAdapter().getNodeUID(shapeResource);\r
57         createGroups();\r
58         updateTransform(graph);\r
59         parent.addChild(this);\r
60     }\r
61     \r
62     @Override\r
63     public String getID() {\r
64         return id;\r
65     }\r
66     \r
67     public void setID(String id) {\r
68         this.id = id;\r
69     }\r
70 \r
71     \r
72     /* (non-Javadoc)\r
73      * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getParent()\r
74      */\r
75     public IGraphicsNode getParent() {\r
76         return parent;\r
77     }\r
78     \r
79     public Collection<IGraphicsNode> getChildren() {\r
80         return children;\r
81     }\r
82 \r
83     private void createGroups() {\r
84         transform = new Node(); // TODO : uid\r
85         parentGroup.attachChild(transform);\r
86     }\r
87     \r
88     public void addChild(IGraphicsNode node) {\r
89         children.add(node);\r
90     }\r
91     \r
92     public void removeChild(IGraphicsNode node) {\r
93         children.remove(node);\r
94     }\r
95 \r
96     /* (non-Javadoc)\r
97      * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getGroup()\r
98      */\r
99     public Node getGroup() {\r
100         //return center;\r
101         \r
102         return transform;\r
103     }\r
104 \r
105     /**\r
106      * Updates rotation and translation of the shape without recalculating\r
107      * geometry\r
108      */\r
109     public void updateTransform(Graph graph) {\r
110         G3DNode shape = getG3DNode(graph);\r
111         if (shape.getLocalPosition() != null)\r
112             transform.setLocalTranslation(VecmathJmeTools.get(G3DTools.getVectorFloat(shape.getLocalPosition())));\r
113         if (shape.getLocalOrientation() != null) \r
114             transform.setLocalRotation(VecmathJmeTools.get(G3DTools.getOrientationFloat(shape.getLocalOrientation())));\r
115 //        if (GraphicsNodeTools.hasCenter(shape)) {\r
116 //              center.setLocalTranslation(VecmathJmeTools.get(GraphicsNodeTools.getCenterFloat(shape)));\r
117 //        }\r
118         // FIXME : typically transforms are updated once per frame (root as initiator) but with threaded access transformation may be read wrong.\r
119         transform.updateWorldVectors();\r
120         editor.getScenegraphAdapter().setChanged(true);\r
121     }\r
122 \r
123 \r
124     \r
125     protected void update(Matrix3d aa) {\r
126         transform.setLocalRotation(VecmathJmeTools.get(aa));\r
127     }\r
128     protected void update(AxisAngle4f aa) {\r
129         transform.setLocalRotation(VecmathJmeTools.get(aa));\r
130     }\r
131     \r
132     protected void update(AxisAngle4d aa) {\r
133         transform.setLocalRotation(VecmathJmeTools.get(aa));\r
134     }\r
135     \r
136     protected void update(Quat4d q) {\r
137         transform.setLocalRotation(VecmathJmeTools.get(q));\r
138     }\r
139     \r
140     protected void update(Vector3f v) {\r
141         transform.setLocalTranslation(VecmathJmeTools.get(v));\r
142     }\r
143 \r
144 \r
145     public void setSelected(boolean selected) {\r
146         if (this.selected == selected)\r
147             return;\r
148         this.selected = selected;\r
149     }\r
150 \r
151     public boolean isSelected() {\r
152         return selected;\r
153     }\r
154 \r
155     /* (non-Javadoc)\r
156      * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getResource()\r
157      */\r
158     public Resource getResource() {\r
159         return shapeResource;\r
160     }\r
161 \r
162     public G3DNode getG3DNode(Graph graph) {\r
163         return new G3DNode(graph,shapeResource);\r
164     }\r
165 \r
166     /* (non-Javadoc)\r
167      * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#dispose()\r
168      */\r
169     public void dispose() {\r
170 //        if (children.size() != 0) {\r
171 //            System.out.print(getResource() + " contains children: ");\r
172 //            ArrayList<IGraphicsNode> c = new ArrayList<IGraphicsNode>(children);\r
173 //            for (IGraphicsNode n : c) {\r
174 //              System.out.print(n.getResource() + " ");\r
175 //            }\r
176 //            System.out.println();\r
177 //            return;\r
178 //        }\r
179         assert (children.size() == 0);\r
180 \r
181         transform.removeFromParent();\r
182         transform.dispose();\r
183         if (parent != null)\r
184             parent.removeChild(this);\r
185     }\r
186     \r
187     \r
188     public abstract void setPickable(boolean pickable);\r
189     \r
190     public String toString() {\r
191         return  this.getClass().toString();\r
192     }\r
193     \r
194     @Override\r
195     public int hashCode() {\r
196         return shapeResource.hashCode();\r
197     }\r
198 \r
199 \r
200 }