]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - 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
index 2c10845f529c4ba86c88445d9f2cc984560c76a9..3c5886d8bbd2779dc75735fc1bdac807d8e08f93 100644 (file)
@@ -9,9 +9,10 @@ import javax.vecmath.Vector3d;
 import org.simantics.g3d.math.MathTools;
 import org.simantics.g3d.math.Ray;
 import org.simantics.g3d.scenegraph.IG3DNode;
-import org.simantics.g3d.vtk.action.TranslateAction;
-import org.simantics.g3d.vtk.common.InteractiveVtkPanel;
+import org.simantics.g3d.scenegraph.base.INode;
 import org.simantics.g3d.vtk.common.VTKNodeMap;
+import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
+import org.simantics.g3d.vtk.swt.TranslateAction;
 import org.simantics.g3d.vtk.utils.vtkUtil;
 import org.simantics.plant3d.Activator;
 import org.simantics.plant3d.scenegraph.InlineComponent;
@@ -20,11 +21,13 @@ import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
 
 public class TranslateInlineAction extends TranslateAction{
        
+    private boolean inline;
+    
        private Vector3d s;
        private Vector3d e;
        private Vector3d dir;
 
-       public TranslateInlineAction(InteractiveVtkPanel panel, VTKNodeMap nodeMap) {
+       public TranslateInlineAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap) {
                super(panel, nodeMap);
                setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
        }
@@ -33,47 +36,72 @@ public class TranslateInlineAction extends TranslateAction{
        public void setNode(IG3DNode node) {
                super.setNode(node);
                if (node instanceof InlineComponent) {
-                       setEnabled(true);
                        InlineComponent comp = (InlineComponent)node;
-                       
-                       PipelineComponent prev = comp.getPrevious();
-                       PipelineComponent next = comp.getNext();
-                       if (prev.getControlPoint().isInline() && !prev.getControlPoint().isFixed())
-                               prev = prev.getPrevious();
-                       if (next.getControlPoint().isInline() && !next.getControlPoint().isFixed()) {
-                               next = next.getNext();
+                       if (comp.isVariableLength()) {
+                setEnabled(false);
+                       } else if (comp.getNext() == null || comp.getPrevious() == null) {
+                setEnabled(true);
+                inline = false;
+                dir = comp.getControlPoint().getPathLegDirection(Direction.NEXT);
+                dir.normalize();
+            } else {
+                               setEnabled(true);
+                               inline = true;
+                               PipelineComponent prev = comp.getPrevious();
+                               PipelineComponent next = comp.getNext();
+                               if (prev == null || next == null) {
+                                       // TODO : we should support open ended translation (when translated component is the first or last of the run).
+                                       setEnabled(false);
+                                       return;
+                               }
+                               if (prev.getControlPoint().isInline() && !prev.getControlPoint().isFixedLength() && prev.getPrevious() != null)
+                                       prev = prev.getPrevious();
+                               if (next.getControlPoint().isInline() && !next.getControlPoint().isFixedLength() && next.getNext() != null) {
+                                       next = next.getNext();
+                               }
+                               Point3d ns = new Point3d();
+                               Point3d ne = new Point3d();
+                               Point3d ps = new Point3d();
+                               Point3d pe = new Point3d();
+                               next.getEnds(ns, ne);
+                               prev.getEnds(ps, pe);
+                               dir = comp.getControlPoint().getPathLegDirection(Direction.NEXT);
+                               dir.normalize();
+                               // We may have offsets in the path leg, hence we have to project the coordinates.
+                               Vector3d wp = node.getWorldPosition();
+                               if (prev.getControlPoint().isVariableLength())
+                                       s = MathTools.closestPointOnStraight(ps, wp, dir);
+                               else
+                                       s = MathTools.closestPointOnStraight(pe, wp, dir);
+                               if (next.getControlPoint().isVariableLength())
+                                       e = MathTools.closestPointOnStraight(ne, wp, dir);
+                               else
+                                       e = MathTools.closestPointOnStraight(ns, wp, dir);
+                               // Remove component's own space from end points to get actual movement range
+                               double l = comp.getControlPoint().getInlineLength();
+                               Vector3d ld = new Vector3d(dir);
+                               ld.scale(l);
+                               s.add(ld);
+                               e.sub(ld);
                        }
-                       Point3d ns = new Point3d();
-                       Point3d ne = new Point3d();
-                       Point3d ps = new Point3d();
-                       Point3d pe = new Point3d();
-                       next.getControlPointEnds(ns, ne);
-                       prev.getControlPointEnds(ps, pe);
-                       dir = comp.getControlPoint().getPathLegDirection(Direction.NEXT);
-                       dir.normalize();
-                       // We may have offsets in the path leg, hence we have to project the coordinates.
-                       Vector3d wp = node.getWorldPosition();
-                       s = MathTools.closestPointOnStraight(pe, wp, dir);
-                       e = MathTools.closestPointOnStraight(ns, wp, dir);
-                       // Remove component's own space from end points to get actual movement range
-                       double l = comp.getControlPoint().getInlineLength();
-                       Vector3d ld = new Vector3d(dir);
-                       ld.scale(l);
-                       s.add(ld);
-                       e.sub(ld);
                } else {
                        setEnabled(false);
                }
        }
        
        @Override
-       public void keyPressed(KeyEvent e) {
+       public boolean keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
                        panel.useDefaultAction();
-               if (valid)
-                       return;
-               
-               update();
+               if (!inline) {
+                   return super.keyPressed(e);
+               } else {
+               if (valid)
+                       return true;
+               
+               update();
+               return true;
+               }
        }
        
        @Override
@@ -82,46 +110,51 @@ public class TranslateInlineAction extends TranslateAction{
        }
        
        @Override
-       public void mouseDragged(MouseEvent e) {
-               if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) { 
-                       
-                       Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
-                       //System.out.println("translate " + translate);
-                       if (translate == null)
-                               return;
-                       boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
-                       Vector3d pos = new Vector3d(node.getWorldPosition());
-                       //pos.add(translate);
-                       pos.set(translate);
-                       //pos = constaints(pos, step);
-                       setWorldPos(pos);
-                       
-                       //mapping.rangeModified(node);
-                       
-                       //nodeMap.modified(node);
-                       update();
+       public boolean mouseDragged(MouseEvent e) {
+               //if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) { 
+           if (e.getButton() == MouseEvent.BUTTON1  && valid) {
+                       if (inline) {
+                       Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
+                       //System.out.println("translate " + translate);
+                       if (translate == null)
+                               return true;
+                       //boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
+                       Vector3d pos = new Vector3d(node.getWorldPosition());
+                       
+                       System.out.println(pos + " " + translate);
+                       //pos.add(translate);
+                       pos.set(translate);
+                       //pos = constaints(pos, step);
+                       setWorldPos(pos);
+                       update();
+                       } else {
+                           super.mouseDragged(e);
+                       }
                } else {
-                       panel.getDefaultAction().mouseDragged(e);
+                       getDefaultAction().mouseDragged(e);
                        update();
-               }
+               } 
+               return true;
        }
        
-       
-       
        protected Vector3d getTranslate(double x, double y, Vector3d offset) {
                
-               Ray ray = vtkUtil.createMouseRay(panel.GetRenderer(),x, y);
+               Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
                
                Vector3d p = node.getWorldPosition();
                
-               Vector3d i1 = new Vector3d();
-        Vector3d i2 = new Vector3d();
-        
-        double mu[] = new double[2];
-        MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,mu);
-        
-        Vector3d t = MathTools.closestPointOnEdge(i1, s, e);
-        return t;
+               if (inline) {
+               Vector3d i1 = new Vector3d();
+               Vector3d i2 = new Vector3d();
+               
+               double mu[] = new double[2];
+               MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,mu);
+               
+               Vector3d t = MathTools.closestPointOnEdge(i1, s, e);
+               return t;
+               } else {
+                   return super.getTranslate(x, y, offset);
+               }
                
        }