a280988451bbf039f35af6d240b678145fac1a05
[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  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  */
11 package org.scilab.modules.renderer.JoGLView.interaction;
12
13 import java.awt.Component;
14 import java.awt.event.MouseAdapter;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseListener;
17 import java.awt.event.MouseMotionAdapter;
18 import java.awt.event.MouseMotionListener;
19 import java.awt.event.MouseWheelEvent;
20 import java.awt.event.MouseWheelListener;
21
22 import org.scilab.modules.commons.OS;
23 import org.scilab.modules.graphic_objects.axes.Axes;
24 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
25 import org.scilab.modules.graphic_objects.graphicObject.GraphicObjectProperties;
26 import org.scilab.modules.renderer.JoGLView.DrawerVisitor;
27
28 /**
29  * This class manage figure interaction.
30  *
31  * @author Pierre Lando
32  */
33 public class DragZoomRotateInteraction extends FigureInteraction {
34
35     private static final int XY_TRANSLATION_MODIFIER = MouseEvent.BUTTON1_MASK;
36     private static final int Z_TRANSLATION_MODIFIER = MouseEvent.BUTTON1_MASK | MouseEvent.ALT_MASK;
37     private static final int ROTATION_MODIFIER = MouseEvent.BUTTON3_MASK;
38     private static final int MACOSX_ROTATION_MODIFIER = MouseEvent.BUTTON1_MASK | MouseEvent.CTRL_MASK;
39
40     /**
41      * The box size is multiply by this value.
42      */
43     private static final double ZOOM_FACTOR = 1.02;
44
45     private final MouseListener mouseListener;
46     private final MouseWheelListener mouseWheelListener;
47     private final MouseMotionListener mouseMotionListener;
48
49     /**
50      * Last important mouse event.
51      */
52     private MouseEvent previousEvent;
53     private Axes currentAxes;
54
55     /**
56      * Default constructor.
57      * @param drawerVisitor parent drawer visitor.
58      */
59     public DragZoomRotateInteraction(DrawerVisitor drawerVisitor) {
60         super(drawerVisitor);
61         mouseMotionListener = new FigureMouseMotionListener();
62         mouseWheelListener = new FigureMouseWheelListener();
63         mouseListener = new FigureMouseListener();
64     }
65
66     @Override
67     protected void changeEnable(boolean isEnable) {
68         Component component = getDrawerVisitor().getComponent();
69         if (component != null) {
70             if (isEnable) {
71                 component.addMouseListener(mouseListener);
72                 component.addMouseWheelListener(mouseWheelListener);
73             } else {
74                 component.removeMouseListener(mouseListener);
75                 component.removeMouseMotionListener(mouseMotionListener);
76                 component.removeMouseWheelListener(mouseWheelListener);
77             }
78         }
79     }
80
81     /**
82      * This {@see MouseListner} activate the {@see MouseMotionListener} when at least
83      * one button is pressed.
84      * The event is saved in {@see previousEvent}
85      */
86     private class FigureMouseListener extends MouseAdapter implements MouseListener {
87
88         private int pressedButtons = 0;
89
90         @Override
91         public void mousePressed(MouseEvent e) {
92             if (pressedButtons == 0) {
93                 previousEvent = e;
94                 if (currentAxes == null) {
95                     currentAxes = getUnderlyingAxes(e.getPoint());
96                     if (currentAxes != null) {
97                         getDrawerVisitor().getComponent().addMouseMotionListener(mouseMotionListener);
98                     }
99                 }
100             }
101             pressedButtons++;
102         }
103
104         @Override
105         public void mouseReleased(MouseEvent e) {
106             pressedButtons--;
107             if (pressedButtons == 0) {
108                 getDrawerVisitor().getComponent().removeMouseMotionListener(mouseMotionListener);
109                 currentAxes = null;
110             }
111         }
112     }
113
114     /**
115      * This {@see MouseWheelListener} manage zoom/un-zoom on the figure.
116      */
117     private class FigureMouseWheelListener implements MouseWheelListener {
118
119         @Override
120         public void mouseWheelMoved(MouseWheelEvent e) {
121             Axes axes = getUnderlyingAxes(e.getPoint());
122             if (axes != null) {
123                 double scale = Math.pow(ZOOM_FACTOR, e.getUnitsToScroll());
124                 Double[] bounds = axes.getDisplayedBounds();
125
126                 double xDelta = (bounds[1] - bounds[0]) / 2;
127                 double xMiddle = (bounds[1] + bounds[0]) / 2;
128                 bounds[0] = xMiddle - xDelta * scale;
129                 bounds[1] = xMiddle + xDelta * scale;
130
131                 double yDelta = (bounds[3] - bounds[2]) / 2;
132                 double yMiddle = (bounds[3] + bounds[2]) / 2;
133                 bounds[2] = yMiddle - yDelta * scale;
134                 bounds[3] = yMiddle + yDelta * scale;
135
136                 double zDelta = (bounds[5] - bounds[4]) / 2;
137                 double zMiddle = (bounds[5] + bounds[4]) / 2;
138                 bounds[4] = zMiddle - zDelta * scale;
139                 bounds[5] = zMiddle + zDelta * scale;
140
141                 Boolean zoomed = tightZoomBounds(axes, bounds);
142                 GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_BOX__, bounds);
143                 GraphicController.getController().setProperty(axes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_ENABLED__, zoomed);
144             }
145         }
146     }
147
148     /**
149      * This {@see MouseMotionListener} manage rotation and translation on the figure.
150      */
151     private class FigureMouseMotionListener extends MouseMotionAdapter implements MouseMotionListener {
152
153         @Override
154         public void mouseMoved(MouseEvent e) {
155             /*
156              * Mac OS X specific case: the users first presses CTRL and then left-clic.
157              */
158             if (OS.get() == OS.MAC && e.isControlDown() && e.getButton() == 0) {
159                 doRotation(e);
160             }
161         }
162         public void mouseDragged(MouseEvent e) {
163             switch (e.getModifiers()) {
164                 case MACOSX_ROTATION_MODIFIER:
165                     /*
166                      * Mac OS X specific case: the users first left-clic and then presses CTRL
167                      */
168                     if (OS.get() == OS.MAC && e.isControlDown()) {
169                         doRotation(e);
170                         break;
171                     }
172                 case XY_TRANSLATION_MODIFIER:
173                     doXYTranslation(e);
174                     break;
175                 case Z_TRANSLATION_MODIFIER:
176                     doZTranslation(e);
177                     break;
178                 case ROTATION_MODIFIER:
179                     doRotation(e);
180                     break;
181             }
182
183             previousEvent = e;
184         }
185
186         private void doRotation(MouseEvent e) {
187             int dx = e.getX() - previousEvent.getX();
188             int dy = e.getY() - previousEvent.getY();
189
190             if (currentAxes != null) {
191                 Double[] angles = currentAxes.getRotationAngles();
192                 angles[0] -= dy / 4.0;
193                 angles[1] -= Math.signum(Math.sin(Math.toRadians(angles[0]))) * (dx / 4.0);
194                 GraphicController.getController().setProperty(currentAxes.getIdentifier(), GraphicObjectProperties.__GO_ROTATION_ANGLES__, angles);
195             }
196         }
197
198         private void doXYTranslation(MouseEvent e) {
199             int dx = e.getX() - previousEvent.getX();
200             int dy = e.getY() - previousEvent.getY();
201
202             if (currentAxes != null) {
203                 if (currentAxes.getZoomEnabled()) {
204                     Double[] bounds = currentAxes.getDisplayedBounds();
205                     double orientation = Math.signum(Math.cos(Math.toRadians(currentAxes.getRotationAngles()[0])));
206                     double angle = - orientation * Math.toRadians(currentAxes.getRotationAngles()[1]);
207
208                     double xDelta = (bounds[0] - bounds[1]) / 100;
209                     double yDelta = (bounds[2] - bounds[3]) / 100;
210
211                     double rotatedDX = dx * Math.sin(angle) + dy * Math.cos(angle);
212                     double rotatedDY = dx * Math.cos(angle) - dy * Math.sin(angle);
213
214                     bounds[0] += xDelta * rotatedDX * orientation;
215                     bounds[1] += xDelta * rotatedDX * orientation;
216
217                     bounds[2] += yDelta * rotatedDY;
218                     bounds[3] += yDelta * rotatedDY;
219
220                     Boolean zoomed = tightZoomBoxToDataBounds(currentAxes, bounds);
221                     GraphicController.getController().setProperty(currentAxes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_BOX__, bounds);
222                     GraphicController.getController().setProperty(currentAxes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_ENABLED__, zoomed);
223                 }
224             }
225         }
226
227         private void doZTranslation(MouseEvent e) {
228             int dy = e.getY() - previousEvent.getY();
229
230             if (currentAxes != null) {
231                 Double[] bounds = currentAxes.getDisplayedBounds();
232
233                 double zDelta = (bounds[5] - bounds[4]) / 100;
234
235                 bounds[4] += zDelta * dy;
236                 bounds[5] += zDelta * dy;
237
238                 Boolean zoomed = tightZoomBoxToDataBounds(currentAxes, bounds);
239                 GraphicController.getController().setProperty(currentAxes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_BOX__, bounds);
240                 GraphicController.getController().setProperty(currentAxes.getIdentifier(), GraphicObjectProperties.__GO_ZOOM_ENABLED__, zoomed);
241             }
242         }
243
244         /**
245          * Tight given bounds to axes data bounds.
246          * Bounds length along axes are conserved.
247          * @param axes the given axes.
248          * @param zoomBounds the zoomBounds.
249          * @return true if actually there is a zoom.
250          */
251         private boolean tightZoomBoxToDataBounds(Axes axes, Double[] zoomBounds) {
252             boolean zoomed = false;
253             Double[] dataBounds = axes.getMaximalDisplayedBounds();
254             for (int i : new int[] {0, 2, 4}) {
255                 if (zoomBounds[i] < dataBounds[i]) {
256                     double delta = dataBounds[i] - zoomBounds[i];
257                     zoomBounds[i] = dataBounds[i]; // zoomBounds[i] += delta;
258                     zoomBounds[i + 1] += delta;
259                 } else {
260                     zoomed = true;
261                 }
262             }
263
264             for (int i : new int[] {1, 3, 5}) {
265                 if (zoomBounds[i] > dataBounds[i]) {
266                     double delta = dataBounds[i] - zoomBounds[i];
267                     zoomBounds[i] = dataBounds[i]; // zoomBounds[i] += delta;
268                     zoomBounds[i - 1] += delta;
269                 } else {
270                     zoomed = true;
271                 }
272             }
273
274             return zoomed;
275         }
276     }
277 }