public class ButtonRotateBehaviour extends com.sun.j3d.utils.behaviors.vp.ViewPlatformBehavior implements MouseListener {
public static final int CLOCKWISE=0;
public static final int ANTICLOCKWISE=1;
private int Direction=0;
private boolean rotation;
private boolean negative;
private WakeupCriterion[] criterion;
private WakeupCriterion runCriterion;
private WakeupCriterion tempSter;
private WakeupOr criterionChoice;
private static final double twoPi = (Math.PI*2);
private double distanceFromCentre = 20.0;
private double rotationAngle = 0.0;
private double latitude = 0.0;
private double xtrans = 0.0;
private double ytrans = 0.0;
private double rotYFactor = 10.0;
private static final double NOMINAL_ROT_FACTOR = .01;
private double rotYMul = NOMINAL_ROT_FACTOR * rotYFactor;
private Vector3d centreToView = new Vector3d();
private Vector3d currentPos = new Vector3d();
private Vector3d transVector = new Vector3d();
private Vector3d distanceVector = new Vector3d();
private Point3d rotationCentre = new Point3d();
private Transform3D targetTransform= new Transform3D();
private Matrix3d rotMatrix = new Matrix3d();
/** Creates a new instance of ButtonRotateBehaviour
*@param newDir The direction of rotation, one of CLOCKWISE or ANTICLOCKWISE
*/
public ButtonRotateBehaviour(int newDir)
{
Direction = newDir;
}
/** The initialisation method. */
public void initialize() {
System.err.println("Initializing the behaviour");
criterion = new WakeupCriterion[2];
criterion[0] = new WakeupOnBehaviorPost(this, MouseEvent.MOUSE_PRESSED);
criterion[1] = new WakeupOnBehaviorPost(this, MouseEvent.MOUSE_RELEASED);
criterionChoice = new WakeupOr(criterion);
runCriterion = new WakeupOnElapsedFrames(1);
wakeupOn( criterionChoice);
System.err.println("Initialized!");
}
/** Process the stimiulus we have just recieved.
*@param enumeration The enumeration of wakeUpCriteria that have been recieved.
*/
public void processStimulus(java.util.Enumeration criteria)
{
System.err.println(“Processing the stimulus!”);
while (criteria.hasMoreElements())
{
tempSter=(WakeupCriterion) criteria.nextElement();
if (tempSter instanceof WakeupOnBehaviorPost)
{
int pid = ( (WakeupOnBehaviorPost) tempSter).getPostId();
if (pid == MouseEvent.MOUSE_PRESSED) rotation=true;
else if (pid == MouseEvent.MOUSE_RELEASED) rotation=false;
}
}
System.err.println(“Rotation = “+rotation);
if (rotation)
{
System.err.println(” 1. rotationAngle=”+rotationAngle);
double ychange=.8;
//double xchange=.8;
if (Direction==0)
{
rotationAngle -= ychange * rotYMul; //xchange * rotXMul;
}
else
{
rotationAngle += ychange * rotYMul;//xchange * rotXMul;
}
System.err.println("2. rotationAngle="+rotationAngle);
integrateTransforms();
wakeupOn(runCriterion);
}
else wakeupOn(criterionChoice);
}
/**
* Sets the ViewingPlatform for this behavior. This method is
* called by the ViewingPlatform.
* If a sub-calls overrides this method, it must call
* super.setViewingPlatform(vp).
* NOTE: Applications should <i>not</i> call this method.
*/
public void setViewingPlatform(ViewingPlatform vp) {
super.setViewingPlatform( vp );
if (vp!=null) {
resetViewPosition();
System.err.println("Setting the viewplatform - not null!");
integrateTransforms();
System.err.println("Setting the viewplatform home to ("+rotationCentre.x+", "+rotationCentre.y+", "+rotationCentre.z+")");
}
else System.err.println("Null viewPlatform problem!");
}
/**
* Reset the orientation and distance of this behavior to the current
* values in the ViewPlatform Transform Group
*/
private void resetViewPosition() {
System.err.println(“Resetting view position”);
targetTG.getTransform( targetTransform );
targetTransform.get( rotMatrix, transVector );
System.err.println("transVector = “+transVector.x+”, “+transVector.y+”, "+transVector.z);
System.err.println(“RotationCentre = “+rotationCentre.x+”, “+rotationCentre.y+”, “+rotationCentre.z+”)”);
centreToView.sub( transVector, rotationCentre );
distanceFromCentre = transVector.length();
System.err.println("centerToView = “+centreToView.x+”, “+centreToView.y+”, "+centreToView.z);
System.err.println("Distance from centre= "+distanceFromCentre);
// targetTransform.get( rotMatrix );
// rotateTransform.set( rotMatrix );
}
/** Sets the centre of rotation for the behaviour.
*@param newCentre The new centre of rotation.
*/
public void setCentre( Point3d newCentre)
{
rotationCentre=newCentre;
}
protected void integrateTransforms()
{
targetTG.getTransform( targetTransform );
targetTransform.get( currentPos );
System.err.println("Starting position = "+currentPos.x+", "+currentPos.y+", "+currentPos.z);
double nowX = currentPos.x;
double nowY = currentPos.z;
System.err.println("RotationCentre = "+rotationCentre.x+", "+rotationCentre.y+", "+rotationCentre.z);
nowX = nowX-rotationCentre.x;
nowY = nowY-rotationCentre.z;
//System.err.println("Position after centre translation = “+nowX+”, “+currentPos.y+”, "+nowY);
double lenVec = distanceFromCentre;//Math.sqrt((Math.pow(nowX, 2d) + Math.pow(nowY, 2d)));
nowY = (Math.sin(rotationAngle)*lenVec);
nowX = -Math.cos(rotationAngle)*lenVec;
//System.err.println(“rotationAngle = “+rotationAngle+” length of vector=”+lenVec);
nowX = nowX+ rotationCentre.x;
nowY = nowY+ rotationCentre.z;
currentPos.x = nowX;
currentPos.z = nowY;
//System.err.println("Position after trig translation = “+nowX+”, “+currentPos.y+”, "+nowY);
targetTransform.set(currentPos);
targetTransform.lookAt(new Point3d(currentPos.x, currentPos.y, currentPos.z), rotationCentre, new Vector3d(0, 1, 0) );
targetTransform.invert();
//System.err.println("Final position = “+currentPos.x+”, “+currentPos.y+”, "+currentPos.z);
targetTG.setTransform(targetTransform);
}
/** Implements the mouseClicked action of the MouseListener interface. Not used.
*@param e Not used.
*/
public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
}
/** Implements the mouseEntered action of the MouseListener interface. Not used.
*@param e Not used.
*/
public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
if(!HouseBuilderFrameMach2.modalMode){
if (mouseEvent.getSource() instanceof RotateCtrl)
{
RotateCtrl check = (RotateCtrl) mouseEvent.getSource();
check.setMouseOver(true);
}
}
}
/** Implements the mouseExited action of the MouseListener interface. Not used.
*@param e Not used.
*/
public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
if (mouseEvent.getSource() instanceof RotateCtrl)
{
RotateCtrl check = (RotateCtrl) mouseEvent.getSource();
check.setMouseOver(false);
}
}
/** Checks for a mouse press. Expects the source to be a JButton with an ActionCommand of
*either "LEFT" or "RIGHT" depending on which direction the camera is to rotate.
*@param mouseEvent The event.
*/
public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
if(!HouseBuilderFrameMach2.modalMode){
System.err.println("Mouse Pressed!");
if (mouseEvent.getSource() instanceof JButton)
{
JButton check = (JButton) mouseEvent.getSource();
String command = check.getActionCommand();
if (command.equals("LEFT")) Direction=0;
else if (command.equals("RIGHT")) Direction=1;
//check.setMousePress(true);
}
postId(MouseEvent.MOUSE_PRESSED);
}
}
/** Checks for a mouse release and stops rotation when it occurs.
*/
public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
System.err.println("Mouse Released!");
if (mouseEvent.getSource() instanceof JButton)
{
JButton check = (JButton) mouseEvent.getSource();
//check.setMousePress(false);
}
postId(MouseEvent.MOUSE_RELEASED);
rotation=false;
}
}