]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/TranslateAction.java
Disable DB level undo, when model modifying actions are active
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / swt / TranslateAction.java
index 32274f037864573f1afaef25cf33211133aad318..f80583a27ac3c966573cc080453d07cab7615513 100644 (file)
@@ -13,6 +13,7 @@ package org.simantics.g3d.vtk.swt;
 
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
+import java.awt.event.MouseWheelEvent;
 import java.math.BigDecimal;
 
 import javax.vecmath.AxisAngle4d;
@@ -21,17 +22,23 @@ import javax.vecmath.Quat4d;
 import javax.vecmath.Vector3d;
 
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.graphics.Cursor;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Combo;
 import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Label;
 import org.simantics.g3d.math.MathTools;
 import org.simantics.g3d.math.Ray;
 import org.simantics.g3d.scenegraph.IG3DNode;
+import org.simantics.g3d.scenegraph.base.INode;
 import org.simantics.g3d.scenegraph.structural.IStructuralNode;
+import org.simantics.g3d.toolbar.ToolComposite;
 import org.simantics.g3d.vtk.Activator;
 import org.simantics.g3d.vtk.common.VTKNodeMap;
 import org.simantics.g3d.vtk.gizmo.TranslateAxisGizmo;
 import org.simantics.g3d.vtk.utils.vtkUtil;
-import org.simantics.utils.threads.AWTThread;
 import org.simantics.utils.threads.ThreadUtils;
 
 import vtk.vtkProp;
@@ -39,14 +46,14 @@ import vtk.vtkProp;
 public class TranslateAction extends vtkSwtAction{
        
        public static final int X = 0;
-    public static final int Y = 1;
-    public static final int Z = 2;
-    public static final int XY = 3;
-    public static final int XZ = 4;
-    public static final int YZ = 5;
-    public static final int P = 6;
+       public static final int Y = 1;
+       public static final int Z = 2;
+       public static final int XY = 3;
+       public static final int XZ = 4;
+       public static final int YZ = 5;
+       public static final int P = 6;
 
-       private VTKNodeMap nodeMap;
+       private VTKNodeMap<?, ? extends INode> nodeMap;
        //private TranslateGizmo  gizmo = new TranslateGizmo();
        private TranslateAxisGizmo gizmo = new TranslateAxisGizmo();
        protected IG3DNode node;
@@ -56,6 +63,9 @@ public class TranslateAction extends vtkSwtAction{
        private Cursor activeCursor;// = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
        private Cursor dragCursor;// = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
        
+       protected ToolComposite toolComposite;
+       protected Combo axisCombo;
+       
        public void setNode(IG3DNode node) {
                this.node = node;
                if ((node instanceof IStructuralNode) && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot()) {
@@ -69,20 +79,55 @@ public class TranslateAction extends vtkSwtAction{
                return node;
        }
        
-       public TranslateAction(InteractiveVtkComposite panel, VTKNodeMap nodeMap) {
+       public TranslateAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap, ToolComposite toolComposite) {
                super(panel);
                setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_out.png"));
                setText("Translate");
                this.nodeMap = nodeMap;
                
                activeCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_HAND);
-        dragCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_SIZEALL);
+               dragCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_SIZEALL);
+               this.toolComposite = toolComposite;
+       }
+       
+       protected void createTools(ToolComposite toolComposite) {
+           Label label = new Label(toolComposite, SWT.READ_ONLY);
+           label.setText("Translate direction:");
+           axisCombo = new Combo(toolComposite, SWT.READ_ONLY);
+        axisCombo.add("X");
+        axisCombo.add("Y");
+        axisCombo.add("Z");
+        axisCombo.add("XY");
+        axisCombo.add("XZ");
+        axisCombo.add("YZ");
+        axisCombo.add("Camera");
+        axisCombo.addSelectionListener(new SelectionAdapter() {
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                Combo c = (Combo)e.getSource();
+                index = c.getSelectionIndex();
+                updateLock();
+                panel.getComponent().setFocus();
+            }
+        });
+        axisCombo.select(index);
+        Button close = new Button(toolComposite, SWT.PUSH);
+        close.setText("Close");
+        close.addSelectionListener(new SelectionAdapter() {
+            public void widgetSelected(SelectionEvent e) {
+                panel.useDefaultAction();
+            };
+        });
+        toolComposite.relayout();
        }
        
        public void attach() {
                if (node == null)
                        return;
-               
+               if (toolComposite != null) {
+                  createTools(toolComposite); 
+               }
+               setDBUndo(false);
                super.attach();
                ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
                        public void run() {
@@ -90,15 +135,16 @@ public class TranslateAction extends vtkSwtAction{
                                update();
                        }
                });
-               
-               
-               
        }
        
        public void deattach() {
-               
+           if (toolComposite != null) {
+               toolComposite.clear();
+               axisCombo = null;
+           }
+           setDBUndo(true);
                node = null;
-               nodeMap.commit();
+               nodeMap.commit("Translate");
                deattachUI();
                super.deattach();
                panel.refresh();
@@ -141,13 +187,19 @@ public class TranslateAction extends vtkSwtAction{
                if (e.getKeyCode() == KeyEvent.VK_G) {
                        worldCoord = !worldCoord;
                }
-               gizmo.setType(index);
                
+               updateLock();
                update();
                //panel.repaint();
                return true;
        }
        
+       private void updateLock() {
+           gizmo.setType(index);
+           if (axisCombo != null)
+               axisCombo.select(index);
+           panel.refresh();
+       }
 
        
        @Override
@@ -225,31 +277,31 @@ public class TranslateAction extends vtkSwtAction{
                if (panel.getRenderer().GetActiveCamera().GetParallelProjection() == 0) {
                        double distance = p.length();
                        p.negate();
-            double fov = panel.getRenderer().GetActiveCamera().GetViewAngle();
-            float s = (float) (Math.sin(fov) * distance * 0.1); 
+                       double fov = panel.getRenderer().GetActiveCamera().GetViewAngle();
+                       float s = (float) (Math.sin(fov) * distance * 0.1); 
 
-            Vector3d scale = new Vector3d(1., 1., 1.);
-            
+                       Vector3d scale = new Vector3d(1., 1., 1.);
+                       
 //            if (p.x > 0.f)
 //                scale.x = -1.;
 //            if (p.y > 0.f)
 //                scale.y = -1.;
 //            if (p.z > 0.f)
 //                scale.z = -1.;
-            scale.scale(s);
-            gizmo.setScale(scale);
+                       scale.scale(s);
+                       gizmo.setScale(scale);
                        
                } else {
                        Vector3d scale = new Vector3d(1.f, 1.f, 1.f);
-            double s = panel.getRenderer().GetActiveCamera().GetParallelScale() / 5.;
+                       double s = panel.getRenderer().GetActiveCamera().GetParallelScale() / 5.;
 //            if (p.x > 0.f)
 //                scale.x = -1.;
 //            if (p.y > 0.f)
 //                scale.y = -1.;
 //            if (p.z > 0.f)
 //                scale.z = -1.;
-            scale.scale(s);
-            gizmo.setScale(scale);
+                       scale.scale(s);
+                       gizmo.setScale(scale);
                }
                
                //panel.Render();
@@ -302,7 +354,8 @@ public class TranslateAction extends vtkSwtAction{
        
        @Override
        public boolean mouseDragged(MouseEvent e) {
-               if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) { 
+               //if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
+           if (e.getButton() == MouseEvent.BUTTON1  && valid) {
                        
                        Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
                        //System.out.println("translate " + translate);
@@ -331,6 +384,11 @@ public class TranslateAction extends vtkSwtAction{
                return true;
        }
        
+       @Override
+       public boolean mouseWheelMoved(MouseWheelEvent e) {
+               return getDefaultAction().mouseWheelMoved(e);
+       }
+       
        protected void setPos(Vector3d pos) {
                node.setPosition(pos);
        }
@@ -339,31 +397,31 @@ public class TranslateAction extends vtkSwtAction{
                node.setWorldPosition(pos);
        }
        
-        private double istep = 10.0;
-        private int decimals = 2;
+       private double istep = 10.0;
+       private int decimals = 2;
        
        protected Vector3d constaints(Vector3d p, boolean step) {
                if(!step)
                        return p;
                switch (index) {
                case X:
-                        p.x = Math.round(istep * p.x) / istep;
-             BigDecimal bx = new BigDecimal(p.x);
-             bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
-             p.x = bx.doubleValue();
+                       p.x = Math.round(istep * p.x) / istep;
+                       BigDecimal bx = new BigDecimal(p.x);
+                       bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
+                       p.x = bx.doubleValue();
                        break;
                case Y:
-                        p.y = Math.round(istep * p.y) / istep;
-             BigDecimal by = new BigDecimal(p.y);
-             by.setScale(decimals, BigDecimal.ROUND_HALF_UP);
-             p.y = by.doubleValue();
+                       p.y = Math.round(istep * p.y) / istep;
+                       BigDecimal by = new BigDecimal(p.y);
+                       by.setScale(decimals, BigDecimal.ROUND_HALF_UP);
+                       p.y = by.doubleValue();
                        break;
-                       
+
                case Z:
-                        p.z = Math.round(istep * p.z) / istep;
-             BigDecimal bz = new BigDecimal(p.z);
-             bz.setScale(decimals, BigDecimal.ROUND_HALF_UP);
-             p.z = bz.doubleValue();
+                       p.z = Math.round(istep * p.z) / istep;
+                       BigDecimal bz = new BigDecimal(p.z);
+                       bz.setScale(decimals, BigDecimal.ROUND_HALF_UP);
+                       p.z = bz.doubleValue();
                        break;
                }
                return p;
@@ -395,85 +453,85 @@ public class TranslateAction extends vtkSwtAction{
                        }
                        normal.normalize();
                        double s[] = new double[1];
-            Vector3d r = new Vector3d();
-            if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
-                r.sub(p);
-                translate.x = r.x;
-                translate.y = r.y;
-                translate.z = r.z;
-            }
-            break;
+                       Vector3d r = new Vector3d();
+                       if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
+                               r.sub(p);
+                               translate.x = r.x;
+                               translate.y = r.y;
+                               translate.z = r.z;
+                       }
+                       break;
 
-                case X :
-                   dir = new Vector3d(1.0,0.0,0.0);
-                   if(!worldCoord)
-                       MathTools.rotate(q, dir, dir);
-                   Vector3d i1 = new Vector3d();
-                   Vector3d i2 = new Vector3d();
-                   s = new double[2];
-                   MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
-                   translate.x = s[0];
-                   
-                   break;
-               case Y :
-                   dir = new Vector3d(0.0,1.0,0.0);
-                   if(!worldCoord)
-                       MathTools.rotate(q, dir, dir);
-                   i1 = new Vector3d();
-                   i2 = new Vector3d();
-                   s = new double[2];
-                   MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
-                   translate.y = s[0];
-                   break;
-               case Z :
-                   dir = new Vector3d(0.0,0.0,1.0);
-                   if(!worldCoord)
-                       MathTools.rotate(q, dir, dir);
-                   i1 = new Vector3d();
-                   i2 = new Vector3d();
-                   s = new double[2];
-                   MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
-                   translate.z = s[0];
-                   break;
-               case XY :
-                   normal = new Vector3d(0.0,0.0,1.0);
-                   if(!worldCoord)
-                       MathTools.rotate(q, normal, normal);
-                   r = new Vector3d();
-                   if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
-                       r.sub(p);
-                       translate.x = r.x;
-                       translate.y = r.y;
-                   }
-                   break;
-               case XZ :
-                   normal = new Vector3d(0.0,1.0,0.0);
-                   if(!worldCoord)
-                       MathTools.rotate(q, normal, normal);
-                   r = new Vector3d();
-                   if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
-                       r.sub(p);
-                       translate.x = r.x;
-                       translate.z = r.z;
-                   }
-                   break;
-               case YZ :
-                   normal = new Vector3d(1.0,0.0,0.0);
-                   if(!worldCoord)
-                       MathTools.rotate(q, normal, normal);
-                   r = new Vector3d();
-                   if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
-                       r.sub(p);
-                       translate.y = r.y;
-                       translate.z = r.z;
-                   }
-                   break;
-               default :
-                   
-                   return null;
-               }
-               translate.sub(offset);
-               return translate;
+               case X :
+                               dir = new Vector3d(1.0,0.0,0.0);
+                               if(!worldCoord)
+                                       MathTools.rotate(q, dir, dir);
+                               Vector3d i1 = new Vector3d();
+                               Vector3d i2 = new Vector3d();
+                               s = new double[2];
+                               MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
+                               translate.x = s[0];
+                               
+                               break;
+               case Y :
+                       dir = new Vector3d(0.0,1.0,0.0);
+                       if(!worldCoord)
+                               MathTools.rotate(q, dir, dir);
+                       i1 = new Vector3d();
+                       i2 = new Vector3d();
+                       s = new double[2];
+                       MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
+                       translate.y = s[0];
+                       break;
+               case Z :
+                       dir = new Vector3d(0.0,0.0,1.0);
+                       if(!worldCoord)
+                               MathTools.rotate(q, dir, dir);
+                       i1 = new Vector3d();
+                       i2 = new Vector3d();
+                       s = new double[2];
+                       MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
+                       translate.z = s[0];
+                       break;
+               case XY :
+                       normal = new Vector3d(0.0,0.0,1.0);
+                       if(!worldCoord)
+                               MathTools.rotate(q, normal, normal);
+                       r = new Vector3d();
+                       if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
+                               r.sub(p);
+                               translate.x = r.x;
+                               translate.y = r.y;
+                       }
+                       break;
+               case XZ :
+                       normal = new Vector3d(0.0,1.0,0.0);
+                       if(!worldCoord)
+                               MathTools.rotate(q, normal, normal);
+                       r = new Vector3d();
+                       if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
+                               r.sub(p);
+                               translate.x = r.x;
+                               translate.z = r.z;
+                       }
+                       break;
+               case YZ :
+                       normal = new Vector3d(1.0,0.0,0.0);
+                       if(!worldCoord)
+                               MathTools.rotate(q, normal, normal);
+                       r = new Vector3d();
+                       if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
+                               r.sub(p);
+                               translate.y = r.y;
+                               translate.z = r.z;
+                       }
+                       break;
+               default :
+                       
+                       return null;
+               }
+               translate.sub(offset);
+               return translate;
        }
        
 }