View Javadoc

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