]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/actions/CameraAction.java
2f6190464271d52f1511dc2c32697c67158e67d3
[simantics/3d.git] / org.simantics.proconf.g3d / src / org / simantics / proconf / g3d / actions / CameraAction.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.actions;\r
12 \r
13 import java.awt.event.KeyEvent;\r
14 import java.awt.event.MouseEvent;\r
15 import java.util.List;\r
16 \r
17 import javax.vecmath.Vector3d;\r
18 \r
19 import org.simantics.db.Graph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.proconf.g3d.base.JmeRenderingComponent;\r
22 import org.simantics.proconf.g3d.base.MathTools;\r
23 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
24 import org.simantics.proconf.g3d.common.OrbitalCamera;\r
25 \r
26 \r
27 \r
28 public class CameraAction extends InteractiveAction{\r
29         \r
30         private JmeRenderingComponent component;\r
31         private OrbitalCamera camera;\r
32         \r
33     public CameraAction(ThreeDimensionalEditorBase parent) {\r
34         super(parent);\r
35         component = parent.getRenderingComponent();\r
36         camera = parent.getCamera();\r
37     }\r
38     \r
39     @Override\r
40     public void activate() {\r
41 \r
42     }\r
43 \r
44     @Override\r
45     public void deactivate() {\r
46 \r
47     }\r
48     \r
49     @Override\r
50   public boolean usable(Graph graph,List<Resource> resources) {\r
51       return true;\r
52   }\r
53 \r
54     Vector3d prevIntersectPoint = new Vector3d();\r
55     @Override\r
56     public void update() {\r
57         double scale = 1.0;\r
58         if (input.keyDown(KeyEvent.VK_CONTROL))\r
59             scale = -1.0;\r
60         if (input.keyPressed(KeyEvent.VK_NUMPAD5)) {\r
61             if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION)\r
62                 component.setProjectionPolicy(JmeRenderingComponent.PARALLEL_PROJECTION);\r
63             else\r
64                 component.setProjectionPolicy(JmeRenderingComponent.PERSPECTIVE_PROJECTION);\r
65             parent.setViewChanged(true);\r
66         }\r
67         if (input.keyPressed(KeyEvent.VK_NUMPAD7)) {\r
68             camera.setCameraPosRelativeToTarget(new Vector3d(camera.getDistanceToTarget() * scale, 0, 0));\r
69             parent.setViewChanged(true);\r
70         }\r
71         if (input.keyPressed(KeyEvent.VK_NUMPAD1)) {\r
72             camera.setCameraPosRelativeToTarget(new Vector3d(0, 0, camera.getDistanceToTarget() * scale));\r
73             parent.setViewChanged(true);\r
74         }\r
75         if (input.keyPressed(KeyEvent.VK_NUMPAD9)) {\r
76             camera.setCameraPosRelativeToTarget(new Vector3d(0, camera.getDistanceToTarget() * scale, 0));\r
77             parent.setViewChanged(true);\r
78         }\r
79 \r
80         if (input.mousePressed() && ((input.pressModifiers() & MouseEvent.BUTTON1_MASK) > 0)) {\r
81             Vector3d o = new Vector3d();\r
82             Vector3d d = new Vector3d();\r
83             parent.createPickRay(o, d);\r
84             Vector3d point = new Vector3d(camera.getTarget());\r
85             Vector3d normal = camera.getUnNormalizedHeading();\r
86             normal.normalize();\r
87             MathTools.intersectStraightPlane(o, d, point, normal, prevIntersectPoint);\r
88         }\r
89         if (!input.mouseDragged())\r
90             return;\r
91         parent.setViewChanged(true);\r
92         Vector3d msTmp = new Vector3d();\r
93         msTmp.x = (input.prevMouseX() - input.mouseX()) / 100f;\r
94         msTmp.y = (input.prevMouseY() - input.mouseY()) / 100f;\r
95 \r
96         if ((input.dragModifiers() & MouseEvent.BUTTON1_MASK) > 0) {\r
97             if ((input.dragModifiers() & MouseEvent.CTRL_MASK) > 0) {\r
98                 Vector3d o = new Vector3d();\r
99                 Vector3d d = new Vector3d();\r
100                 parent.createPickRay(o, d);\r
101                 Vector3d point = new Vector3d(camera.getTarget());\r
102                 Vector3d normal = camera.getUnNormalizedHeading();\r
103                 normal.normalize();\r
104                 Vector3d intersectPoint = new Vector3d();\r
105                 if (MathTools.intersectStraightPlane(o, d, point, normal, intersectPoint)) {\r
106                     Vector3d delta = new Vector3d(intersectPoint);\r
107                     delta.sub(prevIntersectPoint);\r
108                     prevIntersectPoint = intersectPoint;\r
109                     delta.negate();\r
110                     camera.translate(delta);\r
111                     prevIntersectPoint.add(delta);\r
112                 } else {\r
113                     camera.moveRight((float) msTmp.x);\r
114                     camera.moveUp(-(float) msTmp.y);\r
115                 }\r
116             } else {\r
117                 camera.rotateRight((float) msTmp.x);\r
118                 camera.rotateUp((float) msTmp.y);\r
119             }\r
120         } else if ((input.dragModifiers() & MouseEvent.BUTTON2_MASK) > 0) {\r
121             // System.out.println("zoom");\r
122             if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION) {\r
123                 camera.moveScaledToTarget((float) msTmp.y * 0.5f);\r
124             } else {\r
125                 component.setScreenScale(component.getScreenScale() - (float) msTmp.y * 0.5f * component.getScreenScale());\r
126             }\r
127         }\r
128 //        } else {\r
129 //            System.out.println("cameraAction!");\r
130 //            System.out.println(input);\r
131 //        } \r
132        \r
133     }\r
134 }\r