]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/TranslateInlineAction.java
Support for reversible in-line components
[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.action.TranslateAction;
13 import org.simantics.g3d.vtk.common.InteractiveVtkPanel;
14 import org.simantics.g3d.vtk.common.VTKNodeMap;
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(InteractiveVtkPanel 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                         setEnabled(true);
37                         InlineComponent comp = (InlineComponent)node;
38                         
39                         PipelineComponent prev = comp.getPrevious();
40                         PipelineComponent next = comp.getNext();
41                         if (prev.getControlPoint().isInline() && !prev.getControlPoint().isFixed())
42                                 prev = prev.getPrevious();
43                         if (next.getControlPoint().isInline() && !next.getControlPoint().isFixed()) {
44                                 next = next.getNext();
45                         }
46                         Point3d ns = new Point3d();
47                         Point3d ne = new Point3d();
48                         Point3d ps = new Point3d();
49                         Point3d pe = new Point3d();
50                         next.getControlPointEnds(ns, ne);
51                         prev.getControlPointEnds(ps, pe);
52                         dir = comp.getControlPoint().getPathLegDirection(Direction.NEXT);
53                         dir.normalize();
54                         // We may have offsets in the path leg, hence we have to project the coordinates.
55                         Vector3d wp = node.getWorldPosition();
56                         s = MathTools.closestPointOnStraight(pe, wp, dir);
57                         e = MathTools.closestPointOnStraight(ns, wp, dir);
58                         // Remove component's own space from end points to get actual movement range
59                         double l = comp.getControlPoint().getInlineLength();
60                         Vector3d ld = new Vector3d(dir);
61                         ld.scale(l);
62                         s.add(ld);
63                         e.sub(ld);
64                 } else {
65                         setEnabled(false);
66                 }
67         }
68         
69         @Override
70         public void keyPressed(KeyEvent e) {
71                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
72                         panel.useDefaultAction();
73                 if (valid)
74                         return;
75                 
76                 update();
77         }
78         
79         @Override
80         public void setWorldCoord(boolean b) {
81                 
82         }
83         
84         @Override
85         public void mouseDragged(MouseEvent e) {
86                 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) { 
87                         
88                         Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
89                         //System.out.println("translate " + translate);
90                         if (translate == null)
91                                 return;
92                         boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
93                         Vector3d pos = new Vector3d(node.getWorldPosition());
94                         //pos.add(translate);
95                         pos.set(translate);
96                         //pos = constaints(pos, step);
97                         setWorldPos(pos);
98                         
99                         //mapping.rangeModified(node);
100                         
101                         //nodeMap.modified(node);
102                         update();
103                 } else {
104                         panel.getDefaultAction().mouseDragged(e);
105                         update();
106                 }
107         }
108         
109         
110         
111         protected Vector3d getTranslate(double x, double y, Vector3d offset) {
112                 
113                 Ray ray = vtkUtil.createMouseRay(panel.GetRenderer(),x, y);
114                 
115                 Vector3d p = node.getWorldPosition();
116                 
117                 Vector3d i1 = new Vector3d();
118         Vector3d i2 = new Vector3d();
119         
120         double mu[] = new double[2];
121         MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,mu);
122         
123         Vector3d t = MathTools.closestPointOnEdge(i1, s, e);
124         return t;
125                 
126         }
127
128
129 }