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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.actions;
\r
13 import java.awt.event.KeyEvent;
\r
14 import java.awt.event.MouseEvent;
\r
15 import java.util.List;
\r
17 import javax.vecmath.Vector3d;
\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
28 public class CameraAction extends InteractiveAction{
\r
30 private JmeRenderingComponent component;
\r
31 private OrbitalCamera camera;
\r
33 public CameraAction(ThreeDimensionalEditorBase parent) {
\r
35 component = parent.getRenderingComponent();
\r
36 camera = parent.getCamera();
\r
40 public void activate() {
\r
45 public void deactivate() {
\r
50 public boolean usable(Graph graph,List<Resource> resources) {
\r
54 Vector3d prevIntersectPoint = new Vector3d();
\r
56 public void update() {
\r
58 if (input.keyDown(KeyEvent.VK_CONTROL))
\r
60 if (input.keyPressed(KeyEvent.VK_NUMPAD5)) {
\r
61 if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION)
\r
62 component.setProjectionPolicy(JmeRenderingComponent.PARALLEL_PROJECTION);
\r
64 component.setProjectionPolicy(JmeRenderingComponent.PERSPECTIVE_PROJECTION);
\r
65 parent.setViewChanged(true);
\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
71 if (input.keyPressed(KeyEvent.VK_NUMPAD1)) {
\r
72 camera.setCameraPosRelativeToTarget(new Vector3d(0, 0, camera.getDistanceToTarget() * scale));
\r
73 parent.setViewChanged(true);
\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
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
87 MathTools.intersectStraightPlane(o, d, point, normal, prevIntersectPoint);
\r
89 if (!input.mouseDragged())
\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
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
110 camera.translate(delta);
\r
111 prevIntersectPoint.add(delta);
\r
113 camera.moveRight((float) msTmp.x);
\r
114 camera.moveUp(-(float) msTmp.y);
\r
117 camera.rotateRight((float) msTmp.x);
\r
118 camera.rotateUp((float) msTmp.y);
\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
125 component.setScreenScale(component.getScreenScale() - (float) msTmp.y * 0.5f * component.getScreenScale());
\r
129 // System.out.println("cameraAction!");
\r
130 // System.out.println(input);
\r