]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/TranslateInlineAction.java
Get inline component direction irrespectively of connectivity
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / actions / TranslateInlineAction.java
1 package org.simantics.plant3d.actions;
2
3 import java.awt.event.KeyEvent;
4 import java.awt.event.MouseEvent;
5
6 import javax.vecmath.Point3d;
7 import javax.vecmath.Vector3d;
8
9 import org.simantics.g3d.math.MathTools;
10 import org.simantics.g3d.math.Ray;
11 import org.simantics.g3d.scenegraph.IG3DNode;
12 import org.simantics.g3d.scenegraph.base.INode;
13 import org.simantics.g3d.toolbar.ToolComposite;
14 import org.simantics.g3d.vtk.common.VTKNodeMap;
15 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
16 import org.simantics.g3d.vtk.swt.TranslateAction;
17 import org.simantics.g3d.vtk.utils.vtkUtil;
18 import org.simantics.plant3d.Activator;
19 import org.simantics.plant3d.scenegraph.InlineComponent;
20 import org.simantics.plant3d.scenegraph.PipelineComponent;
21 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
22
23 public class TranslateInlineAction extends TranslateAction{
24         
25     private boolean inline;
26     
27         private Vector3d s;
28         private Vector3d e;
29         private Vector3d dir;
30
31         public TranslateInlineAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap, ToolComposite toolComposite) {
32                 super(panel, nodeMap, toolComposite);
33                 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
34         }
35         
36         @Override
37         public void setNode(IG3DNode node) {
38                 super.setNode(node);
39                 if (node instanceof InlineComponent) {
40                         InlineComponent comp = (InlineComponent)node;
41                         if (comp.isVariableLength()) {
42                 setEnabled(false);
43                         } else if (comp.getNext() == null || comp.getPrevious() == null) {
44                 setEnabled(true);
45                 inline = false;
46                 dir = comp.getControlPoint().getInlineDir();
47                 dir.normalize();
48             } else {
49                                 setEnabled(true);
50                                 inline = true;
51                                 PipelineComponent prev = comp.getPrevious();
52                                 PipelineComponent next = comp.getNext();
53                                 if (prev == null || next == null) {
54                                         // TODO : we should support open ended translation (when translated component is the first or last of the run).
55                                         setEnabled(false);
56                                         return;
57                                 }
58                                 if (prev.getControlPoint().isInline() && !prev.getControlPoint().isFixedLength() && prev.getPrevious() != null)
59                                         prev = prev.getPrevious();
60                                 if (next.getControlPoint().isInline() && !next.getControlPoint().isFixedLength() && next.getNext() != null) {
61                                         next = next.getNext();
62                                 }
63                                 Point3d ns = new Point3d();
64                                 Point3d ne = new Point3d();
65                                 Point3d ps = new Point3d();
66                                 Point3d pe = new Point3d();
67                                 next.getEnds(ns, ne);
68                                 prev.getEnds(ps, pe);
69                                 dir = comp.getControlPoint().getInlineDir();
70                                 // We may have offsets in the path leg, hence we have to project the coordinates.
71                                 Vector3d wp = node.getWorldPosition();
72                                 if (prev.getControlPoint().isVariableLength())
73                                         s = MathTools.closestPointOnStraight(ps, wp, dir);
74                                 else
75                                         s = MathTools.closestPointOnStraight(pe, wp, dir);
76                                 if (next.getControlPoint().isVariableLength())
77                                         e = MathTools.closestPointOnStraight(ne, wp, dir);
78                                 else
79                                         e = MathTools.closestPointOnStraight(ns, wp, dir);
80                                 // Remove component's own space from end points to get actual movement range
81                                 double l = comp.getControlPoint().getInlineLength();
82                                 Vector3d ld = new Vector3d(dir);
83                                 ld.scale(l);
84                                 s.add(ld);
85                                 e.sub(ld);
86                         }
87                 } else {
88                         setEnabled(false);
89                 }
90         }
91         
92         @Override
93         protected void createTools(ToolComposite toolComposite) {
94             
95         }
96         
97         @Override
98         public boolean keyPressed(KeyEvent e) {
99                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
100                         panel.useDefaultAction();
101                 if (!inline) {
102                     return super.keyPressed(e);
103                 } else {
104                 if (valid)
105                         return true;
106                 
107                 update();
108                 return true;
109                 }
110         }
111         
112         @Override
113         public void setWorldCoord(boolean b) {
114                 
115         }
116         
117         @Override
118         public boolean mouseDragged(MouseEvent e) {
119                 //if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) { 
120             if (e.getButton() == MouseEvent.BUTTON1  && valid) {
121                         if (inline) {
122                         Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
123                         //System.out.println("translate " + translate);
124                         if (translate == null)
125                                 return true;
126                         //boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
127                         Vector3d pos = new Vector3d(node.getWorldPosition());
128                         
129 //                      System.out.println(pos + " " + translate);
130                         //pos.add(translate);
131                         pos.set(translate);
132                         //pos = constaints(pos, step);
133                         setWorldPos(pos);
134                         update();
135                         } else {
136                             super.mouseDragged(e);
137                         }
138                 } else {
139                         getDefaultAction().mouseDragged(e);
140                         update();
141                 } 
142                 return true;
143         }
144         
145         protected Vector3d getTranslate(double x, double y, Vector3d offset) {
146                 
147                 Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
148                 
149                 Vector3d p = node.getWorldPosition();
150                 
151                 if (inline) {
152                 Vector3d i1 = new Vector3d();
153                 Vector3d i2 = new Vector3d();
154                 
155                 double mu[] = new double[2];
156                 MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,mu);
157                 
158                 Vector3d t = MathTools.closestPointOnEdge(i1, s, e);
159                 return t;
160                 } else {
161                     return super.getTranslate(x, y, offset);
162                 }
163                 
164         }
165
166
167 }