WIP: Drag to make view follow mouse. 15/12715/5
Bruno JOFRET [Fri, 27 Sep 2013 15:27:49 +0000 (17:27 +0200)]
Add hand cursor for moving plots.

Change-Id: I80db6a563cb3d28c5c4b743f27df9627e710cbfa

scilab/modules/renderer/src/java/org/scilab/modules/renderer/JoGLView/interaction/DragZoomRotateInteraction.java

index 61c3bc3..ca5100d 100644 (file)
@@ -11,6 +11,7 @@
 package org.scilab.modules.renderer.JoGLView.interaction;
 
 import java.awt.Component;
+import java.awt.Cursor;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 import java.awt.event.MouseListener;
@@ -100,6 +101,15 @@ public class DragZoomRotateInteraction extends FigureInteraction {
                     currentAxes = getUnderlyingAxes(e.getPoint());
                     if (currentAxes != null) {
                         getDrawerVisitor().getComponent().addMouseMotionListener(mouseMotionListener);
+                        switch(e.getButton()) {
+                        case MouseEvent.BUTTON1 :
+                            Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); 
+                            e.getComponent().setCursor(cursor);
+                            break;
+                        case MouseEvent.BUTTON3 :
+                            // FIXME: add rotation cursor here
+                            break;
+                        }
                     }
                 }
             }
@@ -116,6 +126,7 @@ public class DragZoomRotateInteraction extends FigureInteraction {
                 getDrawerVisitor().getComponent().removeMouseMotionListener(mouseMotionListener);
                 currentAxes = null;
             }
+            e.getComponent().setCursor(Cursor.getDefaultCursor());
         }
     }
 
@@ -195,7 +206,6 @@ public class DragZoomRotateInteraction extends FigureInteraction {
                     doRotation(e);
                     break;
             }
-
             previousEvent = e;
         }
 
@@ -218,20 +228,38 @@ public class DragZoomRotateInteraction extends FigureInteraction {
             if (currentAxes != null) {
                 if (currentAxes.getZoomEnabled()) {
                     Double[] bounds = currentAxes.getDisplayedBounds();
-                    double orientation = Math.signum(Math.cos(Math.toRadians(currentAxes.getRotationAngles()[0])));
-                    double angle = - orientation * Math.toRadians(currentAxes.getRotationAngles()[1]);
-
-                    double xDelta = (bounds[0] - bounds[1]) / 100;
-                    double yDelta = (bounds[2] - bounds[3]) / 100;
-
-                    double rotatedDX = dx * Math.sin(angle) + dy * Math.cos(angle);
-                    double rotatedDY = dx * Math.cos(angle) - dy * Math.sin(angle);
-
-                    bounds[0] += xDelta * rotatedDX * orientation;
-                    bounds[1] += xDelta * rotatedDX * orientation;
+                    
+                    Integer[] winSize = (Integer[]) GraphicController.getController().getProperty(currentAxes.getParent(), GraphicObjectProperties.__GO_AXES_SIZE__);
+                    Double[] axesBounds = (Double[]) GraphicController.getController().getProperty(currentAxes.getIdentifier(), GraphicObjectProperties.__GO_AXES_BOUNDS__);
+                    Double[] axesMargins = (Double[]) GraphicController.getController().getProperty(currentAxes.getIdentifier(), GraphicObjectProperties.__GO_MARGINS__);
+                    Integer view = (Integer) GraphicController.getController().getProperty(currentAxes.getIdentifier(), GraphicObjectProperties.__GO_VIEW__);
+
+                    // Compute ratio from pixel move to user displayed data bounds
+                    double xDelta = Math.abs(bounds[0] - bounds[1]) / (winSize[0] * axesBounds[2] * (1 - axesMargins[0] - axesMargins[1]));
+                    double yDelta = Math.abs(bounds[2] - bounds[3]) / (winSize[1] * axesBounds[3] * (1 - axesMargins[2] - axesMargins[3]));
+                  
+                    if (view == 0) {
+                        // 2D View
+                        bounds[0] -= xDelta * dx;
+                        bounds[1] -= xDelta * dx;
+
+                        bounds[2] += yDelta * dy;
+                        bounds[3] += yDelta * dy;
+                    } else {
+                        // 3D view
+                        double orientation = - Math.signum(Math.cos(Math.toRadians(currentAxes.getRotationAngles()[0])));
+                        double angle = - orientation * Math.toRadians(currentAxes.getRotationAngles()[1]);
+
+                        double rotatedDX = dx * Math.sin(angle) + dy * Math.cos(angle);
+                        double rotatedDY = dx * Math.cos(angle) - dy * Math.sin(angle);
+
+                        bounds[0] -= xDelta * rotatedDX * orientation;
+                        bounds[1] -= xDelta * rotatedDX * orientation;
+
+                        bounds[2] += yDelta * rotatedDY;
+                        bounds[3] += yDelta * rotatedDY;
+                    }
 
-                    bounds[2] += yDelta * rotatedDY;
-                    bounds[3] += yDelta * rotatedDY;
 
                     Boolean zoomed = tightZoomBoxToDataBounds(currentAxes, bounds);
                     GraphicController.getController().setProperty(currentAxes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_BOX__, bounds);