View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class SeekBehavior
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:07 $
11   * 
12   * $Id: SeekBehavior.java,v 1.3 2004/03/03 11:53:07 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
23  package net.jbeans.j3d.util.behavior.seek;
24  
25  /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26  import javax.media.j3d.*;
27  import javax.swing.event.*;
28  import javax.vecmath.*;
29  
30  import com.sun.j3d.utils.picking.*;
31  import com.sun.j3d.utils.picking.behaviors.*;
32  
33  import net.jbeans.j3d.universe.*;
34  
35  /*====================================================================
36                   Implementation of class SeekBehavior                 
37  ====================================================================*/
38  /***
39   * generally describe SeekBehavior in here
40   * 
41   * @version $Revision: 1.3 $
42   * @author Masahiro Takatsuka (masa@jbeans.net)
43   * @see PickMouseBehavior
44   */
45  
46  public class SeekBehavior extends PickMouseBehavior {
47      private transient EventListenerList listenerList;
48  	private ViewingPlatform viewingplatform = null;
49      private TransformGroup transformGroup = null;
50  	private Transform3D transformS;
51  	
52  	/***
53  	* constructs a new PickMouseBehavior.
54  	*
55  	* @param root Root of your scene graph.
56  	* @param canvas Java 3D drawing canvas.
57  	* @param bounds Bounds of your scene.
58  	* Note: If pickMode is set to USE_GEOMETRY, all geometry object in
59  	* the scene graph that allows pickable must have its ALLOW_INTERSECT bit set.
60  	*/
61  	public SeekBehavior(Canvas3D canvas3d, BranchGroup root, Bounds bounds) {
62  		super(canvas3d, root, bounds);
63  		this.listenerList = new EventListenerList();
64          this.transformS = new Transform3D();
65          setSchedulingBounds(bounds);		
66  	}
67  
68  	public void setViewingPlatform(ViewingPlatform viewingplatform) {
69  		this.viewingplatform = viewingplatform;
70  	}
71  
72      public void setTransformGroup(TransformGroup transformgroup) {
73          this.transformGroup = transformgroup;
74      }
75  	
76      public void updateScene(int i, int j) {
77  		int clickCount = mevent.getClickCount();
78  		if (clickCount == 1) {	// pick has to be double-click!
79  			pickCanvas.setShapeLocation(i, j);			
80  			PickResult result = pickCanvas.pickClosest();
81  			Point3d eye = this.viewingplatform.getEyePosition();
82  			Vector3d up = this.viewingplatform.getUpPosition();			
83  			PickIntersection intersection = result.getClosestIntersection(eye);
84  			Point3d seekPoint = intersection.getPointCoordinatesVW();
85  			/*
86  			  here we can interpolate rot/trans matrix to do
87  			  smooth seek.
88  			  ...will be implemented in near future....
89  			 */
90  			if (seekPoint != null) {
91  				this.transformS.lookAt(eye, seekPoint, up);
92  				this.transformS.invert();
93  				this.transformGroup.setTransform(this.transformS);
94  				SeekEvent event = new SeekEvent(result, mevent, eye, up);
95  				fireSeekEvent(event);
96  			}
97  		}
98  	}
99  	
100     /***
101      * Adds a SeekListener to the slider.
102      *
103      * @param l the SeekListener to add
104      */
105     public void addSeekListener(SeekListener l) {
106         this.listenerList.add(SeekListener.class, l);
107     }
108 	
109     /***
110      * Removes a SeekListener from the slider.
111      *
112      * @param l the SeekListener to remove
113 	 
114      */
115     public void removeSeekListener(SeekListener l) {
116         this.listenerList.remove(SeekListener.class, l);
117     }
118 	
119     /***
120      * Send the Selection node to listeners.
121      */
122 	protected void fireSeekEvent(SeekEvent event) {
123         Object[] listeners = this.listenerList.getListenerList();
124         for (int i = listeners.length - 2; i >= 0; i -= 2) {
125             if (listeners[i] == SeekListener.class) {
126                 ((SeekListener)listeners[i+1]).seeked(event);
127             }
128         }
129     }   
130 }
131