]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/MultitouchPanZoomRotateInteractor.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / MultitouchPanZoomRotateInteractor.java
index 56532f27d2cfcfc3b585661d028627982dea226e..3e1f625b8fc1d8f534e9c1c008418b4f5aac20e8 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g2d.participant;\r
-\r
-import java.awt.geom.AffineTransform;\r
-import java.awt.geom.Point2D;\r
-\r
-import org.simantics.g2d.canvas.Hints;\r
-import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
-import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
-import org.simantics.g2d.participant.CanvasGrab.PointerInfo;\r
-import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseExitEvent;\r
-import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;\r
-\r
-/**\r
- * @author Toni Kalajainen\r
- */\r
-public class MultitouchPanZoomRotateInteractor extends AbstractCanvasParticipant {\r
-\r
-       @Dependency TransformUtil util;\r
-       @Dependency CanvasGrab grab;\r
-    \r
-    // Pointer grabbing button\r
-    public static final int BUTTON_ID = 1;\r
-    \r
-    @EventHandler(priority = Integer.MAX_VALUE - 2000)\r
-    public boolean handleEvent(MouseButtonPressedEvent e) {\r
-        // ignore normal mouse        \r
-               if (getHint(Hints.KEY_TOOL) != Hints.PANTOOL) return false;\r
-        if (e.mouseId==0) return false;\r
-        if (e.button != BUTTON_ID) return false;\r
-       assertDependencies();\r
-        //System.out.println(e.mouseId+" down");\r
-        Point2D controlPos = e.controlPosition;\r
-        Point2D canvasPos = util.controlToCanvas(controlPos, null);\r
-        grab.grabCanvas(e.mouseId, canvasPos); \r
-        return false;\r
-    }\r
-    \r
-    @EventHandler(priority = Integer.MAX_VALUE - 2000)\r
-    public boolean handleEvent(MouseButtonReleasedEvent e) {\r
-        // ignore normal mouse\r
-               if (getHint(Hints.KEY_TOOL) != Hints.PANTOOL) return false;\r
-        if (e.mouseId==0) return false;\r
-        if (e.button != BUTTON_ID) return false;\r
-       assertDependencies();\r
-        //System.out.println(e.mouseId+" up");\r
-        grab.grabInfo.remove(e.mouseId);\r
-        \r
-        return false;\r
-    }\r
-    \r
-    @EventHandler(priority = Integer.MAX_VALUE - 2000)\r
-    public boolean handleEvent(MouseExitEvent e) {\r
-        // ignore normal mouse\r
-               if (getHint(Hints.KEY_TOOL) != Hints.PANTOOL) return false;\r
-        if (e.mouseId==0) return false;\r
-       assertDependencies();\r
-        //System.out.println(e.mouseId+" exit");\r
-        grab.grabInfo.remove(e.mouseId);        \r
-        return false;\r
-    }          \r
-       \r
-    @EventHandler(priority = Integer.MAX_VALUE - 2000)\r
-    public boolean handleEvent(MouseMovedEvent e) {   \r
-       if (e.context instanceof MouseUtil) return false;\r
-               if (getHint(Hints.KEY_TOOL) != Hints.PANTOOL) return false;\r
-       assertDependencies();\r
-        PointerInfo ai = grab.grabInfo.get(e.mouseId);\r
-        if (ai==null) return false;\r
-        if (grab.grabInfo.size() == 1) {\r
-            PointerInfo pi = grab.grabInfo.values().iterator().next();            \r
-\r
-            Point2D pt = util.controlToCanvas(e.controlPosition, null);\r
-            \r
-            double     a1_x = pi.anchorPos.getX();\r
-            double     a1_y = pi.anchorPos.getY();\r
-            double     b1_x = pt.getX();\r
-            double     b1_y = pt.getY();\r
-               \r
-            AffineTransform gat = util.getTransform();\r
-            gat.translate(b1_x-a1_x, b1_y-a1_y);\r
-            util.setTransform(gat);\r
-                       \r
-            return true; // TODO Do panning;\r
-        }        \r
-        if (grab.grabInfo.size()!=2) return false;\r
-        \r
-        double EPSILON = 3;\r
\r
-        PointerInfo[] pi = grab.grabInfo.values().toArray(new PointerInfo[2]);\r
-        \r
-        for (PointerInfo p : pi)\r
-               if (p.mouseId == e.mouseId)\r
-                       p.currentPos = util.controlToCanvas(e.controlPosition, null);\r
-\r
-        double a1_x = pi[0].anchorPos.getX();\r
-        double         a1_y = pi[0].anchorPos.getY();\r
-        double         b1_x = pi[0].currentPos.getX();\r
-        double b1_y = pi[0].currentPos.getY();\r
-        \r
-        double  a2_x = pi[1].anchorPos.getX();\r
-        double  a2_y = pi[1].anchorPos.getY();\r
-        double         b2_x = pi[1].currentPos.getX();\r
-        double b2_y = pi[1].currentPos.getY();        \r
-\r
-        for (PointerInfo p : pi)\r
-               if (p.mouseId != e.mouseId)\r
-                       p.currentPos = p.anchorPos;\r
-\r
-        double         da_x = a1_x - a2_x;\r
-        double da_y = a1_y - a2_y;\r
-        double         da_lensq = da_x*da_x + da_y*da_y;\r
-        if(da_lensq < EPSILON) {\r
-               System.err.println("a1 ja a2 ovat liian lähellä toisiaan");\r
-               return false;\r
-        }\r
-\r
-        double         db_x = b1_x - b2_x;\r
-        double db_y = b1_y - b2_y;\r
-        \r
-        double s_x = (db_x * da_x + db_y * da_y) / da_lensq;\r
-        double s_y = (-db_x * da_y + db_y * da_x) / da_lensq;\r
-        \r
-        double t_x = b1_x - s_x * a1_x + s_y * a1_y;\r
-        double t_y = b1_y - s_x * a1_y - s_y * a1_x;\r
-        \r
-        double mat[] = new double[] {s_x, s_y, -s_y, s_x, t_x, t_y};\r
-        AffineTransform at = new AffineTransform(mat);        \r
-               setDirty();\r
-        \r
-        AffineTransform gat = util.getTransform();\r
-        gat.concatenate(at);\r
-        util.setTransform(gat);\r
-        \r
-        return true;\r
-    }\r
-       \r
-       \r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g2d.participant;
+
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
+
+import org.simantics.g2d.canvas.Hints;
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
+import org.simantics.g2d.participant.CanvasGrab.PointerInfo;
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseExitEvent;
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
+
+/**
+ * @author Toni Kalajainen
+ */
+public class MultitouchPanZoomRotateInteractor extends AbstractCanvasParticipant {
+
+       @Dependency TransformUtil util;
+       @Dependency CanvasGrab grab;
+    
+    // Pointer grabbing button
+    public static final int BUTTON_ID = 1;
+    
+    @EventHandler(priority = Integer.MAX_VALUE - 2000)
+    public boolean handleEvent(MouseButtonPressedEvent e) {
+        // ignore normal mouse        
+               if (getHint(Hints.KEY_TOOL) != Hints.PANTOOL) return false;
+        if (e.mouseId==0) return false;
+        if (e.button != BUTTON_ID) return false;
+       assertDependencies();
+        //System.out.println(e.mouseId+" down");
+        Point2D controlPos = e.controlPosition;
+        Point2D canvasPos = util.controlToCanvas(controlPos, null);
+        grab.grabCanvas(e.mouseId, canvasPos); 
+        return false;
+    }
+    
+    @EventHandler(priority = Integer.MAX_VALUE - 2000)
+    public boolean handleEvent(MouseButtonReleasedEvent e) {
+        // ignore normal mouse
+               if (getHint(Hints.KEY_TOOL) != Hints.PANTOOL) return false;
+        if (e.mouseId==0) return false;
+        if (e.button != BUTTON_ID) return false;
+       assertDependencies();
+        //System.out.println(e.mouseId+" up");
+        grab.grabInfo.remove(e.mouseId);
+        
+        return false;
+    }
+    
+    @EventHandler(priority = Integer.MAX_VALUE - 2000)
+    public boolean handleEvent(MouseExitEvent e) {
+        // ignore normal mouse
+               if (getHint(Hints.KEY_TOOL) != Hints.PANTOOL) return false;
+        if (e.mouseId==0) return false;
+       assertDependencies();
+        //System.out.println(e.mouseId+" exit");
+        grab.grabInfo.remove(e.mouseId);        
+        return false;
+    }          
+       
+    @EventHandler(priority = Integer.MAX_VALUE - 2000)
+    public boolean handleEvent(MouseMovedEvent e) {   
+       if (e.context instanceof MouseUtil) return false;
+               if (getHint(Hints.KEY_TOOL) != Hints.PANTOOL) return false;
+       assertDependencies();
+        PointerInfo ai = grab.grabInfo.get(e.mouseId);
+        if (ai==null) return false;
+        if (grab.grabInfo.size() == 1) {
+            PointerInfo pi = grab.grabInfo.values().iterator().next();            
+
+            Point2D pt = util.controlToCanvas(e.controlPosition, null);
+            
+            double     a1_x = pi.anchorPos.getX();
+            double     a1_y = pi.anchorPos.getY();
+            double     b1_x = pt.getX();
+            double     b1_y = pt.getY();
+               
+            AffineTransform gat = util.getTransform();
+            gat.translate(b1_x-a1_x, b1_y-a1_y);
+            util.setTransform(gat);
+                       
+            return true; // TODO Do panning;
+        }        
+        if (grab.grabInfo.size()!=2) return false;
+        
+        double EPSILON = 3;
+        PointerInfo[] pi = grab.grabInfo.values().toArray(new PointerInfo[2]);
+        
+        for (PointerInfo p : pi)
+               if (p.mouseId == e.mouseId)
+                       p.currentPos = util.controlToCanvas(e.controlPosition, null);
+
+        double a1_x = pi[0].anchorPos.getX();
+        double         a1_y = pi[0].anchorPos.getY();
+        double         b1_x = pi[0].currentPos.getX();
+        double b1_y = pi[0].currentPos.getY();
+        
+        double  a2_x = pi[1].anchorPos.getX();
+        double  a2_y = pi[1].anchorPos.getY();
+        double         b2_x = pi[1].currentPos.getX();
+        double b2_y = pi[1].currentPos.getY();        
+
+        for (PointerInfo p : pi)
+               if (p.mouseId != e.mouseId)
+                       p.currentPos = p.anchorPos;
+
+        double         da_x = a1_x - a2_x;
+        double da_y = a1_y - a2_y;
+        double         da_lensq = da_x*da_x + da_y*da_y;
+        if(da_lensq < EPSILON) {
+               System.err.println("a1 ja a2 ovat liian lähellä toisiaan");
+               return false;
+        }
+
+        double         db_x = b1_x - b2_x;
+        double db_y = b1_y - b2_y;
+        
+        double s_x = (db_x * da_x + db_y * da_y) / da_lensq;
+        double s_y = (-db_x * da_y + db_y * da_x) / da_lensq;
+        
+        double t_x = b1_x - s_x * a1_x + s_y * a1_y;
+        double t_y = b1_y - s_x * a1_y - s_y * a1_x;
+        
+        double mat[] = new double[] {s_x, s_y, -s_y, s_x, t_x, t_y};
+        AffineTransform at = new AffineTransform(mat);        
+               setDirty();
+        
+        AffineTransform gat = util.getTransform();
+        gat.concatenate(at);
+        util.setTransform(gat);
+        
+        return true;
+    }
+       
+       
+}