]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d/src/org/simantics/proconf/g3d/actions/CameraAction.java
git-svn-id: https://www.simantics.org/svn/simantics/3d/trunk@22280 ac1ea38d-2e2b...
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / proconf / g3d / actions / CameraAction.java
diff --git a/org.simantics.g3d/src/org/simantics/proconf/g3d/actions/CameraAction.java b/org.simantics.g3d/src/org/simantics/proconf/g3d/actions/CameraAction.java
new file mode 100644 (file)
index 0000000..71d15c4
--- /dev/null
@@ -0,0 +1,134 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\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.proconf.g3d.actions;\r
+\r
+import java.awt.event.KeyEvent;\r
+import java.awt.event.MouseEvent;\r
+import java.util.List;\r
+\r
+import javax.vecmath.Vector3d;\r
+\r
+import org.simantics.db.Graph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.proconf.g3d.base.JmeRenderingComponent;\r
+import org.simantics.proconf.g3d.base.MathTools;\r
+import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
+import org.simantics.proconf.g3d.common.OrbitalCamera;\r
+\r
+\r
+\r
+public class CameraAction extends InteractiveAction{\r
+       \r
+       private JmeRenderingComponent component;\r
+       private OrbitalCamera camera;\r
+       \r
+    public CameraAction(ThreeDimensionalEditorBase parent) {\r
+        super(parent);\r
+        component = parent.getRenderingComponent();\r
+        camera = parent.getCamera();\r
+    }\r
+    \r
+    @Override\r
+    public void activate() {\r
+\r
+    }\r
+\r
+    @Override\r
+    public void deactivate() {\r
+\r
+    }\r
+    \r
+    @Override\r
+  public boolean usable(Graph graph,List<Resource> resources) {\r
+      return true;\r
+  }\r
+\r
+    Vector3d prevIntersectPoint = new Vector3d();\r
+    @Override\r
+    public void update() {\r
+        double scale = 1.0;\r
+        if (input.keyDown(KeyEvent.VK_CONTROL))\r
+            scale = -1.0;\r
+        if (input.keyPressed(KeyEvent.VK_NUMPAD5)) {\r
+            if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION)\r
+                component.setProjectionPolicy(JmeRenderingComponent.PARALLEL_PROJECTION);\r
+            else\r
+                component.setProjectionPolicy(JmeRenderingComponent.PERSPECTIVE_PROJECTION);\r
+            parent.setViewChanged(true);\r
+        }\r
+        if (input.keyPressed(KeyEvent.VK_NUMPAD7)) {\r
+            camera.setCameraPosRelativeToTarget(new Vector3d(camera.getDistanceToTarget() * scale, 0, 0));\r
+            parent.setViewChanged(true);\r
+        }\r
+        if (input.keyPressed(KeyEvent.VK_NUMPAD1)) {\r
+            camera.setCameraPosRelativeToTarget(new Vector3d(0, 0, camera.getDistanceToTarget() * scale));\r
+            parent.setViewChanged(true);\r
+        }\r
+        if (input.keyPressed(KeyEvent.VK_NUMPAD9)) {\r
+            camera.setCameraPosRelativeToTarget(new Vector3d(0, camera.getDistanceToTarget() * scale, 0));\r
+            parent.setViewChanged(true);\r
+        }\r
+\r
+        if (input.mousePressed() && ((input.pressModifiers() & MouseEvent.BUTTON1_MASK) > 0)) {\r
+            Vector3d o = new Vector3d();\r
+            Vector3d d = new Vector3d();\r
+            parent.createPickRay(o, d);\r
+            Vector3d point = new Vector3d(camera.getTarget());\r
+            Vector3d normal = camera.getUnNormalizedHeading();\r
+            normal.normalize();\r
+            MathTools.intersectStraightPlane(o, d, point, normal, prevIntersectPoint);\r
+        }\r
+        if (!input.mouseDragged())\r
+            return;\r
+        parent.setViewChanged(true);\r
+        Vector3d msTmp = new Vector3d();\r
+        msTmp.x = (input.prevMouseX() - input.mouseX()) / 100f;\r
+        msTmp.y = (input.prevMouseY() - input.mouseY()) / 100f;\r
+\r
+        if ((input.dragModifiers() & MouseEvent.BUTTON1_MASK) > 0) {\r
+            if ((input.dragModifiers() & MouseEvent.CTRL_MASK) > 0) {\r
+                Vector3d o = new Vector3d();\r
+                Vector3d d = new Vector3d();\r
+                parent.createPickRay(o, d);\r
+                Vector3d point = new Vector3d(camera.getTarget());\r
+                Vector3d normal = camera.getUnNormalizedHeading();\r
+                normal.normalize();\r
+                Vector3d intersectPoint = new Vector3d();\r
+                if (MathTools.intersectStraightPlane(o, d, point, normal, intersectPoint)) {\r
+                    Vector3d delta = new Vector3d(intersectPoint);\r
+                    delta.sub(prevIntersectPoint);\r
+                    prevIntersectPoint = intersectPoint;\r
+                    delta.negate();\r
+                    camera.translate(delta);\r
+                    prevIntersectPoint.add(delta);\r
+                } else {\r
+                    camera.moveRight((float) msTmp.x);\r
+                    camera.moveUp(-(float) msTmp.y);\r
+                }\r
+            } else {\r
+                camera.rotateRight((float) msTmp.x);\r
+                camera.rotateUp((float) msTmp.y);\r
+            }\r
+        } else if ((input.dragModifiers() & MouseEvent.BUTTON2_MASK) > 0) {\r
+            // System.out.println("zoom");\r
+            if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION) {\r
+                camera.moveScaledToTarget((float) msTmp.y * 0.5f);\r
+            } else {\r
+                component.setScreenScale(component.getScreenScale() - (float) msTmp.y * 0.5f * component.getScreenScale());\r
+            }\r
+        }\r
+//        } else {\r
+//            System.out.println("cameraAction!");\r
+//            System.out.println(input);\r
+//        } \r
+       \r
+    }\r
+}\r