1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.ui.behaviors;
39
40 import java.awt.AWTEvent;
41 import java.awt.event.MouseEvent;
42 import java.util.Iterator;
43 import org.jogamp.java3d.Behavior;
44 import org.jogamp.java3d.Transform3D;
45 import org.jogamp.java3d.TransformGroup;
46 import org.jogamp.java3d.WakeupCriterion;
47 import org.jogamp.java3d.WakeupOnAWTEvent;
48 import org.jogamp.vecmath.Matrix4d;
49 import org.jogamp.vecmath.Vector3d;
50
51
52
53
54
55
56
57
58 public class MouseRotate extends MouseBehavior {
59
60 private static final Vector3d zero3d = new Vector3d(0.0, 0.0, 0.0);
61 private static Transform3D VPTG_T3D = new Transform3D();
62 private static Vector3d translation = new Vector3d();
63 private static Matrix4d mat = new Matrix4d();
64 double xAngle, yAngle;
65 double xFactor = 0.001;
66 double yFactor = 0.001;
67 int doneID = 0;
68 private MouseBehaviorCallback callback = null;
69
70
71
72
73
74
75
76 public MouseRotate(int flags, TransformGroup VPTG) {
77 super(flags, VPTG);
78 }
79
80
81
82
83
84
85
86
87
88
89 public MouseRotate(int flags, TransformGroup VPTG, Behavior behavior, int postID, int dID) {
90 super(flags, VPTG, behavior, postID);
91 doneID = dID;
92 }
93
94
95
96
97
98
99 public double getXFactor() {
100 return xFactor;
101 }
102
103
104
105
106
107
108 public double getYFactor() {
109 return yFactor;
110 }
111
112
113 public void initialize() {
114 super.initialize();
115 xAngle = 0;
116 yAngle = 0;
117 if ((flags & INVERT_INPUT) == INVERT_INPUT) {
118 invert = true;
119 xFactor *= -1;
120 yFactor *= -1;
121 }
122 }
123
124
125 public void processStimulus(Iterator<WakeupCriterion> criteria) {
126 while (criteria.hasNext()) {
127 WakeupCriterion wakeup = criteria.next();
128 if (wakeup instanceof WakeupOnAWTEvent) {
129 AWTEvent[] event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
130 for (AWTEvent awtEvent : event) {
131 MouseEvent mevent = (MouseEvent) awtEvent;
132 processMouseEvent(mevent);
133 int id = awtEvent.getID();
134
135 if ((id == MouseEvent.MOUSE_DRAGGED)
136 && ((mevent.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK)
137 == MouseEvent.BUTTON1_DOWN_MASK)
138 && transformGroup != null) {
139 x = ((MouseEvent) awtEvent).getX();
140 y = ((MouseEvent) awtEvent).getY();
141 int dx = x - xLast;
142 int dy = y - yLast;
143 if (!reset) {
144 xAngle = dy * yFactor;
145 yAngle = dx * xFactor;
146 transformX.rotX(xAngle);
147 transformY.rotY(yAngle);
148 transformGroup.getTransform(currXform);
149 currXform.get(mat);
150 currXform.setTranslation(zero3d);
151 if (ViewerTG != null) {
152 ViewerTG.getTransform(VPTG_T3D);
153 VPTG_T3D.setTranslation(zero3d);
154 VPTG_T3D.invert();
155 currXform.mul(VPTG_T3D, currXform);
156 }
157 if (invert) {
158 currXform.mul(currXform, transformX);
159 currXform.mul(currXform, transformY);
160 } else {
161 currXform.mul(transformX, currXform);
162 currXform.mul(transformY, currXform);
163 }
164 if (ViewerTG != null) {
165 VPTG_T3D.invert();
166 currXform.mul(VPTG_T3D, currXform);
167 }
168 translation.set(mat.m03, mat.m13, mat.m23);
169 currXform.setTranslation(translation);
170 transformGroup.setTransform(currXform);
171 transformChanged(currXform);
172 if (callback != null) {
173 callback.transformChanged(MouseBehaviorCallback.TRANSLATE, currXform);
174 }
175 } else {
176 reset = false;
177 }
178 xLast = x;
179 yLast = y;
180 } else if (id == MouseEvent.MOUSE_PRESSED) {
181 xLast = ((MouseEvent) awtEvent).getX();
182 yLast = ((MouseEvent) awtEvent).getY();
183 } else if (id == MouseEvent.MOUSE_RELEASED) {
184 setTransformGroup(null);
185 }
186 }
187 }
188 }
189 if (transformGroup != null || postCriterion == null) {
190 wakeupOn(mouseCriterion);
191 } else {
192 postId(doneID);
193 wakeupOn(postCriterion);
194 }
195 }
196
197
198
199
200
201
202 public void setFactor(double factor) {
203 xFactor = yFactor = factor;
204 }
205
206
207
208
209
210
211
212 public void setFactor(double xFactor, double yFactor) {
213 this.xFactor = xFactor;
214 this.yFactor = yFactor;
215 }
216
217
218
219
220
221
222 public void setupCallback(MouseBehaviorCallback c) {
223 callback = c;
224 }
225
226
227
228
229
230
231 public void transformChanged(Transform3D transform) {}
232 }