-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathZebraInputHandler.java
256 lines (217 loc) · 7.43 KB
/
ZebraInputHandler.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.zebraimaging;
import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.awt.*;
import javax.media.opengl.awt.GLCanvas;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
/**
* An alternative input handler used to synchronize input with the Zebra Imaging display controller. Applications are
* not expected to create instances of this class directly or call its methods. To use it, specify it as the
* gov.nasa.worldwind.avkey.InputHandlerClassName in the World Wind configuration file.
*/
public class ZebraInputHandler extends AWTInputHandler
{
/** All instantiations of this class are stored for internal retrieval. */
private static List<ZebraInputHandler> instances = new ArrayList<ZebraInputHandler>();
private static Timer repaintContextsTimer = null;
final static TimerTask repaintContextsTask = new TimerTask()
{
public void run()
{
Iterator<ZebraInputHandler> itr = instances.iterator();
while (itr.hasNext())
{
ZebraInputHandler h = itr.next();
if (h.NeedsRefresh() == true)
{
h.SetRefresh(false);
h.getWorldWindow().redraw();
}
}
}
};
private long hwnd = 0;
private boolean arGL2Present = false;
private boolean refresh = false;
public ZebraInputHandler()
{
/**
* Attempt to load zebraIntegrator. If it's not found, assume we're either:
* (a) Not connected to the Zebra UPSD Dynamic Display.
* (b) Not using the Zebra integration tools.
*/
try
{
System.loadLibrary("arGL2Integrator");
arGL2Present = true;
instances.add(this);
System.out.println("Loaded arGL2Integrator successfully");
}
catch (UnsatisfiedLinkError e)
{
System.out.println("FAILED to load arGL2Integrator.dll");
}
if (repaintContextsTimer == null)
{
repaintContextsTimer = new Timer();
repaintContextsTimer.scheduleAtFixedRate(repaintContextsTask, 0, 10);
}
}
private synchronized void SetRefresh(boolean value)
{
refresh = value;
}
private synchronized boolean NeedsRefresh()
{
return refresh;
}
public void keyPressed(KeyEvent e)
{
boolean consumed = false;
if (arGL2Present)
consumed = zebraKeyPressed(getGLCanvasHandle(), e.getKeyCode());
if (consumed)
e.consume();
else
super.keyPressed(e);
}
public void keyReleased(KeyEvent e)
{
boolean consumed = false;
if (arGL2Present)
consumed = zebraKeyReleased(getGLCanvasHandle(), e.getKeyCode());
if (consumed)
e.consume();
else
super.keyReleased(e);
}
public void mouseClicked(MouseEvent e)
{
boolean consumed = false;
if (arGL2Present)
consumed = zebraMouseReleased(getGLCanvasHandle(), e.getButton(), e.getX(), e.getY());
if (consumed)
e.consume();
else
super.mouseClicked(e);
}
public void mousePressed(MouseEvent e)
{
boolean consumed = false;
if (arGL2Present)
consumed = zebraMousePressed(getGLCanvasHandle(), e.getButton(), e.getX(), e.getY());
if (consumed)
e.consume();
else
super.mousePressed(e);
}
public void mouseReleased(MouseEvent e)
{
boolean consumed = false;
if (arGL2Present)
consumed = zebraMouseReleased(getGLCanvasHandle(), e.getButton(), e.getX(), e.getY());
if (consumed)
e.consume();
else
super.mouseReleased(e);
}
public void mouseDragged(MouseEvent e)
{
/** The mouseDragged event does not populate the button field of MouseEvent. Therefore it must be done manually. */
int button = 0;
button = (e.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) == InputEvent.BUTTON1_DOWN_MASK ? 1 : button;
button = (e.getModifiersEx() & InputEvent.BUTTON2_DOWN_MASK) == InputEvent.BUTTON2_DOWN_MASK ? 2 : button;
button = (e.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) == InputEvent.BUTTON3_DOWN_MASK ? 3 : button;
boolean consumed = false;
if (arGL2Present)
consumed = zebraMouseMoved(getGLCanvasHandle(), button, e.getX(), e.getY());
if (consumed)
e.consume();
else
super.mouseDragged(e);
}
public void mouseWheelMoved(MouseWheelEvent e)
{
boolean consumed = false;
if (arGL2Present)
consumed = zebraMouseWheel(getGLCanvasHandle(), e.getWheelRotation());
if (consumed)
e.consume();
else
super.mouseWheelMoved(e);
}
private long getGLCanvasHandle()
{
/**
* Returns the win32 HWND handle of the GLCanvas component by calling native
* C++ code in arGL2Integrator.
*/
if (hwnd == 0)
{
WorldWindow ww = this.getWorldWindow();
if (ww != null)
{
WorldWindowGLCanvas wwgl = (WorldWindowGLCanvas) ww;
GLCanvas glc = wwgl;
Canvas cv = glc;
Component c = cv;
hwnd = zebraGetWin32Handle(c);
}
}
return hwnd;
}
private static ZebraInputHandler getInstance(long hwnd)
{
Iterator<ZebraInputHandler> itr = instances.iterator();
while (itr.hasNext())
{
ZebraInputHandler h = itr.next();
if (h.hwnd == hwnd)
return h;
}
return null;
}
// Java static methods executed by arGL2Integrator.dll via JNI
public static void forceRepaint(long hwnd)
{
/** Force the instance of the ZebraViewInputHandler class to redraw it's associated OpenGL window. */
ZebraInputHandler h = getInstance(hwnd);
if (h != null)
{
h.SetRefresh(true);
//h.refresh = true;
}
}
public static double[] getModelviewMatrix(long hwnd)
{
double[] matrix = new double[16];
ZebraInputHandler h = getInstance(hwnd);
if (h != null)
{
h.getWorldWindow().getView().getModelviewMatrix().toArray(matrix, 0, false);
}
return matrix;
}
public static double[] getProjectionMatrix(long hwnd)
{
double[] matrix = new double[16];
ZebraInputHandler h = getInstance(hwnd);
if (h != null)
{
h.getWorldWindow().getView().getProjectionMatrix().toArray(matrix, 0, false);
}
return matrix;
}
// Methods imported from the zebra's arGL2Integrator.dll library and executed by java
public native boolean zebraKeyPressed(long hwnd, int keyCode);
public native boolean zebraKeyReleased(long hwnd, int keyCode);
public native boolean zebraMousePressed(long hwnd, int button, int x, int y);
public native boolean zebraMouseReleased(long hwnd, int button, int x, int y);
public native boolean zebraMouseMoved(long hwnd, int button, int x, int y);
public native boolean zebraMouseWheel(long hwnd, int delta);
public native void zebraSetModelview(long hwnd, double[] matrix);
public native void zebraSetProjection(long hwnd, double[] matrix);
public native long zebraGetWin32Handle(Component component);
}