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