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 fi.vtt.simantics.processeditor.actions;
\r
13 import java.util.List;
\r
15 import javax.vecmath.Point3d;
\r
16 import javax.vecmath.Vector3d;
\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.proconf.g3d.actions.ConstrainedTransformAction;
\r
27 import org.simantics.proconf.g3d.base.G3DAPI;
\r
28 import org.simantics.proconf.g3d.base.G3DTools;
\r
29 import org.simantics.proconf.g3d.base.MathTools;
\r
30 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;
\r
31 import org.simantics.proconf.g3d.gizmo.TransformInlineGizmo;
\r
32 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;
\r
33 import org.simantics.proconf.g3d.stubs.G3DNode;
\r
35 import fi.vtt.simantics.processeditor.Activator;
\r
36 import fi.vtt.simantics.processeditor.ProcessResource;
\r
37 import fi.vtt.simantics.processeditor.common.PipingTools2;
\r
38 import fi.vtt.simantics.processeditor.stubs.InlineComponent;
\r
39 import fi.vtt.simantics.processeditor.stubs.PipeControlPoint;
\r
42 public class TranslateInlineComponentAction extends ConstrainedTransformAction {
\r
44 TransformInlineGizmo gizmo;
\r
45 List<IGraphicsNode> mos;
\r
49 double istep = 10.0;
\r
52 public TranslateInlineComponentAction(ThreeDimensionalEditorBase parent) {
\r
54 gizmo = new TransformInlineGizmo(component.getDisplaySystem().getRenderer());
\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
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
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
79 if (pcp.getNext() == null || pcp.getPrevious() == null)
\r
85 public void deactivate() {
\r
86 parent.setGizmo(null);
\r
91 public void activate() {
\r
92 parent.setGizmo(gizmo);
\r
95 mos = parent.getSelectionAdapter().getSelectedObjects();
\r
96 // for (IGraphicsNode mo : mos) {
\r
97 // text += GraphicsNodeTools.getWorldTranslation(mo.getGraphicsNode());//mo.getWorldPosition() + " ";
\r
100 mos.iterator().next().getGroup().attachChild(gizmo.getNode());
\r
101 setInfoText(text);
\r
104 start = new Point3d();
\r
105 end = new Point3d();
\r
106 dir = new Vector3d();
\r
107 parent.getSession().syncRead(new GraphRequestAdapter() {
\r
109 public GraphRequestStatus perform(Graph g) throws Exception {
\r
111 InlineComponent ic = new InlineComponent(mos.iterator().next().getG3DNode(g));
\r
112 PipingTools2.getInlineMovement(ic, start, end);
\r
114 //PipingTools2.getInlineComponentEnds(ic, start, end);
\r
118 return GraphRequestStatus.transactionComplete();
\r
123 parent.setViewChanged(true);
\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
137 Vector3d i1 = new Vector3d();
\r
138 Vector3d i2 = new Vector3d();
\r
140 MathTools.intersectStraightStraight( start, dir,o, d, i2, i1,s);
\r
141 translate.set(dir);
\r
144 else if (s[0] > 1.0)
\r
146 translate.scale(s[0]);
\r
147 translate.add(start);
\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
163 Vector3d prevTranslate = new Vector3d();
\r
166 public void doChanges(Graph graph) throws Exception {
\r
167 if (input.mousePressed()) {
\r
168 //prevTranslate = getTranslate();
\r
171 if (input.mouseClicked()) {
\r
175 if (!input.mouseDragged()) {
\r
176 parent.getDefaultAction().update();
\r
179 parent.setViewChanged(true);
\r
182 List<IGraphicsNode> mos = parent.getSelectionAdapter().getSelectedObjects();
\r
183 Vector3d translate = getTranslate();
\r
185 if (translate == null) {
\r
186 //cameraRotateAction.update();
\r
187 parent.getDefaultAction().update();
\r
191 //translate.sub(prevTranslate);
\r
193 // if ((input.dragModifiers() & MouseEvent.CTRL_MASK) > 0) {
\r
194 // String text = "";
\r
195 // for (IGraphicsNode mo : mos) {
\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
205 // text += p + " ";
\r
206 // mo.setWorldTranslation(p);
\r
209 // this.parent.setInfoText(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
226 protected void updateGizmo() {
\r
227 gizmo.update(camera.getCameraPos(),component);
\r
230 public void setInfoText(String text) {
\r