001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/vpbehaviors/J3dFlyMouseBehavior.java,v 1.1 2005/04/20 21:05:13 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly.utils.vpbehaviors;
019:
020: import java.awt.Component;
021: import java.awt.Cursor;
022: import java.awt.event.MouseEvent;
023: import javax.media.j3d.Transform3D;
024: import javax.media.j3d.Canvas3D;
025: import javax.vecmath.Point3d;
026:
027: /**
028: *
029: * @author paulby
030: * @version
031: */
032: public abstract class J3dFlyMouseBehavior extends VPCollisionBehavior {
033:
034: protected java.awt.Rectangle canvasBounds;
035: protected java.awt.geom.Point2D.Float canvasCenter;
036:
037: protected Transform3D targetTransform;
038:
039: protected float MAX_ANGLE = (float) Math.toRadians(3);
040: protected float MAX_VELOCITY = 10f;
041:
042: protected boolean ignoreMouseMotion = false;
043: protected float deadXSize;
044: protected float deadYSize;
045: protected FlyControlDialog flyControlDialog = null;
046: protected boolean doingDrag = false;
047:
048: //protected Transform3D currentImagePlateToCoexistance;
049:
050: /**
051: * Flag indicating that the View has been initialized
052: */
053: //protected boolean viewInitialized = false;
054: /** Creates new J3dFlyMouseBehavior
055: * @deprecated use J3dFlyMouseBehavior()
056: */
057: public J3dFlyMouseBehavior(Canvas3D c) {
058: this ();
059: }
060:
061: public J3dFlyMouseBehavior() {
062: super ();
063: canvasCenter = new java.awt.geom.Point2D.Float();
064: targetTransform = new Transform3D();
065: }
066:
067: /*
068: public void processStimulus( java.util.Enumeration enum ) {
069: if (!viewInitialized) {
070: canvasBounds = getCoexistanceBounds( vp.getViewers()[0] );
071: viewInitialized = true;
072: canvasCenter.x = canvasBounds.width/2;
073: canvasCenter.y = canvasBounds.height/2;
074: deadXSize = 15.0 / canvasCenter.x;
075: deadYSize = 15.0 / canvasCenter.y;
076: }
077: super.processStimulus( enum );
078: }
079: */
080:
081: protected void queueAWTEvent(java.awt.AWTEvent evt) {
082: super .queueAWTEvent(evt);
083: }
084:
085: /**
086: * Process any events the subclass did not handle
087: */
088: protected void processUnusedEvent(java.awt.AWTEvent evt) {
089: if (evt instanceof MouseEvent) {
090: if (((MouseEvent) evt).getID() == MouseEvent.MOUSE_ENTERED)
091: processMouseEntered((MouseEvent) evt);
092: else if (((MouseEvent) evt).getID() == MouseEvent.MOUSE_EXITED)
093: processMouseExited((MouseEvent) evt);
094: }
095: }
096:
097: protected void processMouseEntered(java.awt.event.MouseEvent evt) {
098: Component component = evt.getComponent();
099: if (component instanceof javax.media.j3d.Canvas3D) {
100: canvasBounds = component.getBounds();
101: canvasCenter.x = canvasBounds.width / 2;
102: canvasCenter.y = canvasBounds.height / 2;
103: deadXSize = 15f / canvasCenter.x;
104: deadYSize = 15f / canvasCenter.y;
105: if (!ignoreMouseMotion) {
106: component.setCursor(Cursor
107: .getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
108: }
109: }
110: /*
111: System.out.println(evt);
112: Component component = evt.getComponent();
113: if (component instanceof javax.media.j3d.Canvas3D) {
114: currentImagePlateToCoexistance = getImagePlateToCoexistance( (Canvas3D)component, vp.getViewers()[0] );
115: if (!ignoreMouseMotion) {
116: component.setCursor( Cursor.getPredefinedCursor( Cursor.CROSSHAIR_CURSOR ));
117: }
118:
119: }
120: */
121: }
122:
123: public void processMouseExited(java.awt.event.MouseEvent evt) {
124: Component component = evt.getComponent();
125: if (component instanceof javax.media.j3d.Canvas3D) {
126: component.setCursor(Cursor.getDefaultCursor());
127: }
128: }
129:
130: /**
131: * Compute the Bounds of Coexistance for all canvases in the view
132: */
133: /*
134: private java.awt.geom.Rectangle2D.Double getCoexistanceBounds( com.sun.j3d.utils.universe.Viewer viewer ) {
135: java.awt.geom.Rectangle2D.Double rect =
136: new java.awt.geom.Rectangle2D.Double( Double.POSITIVE_INFINITY,
137: Double.POSITIVE_INFINITY,
138: Double.NEGATIVE_INFINITY,
139: Double.NEGATIVE_INFINITY );
140: javax.vecmath.Point3d p3d = new javax.vecmath.Point3d();
141:
142: Canvas3D[] canvases = viewer.getCanvas3Ds();
143:
144: for(int i=0; i<canvases.length; i++) {
145: System.out.println( canvases[i] );
146: Transform3D trans = getImagePlateToCoexistance( canvases[i], viewer );
147:
148: canvases[i].getPixelLocationInImagePlate( 0, 0, p3d );
149: trans.transform( p3d );
150: rect.x = Math.min( rect.x, p3d.x );
151: rect.y = Math.min( rect.y, p3d.y );
152:
153: canvases[i].getPixelLocationInImagePlate( canvases[i].getWidth(),
154: canvases[i].getHeight(), p3d );
155:
156:
157: trans.transform( p3d );
158: rect.width = Math.max( rect.width, p3d.x );
159: rect.height = Math.max( rect.height, p3d.y );
160: }
161:
162: return rect;
163: }
164:
165: private Transform3D getImagePlateToCoexistance( Canvas3D canvas, com.sun.j3d.utils.universe.Viewer viewer ) {
166: Transform3D imagePlateToTrackerBase = new Transform3D();
167: canvas.getScreen3D().getTrackerBaseToImagePlate( imagePlateToTrackerBase );
168: imagePlateToTrackerBase.invert();
169:
170: Transform3D trackerBaseToCoexistance = new Transform3D();
171:
172: viewer.getPhysicalEnvironment().getCoexistenceToTrackerBase( trackerBaseToCoexistance );
173: trackerBaseToCoexistance.invert();
174:
175:
176: imagePlateToTrackerBase.mul( trackerBaseToCoexistance );
177:
178: return imagePlateToTrackerBase;
179: }
180: */
181:
182: /*
183: protected void getPixelInCoexistance( Canvas3D canvas, int pixelX, int pixelY, Point3d result ) {
184: canvas.getPixelLocationInImagePlate( pixelX, pixelY, result );
185: currentImagePlateToCoexistance.transform( result );
186: }
187: */
188:
189: /**
190: * Called when the user wants to change the parameters of the
191: * behavior.
192: *
193: * This method may be called from the AWT event thread
194: */
195: public void editParameters(java.awt.Frame parent) {
196: if (flyControlDialog == null) {
197: flyControlDialog = new FlyControlDialog(parent, true);
198: }
199:
200: flyControlDialog.setStep(MAX_VELOCITY);
201: flyControlDialog.setAngle((float) Math.toDegrees(MAX_ANGLE));
202: flyControlDialog.setVisible(true);
203:
204: if (!flyControlDialog.userCancelled()) {
205: MAX_ANGLE = (float) Math.toRadians(flyControlDialog
206: .getAngle());
207: MAX_VELOCITY = flyControlDialog.getStep();
208: }
209: }
210:
211: /**
212: * Set the maximum angle moved per frame
213: *
214: * @param angle The angle in radians
215: */
216: public void setAngleStep(float angle) {
217: MAX_ANGLE = angle;
218: }
219:
220: /**
221: * Set the largest forward step per frame
222: */
223: public void setForwardStep(float forward) {
224: MAX_VELOCITY = forward;
225: }
226: }
|