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.Transform3D;
44 import org.jogamp.java3d.TransformGroup;
45 import org.jogamp.java3d.WakeupCriterion;
46 import org.jogamp.java3d.WakeupOnAWTEvent;
47 import org.jogamp.vecmath.Matrix4d;
48 import org.jogamp.vecmath.Vector3d;
49 import org.jogamp.vecmath.Vector3f;
50
51
52
53
54
55
56 public class MouseOrbit extends MouseBehavior {
57
58 double xAngle, yAngle;
59 double xFactor = 0.01;
60 double yFactor = 0.01;
61 private TransformGroup tg_ghost;
62 private MouseBehaviorCallback callback = null;
63
64
65
66
67
68
69
70 public MouseOrbit(int flags, TransformGroup VPTG) {
71 super(flags, VPTG);
72 }
73
74
75
76
77
78
79 public double getXFactor() {
80 return xFactor;
81 }
82
83
84
85
86
87
88 public double getYFactor() {
89 return yFactor;
90 }
91
92
93 public void initialize() {
94 super.initialize();
95 xAngle = 0;
96 yAngle = 0;
97 if ((flags & INVERT_INPUT) == INVERT_INPUT) {
98 invert = true;
99 xFactor *= -1;
100 yFactor *= -1;
101 }
102 }
103
104
105 public void processStimulus(Iterator<WakeupCriterion> criteria) {
106 while (criteria.hasNext()) {
107 WakeupCriterion wakeup = criteria.next();
108 if (wakeup instanceof WakeupOnAWTEvent) {
109 AWTEvent[] event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
110 for (AWTEvent awtEvent : event) {
111 processMouseEvent((MouseEvent) awtEvent);
112 if (((buttonPress) && ((flags & MANUAL_WAKEUP) == 0))
113 || ((wakeUp) && ((flags & MANUAL_WAKEUP) != 0))) {
114 int id = awtEvent.getID();
115 if ((id == MouseEvent.MOUSE_DRAGGED)) {
116 x = ((MouseEvent) awtEvent).getX();
117 y = ((MouseEvent) awtEvent).getY();
118 int dx = x - xLast;
119 int dy = y - yLast;
120 if (!reset) {
121 Transform3D tempT3D = new Transform3D();
122 Transform3D orbitT3D = new Transform3D();
123 tempT3D.rotX(-dy * yFactor);
124 orbitT3D.mul(tempT3D);
125 tempT3D.rotY(-dx * xFactor);
126 orbitT3D.mul(tempT3D);
127 Transform3D tg_ghost_T3D = new Transform3D();
128 tg_ghost.getTransform(tg_ghost_T3D);
129 Vector3f tg_ghost_vec3f = new Vector3f();
130 tg_ghost_T3D.get(tg_ghost_vec3f);
131 Matrix4d tg_ghost_mat4d = new Matrix4d();
132 tg_ghost_T3D.get(tg_ghost_mat4d);
133 Transform3D VPTG_ghost_T3D_inverted = new Transform3D();
134 Transform3D VPTG_ghost_T3D_noninverted = new Transform3D();
135
136 ViewerTG.getTransform(VPTG_ghost_T3D_inverted);
137
138 ViewerTG.getTransform(VPTG_ghost_T3D_noninverted);
139 VPTG_ghost_T3D_inverted.setTranslation(new Vector3d(0.0, 0.0, 0.0));
140 VPTG_ghost_T3D_noninverted.setTranslation(new Vector3d(0.0, 0.0, 0.0));
141 VPTG_ghost_T3D_inverted.invert();
142 tg_ghost_T3D.mul(VPTG_ghost_T3D_inverted, tg_ghost_T3D);
143 tg_ghost_T3D.setTranslation(new Vector3d(0.0, 0.0, 0.0));
144 if (invert) {
145 tg_ghost_T3D.mul(tg_ghost_T3D, orbitT3D);
146 } else {
147 tg_ghost_T3D.mul(orbitT3D, tg_ghost_T3D);
148 }
149 tg_ghost_T3D.mul(VPTG_ghost_T3D_noninverted, tg_ghost_T3D);
150 tg_ghost_T3D.setTranslation(tg_ghost_vec3f);
151 tg_ghost.setTransform(tg_ghost_T3D);
152 Transform3D VPTG_ghost_T3D = new Transform3D();
153
154 ViewerTG.getTransform(VPTG_ghost_T3D);
155 Vector3f VPTG_ghost_vec3f = new Vector3f();
156 VPTG_ghost_T3D.get(VPTG_ghost_vec3f);
157 Vector3f temp_vec3f = new Vector3f();
158 temp_vec3f.x = VPTG_ghost_vec3f.x - tg_ghost_vec3f.x;
159 temp_vec3f.y = VPTG_ghost_vec3f.y - tg_ghost_vec3f.y;
160 temp_vec3f.z = VPTG_ghost_vec3f.z - tg_ghost_vec3f.z;
161 VPTG_ghost_T3D.setTranslation(temp_vec3f);
162 VPTG_ghost_T3D.mul(VPTG_ghost_T3D_inverted, VPTG_ghost_T3D);
163 if (invert) {
164 VPTG_ghost_T3D.mul(VPTG_ghost_T3D, orbitT3D);
165 } else {
166 VPTG_ghost_T3D.mul(orbitT3D, VPTG_ghost_T3D);
167 }
168 VPTG_ghost_T3D.mul(VPTG_ghost_T3D_noninverted, VPTG_ghost_T3D);
169 VPTG_ghost_T3D.get(temp_vec3f);
170 temp_vec3f.x = temp_vec3f.x + tg_ghost_vec3f.x;
171 temp_vec3f.y = temp_vec3f.y + tg_ghost_vec3f.y;
172 temp_vec3f.z = temp_vec3f.z + tg_ghost_vec3f.z;
173 VPTG_ghost_T3D.setTranslation(temp_vec3f);
174
175 ViewerTG.setTransform(VPTG_ghost_T3D);
176 transformChanged(currXform);
177 if (callback != null) {
178 callback.transformChanged(MouseBehaviorCallback.ORBIT, currXform);
179 }
180 } else {
181 reset = false;
182 }
183 xLast = x;
184 yLast = y;
185 } else if (id == MouseEvent.MOUSE_PRESSED) {
186 xLast = ((MouseEvent) awtEvent).getX();
187 yLast = ((MouseEvent) awtEvent).getY();
188 }
189 }
190 }
191 }
192 }
193 wakeupOn(mouseCriterion);
194 }
195
196
197
198
199
200
201 public void setFactor(double factor) {
202 xFactor = yFactor = factor;
203 }
204
205
206
207
208
209
210
211 public void setFactor(double xFactor, double yFactor) {
212 this.xFactor = xFactor;
213 this.yFactor = yFactor;
214 }
215
216
217
218
219
220
221
222 public void setTransformGroups(TransformGroup tg, TransformGroup VPTG) {
223 super.ViewerTG = VPTG;
224 tg_ghost = new TransformGroup();
225 Transform3D tgT3D = new Transform3D();
226 tg.getTransform(tgT3D);
227
228 tg_ghost.setTransform(tgT3D);
229 }
230
231
232
233
234
235
236
237 public void setupCallback(MouseBehaviorCallback c) {
238 callback = c;
239 }
240
241
242
243
244
245
246 public void transformChanged(Transform3D transform) {}
247 }