]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/MousePanZoomInteractor.java
Fixed invalid comparisons which were identified by Eclipse IDE 2018-09
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / MousePanZoomInteractor.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  *
14  * @author Toni Kalajainen
15  */
16 package org.simantics.g2d.participant;
17
18 import java.awt.Cursor;
19 import java.awt.geom.Point2D;
20
21 import org.simantics.g2d.canvas.ICanvasContext;
22 import org.simantics.g2d.canvas.IMouseCursorContext;
23 import org.simantics.g2d.canvas.IMouseCursorHandle;
24 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
25 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
26 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
27 import org.simantics.scenegraph.g2d.events.Event;
28 import org.simantics.scenegraph.g2d.events.MouseEvent;
29 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
30 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;
31 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;
32 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;
33 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
34 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseWheelMovedEvent;
35 import org.simantics.scenegraph.g2d.nodes.NavigationNode;
36 import org.simantics.utils.datastructures.hints.IHintContext.Key;
37 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
38
39 /**
40  * Mouse pan & zoom
41  * 
42  * Keyboard pan & zoom & zoom to fit etc
43  * 
44  * This interactor depends on : - TransformUtil - MouseUtil - KeyUtil -
45  * PanZoomRotateHandler
46  * 
47  * TODO Add rotate mode
48  * 
49  * @deprecated this logic has been moved into scenegraph {@link NavigationNode}.
50  *             Using this participant will cause double navigation.
51  */
52 @Deprecated
53 public class MousePanZoomInteractor extends AbstractCanvasParticipant {
54
55     @Dependency TransformUtil util;
56     @Dependency MouseUtil mice;
57     @Dependency KeyUtil keys;
58     @Dependency PanZoomRotateHandler pzr;
59
60     /** Event priority for initiating pan / zoom */
61     public final static int MOUSE_PAN_PRIORITY = Integer.MAX_VALUE - 2000;
62
63     /** Event priority when panning is enabled */
64     public final static int PAN_PRIORITY = Integer.MAX_VALUE - 1000;
65     public final static int ZOOM_PRIORITY = Integer.MAX_VALUE - 1000 +1;
66
67     /** Cursor when panning */
68     public final static Cursor PAN_CURSOR = new Cursor(Cursor.MOVE_CURSOR);
69
70     /** Cursor when panning */
71     public final static Cursor ZOOM_CURSOR = new Cursor(Cursor.N_RESIZE_CURSOR);
72
73     /** Is pan enabled */
74     public final static Key KEY_MOUSE_PAN_ENABLED = new KeyOf(Boolean.class);
75
76     /** Is zoom enabled */
77     public final static Key KEY_MOUSE_ZOOM_ENABLED = new KeyOf(Boolean.class);
78
79     /** are mouse wheel action enabled */
80     public final static Key KEY_MOUSE_WHEEL_ZOOM_PAN_ENABLED = new KeyOf(Boolean.class);
81
82     /** The speed of mouse zoom , eg 0..100..X */
83     public final static Key KEY_MOUSE_ZOOM_SPEED = new KeyOf(Double.class);
84
85     /** Default mouse zoom speed */
86     public final static double DEFAULT_MOUSE_ZOOM_SPEED = 100;
87
88     /** The speed of mouse zoom , eg 0..100..X */
89     public final static Key KEY_MOUSE_WHEEL_ZOOM_SPEED = new KeyOf(Double.class);
90
91     /** Default mouse zoom speed */
92     public final static double DEFAULT_MOUSE_WHEEL_ZOOM_SPEED = 1000;
93
94     /** The speed of mouse zoom , eg 0..100..X */
95     public final static Key KEY_MOUSE_WHEEL_TRANSLATE_AMOUNT = new KeyOf(Double.class);
96
97     /** Default mouse zoom speed */
98     public final static double DEFAULT_MOUSE_WHEEL_TRANSLATE_AMOUNT = 10;
99
100     @Override
101     public void addedToContext(ICanvasContext ctx) {
102         super.addedToContext(ctx);
103         // Set Default values
104         setHint(KEY_MOUSE_ZOOM_SPEED, DEFAULT_MOUSE_ZOOM_SPEED);
105         setHint(KEY_MOUSE_ZOOM_ENABLED, true);
106         setHint(KEY_MOUSE_PAN_ENABLED, true);
107         setHint(KEY_MOUSE_WHEEL_ZOOM_PAN_ENABLED, true);
108         setHint(KEY_MOUSE_WHEEL_ZOOM_SPEED, DEFAULT_MOUSE_WHEEL_ZOOM_SPEED);
109         setHint(KEY_MOUSE_WHEEL_TRANSLATE_AMOUNT, DEFAULT_MOUSE_WHEEL_TRANSLATE_AMOUNT);
110     }
111
112     /**
113      * Inits mouse pan and zoom interactors.
114      * Eats mouse presses if shift is pressed.
115      * 
116      * @param e
117      * @return
118      */
119     @EventHandler(priority = MOUSE_PAN_PRIORITY)
120     public boolean handleEvent(MouseEvent e) {
121         assertDependencies();
122         if (e.context instanceof MouseUtil)
123             return false;
124
125         if ((e instanceof MouseButtonPressedEvent) && (e.stateMask & MouseEvent.SHIFT_MASK) != 0 && ((MouseButtonPressedEvent)e).button==MouseEvent.LEFT_BUTTON)
126             return true;
127
128         // Mouse pan & zoom
129         if (e instanceof MouseMovedEvent) {
130             MouseMovedEvent mme = (MouseMovedEvent) e;
131             MouseInfo mi = mice.getMouseInfo(mme.mouseId);
132             if (mi == null)
133                 return false;
134             if ((e.stateMask & MouseEvent.SHIFT_MASK) != 0 & mi.isMouseButtonPressed(MouseEvent.LEFT_BUTTON))
135             {
136                 if (!isPanEnabled())
137                     return false;
138                 if (!isPanning()) {
139                     getContext().add( new PanMode(mme.mouseId, mme.controlPosition, MouseEvent.LEFT_BUTTON) );
140                 }
141                 return true;
142             }
143             if (mi.isMouseButtonPressed(MouseEvent.MIDDLE_BUTTON))
144             {
145                 if (!isPanEnabled())
146                     return false;
147                 if (!isPanning()) {
148                     getContext().add( new PanMode(mme.mouseId, mme.controlPosition, MouseEvent.MIDDLE_BUTTON) );
149                 }
150                 return true;
151             }
152             if ((e.stateMask & MouseEvent.SHIFT_MASK) != 0 & mi.isMouseButtonPressed(MouseEvent.RIGHT_BUTTON))
153             {
154                 if (!isZoomEnabled())
155                     return false;
156                 if (!isZooming()) {
157                     Point2D diagramPosition = util.getInverseTransform().transform(mme.controlPosition, new Point2D.Double());
158
159                     getContext().add( new ZoomMode(mme.mouseId, mme.controlPosition, diagramPosition, MouseEvent.RIGHT_BUTTON) );
160                 }
161                 return true;
162             }
163         }
164
165         // Mouse wheel zoom / scroll
166         if (e instanceof MouseWheelMovedEvent) {
167             if (!isMouseWheelZoomPanEnabled())
168                 return false;
169             MouseWheelMovedEvent we = (MouseWheelMovedEvent) e;
170             //if (!mm.isMouseButtonPressed(we.mouseId, MouseEvent.MIDDLE_BUTTON))
171             /*
172             if (!ctrl())
173                         {
174                                 if (we.scrollType == MouseWheelMovedEvent.WHEEL_UNIT_SCROLL)
175                                 {
176                                         double dy = we.wheelRotation * getMouseWheelTranslateAmount();
177                                         if (shift())
178                                                 util.translateWithControlCoordinates(new Point2D.Double(dy, 0));
179                                         else
180                                                 util.translateWithControlCoordinates(new Point2D.Double(0, dy));
181                                         return true;
182                                 }
183                         } else
184              */
185             {
186                 if (we.scrollType == MouseWheelMovedEvent.WHEEL_UNIT_SCROLL)
187                 {
188                     double dy = we.wheelRotation;
189                     double zoom = getMouseWheelZoomSpeed();
190                     double base = 1.00 + (zoom / 10000);
191                     double scaleFactor = Math.pow(base, dy);
192
193                     scaleFactor = pzr.limitScaleFactor(scaleFactor);
194                     util.zoomAroundControlPoint(scaleFactor, we.controlPosition);
195                     //ti.zoom(scaleFactor);
196                     return true;
197                 }
198             }
199         }
200         return false;
201     }
202
203     public boolean isPanEnabled()
204     {
205         Boolean h = getHint(KEY_MOUSE_PAN_ENABLED);
206         if (h==null) return false;
207         return h;
208     }
209
210     public boolean isPanning()
211     {
212         return !getContext().getItemsByClass(PanMode.class).isEmpty();
213     }
214
215     public boolean isZooming()
216     {
217         return !getContext().getItemsByClass(ZoomMode.class).isEmpty();
218     }
219
220     public boolean isZoomEnabled()
221     {
222         Boolean h = getHint(KEY_MOUSE_ZOOM_ENABLED);
223         if (h==null) return false;
224         return h;
225     }
226
227     public boolean isMouseWheelZoomPanEnabled()
228     {
229         Boolean h = getHint(KEY_MOUSE_WHEEL_ZOOM_PAN_ENABLED);
230         if (h==null) return false;
231         return h;
232     }
233
234     /**
235      * is Shift key down
236      * @return
237      */
238     /*public boolean shift()
239         {
240                 return keys.isKeyPressed(KeyEvent.VK_SHIFT);
241         }
242
243         public boolean ctrl()
244         {
245                 return keys.isKeyPressed(KeyEvent.VK_CONTROL);
246         }*/
247
248     public double getMouseZoomSpeed()
249     {
250         Double h = getHint(KEY_MOUSE_ZOOM_SPEED);
251         if (h==null) return DEFAULT_MOUSE_ZOOM_SPEED;
252         return h;
253     }
254
255     public double getMouseWheelZoomSpeed()
256     {
257         Double h = getHint(KEY_MOUSE_WHEEL_ZOOM_SPEED);
258         if (h==null) return DEFAULT_MOUSE_WHEEL_ZOOM_SPEED;
259         return h;
260     }
261
262     public double getMouseWheelTranslateAmount()
263     {
264         Double h = getHint(KEY_MOUSE_WHEEL_TRANSLATE_AMOUNT);
265         if (h==null) return DEFAULT_MOUSE_WHEEL_TRANSLATE_AMOUNT;
266         return h;
267     }
268
269     /**
270      * Shift and left mouse button is pressed, do panning
271      */
272     class PanMode extends AbstractCanvasParticipant
273     {
274         @Dependency TransformUtil util;
275         final int mouseId;
276         int releaseButton;
277         // Mouse system coordinates
278         Point2D mousePos;
279         IMouseCursorHandle cursor;
280
281         public PanMode(int mouseId, Point2D mousePos, int releaseButton) {
282             super();
283             this.mouseId = mouseId;
284             this.mousePos = new Point2D.Double(mousePos.getX(), mousePos.getY());
285             this.releaseButton = releaseButton;
286         }
287
288         @Override
289         public void addedToContext(ICanvasContext ctx) {
290             super.addedToContext(ctx);
291             IMouseCursorContext mctx = getContext().getMouseCursorContext();
292             if (mctx!=null)
293                 cursor = mctx.setCursor(mouseId, PAN_CURSOR);
294         }
295
296         @EventHandler(priority = PAN_PRIORITY)
297         public boolean handleEvent(Event e) {
298             if (e instanceof MouseButtonReleasedEvent) {
299                 MouseButtonReleasedEvent mpe = (MouseButtonReleasedEvent) e;
300                 if (mpe.button == releaseButton && mpe.mouseId == mouseId)
301                 {
302                     remove();
303                     return false;
304                 }
305             }
306
307             if (e instanceof KeyReleasedEvent) {
308                 KeyReleasedEvent ke = (KeyReleasedEvent) e;
309                 if (ke.keyCode == java.awt.event.KeyEvent.VK_SHIFT)
310                 {
311                     remove();
312                     return false;
313                 }
314             }
315
316             if (e instanceof MouseMovedEvent) {
317                 if (e.getContext() instanceof MouseUtil) return true;
318                 MouseMovedEvent mme = (MouseMovedEvent) e;
319                 if (mme.mouseId == mouseId) {
320                     Point2D oldPos = mousePos;
321                     Point2D newPos = mme.controlPosition;
322
323                     double dx = newPos.getX() - oldPos.getX();
324                     double dy = newPos.getY() - oldPos.getY();
325                     if (dx==0 && dy==0) return true;
326
327                     this.mousePos.setLocation(newPos);
328
329                     util.translateWithControlCoordinates(new Point2D.Double(dx, dy));
330                     return true;
331                 }
332             }
333
334             return false;
335         }
336
337         @Override
338         public void removedFromContext(ICanvasContext ctx) {
339             if (cursor!=null) {
340                 cursor.remove();
341                 cursor = null;
342             }
343             super.removedFromContext(ctx);
344         }
345
346     }
347
348     /**
349      * Shift and right mouse button is pressed, do zooming
350      */
351     class ZoomMode extends AbstractCanvasParticipant
352     {
353         final int mouseId;
354         int releaseButton;
355
356         // Mouse system coordinates
357         Point2D prevMousePos;
358         Point2D origControlMousePos;
359         Point2D origDiagramMousePos;
360         IMouseCursorHandle cursor;
361
362         public ZoomMode(int mouseId, Point2D controlMousePos, Point2D mouseDiagramPos, int releaseButton) {
363             super();
364             this.mouseId = mouseId;
365             this.prevMousePos = (Point2D) controlMousePos.clone();
366             this.origControlMousePos = (Point2D) controlMousePos.clone();
367             this.origDiagramMousePos = (Point2D) mouseDiagramPos.clone();
368             this.releaseButton = releaseButton;
369         }
370
371         @Override
372         public void addedToContext(ICanvasContext ctx) {
373             super.addedToContext(ctx);
374             IMouseCursorContext mctx = getContext().getMouseCursorContext();
375             if (mctx!=null)
376                 cursor = mctx.setCursor(mouseId, ZOOM_CURSOR);
377         }
378
379         @EventHandler(priority = ZOOM_PRIORITY)
380         public boolean handleEvent(Event e) {
381             if (e instanceof MouseButtonReleasedEvent) {
382                 MouseButtonReleasedEvent mpe = (MouseButtonReleasedEvent) e;
383                 if (mpe.button == releaseButton && mpe.mouseId == mouseId)
384                 {
385                     remove();
386                     return false;
387                 }
388             }
389
390             if (e instanceof KeyReleasedEvent) {
391                 KeyReleasedEvent ke = (KeyReleasedEvent) e;
392                 if (ke.keyCode == java.awt.event.KeyEvent.VK_SHIFT)
393                 {
394                     remove();
395                     return false;
396                 }
397             }
398
399             if (e instanceof MouseMovedEvent) {
400                 if (e.getContext() instanceof MouseUtil) return true;
401                 MouseMovedEvent mme = (MouseMovedEvent) e;
402                 if (mme.mouseId == mouseId) {
403                     Point2D oldPos = prevMousePos;
404                     Point2D newPos = mme.controlPosition;
405
406                     double dy = newPos.getY() - oldPos.getY();
407 //                                      double dx = newPos.getX() - oldPos.getX();
408                     this.prevMousePos.setLocation(newPos);
409
410                     double zoomSpeed = getMouseZoomSpeed();
411                     double base = 1.00 + (zoomSpeed / 10000);
412                     double scaleFactor = Math.pow(base, -dy);
413
414                     scaleFactor = pzr.limitScaleFactor(scaleFactor);
415                     util.zoomAroundDiagramPoint(scaleFactor, origDiagramMousePos);
416                     return true;
417                 }
418             }
419
420             return false;
421         }
422
423         @Override
424         public void removedFromContext(ICanvasContext ctx) {
425             if (cursor!=null) {
426                 cursor.remove();
427                 cursor = null;
428             }
429
430             super.removedFromContext(ctx);
431         }
432     }
433
434 }