[bug_14775] bad error management in load function
[scilab.git] / scilab / modules / renderer / src / java / org / scilab / modules / renderer / JoGLView / interaction / DragZoomRotateInteraction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009-2012 - DIGITEO - Pierre Lando
4  * Copyright (C) 2013 - Scilab Enterprises - Calixte DENIZET
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  */
15 package org.scilab.modules.renderer.JoGLView.interaction;
16
17 import java.awt.Component;
18 import java.awt.Cursor;
19 import java.awt.event.MouseAdapter;
20 import java.awt.event.MouseEvent;
21 import java.awt.event.MouseListener;
22 import java.awt.event.MouseMotionAdapter;
23 import java.awt.event.MouseMotionListener;
24 import java.awt.event.MouseWheelEvent;
25 import java.awt.event.MouseWheelListener;
26
27 import org.scilab.modules.commons.OS;
28 import org.scilab.modules.graphic_objects.axes.Axes;
29 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
30 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
31 import org.scilab.modules.renderer.JoGLView.DrawerVisitor;
32 import org.scilab.modules.renderer.JoGLView.util.ScaleUtils;
33
34 /**
35  * This class manage figure interaction.
36  *
37  * @author Pierre Lando
38  */
39 public class DragZoomRotateInteraction extends FigureInteraction {
40
41     private static final int XY_TRANSLATION_MODIFIER = MouseEvent.BUTTON1_MASK;
42     private static final int Z_TRANSLATION_MODIFIER = MouseEvent.BUTTON1_MASK | MouseEvent.ALT_MASK;
43     private static final int ROTATION_MODIFIER = MouseEvent.BUTTON3_MASK;
44     private static final int MACOSX_ROTATION_MODIFIER = MouseEvent.BUTTON1_MASK | MouseEvent.CTRL_MASK;
45
46     /**
47      * The box size is multiply by this value.
48      */
49     private static final double ZOOM_FACTOR = 1.02;
50
51     private final MouseListener mouseListener;
52     private final MouseWheelListener mouseWheelListener;
53     private final MouseMotionListener mouseMotionListener;
54
55     /**
56      * Last important mouse event.
57      */
58     private MouseEvent previousEvent;
59     private Axes[] currentAxes;
60
61
62     /**
63      * Default constructor.
64      * @param drawerVisitor parent drawer visitor.
65      */
66     public DragZoomRotateInteraction(DrawerVisitor drawerVisitor) {
67         super(drawerVisitor);
68         mouseMotionListener = new FigureMouseMotionListener();
69         mouseWheelListener = new FigureMouseWheelListener();
70         mouseListener = new FigureMouseListener();
71         currentAxes = new Axes[0];
72     }
73
74     @Override
75     protected void changeEnable(boolean isEnable) {
76         Component component = getDrawerVisitor().getComponent();
77         if (component != null) {
78             if (isEnable) {
79                 component.addMouseListener(mouseListener);
80                 component.addMouseWheelListener(mouseWheelListener);
81             } else {
82                 component.removeMouseListener(mouseListener);
83                 component.removeMouseMotionListener(mouseMotionListener);
84                 component.removeMouseWheelListener(mouseWheelListener);
85             }
86         }
87     }
88
89     public void setTranslationEnable(boolean status) {
90         ((FigureMouseMotionListener)mouseMotionListener).setTranslateEnable(status);
91     }
92
93     /**
94      * This {@see MouseListner} activate the {@see MouseMotionListener} when at least
95      * one button is pressed.
96      * The event is saved in {@see previousEvent}
97      */
98     private class FigureMouseListener extends MouseAdapter implements MouseListener {
99
100         private int pressedButtons = 0;
101
102         @Override
103         public void mousePressed(MouseEvent e) {
104             if (pressedButtons == 0) {
105                 previousEvent = e;
106                 if (currentAxes.length == 0) {
107                     currentAxes = getAllUnderlyingAxes(e.getPoint());
108                     if (currentAxes.length > 0) {
109                         getDrawerVisitor().getComponent().addMouseMotionListener(mouseMotionListener);
110                         switch (e.getButton()) {
111                             case MouseEvent.BUTTON1 :
112                                 Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
113                                 e.getComponent().setCursor(cursor);
114                                 break;
115                             case MouseEvent.BUTTON3 :
116                                 // FIXME: add rotation cursor here
117                                 break;
118                         }
119                     }
120                 }
121             }
122             pressedButtons++;
123         }
124
125         @Override
126         public void mouseReleased(MouseEvent e) {
127             if (pressedButtons > 0) {
128                 pressedButtons--;
129             }
130
131             if (pressedButtons == 0) {
132                 getDrawerVisitor().getComponent().removeMouseMotionListener(mouseMotionListener);
133                 currentAxes = new Axes[0];
134             }
135             e.getComponent().setCursor(Cursor.getDefaultCursor());
136         }
137     }
138
139     /**
140      * This {@see MouseWheelListener} manage zoom/un-zoom on the figure.
141      */
142     private class FigureMouseWheelListener implements MouseWheelListener {
143
144         private void applyZoom(Axes axes, double scale) {
145             if (axes != null) {
146                 Double[] bounds = axes.getDisplayedBounds();
147                 double[][] factors = axes.getScaleTranslateFactors();
148
149                 double xDelta = (bounds[1] - bounds[0]) / 2;
150                 double xMiddle = (bounds[1] + bounds[0]) / 2;
151                 bounds[0] = xMiddle - xDelta * scale;
152                 bounds[1] = xMiddle + xDelta * scale;
153
154                 double yDelta = (bounds[3] - bounds[2]) / 2;
155                 double yMiddle = (bounds[3] + bounds[2]) / 2;
156                 bounds[2] = yMiddle - yDelta * scale;
157                 bounds[3] = yMiddle + yDelta * scale;
158
159                 double zDelta = (bounds[5] - bounds[4]) / 2;
160                 double zMiddle = (bounds[5] + bounds[4]) / 2;
161                 bounds[4] = zMiddle - zDelta * scale;
162                 bounds[5] = zMiddle + zDelta * scale;
163
164                 bounds[0] = bounds[0] * factors[0][0] + factors[1][0];
165                 bounds[1] = bounds[1] * factors[0][0] + factors[1][0];
166                 bounds[2] = bounds[2] * factors[0][1] + factors[1][1];
167                 bounds[3] = bounds[3] * factors[0][1] + factors[1][1];
168                 bounds[4] = bounds[4] * factors[0][2] + factors[1][2];
169                 bounds[5] = bounds[5] * factors[0][2] + factors[1][2];
170
171                 Boolean zoomed = tightZoomBounds(axes, bounds);
172
173                 bounds[0] = (bounds[0] - factors[1][0]) / factors[0][0];
174                 bounds[1] = (bounds[1] - factors[1][0]) / factors[0][0];
175                 bounds[2] = (bounds[2] - factors[1][1]) / factors[0][1];
176                 bounds[3] = (bounds[3] - factors[1][1]) / factors[0][1];
177                 bounds[4] = (bounds[4] - factors[1][2]) / factors[0][2];
178                 bounds[5] = (bounds[5] - factors[1][2]) / factors[0][2];
179
180                 boolean[] logFlags = { axes.getXAxisLogFlag(), axes.getYAxisLogFlag(), axes.getZAxisLogFlag()};
181                 ScaleUtils.applyInverseLogScaleToBounds(bounds, logFlags);
182
183                 GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_BOX__, bounds);
184                 GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_ENABLED__, zoomed);
185             }
186         }
187         @Override
188         public void mouseWheelMoved(MouseWheelEvent e) {
189             Axes[] allAxes;
190             if (e.isControlDown()) {
191                 allAxes = getAllVisibleAxes(e.getPoint());
192             } else {
193                 allAxes = getAllUnderlyingAxes(e.getPoint());
194             }
195             double scale = Math.pow(ZOOM_FACTOR, e.getUnitsToScroll());
196             for (Axes axes : allAxes) {
197                 applyZoom(axes, scale);
198             }
199         }
200     }
201
202     private static void applyUnlog(Double[] bounds, Axes axes) {
203         if (axes.getXAxisLogFlag()) {
204             bounds[0] = Math.pow(10, bounds[0]);
205             bounds[1] = Math.pow(10, bounds[1]);
206         }
207
208         if (axes.getYAxisLogFlag()) {
209             bounds[2] = Math.pow(10, bounds[2]);
210             bounds[3] = Math.pow(10, bounds[3]);
211         }
212
213         if (axes.getZAxisLogFlag()) {
214             bounds[4] = Math.pow(10, bounds[4]);
215             bounds[5] = Math.pow(10, bounds[5]);
216         }
217     }
218
219     /**
220      * This {@see MouseMotionListener} manage rotation and translation on the figure.
221      */
222     private class FigureMouseMotionListener extends MouseMotionAdapter implements MouseMotionListener {
223
224         private boolean translateEnabled = true;
225
226         public void setTranslateEnable(boolean status) {
227             translateEnabled = status;
228         }
229
230         @Override
231         public void mouseMoved(MouseEvent e) {
232             /*
233              * Mac OS X specific case: the users first presses CTRL and then left-clic.
234              */
235             if (OS.get() == OS.MAC && e.isControlDown() && e.getButton() == 0) {
236                 doRotation(e);
237             }
238         }
239         public void mouseDragged(MouseEvent e) {
240             switch (e.getModifiers()) {
241                 case MACOSX_ROTATION_MODIFIER:
242                     /*
243                      * Mac OS X specific case: the users first left-clic and then presses CTRL
244                      */
245                     if (OS.get() == OS.MAC && e.isControlDown()) {
246                         doRotation(e);
247                         break;
248                     }
249                     break;
250                 case XY_TRANSLATION_MODIFIER:
251                     if (translateEnabled) {
252                         doXYTranslation(e);
253                     }
254                     break;
255                 case Z_TRANSLATION_MODIFIER:
256                     doZTranslation(e);
257                     break;
258                 case ROTATION_MODIFIER:
259                     doRotation(e);
260                     break;
261             }
262             previousEvent = e;
263         }
264
265         private void doRotation(MouseEvent e) {
266             int dx = e.getX() - previousEvent.getX();
267             int dy = e.getY() - previousEvent.getY();
268
269             for (Axes axes : currentAxes) {
270                 Double[] angles = axes.getRotationAngles();
271                 angles[0] -= dy / 4.0;
272                 angles[1] -= Math.signum(Math.sin(Math.toRadians(angles[0]))) * (dx / 4.0);
273                 GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ROTATION_ANGLES__, angles);
274             }
275         }
276
277         private void doXYTranslation(MouseEvent e) {
278             int dx = e.getX() - previousEvent.getX();
279             int dy = e.getY() - previousEvent.getY();
280
281             for (Axes axes : currentAxes) {
282                 if (axes.getZoomEnabled()) {
283                     Double[] bounds = axes.getDisplayedBounds();
284
285                     Integer[] winSize = (Integer[]) GraphicController.getController().getProperty(axes.getParent(), GraphicObjectProperties.__GO_AXES_SIZE__);
286                     if (winSize == null) {
287                         // We are in a Frame
288                         Double[] position = (Double[]) GraphicController.getController().getProperty(axes.getParent(), GraphicObjectProperties.__GO_POSITION__);
289                         winSize = new Integer[2];
290                         winSize[0] = position[2].intValue();
291                         winSize[1] = position[3].intValue();
292                     }
293                     Double[] axesBounds = (Double[]) GraphicController.getController().getProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_AXES_BOUNDS__);
294                     Double[] axesMargins = (Double[]) GraphicController.getController().getProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_MARGINS__);
295                     Integer view = (Integer) GraphicController.getController().getProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_VIEW__);
296
297                     // Compute ratio from pixel move to user displayed data bounds
298                     double xDelta = Math.abs(bounds[0] - bounds[1]) / (winSize[0] * axesBounds[2] * (1 - axesMargins[0] - axesMargins[1]));
299                     double yDelta = Math.abs(bounds[2] - bounds[3]) / (winSize[1] * axesBounds[3] * (1 - axesMargins[2] - axesMargins[3]));
300
301                     if (view == 0) {
302                         // 2D View
303                         bounds[0] -= xDelta * dx;
304                         bounds[1] -= xDelta * dx;
305
306                         bounds[2] += yDelta * dy;
307                         bounds[3] += yDelta * dy;
308                     } else {
309                         // 3D view
310                         double orientation = - Math.signum(Math.cos(Math.toRadians(axes.getRotationAngles()[0])));
311                         double angle = - orientation * Math.toRadians(axes.getRotationAngles()[1]);
312
313                         double rotatedDX = dx * Math.sin(angle) + dy * Math.cos(angle);
314                         double rotatedDY = dx * Math.cos(angle) - dy * Math.sin(angle);
315
316                         bounds[0] -= xDelta * rotatedDX * orientation;
317                         bounds[1] -= xDelta * rotatedDX * orientation;
318
319                         bounds[2] += yDelta * rotatedDY;
320                         bounds[3] += yDelta * rotatedDY;
321                     }
322
323                     Boolean zoomed = tightZoomBoxToDataBounds(axes, bounds);
324                     boolean[] logFlags = { axes.getXAxisLogFlag(), axes.getYAxisLogFlag(), axes.getZAxisLogFlag()};
325                     ScaleUtils.applyInverseLogScaleToBounds(bounds, logFlags);
326
327                     GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_BOX__, bounds);
328                     GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_ENABLED__, zoomed);
329                 }
330             }
331         }
332
333         private void doZTranslation(MouseEvent e) {
334             int dy = e.getY() - previousEvent.getY();
335
336             for (Axes axes : currentAxes) {
337                 Double[] bounds = axes.getDisplayedBounds();
338
339                 double zDelta = (bounds[5] - bounds[4]) / 100;
340
341                 bounds[4] += zDelta * dy;
342                 bounds[5] += zDelta * dy;
343
344                 Boolean zoomed = tightZoomBoxToDataBounds(axes, bounds);
345                 boolean[] logFlags = { axes.getXAxisLogFlag(), axes.getYAxisLogFlag(), axes.getZAxisLogFlag()};
346                 ScaleUtils.applyInverseLogScaleToBounds(bounds, logFlags);
347
348                 GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_BOX__, bounds);
349                 GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_ENABLED__, zoomed);
350             }
351         }
352
353         /**
354          * Tight given bounds to axes data bounds.
355          * Bounds length along axes are conserved.
356          * @param axes the given axes.
357          * @param zoomBounds the zoomBounds.
358          * @return true if actually there is a zoom.
359          */
360         private boolean tightZoomBoxToDataBounds(Axes axes, Double[] zoomBounds) {
361             boolean zoomed = false;
362             Double[] dataBounds = axes.getMaximalDisplayedBounds();
363             for (int i : new int[] {0, 2, 4}) {
364                 if (zoomBounds[i] < dataBounds[i]) {
365                     double delta = dataBounds[i] - zoomBounds[i];
366                     zoomBounds[i] = dataBounds[i]; // zoomBounds[i] += delta;
367                     zoomBounds[i + 1] += delta;
368                 } else {
369                     zoomed = true;
370                 }
371             }
372
373             for (int i : new int[] {1, 3, 5}) {
374                 if (zoomBounds[i] > dataBounds[i]) {
375                     double delta = dataBounds[i] - zoomBounds[i];
376                     zoomBounds[i] = dataBounds[i]; // zoomBounds[i] += delta;
377                     zoomBounds[i - 1] += delta;
378                 } else {
379                     zoomed = true;
380                 }
381             }
382
383             return zoomed;
384         }
385     }
386 }