]> gerrit.simantics Code Review - simantics/3d.git/blob
e11be5d5b7505bea625d0d61099166a49f1eea6a
[simantics/3d.git] /
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.processeditor.actions;\r
12 \r
13 import java.util.List;\r
14 \r
15 import javax.vecmath.Point3d;\r
16 import javax.vecmath.Vector3d;\r
17 \r
18 import org.eclipse.jface.action.Action;\r
19 import org.eclipse.jface.action.IToolBarManager;\r
20 import org.simantics.db.Graph;\r
21 import org.simantics.db.GraphRequestAdapter;\r
22 import org.simantics.db.GraphRequestStatus;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.layer0.utils.EntityFactory;\r
25 import org.simantics.layer0.utils.IEntity;\r
26 import org.simantics.processeditor.Activator;\r
27 import org.simantics.processeditor.ProcessResource;\r
28 import org.simantics.processeditor.common.PipingTools2;\r
29 import org.simantics.processeditor.stubs.InlineComponent;\r
30 import org.simantics.processeditor.stubs.PipeControlPoint;\r
31 import org.simantics.proconf.g3d.actions.ConstrainedTransformAction;\r
32 import org.simantics.proconf.g3d.base.G3DAPI;\r
33 import org.simantics.proconf.g3d.base.G3DTools;\r
34 import org.simantics.proconf.g3d.base.MathTools;\r
35 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
36 import org.simantics.proconf.g3d.gizmo.TransformInlineGizmo;\r
37 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;\r
38 import org.simantics.proconf.g3d.stubs.G3DNode;\r
39 \r
40 \r
41 \r
42 public class TranslateInlineComponentAction extends ConstrainedTransformAction {\r
43         \r
44     TransformInlineGizmo gizmo;\r
45     List<IGraphicsNode> mos;\r
46     Point3d start;\r
47     Point3d end;\r
48     Vector3d dir;\r
49     double istep = 10.0;\r
50     int decimals = 2;\r
51     \r
52     public TranslateInlineComponentAction(ThreeDimensionalEditorBase parent) {\r
53         super(parent);\r
54         gizmo = new TransformInlineGizmo(component.getDisplaySystem().getRenderer());\r
55 \r
56     }\r
57     \r
58     public void init() {\r
59         this.setText("Translate");\r
60         this.setToolTipText("Translate the object");\r
61         this.setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/translate_d.png"));\r
62     }\r
63     \r
64    \r
65     \r
66     @Override\r
67     public boolean usable(Graph graph, List<Resource> resources) {\r
68         // TODO : it should be possible to move multiple components on the same straight\r
69         if (resources.size() != 1)\r
70             return false;\r
71         IEntity r = EntityFactory.create(graph,resources.get(0));\r
72         PipeControlPoint pcp = null;\r
73         if (r.isInstanceOf(ProcessResource.plant3Dresource.FixedLengthInlineComponent)) {\r
74                 InlineComponent component = new InlineComponent(r);\r
75                 pcp = component.getControlPoint();\r
76                 } else {\r
77                         return false;\r
78                 }\r
79         if (pcp.getNext() == null || pcp.getPrevious() == null)\r
80                 return false;\r
81         return true;\r
82     }\r
83     \r
84     @Override\r
85     public void deactivate() {\r
86         parent.setGizmo(null);\r
87         super.deactivate();\r
88     }\r
89 \r
90     @Override\r
91     public void activate() {\r
92         parent.setGizmo(gizmo);\r
93 \r
94         String text = "";\r
95         mos = parent.getSelectionAdapter().getSelectedObjects();\r
96 //        for (IGraphicsNode mo : mos) {\r
97 //            text += GraphicsNodeTools.getWorldTranslation(mo.getGraphicsNode());//mo.getWorldPosition() + " ";\r
98 //        }\r
99 \r
100         mos.iterator().next().getGroup().attachChild(gizmo.getNode());\r
101         setInfoText(text);    \r
102            \r
103         \r
104         start = new Point3d();\r
105         end = new Point3d();\r
106         dir = new Vector3d();\r
107         parent.getSession().syncRead(new GraphRequestAdapter() {\r
108                 @Override\r
109                 public GraphRequestStatus perform(Graph g) throws Exception {\r
110                         \r
111                         InlineComponent ic = new InlineComponent(mos.iterator().next().getG3DNode(g));\r
112                         PipingTools2.getInlineMovement(ic, start, end);\r
113                         \r
114                         //PipingTools2.getInlineComponentEnds(ic, start, end);\r
115                 dir.set(end);\r
116                 dir.sub(start);\r
117 \r
118                         return GraphRequestStatus.transactionComplete();\r
119                 }\r
120         });\r
121         \r
122         updateGizmo();\r
123         parent.setViewChanged(true);\r
124     }\r
125     \r
126     \r
127     \r
128     Vector3d getTranslate() {\r
129         Vector3d translate = new Vector3d();\r
130         Vector3d o = new Vector3d();\r
131         Vector3d d = new Vector3d();\r
132         parent.createPickRay(o, d);\r
133         //Vector3d p = gizmo.getPosition();\r
134         if (gizmo.isSelected()) {\r
135             double s[] = new double[1];\r
136            \r
137             Vector3d i1 = new Vector3d();\r
138             Vector3d i2 = new Vector3d();\r
139             s = new double[2];\r
140             MathTools.intersectStraightStraight( start, dir,o, d, i2, i1,s);\r
141             translate.set(dir);\r
142             if (s[0] < 0.0)\r
143                 s[0] = 0.0;\r
144             else if (s[0] > 1.0)\r
145                 s[0] = 1.0;\r
146             translate.scale(s[0]);\r
147             translate.add(start);\r
148             \r
149             if (useConstraints) {\r
150                 Vector3d t = new Vector3d(translate);\r
151 //              FIXME : snapped point may be outside of proper range\r
152                 Point3d snap = detector.getPointSnap2(t, dir);\r
153                 if (snap != null) {\r
154                     translate = new Vector3d(snap);\r
155                 }\r
156             }\r
157         \r
158             return translate;\r
159         }\r
160         return null;\r
161     }\r
162     \r
163     Vector3d prevTranslate = new Vector3d();\r
164     \r
165     @Override\r
166     public void doChanges(Graph graph) throws Exception {\r
167         if (input.mousePressed()) {\r
168             //prevTranslate = getTranslate();\r
169             \r
170         }\r
171         if (input.mouseClicked()) {\r
172             end();\r
173             return;\r
174         }\r
175         if (!input.mouseDragged()) {\r
176             parent.getDefaultAction().update();\r
177             return;\r
178         }   \r
179         parent.setViewChanged(true);\r
180         \r
181         \r
182         List<IGraphicsNode> mos = parent.getSelectionAdapter().getSelectedObjects();\r
183         Vector3d translate = getTranslate();\r
184        \r
185         if (translate == null) {\r
186             //cameraRotateAction.update();\r
187             parent.getDefaultAction().update();\r
188             updateGizmo();\r
189             return;\r
190         }\r
191         //translate.sub(prevTranslate);\r
192        \r
193 //        if ((input.dragModifiers() & MouseEvent.CTRL_MASK) > 0) {\r
194 //            String text = "";\r
195 //            for (IGraphicsNode mo : mos) {\r
196 //                \r
197 //                Point3d p = mo.getWorldPosition();\r
198 //                p.add(translate);\r
199 //                p.x = Math.round(istep * p.x) / istep;\r
200 //                BigDecimal bx = new BigDecimal(p.x);\r
201 //                bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);\r
202 //                p.x = bx.doubleValue();\r
203 //\r
204 //                \r
205 //                 text += p + " ";\r
206 //                mo.setWorldTranslation(p);\r
207 //                \r
208 //            }\r
209 //            this.parent.setInfoText(text);\r
210 //            \r
211 //        } else {\r
212             String text = "";\r
213             for (IGraphicsNode mo : mos) {\r
214                 G3DNode node = mo.getG3DNode(graph);\r
215                 G3DAPI.setWorldPosition(node, translate);\r
216                 //G3DTools.setLocalTranslation(node, translate);\r
217                 //mo.setLocalTranslation(translate);\r
218                 text += G3DTools.getVector(node.getWorldPosition()) +" " + translate;//mo.getWorldPosition() + " " + translate;\r
219             }\r
220             setInfoText(text);\r
221 //        }\r
222         updateGizmo();\r
223 \r
224     }\r
225     \r
226     protected void updateGizmo() {\r
227         gizmo.update(camera.getCameraPos(),component);\r
228     }\r
229     \r
230     public void setInfoText(String text) {\r
231         \r
232     }\r
233 \r
234 }