001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/visualtools/ViewPlatformMotionMonitorVisualTool.java,v 1.1 2005/04/20 22:21:31 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 the Java 3D(tm) Scene Graph Editor.
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.j3dedit.scenegrapheditor.visualtools;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import javax.media.j3d.TransformGroup;
023: import javax.media.j3d.Transform3D;
024: import javax.media.j3d.WakeupOnTransformChange;
025: import javax.media.j3d.Behavior;
026: import javax.media.j3d.BoundingSphere;
027: import javax.vecmath.Vector3d;
028: import javax.vecmath.Point3d;
029: import javax.vecmath.Matrix3d;
030:
031: import org.jdesktop.j3dfly.utils.vpbehaviors.ViewUtils;
032:
033: /**
034: * Monitor motion of the view platform and notify all listeners when
035: * is moves.
036: *
037: * @author paulby
038: * @version
039: */
040: public class ViewPlatformMotionMonitorVisualTool extends VisualTool {
041:
042: private ArrayList listeners = null;
043: private TransformMonitor monitor = null;
044:
045: /** Creates new ViewPlatformMotionMonitorVisualTool */
046: public ViewPlatformMotionMonitorVisualTool(
047: TransformGroup viewPlatformTG) {
048: monitor = new TransformMonitor(viewPlatformTG);
049: monitor.setEnable(false);
050: this .addChild(monitor);
051: }
052:
053: public void addViewPlatformMotionListener(
054: ViewPlatformMotionListener listener) {
055: if (listeners == null) {
056: listeners = new ArrayList();
057: listeners.add(listener);
058: monitor.setEnable(true);
059: }
060: }
061:
062: void notifyListeners(double x, double y, double z, double roll,
063: double pitch, double yaw) {
064: Iterator it = listeners.iterator();
065: while (it.hasNext())
066: ((ViewPlatformMotionListener) it.next()).viewPlatformMoved(
067: x, y, z, roll, pitch, yaw);
068: }
069:
070: /**
071: * Reset the Tool, called when user starts editing a new SceneGraph
072: */
073: public void reset() {
074: }
075:
076: /**
077: * Listener interface for ViewPlatform motion.
078: */
079: public interface ViewPlatformMotionListener {
080:
081: /**
082: * The new view platform position and orientation
083: */
084: public void viewPlatformMoved(double x, double y, double z,
085: double roll, double pitch, double yaw);
086: }
087:
088: class TransformMonitor extends Behavior {
089: private WakeupOnTransformChange criteria;
090: private TransformGroup tg;
091: private Transform3D trans = new Transform3D();
092: private Vector3d pos = new Vector3d();
093: private Matrix3d m3d = new Matrix3d();
094:
095: public TransformMonitor(TransformGroup tg) {
096: this .tg = tg;
097: criteria = new WakeupOnTransformChange(tg);
098: this .setSchedulingBounds(new BoundingSphere(new Point3d(),
099: Double.POSITIVE_INFINITY));
100: }
101:
102: public void initialize() {
103: wakeupOn(criteria);
104: }
105:
106: public void processStimulus(java.util.Enumeration e) {
107:
108: tg.getTransform(trans);
109: trans.get(m3d, pos);
110:
111: Vector3d euler = new Vector3d();
112: ViewUtils.toEuler(m3d, euler);
113:
114: //System.out.println(trans);
115:
116: notifyListeners(pos.x, pos.y, pos.z, euler.x, euler.y,
117: euler.z);
118:
119: wakeupOn(criteria);
120: }
121: }
122:
123: }
|