View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class MouseTranslate
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: MouseTranslate.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.mouse;
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 java.awt.*;
27  import java.awt.event.*;
28  import java.util.*;
29  import javax.media.j3d.*;
30  import javax.vecmath.*;
31  
32  /*====================================================================
33                  Implementation of class MouseTranslate                
34  ====================================================================*/
35  /***
36   * MouseTranslate is a behavior object which translates a 3D object
37   * according to a mouse event.
38   * 
39   * @version $Revision: 1.3 $
40   * @author Masahiro Takatsuka (masa@jbeans.net)
41   * @see com.sun.j3d.utils.behaviors.mouse.MouseTranslate
42   */
43  
44  public class MouseTranslate extends com.sun.j3d.utils.behaviors.mouse.MouseTranslate {
45      private double x_factor;
46      private double y_factor;
47      private Vector3d translation;
48  	private boolean useL2U;
49  	private Transform3D l2u;
50  	private Matrix3d m3d;
51  	private boolean invertMouseInput;
52  	private com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback callback;
53  	
54      public MouseTranslate() {
55  		super(0);
56          this.x_factor = 0.02D;
57          this.y_factor = 0.02D;
58          this.translation = new Vector3d();
59          this.callback = null;
60  		this.useL2U = false;
61  		this.l2u = null;
62  		this.invertMouseInput = false;		
63      }
64  
65      public MouseTranslate(int i) {
66          super(i);
67          this.x_factor = 0.02D;
68          this.y_factor = 0.02D;
69          this.translation = new Vector3d();
70          this.callback = null;
71  		this.useL2U = false;
72  		this.l2u = null;
73  		this.invertMouseInput = false;		
74      }
75  
76      public MouseTranslate(TransformGroup transformgroup) {
77          super(transformgroup);
78          this.x_factor = 0.02D;
79          this.y_factor = 0.02D;
80          this.translation = new Vector3d();
81          this.callback = null;
82  		this.useL2U = false;
83  		this.l2u = null;
84  		this.invertMouseInput = false;
85      }
86  
87  	public void invertMouseInput(boolean invert) {
88  		this.invertMouseInput = invert;
89  	}
90  
91  	public boolean doesInvertMouseInput() {
92  		return this.invertMouseInput;
93  	}
94  
95  	public void setTransformGroup(TransformGroup transformgroup) {
96  		super.setTransformGroup(transformgroup);
97  		this.useL2U = false;
98  		this.l2u = null;
99  	}
100 	
101 	public void useLocalToUniverseTransform(boolean use) {
102 		this.useL2U = use;
103 	}
104 
105 	public boolean isLocalToUniverseTransformUsed() {
106 		return this.useL2U;
107 	}
108 
109     public double getXFactor() {
110         return this.x_factor;
111     }
112 
113     public double getYFactor() {
114         return this.y_factor;
115     }
116 
117     public void initialize() {
118         super.initialize();
119         if ((flags & 0x2) == com.sun.j3d.utils.behaviors.mouse.MouseBehavior.INVERT_INPUT) {
120             invert = true;
121             this.x_factor *= -1D;
122             this.y_factor *= -1D;
123         }
124     }
125 
126     public void processStimulus(Enumeration enumeration) {
127 		if (this.useL2U) {
128 			if (this.l2u == null) {
129 				this.l2u = new Transform3D();
130 			}
131 			if (this.m3d == null) {
132 				this.m3d = new Matrix3d();
133 			}
134 			try {
135 				transformGroup.getLocalToVworld(this.l2u);
136 				this.l2u.get(this.m3d);		// rotation matrix
137 				this.l2u.set(this.m3d);		// just rotation
138 				this.l2u.invert();
139 			} catch (CapabilityNotSetException e) {
140 				this.useL2U = false;
141 			}
142 		}
143 		
144         while (enumeration.hasMoreElements()) {
145             WakeupCriterion wakeupcriterion = (WakeupCriterion)enumeration.nextElement();
146             if (wakeupcriterion instanceof WakeupOnAWTEvent) {
147                 AWTEvent aawtevent[] = ((WakeupOnAWTEvent)wakeupcriterion).getAWTEvent();
148                 for (int l = 0; l < aawtevent.length; l++) {
149                     processMouseEvent((MouseEvent)aawtevent[l]);
150                     if (buttonPress && (flags & 0x1) == 0 || wakeUp && (flags & 0x1) != 0) {
151                         int i = aawtevent[l].getID();
152                         if (i == MouseEvent.MOUSE_DRAGGED && !((MouseEvent)aawtevent[l]).isAltDown() && ((MouseEvent)aawtevent[l]).isMetaDown()) {
153                             x = ((MouseEvent)aawtevent[l]).getX();
154                             y = ((MouseEvent)aawtevent[l]).getY();
155                             int j = x - x_last;
156                             int k = y - y_last;
157 							if (this.invertMouseInput) {
158 								j = -j;
159 								k = -k;
160 							}
161                             if (!reset && Math.abs(k) < 50 && Math.abs(j) < 50) {
162                                 transformGroup.getTransform(currXform);
163                                 this.translation.x = (double)j * this.x_factor;
164                                 this.translation.y = (double)(-k) * this.y_factor;
165 								this.translation.z = 0;
166 								if (this.useL2U) {
167 									this.l2u.transform(this.translation);
168 								}
169                                 transformX.set(this.translation);
170                                 if (invert) {
171                                     currXform.mul(currXform, transformX);
172 								} else {
173                                     currXform.mul(transformX, currXform);
174 								}
175                                 transformGroup.setTransform(currXform);
176                                 transformChanged(currXform);
177                                 if (this.callback != null) {
178                                     this.callback.transformChanged(1, currXform);
179 								}
180                             } else {
181                                 reset = false;
182                             }
183                             x_last = x;
184                             y_last = y;
185                         } else if (i == MouseEvent.MOUSE_PRESSED) {
186                             x_last = ((MouseEvent)aawtevent[l]).getX();
187                             y_last = ((MouseEvent)aawtevent[l]).getY();
188                         }
189                     }
190                 }
191             }
192         }
193 
194         wakeupOn(mouseCriterion);
195     }
196 
197     public void setFactor(double d) {
198         this.x_factor = this.y_factor = d;
199     }
200 
201     public void setFactor(double d, double d1) {
202         this.x_factor = d;
203         this.y_factor = d1;
204     }
205 
206     public void setupCallback(com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback mousebehaviorcallback) {
207         this.callback = mousebehaviorcallback;
208     }
209 
210     public void transformChanged(Transform3D transform3d) {
211     }
212 }