forked from hharrison/java3d-utils
/
KeyNavigatorBehavior.java
225 lines (204 loc) · 7.3 KB
/
KeyNavigatorBehavior.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or
* intended for use in the design, construction, operation or
* maintenance of any nuclear facility.
*
*/
package org.jogamp.java3d.utils.behaviors.keyboard;
import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.LinkedList;
import org.jogamp.java3d.Behavior;
import org.jogamp.java3d.TransformGroup;
import org.jogamp.java3d.WakeupCondition;
import org.jogamp.java3d.WakeupCriterion;
import org.jogamp.java3d.WakeupOnAWTEvent;
import org.jogamp.java3d.WakeupOnBehaviorPost;
import org.jogamp.java3d.WakeupOnElapsedFrames;
import org.jogamp.java3d.WakeupOr;
import org.jogamp.java3d.internal.J3dUtilsI18N;
/**
* This class is a simple behavior that invokes the KeyNavigator
* to modify the view platform transform.
*/
public class KeyNavigatorBehavior extends Behavior implements KeyListener {
private WakeupCriterion w1 = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
private WakeupCriterion w2 = new WakeupOnAWTEvent(KeyEvent.KEY_RELEASED);
private WakeupOnElapsedFrames w3 = new WakeupOnElapsedFrames(0);
private WakeupCriterion[] warray = { w1, w2, w3 };
private WakeupCondition w = new WakeupOr(warray);
private KeyEvent eventKey;
private KeyNavigator keyNavigator;
private boolean listener = false;
private LinkedList eventq;
/**
* Override Behavior's initialize method to setup wakeup criteria.
*/
@Override
public void initialize() {
// Establish initial wakeup criteria
if (listener) {
w1 = new WakeupOnBehaviorPost(this, KeyEvent.KEY_PRESSED);
w2 = new WakeupOnBehaviorPost(this, KeyEvent.KEY_RELEASED);
warray[0] = w1;
warray[1] = w2;
w = new WakeupOr(warray);
eventq = new LinkedList();
}
wakeupOn(w);
}
/**
* Override Behavior's stimulus method to handle the event.
*/
@Override
public void processStimulus(Iterator<WakeupCriterion> criteria) {
WakeupOnAWTEvent ev;
WakeupCriterion genericEvt;
AWTEvent[] events;
boolean sawFrame = false;
while (criteria.hasNext()) {
genericEvt = criteria.next();
if (genericEvt instanceof WakeupOnAWTEvent) {
ev = (WakeupOnAWTEvent) genericEvt;
events = ev.getAWTEvent();
processAWTEvent(events);
} else if (genericEvt instanceof WakeupOnElapsedFrames &&
eventKey != null) {
sawFrame = true;
} else if ((genericEvt instanceof WakeupOnBehaviorPost)) {
while(true) {
// access to the queue must be synchronized
synchronized (eventq) {
if (eventq.isEmpty()) break;
eventKey = (KeyEvent)eventq.remove(0);
if (eventKey.getID() == KeyEvent.KEY_PRESSED ||
eventKey.getID() == KeyEvent.KEY_RELEASED) {
keyNavigator.processKeyEvent(eventKey);
}
}
}
}
}
if (sawFrame)
keyNavigator.integrateTransformChanges();
// Set wakeup criteria for next time
wakeupOn(w);
}
/**
* Process a keyboard event
*/
private void processAWTEvent(AWTEvent[] events) {
for (int loop = 0; loop < events.length; loop++) {
if (events[loop] instanceof KeyEvent) {
eventKey = (KeyEvent) events[loop];
// change the transformation; for example to zoom
if (eventKey.getID() == KeyEvent.KEY_PRESSED ||
eventKey.getID() == KeyEvent.KEY_RELEASED) {
//System.out.println("Keyboard is hit! " + eventKey);
keyNavigator.processKeyEvent(eventKey);
}
}
}
}
/**
* Adds this behavior as a KeyListener to the specified component.
* This method can only be called if
* the behavior was created with one of the constructors that takes
* a Component as a parameter.
* @param c The component to add the KeyListener to.
* @exception IllegalStateException if the behavior was not created
* as a listener
* @since Java 3D 1.2.1
*/
public void addListener(Component c) {
if (!listener) {
throw new IllegalStateException(J3dUtilsI18N.getString("Behavior0"));
}
c.addKeyListener(this);
}
/**
* Constructs a new key navigator behavior node that operates
* on the specified transform group.
* @param targetTG the target transform group
*/
public KeyNavigatorBehavior(TransformGroup targetTG) {
keyNavigator = new KeyNavigator(targetTG);
}
/**
* Constructs a key navigator behavior that uses AWT listeners
* and behavior posts rather than WakeupOnAWTEvent. The behavior
* is added to the specified Component and works on the given
* TransformGroup. A null component can be passed to specify
* the behavior should use listeners. Components can then be added
* to the behavior with the addListener(Component c) method.
* @param c The component to add the KeyListener to.
* @param targetTG The target transform group.
* @since Java 3D 1.2.1
*/
public KeyNavigatorBehavior(Component c, TransformGroup targetTG) {
this(targetTG);
if (c != null) {
c.addKeyListener(this);
}
listener = true;
}
@Override
public void keyPressed(KeyEvent evt) {
// System.out.println("keyPressed");
// add new event to the queue
// must be MT safe
synchronized (eventq) {
eventq.add(evt);
// only need to post if this is the only event in the queue
if (eventq.size() == 1) postId(KeyEvent.KEY_PRESSED);
}
}
@Override
public void keyReleased(KeyEvent evt) {
// System.out.println("keyReleased");
// add new event to the queue
// must be MT safe
synchronized (eventq) {
eventq.add(evt);
// only need to post if this is the only event in the queue
if (eventq.size() == 1) postId(KeyEvent.KEY_RELEASED);
}
}
@Override
public void keyTyped(KeyEvent evt) {}
}