X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Factions%2FCameraAction.java;fp=org.simantics.g3d%2Fsrc%2Forg%2Fsimantics%2Fproconf%2Fg3d%2Factions%2FCameraAction.java;h=71d15c4fa2605bf16998cffabcd5dd3165d7d1cd;hb=10f144a2bb2d7bec98b812b83acecb333fd098ea;hp=0000000000000000000000000000000000000000;hpb=3055b543aa5afc0cca4bb3b341704e7c5103fa6a;p=simantics%2F3d.git 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 index 00000000..71d15c4f --- /dev/null +++ b/org.simantics.g3d/src/org/simantics/proconf/g3d/actions/CameraAction.java @@ -0,0 +1,134 @@ +/******************************************************************************* + * Copyright (c) 2007- VTT Technical Research Centre of Finland. + * 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.proconf.g3d.actions; + +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.util.List; + +import javax.vecmath.Vector3d; + +import org.simantics.db.Graph; +import org.simantics.db.Resource; +import org.simantics.proconf.g3d.base.JmeRenderingComponent; +import org.simantics.proconf.g3d.base.MathTools; +import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase; +import org.simantics.proconf.g3d.common.OrbitalCamera; + + + +public class CameraAction extends InteractiveAction{ + + private JmeRenderingComponent component; + private OrbitalCamera camera; + + public CameraAction(ThreeDimensionalEditorBase parent) { + super(parent); + component = parent.getRenderingComponent(); + camera = parent.getCamera(); + } + + @Override + public void activate() { + + } + + @Override + public void deactivate() { + + } + + @Override + public boolean usable(Graph graph,List resources) { + return true; + } + + Vector3d prevIntersectPoint = new Vector3d(); + @Override + public void update() { + double scale = 1.0; + if (input.keyDown(KeyEvent.VK_CONTROL)) + scale = -1.0; + if (input.keyPressed(KeyEvent.VK_NUMPAD5)) { + if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION) + component.setProjectionPolicy(JmeRenderingComponent.PARALLEL_PROJECTION); + else + component.setProjectionPolicy(JmeRenderingComponent.PERSPECTIVE_PROJECTION); + parent.setViewChanged(true); + } + if (input.keyPressed(KeyEvent.VK_NUMPAD7)) { + camera.setCameraPosRelativeToTarget(new Vector3d(camera.getDistanceToTarget() * scale, 0, 0)); + parent.setViewChanged(true); + } + if (input.keyPressed(KeyEvent.VK_NUMPAD1)) { + camera.setCameraPosRelativeToTarget(new Vector3d(0, 0, camera.getDistanceToTarget() * scale)); + parent.setViewChanged(true); + } + if (input.keyPressed(KeyEvent.VK_NUMPAD9)) { + camera.setCameraPosRelativeToTarget(new Vector3d(0, camera.getDistanceToTarget() * scale, 0)); + parent.setViewChanged(true); + } + + if (input.mousePressed() && ((input.pressModifiers() & MouseEvent.BUTTON1_MASK) > 0)) { + Vector3d o = new Vector3d(); + Vector3d d = new Vector3d(); + parent.createPickRay(o, d); + Vector3d point = new Vector3d(camera.getTarget()); + Vector3d normal = camera.getUnNormalizedHeading(); + normal.normalize(); + MathTools.intersectStraightPlane(o, d, point, normal, prevIntersectPoint); + } + if (!input.mouseDragged()) + return; + parent.setViewChanged(true); + Vector3d msTmp = new Vector3d(); + msTmp.x = (input.prevMouseX() - input.mouseX()) / 100f; + msTmp.y = (input.prevMouseY() - input.mouseY()) / 100f; + + if ((input.dragModifiers() & MouseEvent.BUTTON1_MASK) > 0) { + if ((input.dragModifiers() & MouseEvent.CTRL_MASK) > 0) { + Vector3d o = new Vector3d(); + Vector3d d = new Vector3d(); + parent.createPickRay(o, d); + Vector3d point = new Vector3d(camera.getTarget()); + Vector3d normal = camera.getUnNormalizedHeading(); + normal.normalize(); + Vector3d intersectPoint = new Vector3d(); + if (MathTools.intersectStraightPlane(o, d, point, normal, intersectPoint)) { + Vector3d delta = new Vector3d(intersectPoint); + delta.sub(prevIntersectPoint); + prevIntersectPoint = intersectPoint; + delta.negate(); + camera.translate(delta); + prevIntersectPoint.add(delta); + } else { + camera.moveRight((float) msTmp.x); + camera.moveUp(-(float) msTmp.y); + } + } else { + camera.rotateRight((float) msTmp.x); + camera.rotateUp((float) msTmp.y); + } + } else if ((input.dragModifiers() & MouseEvent.BUTTON2_MASK) > 0) { + // System.out.println("zoom"); + if (component.getProjectionPolicy() == JmeRenderingComponent.PERSPECTIVE_PROJECTION) { + camera.moveScaledToTarget((float) msTmp.y * 0.5f); + } else { + component.setScreenScale(component.getScreenScale() - (float) msTmp.y * 0.5f * component.getScreenScale()); + } + } +// } else { +// System.out.println("cameraAction!"); +// System.out.println(input); +// } + + } +}