/*
* $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/vpbehaviors/HoverBehavior.java,v 1.1 2005/04/20 21:05:13 paulby Exp $
*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License Version
* 1.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is available at http://www.sun.com/
*
* The Original Code is Java 3D(tm) Fly Through.
* The Initial Developer of the Original Code is Paul Byrne.
* Portions created by Paul Byrne are Copyright (C) 2002.
* All Rights Reserved.
*
* Contributor(s): Paul Byrne.
*
**/
package org.jdesktop.j3dfly.utils.vpbehaviors;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Cursor;
import javax.swing.SwingUtilities;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.media.j3d.WakeupOr;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupCondition;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.View;
import javax.vecmath.Vector3f;
/**
<P>This behavior will allow the user to move to any point in 3 space whilst
maintaining the orientation of -ve Y is down.</P>
<P>All controls are provided by the mouse. Each mouse button
generates a different type of motion while the button is pressed, the
distance of the mouse cursor from the center of the canvas3D will
control the speed of motion. The behavior of each button is described
below with reference to the View coordinate system.</P>
<P>Left Mouse Button - Moving the mouse up and down will move the
view forward and backward. Moving the mouse left and right will yaw
the view around the Y axis.</P>
<P>Right Mouse Button - Moving the mouse up and down will move the
view up and down the Y axis. Movig the mouse left and right will
translate the view along the X axis.</P>
*
* Canvas3D size changes are tracked
*
* @author Paul Byrne
* @version 1.15, 01/18/02
*/
public class HoverBehavior extends J3dFlyMouseBehavior {
private Transform3D velocityTransform;
private Transform3D transform;
private Transform3D newTargetTransform = new Transform3D();
private Vector3f velocity = new Vector3f();
private float yawAngle = 0f;
/**
* @deprecated use HoverBehavior()
*/
public HoverBehavior( View view ) {
super();
}
public HoverBehavior() {
super();
}
public void initialize() {
transform = new Transform3D();
targetTransform = new Transform3D();
velocityTransform = new Transform3D();
super.initialize();
}
protected void processAWTEvents( final java.awt.AWTEvent[] events ) {
for(int i=0; i<events.length; i++)
if (events[i] instanceof MouseEvent) {
if ( ((MouseEvent)events[i]).getID()==MouseEvent.MOUSE_RELEASED)
processMouseReleased( (MouseEvent)events[i] );
else if ( ((MouseEvent)events[i]).getID()==MouseEvent.MOUSE_DRAGGED)
processMouseDragged( (MouseEvent)events[i] );
else
processUnusedEvent( events[i] );
} else
processUnusedEvent( events[i] );
}
protected void processMouseReleased(final MouseEvent evt) {
if (SwingUtilities.isLeftMouseButton( evt ) ) {
yawAngle = 0;
velocity.z = 0;
motion = false;
} else if ( SwingUtilities.isRightMouseButton( evt )) {
velocity.x = 0;
velocity.y = 0;
motion = false;
} else if ( SwingUtilities.isMiddleMouseButton( evt )) {
motion = false;
} else {
yawAngle = 0f;
velocity.x = 0;
velocity.z = 0;
velocity.y = 0;
motion = false;
}
}
protected void processMouseDragged(final MouseEvent evt) {
float offsetX = (evt.getX() - canvasCenter.x) / canvasCenter.x;
float offsetY = (evt.getY() - canvasCenter.y) / canvasCenter.y;
float factorX=0f;
float factorY=0f;
motion = false;
if (Math.abs(offsetX)-deadXSize > 0f) {
factorX = (float)Math.pow( Math.abs(offsetX)-deadXSize, 2f );
motion = true;
} else {
factorX = 0f;
}
if (Math.abs(offsetY)-deadYSize > 0f) {
factorY = (float)Math.pow( Math.abs(offsetY)-deadYSize, 2f );
motion = true;
} else {
factorY = 0f;
}
if (offsetX>0f)
factorX = -factorX;
if (offsetY<0f)
factorY = -factorY;
if (SwingUtilities.isLeftMouseButton( evt ) ) {
//System.out.println( factorX +" "+factorY );
yawAngle = MAX_ANGLE * factorX;
velocity.z = MAX_VELOCITY * factorY;
} else if ( SwingUtilities.isRightMouseButton( evt )) {
velocity.x = MAX_VELOCITY * factorX;
velocity.y = MAX_VELOCITY * factorY;
} else {
yawAngle = 0f;
velocity.x = 0;
velocity.z = 0;
velocity.y = 0;
motion = false;
}
}
protected void integrateTransforms() {
if (!collisionEnabled()) {
transform.rotY( yawAngle );
velocityTransform.set( velocity );
velocityTransform.mul(transform);
targetTG.getTransform( targetTransform );
targetTransform.mul( transform );
targetTransform.mul( velocityTransform );
targetTG.setTransform( targetTransform );
} else {
targetTG.getTransform( targetTransform );
SweptVolumeCollision collision = collisionControl.getCollisions( targetTransform, newTargetTransform, velocity, 0f, 0f, yawAngle );
targetTG.setTransform( newTargetTransform );
}
}
}
|