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