]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/actions/TranslateInlineAction.java
Compiler warning elimination
[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.vtk.common.VTKNodeMap;
14 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
15 import org.simantics.g3d.vtk.swt.TranslateAction;
16 import org.simantics.g3d.vtk.utils.vtkUtil;
17 import org.simantics.plant3d.Activator;
18 import org.simantics.plant3d.scenegraph.InlineComponent;
19 import org.simantics.plant3d.scenegraph.PipelineComponent;
20 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
21
22 public class TranslateInlineAction extends TranslateAction{
23         
24     private boolean inline;
25     
26         private Vector3d s;
27         private Vector3d e;
28         private Vector3d dir;
29
30         public TranslateInlineAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap) {
31                 super(panel, nodeMap);
32                 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
33         }
34         
35         @Override
36         public void setNode(IG3DNode node) {
37                 super.setNode(node);
38                 if (node instanceof InlineComponent) {
39                         InlineComponent comp = (InlineComponent)node;
40                         if (comp.isVariableLength()) {
41                 setEnabled(false);
42                         } else if (comp.getNext() == null || comp.getPrevious() == null) {
43                 setEnabled(true);
44                 inline = false;
45                 dir = comp.getControlPoint().getPathLegDirection(Direction.NEXT);
46                 dir.normalize();
47             } else {
48                                 setEnabled(true);
49                                 inline = true;
50                                 PipelineComponent prev = comp.getPrevious();
51                                 PipelineComponent next = comp.getNext();
52                                 if (prev == null || next == null) {
53                                         // TODO : we should support open ended translation (when translated component is the first or last of the run).
54                                         setEnabled(false);
55                                         return;
56                                 }
57                                 if (prev.getControlPoint().isInline() && !prev.getControlPoint().isFixedLength() && prev.getPrevious() != null)
58                                         prev = prev.getPrevious();
59                                 if (next.getControlPoint().isInline() && !next.getControlPoint().isFixedLength() && next.getNext() != null) {
60                                         next = next.getNext();
61                                 }
62                                 Point3d ns = new Point3d();
63                                 Point3d ne = new Point3d();
64                                 Point3d ps = new Point3d();
65                                 Point3d pe = new Point3d();
66                                 next.getEnds(ns, ne);
67                                 prev.getEnds(ps, pe);
68                                 dir = comp.getControlPoint().getPathLegDirection(Direction.NEXT);
69                                 dir.normalize();
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         public boolean keyPressed(KeyEvent e) {
94                 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
95                         panel.useDefaultAction();
96                 if (!inline) {
97                     return super.keyPressed(e);
98                 } else {
99                 if (valid)
100                         return true;
101                 
102                 update();
103                 return true;
104                 }
105         }
106         
107         @Override
108         public void setWorldCoord(boolean b) {
109                 
110         }
111         
112         @Override
113         public boolean mouseDragged(MouseEvent e) {
114                 //if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) { 
115             if (e.getButton() == MouseEvent.BUTTON1  && valid) {
116                         if (inline) {
117                         Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
118                         //System.out.println("translate " + translate);
119                         if (translate == null)
120                                 return true;
121                         //boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
122                         Vector3d pos = new Vector3d(node.getWorldPosition());
123                         
124                         System.out.println(pos + " " + translate);
125                         //pos.add(translate);
126                         pos.set(translate);
127                         //pos = constaints(pos, step);
128                         setWorldPos(pos);
129                         update();
130                         } else {
131                             super.mouseDragged(e);
132                         }
133                 } else {
134                         getDefaultAction().mouseDragged(e);
135                         update();
136                 } 
137                 return true;
138         }
139         
140         protected Vector3d getTranslate(double x, double y, Vector3d offset) {
141                 
142                 Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
143                 
144                 Vector3d p = node.getWorldPosition();
145                 
146                 if (inline) {
147                 Vector3d i1 = new Vector3d();
148                 Vector3d i2 = new Vector3d();
149                 
150                 double mu[] = new double[2];
151                 MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,mu);
152                 
153                 Vector3d t = MathTools.closestPointOnEdge(i1, s, e);
154                 return t;
155                 } else {
156                     return super.getTranslate(x, y, offset);
157                 }
158                 
159         }
160
161
162 }