2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
6 package edu
.mit
.ezyang
.gravity
.j3d
;
8 import com
.sun
.j3d
.utils
.behaviors
.mouse
.MouseTranslate
;
10 import java
.awt
.event
.*;
12 import javax
.media
.j3d
.*;
13 import javax
.vecmath
.*;
16 * Modified MouseTranslate class which accounts for a rotation in a parent
17 * TransformGroup when calculating translation based off of screen change.
18 * @warning Only the flags constructor was reimplemented; manually call
20 * @author Edward Z. Yang <ezyang@mit.edu>
22 public class SmartMouseTranslate
extends MouseTranslate
implements SmartMouseBehavior
{
23 protected TransformGroup rotateTG
;
25 public SmartMouseTranslate(int flags
) {
30 * Sets the TransformGroup used to determine the rotation that will
31 * be adjusted against.
32 * @param group TransformGroup with Transform3D rotation to adjust for
34 public void setRotateTransformGroup(TransformGroup group
) {
40 * @note Overridden in order to force MouseTranslate to call our overloaded
41 * doProcess() function.
45 public void processStimulus (Enumeration criteria
) {
46 WakeupCriterion wakeup
;
50 while (criteria
.hasMoreElements()) {
51 wakeup
= (WakeupCriterion
) criteria
.nextElement();
53 if (wakeup
instanceof WakeupOnAWTEvent
) {
54 events
= ((WakeupOnAWTEvent
)wakeup
).getAWTEvent();
55 if (events
.length
> 0) {
56 evt
= (MouseEvent
) events
[events
.length
-1];
61 else if (wakeup
instanceof WakeupOnBehaviorPost
) {
63 // access to the queue must be synchronized
64 synchronized (mouseq
) {
65 if (mouseq
.isEmpty()) break;
66 evt
= (MouseEvent
)mouseq
.remove(0);
67 // consolodate MOUSE_DRAG events
68 while ((evt
.getID() == MouseEvent
.MOUSE_DRAGGED
) &&
70 (((MouseEvent
)mouseq
.get(0)).getID() ==
71 MouseEvent
.MOUSE_DRAGGED
)) {
72 evt
= (MouseEvent
)mouseq
.remove(0);
80 wakeupOn(mouseCriterion
);
84 * Modified doProcess() function with transform from rotateTG
86 private void doProcess(MouseEvent evt
) {
90 processMouseEvent(evt
);
92 if (((buttonPress
)&&((flags
& MANUAL_WAKEUP
) == 0)) ||
93 ((wakeUp
)&&((flags
& MANUAL_WAKEUP
) != 0))){
95 if ((id
== MouseEvent
.MOUSE_DRAGGED
) &&
96 !evt
.isAltDown() && evt
.isMetaDown()) {
104 if ((!reset
) && ((Math
.abs(dy
) < 50) && (Math
.abs(dx
) < 50))) {
105 //System.out.println("dx " + dx + " dy " + dy);
106 transformGroup
.getTransform(currXform
);
108 Vector3d trans
= new Vector3d();
109 trans
.x
= dx
*this.getXFactor();
110 trans
.y
= -dy
*this.getYFactor();
112 // The magic happens here:
113 Transform3D transform
= new Transform3D();
114 rotateTG
.getTransform(transform
);
116 transform
.transform(trans
);
119 transformX
.set(trans
);
122 currXform
.mul(currXform
, transformX
);
124 currXform
.mul(transformX
, currXform
);
127 transformGroup
.setTransform(currXform
);
129 transformChanged( currXform
);
131 // callback is a private property, so we don't support it.
132 // For completeness sake, we could overload all of the
133 // relevant functions to reimplement it.
134 //if (callback!=null)
135 //callback.transformChanged( MouseBehaviorCallback.TRANSLATE,
145 else if (id
== MouseEvent
.MOUSE_PRESSED
) {