View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class MouseScale
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: MouseScale.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  import com.sun.j3d.utils.behaviors.mouse.*;
33  
34  /*====================================================================
35                    Implementation of class MouseScale                  
36  ====================================================================*/
37  /***
38   * MouseScale is a behavior object which scales a 3D object
39   * according to a mouse event.
40   * 
41   * @version $Revision: 1.3 $
42   * @author Masahiro Takatsuka (masa@jbeans.net)
43   * @see MouseBehavior
44   */
45  
46  public class MouseScale extends MouseBehavior {
47      private double s_factor;
48  	private double scale;
49  	private boolean useL2U;
50  	private boolean invertMouseInput;
51  	private com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback callback;
52  	
53      public MouseScale() {
54          super(0);
55          this.s_factor = 0.040000000000000001D;
56  		this.scale = 1;
57          this.callback = null;
58  		this.useL2U = false;
59  		this.invertMouseInput = false;		
60      }
61  
62      public MouseScale(int i) {
63          super(i);
64          this.s_factor = 0.040000000000000001D;
65  		this.scale = 1;		
66          this.callback = null;
67  		this.useL2U = false;
68  		this.invertMouseInput = false;
69      }
70  
71      public MouseScale(TransformGroup transformgroup) {
72          super(transformgroup);
73          this.s_factor = 0.040000000000000001D;
74  		this.scale = 1;		
75          this.callback = null;
76  		this.useL2U = false;
77  		this.invertMouseInput = false;
78      }
79  
80  	public void invertMouseInput(boolean invert) {
81  		this.invertMouseInput = invert;
82  	}
83  
84  	public boolean doesInvertMouseInput() {
85  		return this.invertMouseInput;
86  	}
87  
88  	public void setTransformGroup(TransformGroup transformgroup) {
89  		super.setTransformGroup(transformgroup);
90  		this.useL2U = false;
91  	}
92  	
93  	public void useLocalToUniverseTransform(boolean use) {
94  		this.useL2U = false;
95  	}
96  
97  	public boolean isLocalToUniverseTransformUsed() {
98  		return this.useL2U;
99  	}
100 
101     public void setScale(double d) {
102         this.s_factor = d;
103     }
104 
105     public double getScale() {
106         return this.s_factor;
107     }
108 
109     public void initialize() {
110         super.initialize();
111         if ((flags & 0x2) == MouseBehavior.INVERT_INPUT) {
112             this.s_factor *= -1D;
113             invert = true;
114         }
115     }
116 
117     public void processStimulus(Enumeration enumeration) {
118         while(enumeration.hasMoreElements()) {
119             WakeupCriterion wakeupcriterion = (WakeupCriterion)enumeration.nextElement();
120             if (wakeupcriterion instanceof WakeupOnAWTEvent) {
121                 AWTEvent aawtevent[] = ((WakeupOnAWTEvent)wakeupcriterion).getAWTEvent();
122                 for (int l = 0; l < aawtevent.length; l++) {
123 					processMouseEvent((MouseEvent)aawtevent[l]);
124 					if ((buttonPress && (flags & 0x1) == 0 || wakeUp && (flags & 0x1) != 0) &&
125 						((MouseEvent)aawtevent[l]).isShiftDown()) {
126                         int i = aawtevent[l].getID();
127                         if (i == MouseEvent.MOUSE_DRAGGED && ((MouseEvent)aawtevent[l]).isAltDown() && !((MouseEvent)aawtevent[l]).isMetaDown()) {
128                             x = ((MouseEvent)aawtevent[l]).getX();
129                             y = ((MouseEvent)aawtevent[l]).getY();
130                             int j = x - x_last;
131                             int k = y - y_last;
132 							if (this.invertMouseInput) {
133 								j = -j;
134 								k = -k;
135 							}
136                             if (!reset) {
137 								this.scale = Math.exp((double)k * this.s_factor);
138 								transformX.set(this.scale);
139                                 transformGroup.getTransform(currXform);
140                                 Matrix4d matrix4d = new Matrix4d();
141                                 currXform.get(matrix4d);
142                                 currXform.setTranslation(new Vector3d(0.0D, 0.0D, 0.0D));
143 								if (invert) {
144 									currXform.mul(currXform, transformX);
145 								} else {
146 									currXform.mul(transformX, currXform);
147 								}
148                                 Vector3d vector3d = new Vector3d(matrix4d.m03, matrix4d.m13, matrix4d.m23);
149                                 currXform.setTranslation(vector3d);
150                                 transformGroup.setTransform(currXform);
151                                 transformChanged(currXform);
152                                 if (this.callback != null) {
153                                     this.callback.transformChanged(1, currXform);
154 								}
155                             } else {
156                                 reset = false;
157                             }
158                             x_last = x;
159                             y_last = y;
160                         } else if (i == MouseEvent.MOUSE_PRESSED) {
161                             x_last = ((MouseEvent)aawtevent[l]).getX();
162                             y_last = ((MouseEvent)aawtevent[l]).getY();
163                         }
164                     }
165                 }
166             }
167         }
168 		
169         wakeupOn(mouseCriterion);
170     }
171 
172     public void setupCallback(com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback mousebehaviorcallback) {
173         this.callback = mousebehaviorcallback;
174     }
175 
176     public void transformChanged(Transform3D transform3d) {
177     }
178 }