1 | package org.expeditee.gio.swing;
|
---|
2 |
|
---|
3 | import java.awt.AWTException;
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.KeyEventDispatcher;
|
---|
7 | import java.awt.KeyboardFocusManager;
|
---|
8 | import java.awt.MouseInfo;
|
---|
9 | import java.awt.Robot;
|
---|
10 | import java.awt.Toolkit;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.awt.event.ActionListener;
|
---|
13 | import java.awt.event.ComponentEvent;
|
---|
14 | import java.awt.event.ComponentListener;
|
---|
15 | import java.awt.event.KeyEvent;
|
---|
16 | import java.awt.event.KeyListener;
|
---|
17 | import java.awt.event.MouseEvent;
|
---|
18 | import java.awt.event.MouseListener;
|
---|
19 | import java.awt.event.MouseMotionListener;
|
---|
20 | import java.awt.event.MouseWheelEvent;
|
---|
21 | import java.awt.event.MouseWheelListener;
|
---|
22 | import java.awt.event.WindowEvent;
|
---|
23 | import java.awt.event.WindowListener;
|
---|
24 | import java.awt.event.WindowStateListener;
|
---|
25 | import java.util.List;
|
---|
26 |
|
---|
27 | import javax.swing.SwingUtilities;
|
---|
28 | import javax.swing.Timer;
|
---|
29 |
|
---|
30 | import org.expeditee.core.Line;
|
---|
31 | import org.expeditee.core.Point;
|
---|
32 | import org.expeditee.gio.EcosystemManager;
|
---|
33 | import org.expeditee.gio.InputManager;
|
---|
34 | import org.expeditee.gio.input.InputEvent;
|
---|
35 | import org.expeditee.gio.input.InputEvent.InputType;
|
---|
36 | import org.expeditee.gio.input.KBMInputEvent;
|
---|
37 | import org.expeditee.gui.Popup;
|
---|
38 | import org.expeditee.gui.PopupManager;
|
---|
39 | import org.expeditee.items.widgets.Widget;
|
---|
40 |
|
---|
41 | public class SwingInputManager extends InputManager implements ComponentListener, WindowListener, WindowStateListener,
|
---|
42 | KeyListener, MouseListener, MouseMotionListener, MouseWheelListener, KeyEventDispatcher {
|
---|
43 | /** Singleton instance. */
|
---|
44 | private static SwingInputManager _instance;
|
---|
45 |
|
---|
46 | /** Singleton constructor. */
|
---|
47 | public static SwingInputManager getInstance() {
|
---|
48 | if (_instance == null) {
|
---|
49 | try {
|
---|
50 | SwingUtilities.invokeAndWait(new Runnable() {
|
---|
51 | @Override
|
---|
52 | public void run() {
|
---|
53 | _instance = new SwingInputManager();
|
---|
54 | }
|
---|
55 | });
|
---|
56 | } catch (Exception e) {
|
---|
57 | System.err.println("Error while initialising InputManager. Aborting...");
|
---|
58 | System.exit(1);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | return _instance;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** TODO: What functionality does this object provide? */
|
---|
66 | private MouseEventRouter _mouseEventRouter;
|
---|
67 |
|
---|
68 | /** For robotically controlling the mouse position. */
|
---|
69 | // private Robot _robot;
|
---|
70 | private MouseCorrectRobot _robot;
|
---|
71 |
|
---|
72 | private SwingInputManager() {
|
---|
73 | SwingInputManager swingInputManager = this;
|
---|
74 |
|
---|
75 | // Sign up to receive input as events
|
---|
76 | SwingGraphicsManager graphicsManager = SwingMiscManager.getIfUsingSwingGraphicsManager();
|
---|
77 | if (graphicsManager != null) {
|
---|
78 | graphicsManager.addWindowListener(swingInputManager);
|
---|
79 | graphicsManager.addWindowStateListener(swingInputManager);
|
---|
80 | graphicsManager.getContentPane().addKeyListener(swingInputManager); // TODO: Why do we need...
|
---|
81 | graphicsManager.addKeyListener(swingInputManager); // TODO: ...both of these? cts16
|
---|
82 | graphicsManager.addComponentListener(swingInputManager);
|
---|
83 |
|
---|
84 | // Sign up to receive mouse events, from the SwingGraphicsManager
|
---|
85 | // TODO: Decide if we will keep using MouseEventRouter. cts16
|
---|
86 | _mouseEventRouter = new MouseEventRouter(graphicsManager.getJMenuBar(), graphicsManager.getContentPane());
|
---|
87 | _mouseEventRouter.addExpediteeMouseListener(swingInputManager);
|
---|
88 | _mouseEventRouter.addExpediteeMouseMotionListener(swingInputManager);
|
---|
89 | _mouseEventRouter.addExpediteeMouseWheelListener(swingInputManager);
|
---|
90 |
|
---|
91 | // enable the glasspane-for capturing all mouse events
|
---|
92 | graphicsManager.setGlassPane(_mouseEventRouter);
|
---|
93 | graphicsManager.getGlassPane().setVisible(true);
|
---|
94 | graphicsManager.getContentPane().setBackground(Color.WHITE);
|
---|
95 | }
|
---|
96 | graphicsManager.getContentPane().setFocusTraversalKeysEnabled(false);
|
---|
97 |
|
---|
98 | // Set up the robot (for controlling the mouse)
|
---|
99 | // TODO: What to do if robot throws exception??? cts16
|
---|
100 | try {
|
---|
101 | _robot = new MouseCorrectRobot();
|
---|
102 | } catch (AWTException e) {
|
---|
103 | e.printStackTrace();
|
---|
104 | }
|
---|
105 |
|
---|
106 | // Create the timer for firing timeout events
|
---|
107 | Timer.setLogTimers(false);
|
---|
108 | _timer = new Timer(1, new ActionListener() {
|
---|
109 | @Override
|
---|
110 | public void actionPerformed(ActionEvent e) {
|
---|
111 | swingInputManager.triggerTimeoutEvents();
|
---|
112 | }
|
---|
113 | });
|
---|
114 | _timer.setRepeats(false);
|
---|
115 |
|
---|
116 | // AWTEventListener[] listeners = Toolkit.getDefaultToolkit().getAWTEventListeners();
|
---|
117 | // for (AWTEventListener listener: listeners) {
|
---|
118 | // System.err.println(listener);
|
---|
119 | // F10FriendlyKeyboardFocusManager keyboardManager = new F10FriendlyKeyboardFocusManager(listener);
|
---|
120 | // KeyboardFocusManager.setCurrentKeyboardFocusManager(keyboardManager);
|
---|
121 | // }
|
---|
122 | //
|
---|
123 | // F10FriendlyKeyboardFocusManager keyboardManager = new F10FriendlyKeyboardFocusManager(this);
|
---|
124 | // KeyboardFocusManager.setCurrentKeyboardFocusManager(keyboardManager);
|
---|
125 |
|
---|
126 | KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | protected Point getRealCursorPosition() {
|
---|
131 | Point mouseScreenPos = SwingConversions.fromSwingPoint(MouseInfo.getPointerInfo().getLocation());
|
---|
132 | return screenToWindowPosition(mouseScreenPos);
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public void setCursorPosition(Point position) {
|
---|
137 | // System.err.println("Move to position; Moving to: " + position);
|
---|
138 | Point screenPosition = windowToScreenPosition(position);
|
---|
139 | // System.err.println("Move to position: window adjusted; Moving to: " +
|
---|
140 | // screenPosition);
|
---|
141 | // _robot.mouseMove(screenPosition.x, screenPosition.y);
|
---|
142 | final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
143 | _robot.MoveMouseControlled(screenPosition.getX() / screenSize.getWidth(),
|
---|
144 | screenPosition.getY() / screenSize.getHeight());
|
---|
145 | updateCursorPosition(position.getX(), position.getY());
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public void windowStateChanged(WindowEvent e) {
|
---|
150 | // Does nothing. cts16
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Override
|
---|
154 | public void windowOpened(WindowEvent e) {
|
---|
155 | // Does nothing. cts16
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public void windowClosing(WindowEvent e) {
|
---|
160 | // Does nothing. cts16
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public void windowClosed(WindowEvent e) {
|
---|
165 | SwingGraphicsManager g = SwingMiscManager.getIfUsingSwingGraphicsManager();
|
---|
166 |
|
---|
167 | if (g == null || !g.isFullscreenTransitionPending()) {
|
---|
168 | distributeWindowEvent(WindowEventType.WINDOW_CLOSED);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Override
|
---|
173 | public void windowIconified(WindowEvent e) {
|
---|
174 | // Does nothing. cts16
|
---|
175 | }
|
---|
176 |
|
---|
177 | @Override
|
---|
178 | public void windowDeiconified(WindowEvent e) {
|
---|
179 | // Does nothing. cts16
|
---|
180 | }
|
---|
181 |
|
---|
182 | @Override
|
---|
183 | public void windowActivated(WindowEvent e) {
|
---|
184 | // Does nothing. cts16
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | public void windowDeactivated(WindowEvent e) {
|
---|
189 | // Does nothing. cts16
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public void componentResized(ComponentEvent e) {
|
---|
194 | SwingGraphicsManager g = SwingMiscManager.getIfUsingSwingGraphicsManager();
|
---|
195 | if (g != null) {
|
---|
196 | g.refreshRootSurface();
|
---|
197 | }
|
---|
198 |
|
---|
199 | distributeWindowEvent(WindowEventType.WINDOW_RESIZED);
|
---|
200 | }
|
---|
201 |
|
---|
202 | @Override
|
---|
203 | public void componentMoved(ComponentEvent e) {
|
---|
204 | // Does nothing. cts16
|
---|
205 |
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Override
|
---|
209 | public void componentShown(ComponentEvent e) {
|
---|
210 | SwingGraphicsManager g = SwingMiscManager.getIfUsingSwingGraphicsManager();
|
---|
211 |
|
---|
212 | if (g != null) {
|
---|
213 | g.finishFullscreenTransition();
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Override
|
---|
218 | public void componentHidden(ComponentEvent e) {
|
---|
219 | // Does nothing. cts16
|
---|
220 | }
|
---|
221 |
|
---|
222 | /** Converts a location given in screen-space to window-space. */
|
---|
223 | private Point screenToWindowPosition(Point screenPosition) {
|
---|
224 | Point windowTopLeft = EcosystemManager.getGraphicsManager().getWindowLocation();
|
---|
225 |
|
---|
226 | return Point.difference(screenPosition, windowTopLeft);
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Converts a location given in window-space to screen-space. */
|
---|
230 | private Point windowToScreenPosition(Point windowPosition) {
|
---|
231 | Point windowTopLeft = EcosystemManager.getGraphicsManager().getWindowLocation();
|
---|
232 |
|
---|
233 | return windowPosition.clone().add(windowTopLeft);
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Override
|
---|
237 | public void mouseWheelMoved(MouseWheelEvent e) {
|
---|
238 | //ensureShiftAltStateCorrect(e);
|
---|
239 | // Give widgets first whack at input
|
---|
240 | distributeNativeInput(e);
|
---|
241 | if (e.isConsumed()) {
|
---|
242 | return;
|
---|
243 | }
|
---|
244 |
|
---|
245 | try {
|
---|
246 | // Create an input event
|
---|
247 | Integer rotation = new Integer(e.getWheelRotation());
|
---|
248 | InputEvent event = new KBMInputEvent(KBMInputEvent.EventType.MOUSE_WHEEL_SCROLL, rotation);
|
---|
249 |
|
---|
250 | // Translate and perform the input
|
---|
251 | distributeInputEvent(event);
|
---|
252 | } catch (Exception exception) {
|
---|
253 | System.err.println(exception.getMessage());
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | @Override
|
---|
258 | public void mouseDragged(MouseEvent e) {
|
---|
259 | mouseMoved(e);
|
---|
260 | }
|
---|
261 |
|
---|
262 | @Override
|
---|
263 | public void mouseMoved(MouseEvent e) {
|
---|
264 | //ensureShiftAltStateCorrect(e);
|
---|
265 |
|
---|
266 | // Update the cursor position
|
---|
267 | updateCursorPosition(e.getX(), e.getY());
|
---|
268 |
|
---|
269 | // Give widgets first whack at input
|
---|
270 | distributeNativeInput(e);
|
---|
271 | if (e.isConsumed()) {
|
---|
272 | return;
|
---|
273 | }
|
---|
274 |
|
---|
275 | try {
|
---|
276 | // Create an input event
|
---|
277 | Point movedFrom = getCursorPosition();
|
---|
278 | Point movedTo = new Point(e.getX(), e.getY());
|
---|
279 | InputEvent event = new KBMInputEvent(KBMInputEvent.EventType.MOUSE_MOVE, new Line(movedFrom, movedTo));
|
---|
280 |
|
---|
281 | // Translate and perform the input
|
---|
282 | distributeInputEvent(event);
|
---|
283 | } catch (Exception exception) {
|
---|
284 | System.err.println(exception.getMessage());
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | @Override
|
---|
289 | public void mouseClicked(MouseEvent e) {
|
---|
290 | //ensureShiftAltStateCorrect(e);
|
---|
291 | // Give widgets first whack at input
|
---|
292 | distributeNativeInput(e);
|
---|
293 | if (e.isConsumed()) {
|
---|
294 | return;
|
---|
295 | }
|
---|
296 |
|
---|
297 | // Do nothing else (handled by mousePressed and mouseReleased)
|
---|
298 | }
|
---|
299 |
|
---|
300 | @Override
|
---|
301 | public void mousePressed(MouseEvent e) {
|
---|
302 | //ensureShiftAltStateCorrect(e);
|
---|
303 | mouseAction(e, true);
|
---|
304 | }
|
---|
305 |
|
---|
306 | @Override
|
---|
307 | public void mouseReleased(MouseEvent e) {
|
---|
308 | //ensureShiftAltStateCorrect(e);
|
---|
309 | mouseAction(e, false);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /** Handles mouse pressed/released events. */
|
---|
313 | private void mouseAction(MouseEvent e, boolean down) {
|
---|
314 | // Give widgets first whack at input
|
---|
315 | distributeNativeInput(e);
|
---|
316 | if (e.isConsumed()) {
|
---|
317 | return;
|
---|
318 | }
|
---|
319 |
|
---|
320 | // Work out which button was pressed
|
---|
321 | KBMInputEvent.MouseButton button;
|
---|
322 | switch (e.getButton()) {
|
---|
323 | case MouseEvent.BUTTON1:
|
---|
324 | button = KBMInputEvent.MouseButton.LEFT;
|
---|
325 | break;
|
---|
326 | case MouseEvent.BUTTON2:
|
---|
327 | button = KBMInputEvent.MouseButton.MIDDLE;
|
---|
328 | break;
|
---|
329 | case MouseEvent.BUTTON3:
|
---|
330 | button = KBMInputEvent.MouseButton.RIGHT;
|
---|
331 | break;
|
---|
332 | default:
|
---|
333 | return;
|
---|
334 | }
|
---|
335 |
|
---|
336 | try {
|
---|
337 | // Create an input event
|
---|
338 | KBMInputEvent.EventType type = down ? KBMInputEvent.EventType.MOUSE_BUTTON_DOWN
|
---|
339 | : KBMInputEvent.EventType.MOUSE_BUTTON_UP;
|
---|
340 | InputEvent event = new KBMInputEvent(type, button);
|
---|
341 |
|
---|
342 | // Translate and perform the input
|
---|
343 | distributeInputEvent(event);
|
---|
344 | } catch (Exception exception) {
|
---|
345 | System.err.println("SwingInputManager::mouseAction: " + exception.getMessage());
|
---|
346 | exception.printStackTrace();
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * Prevents forwarding mouse-entered-window events when coming back from a
|
---|
352 | * widget.
|
---|
353 | */
|
---|
354 | private boolean _inWindow = false;
|
---|
355 |
|
---|
356 | @Override
|
---|
357 | public void mouseEntered(MouseEvent e) {
|
---|
358 | if (!_inWindow) {
|
---|
359 | _inWindow = true;
|
---|
360 | distributeWindowEvent(WindowEventType.MOUSE_ENTERED_WINDOW);
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | @Override
|
---|
365 | public void mouseExited(MouseEvent e) {
|
---|
366 | _inWindow = false;
|
---|
367 | distributeWindowEvent(WindowEventType.MOUSE_EXITED_WINDOW);
|
---|
368 | }
|
---|
369 |
|
---|
370 | @Override
|
---|
371 | public void keyTyped(KeyEvent e) {
|
---|
372 | //System.err.println("SwingInputManager::keyTyped::" + KeyEvent.getKeyText(e.getKeyCode()));
|
---|
373 | //ensureShiftAltStateCorrect(e);
|
---|
374 |
|
---|
375 | // Give widgets first whack at input
|
---|
376 | distributeNativeInput(e);
|
---|
377 | if (e.isConsumed()) {
|
---|
378 | return;
|
---|
379 | }
|
---|
380 |
|
---|
381 | try {
|
---|
382 | // Ignore escape character and control characters
|
---|
383 | if (e.getKeyChar() == KeyEvent.VK_ESCAPE || e.isControlDown() || e.isAltDown()) {
|
---|
384 | return;
|
---|
385 | }
|
---|
386 |
|
---|
387 | // Create an input event
|
---|
388 | Character character = new Character(e.getKeyChar());
|
---|
389 | InputEvent event = new KBMInputEvent(KBMInputEvent.EventType.CHAR_TYPED, character);
|
---|
390 |
|
---|
391 | // Translate and perform the input
|
---|
392 | distributeInputEvent(event);
|
---|
393 | } catch (Exception exception) {
|
---|
394 | exception.printStackTrace();
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | @Override
|
---|
399 | public void keyPressed(KeyEvent e) {
|
---|
400 | //System.err.println("SwingInputManager::keyPressed::" + KeyEvent.getKeyText(e.getKeyCode()));
|
---|
401 | //ensureShiftAltStateCorrect(e);
|
---|
402 | keyAction(e, true);
|
---|
403 | }
|
---|
404 |
|
---|
405 | @Override
|
---|
406 | public void keyReleased(KeyEvent e) {
|
---|
407 | //System.err.println("SwingInputManager::keyReleased::" + KeyEvent.getKeyText(e.getKeyCode()));
|
---|
408 | keyAction(e, false);
|
---|
409 | }
|
---|
410 |
|
---|
411 | // private void ensureShiftAltStateCorrect(java.awt.event.InputEvent inputEvent) {
|
---|
412 | //// Component source = (Component) e.getSource();
|
---|
413 | //// int id = KeyEvent.KEY_RELEASED;
|
---|
414 | //// long when = System.currentTimeMillis();
|
---|
415 | //// int keyCode = KeyEvent.VK_UNDEFINED;
|
---|
416 | //// char keyChar = KeyEvent.CHAR_UNDEFINED;
|
---|
417 | //
|
---|
418 | //// int modifiers = (e.getModifiers() & ~KeyEvent.SHIFT_DOWN_MASK);
|
---|
419 | //// KeyEvent event = new KeyEvent(source, id, when, modifiers, keyCode, keyChar);
|
---|
420 | //// Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
|
---|
421 | //
|
---|
422 | // boolean shiftIsDownInRecordedState = StandardInputEventListeners.kbmStateListener.isKeyDown(Key.SHIFT);
|
---|
423 | // boolean shiftIsDownEventState = inputEvent.isShiftDown();
|
---|
424 | // if (shiftIsDownInRecordedState && !shiftIsDownEventState) {
|
---|
425 | // try {
|
---|
426 | // // Create an input event
|
---|
427 | // InputEvent event = new KBMInputEvent(KBMInputEvent.EventType.KEY_UP, KBMInputEvent.Key.SHIFT);
|
---|
428 | // System.err.println("SwingInputManager::ensureShiftAltStateCorrect::Sending followup Shift Up");
|
---|
429 | //
|
---|
430 | // // Translate and perform the input
|
---|
431 | // distributeInputEvent(event);
|
---|
432 | // } catch (Exception exception) {
|
---|
433 | // System.err.println(exception.getMessage());
|
---|
434 | // }
|
---|
435 | // }
|
---|
436 |
|
---|
437 | // System.err.println("SwingInputManager::ensureShiftAltStateCorrect::datastructure shift down: " + shiftIsDownInRecordedState);
|
---|
438 | // if (shiftIsDownInRecordedState != e.isShiftDown() && !e.isShiftDown()) {
|
---|
439 | // System.err.println("SwingInputManager::ensureShiftAltStateCorrect::Sending shift up event.");
|
---|
440 | // int modifiers = (e.getModifiers() & ~KeyEvent.SHIFT_DOWN_MASK);
|
---|
441 | // KeyEvent event = new KeyEvent(source, id, when, modifiers, keyCode, keyChar);
|
---|
442 | // Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
|
---|
443 | // }
|
---|
444 |
|
---|
445 | // boolean altIsDown = StandardInputEventListeners.kbmStateListener.isKeyDown(Key.ALT);
|
---|
446 | // if (altIsDown != e.isAltDown() && !e.isAltDown()) {
|
---|
447 | // System.err.println("SwingInputManager::ensureShiftAltStateCorrect::Sending alt up event.");
|
---|
448 | // int modifiers = (e.getModifiers() & ~KeyEvent.ALT_DOWN_MASK);
|
---|
449 | // KeyEvent event = new KeyEvent(source, id, when, modifiers, keyCode, keyChar);
|
---|
450 | // Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
|
---|
451 | // }
|
---|
452 | // }
|
---|
453 |
|
---|
454 | /** Handles key pressed/released events. */
|
---|
455 | private void keyAction(KeyEvent e, boolean down) {
|
---|
456 | // Give widgets first whack at input
|
---|
457 | distributeNativeInput(e);
|
---|
458 | if (e.isConsumed()) {
|
---|
459 | return;
|
---|
460 | }
|
---|
461 |
|
---|
462 | KBMInputEvent.Key key = SwingConversions.fromSwingVirtualKey(e.getKeyCode());
|
---|
463 | if (key == null) {
|
---|
464 | return;
|
---|
465 | }
|
---|
466 |
|
---|
467 | try {
|
---|
468 | // Create an input event
|
---|
469 | KBMInputEvent.EventType type = down ? KBMInputEvent.EventType.KEY_DOWN : KBMInputEvent.EventType.KEY_UP;
|
---|
470 | InputEvent event = new KBMInputEvent(type, key);
|
---|
471 |
|
---|
472 | // Translate and perform the input
|
---|
473 | distributeInputEvent(event);
|
---|
474 | } catch (Exception exception) {
|
---|
475 | System.err.println(exception.getMessage());
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | @Override
|
---|
480 | protected final boolean isInputTypeSupported(InputType type) {
|
---|
481 | // Only keyboard/mouse input is currently supported
|
---|
482 | if (type == InputEvent.InputType.KBM) {
|
---|
483 | return true;
|
---|
484 | }
|
---|
485 |
|
---|
486 | return false;
|
---|
487 | }
|
---|
488 |
|
---|
489 | /** Forwards the given input event to the listener if it is listening for it. */
|
---|
490 | private void forwardNativeInputToListener(Object listener, java.awt.event.InputEvent event) {
|
---|
491 | if (listener == null || event == null) {
|
---|
492 | return;
|
---|
493 | }
|
---|
494 |
|
---|
495 | if (listener instanceof KeyListener && event instanceof KeyEvent) {
|
---|
496 |
|
---|
497 | KeyListener keyListener = (KeyListener) listener;
|
---|
498 | KeyEvent ke = (KeyEvent) event;
|
---|
499 |
|
---|
500 | switch (event.getID()) {
|
---|
501 | case KeyEvent.KEY_PRESSED:
|
---|
502 | keyListener.keyPressed(ke);
|
---|
503 | return;
|
---|
504 | case KeyEvent.KEY_RELEASED:
|
---|
505 | keyListener.keyReleased(ke);
|
---|
506 | return;
|
---|
507 | case KeyEvent.KEY_TYPED:
|
---|
508 | keyListener.keyTyped(ke);
|
---|
509 | return;
|
---|
510 | }
|
---|
511 |
|
---|
512 | } else if (event instanceof MouseEvent) {
|
---|
513 |
|
---|
514 | MouseEvent me = (MouseEvent) event;
|
---|
515 |
|
---|
516 | if (listener instanceof MouseListener) {
|
---|
517 | MouseListener mouseListener = (MouseListener) listener;
|
---|
518 | switch (event.getID()) {
|
---|
519 | case MouseEvent.MOUSE_CLICKED:
|
---|
520 | mouseListener.mouseClicked(me);
|
---|
521 | return;
|
---|
522 | case MouseEvent.MOUSE_PRESSED:
|
---|
523 | mouseListener.mousePressed(me);
|
---|
524 | return;
|
---|
525 | case MouseEvent.MOUSE_RELEASED:
|
---|
526 | mouseListener.mouseReleased(me);
|
---|
527 | return;
|
---|
528 | case MouseEvent.MOUSE_ENTERED:
|
---|
529 | mouseListener.mouseEntered(me);
|
---|
530 | return;
|
---|
531 | case MouseEvent.MOUSE_EXITED:
|
---|
532 | mouseListener.mouseExited(me);
|
---|
533 | return;
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | if (listener instanceof MouseMotionListener) {
|
---|
538 | MouseMotionListener motionListener = (MouseMotionListener) listener;
|
---|
539 | switch (event.getID()) {
|
---|
540 | case MouseEvent.MOUSE_MOVED:
|
---|
541 | motionListener.mouseMoved(me);
|
---|
542 | return;
|
---|
543 | case MouseEvent.MOUSE_DRAGGED:
|
---|
544 | motionListener.mouseDragged(me);
|
---|
545 | return;
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 | if (listener instanceof MouseWheelListener && me instanceof MouseWheelEvent) {
|
---|
550 | MouseWheelListener wheelListener = (MouseWheelListener) listener;
|
---|
551 | MouseWheelEvent mwe = (MouseWheelEvent) me;
|
---|
552 | switch (event.getID()) {
|
---|
553 | case MouseEvent.MOUSE_WHEEL:
|
---|
554 | wheelListener.mouseWheelMoved(mwe);
|
---|
555 | return;
|
---|
556 | }
|
---|
557 | }
|
---|
558 | }
|
---|
559 | }
|
---|
560 |
|
---|
561 | /** Distributes the native input to all pop-ups and widgets. */
|
---|
562 | private void distributeNativeInput(java.awt.event.InputEvent event) {
|
---|
563 | // Distribute to pop-ups first
|
---|
564 | distributeNativeInputToPopups(event);
|
---|
565 | if (event.isConsumed()) {
|
---|
566 | return;
|
---|
567 | }
|
---|
568 |
|
---|
569 | // Then distribute to widgets
|
---|
570 | distributeNativeInputToWidgets(event);
|
---|
571 | }
|
---|
572 |
|
---|
573 | /** Distributes the input event to registered widgets. */
|
---|
574 | private void distributeNativeInputToPopups(java.awt.event.InputEvent event) {
|
---|
575 | if (event == null) {
|
---|
576 | return;
|
---|
577 | }
|
---|
578 | final List<Popup> popups = PopupManager.getInstance().getPopups();
|
---|
579 | for (Popup popup : popups) {
|
---|
580 | forwardNativeInputToListener(popup, event);
|
---|
581 |
|
---|
582 | if (event.isConsumed()) {
|
---|
583 | return;
|
---|
584 | }
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | /** Distributes the input event to registered widgets. */
|
---|
589 | private void distributeNativeInputToWidgets(java.awt.event.InputEvent event) {
|
---|
590 | if (event == null) {
|
---|
591 | return;
|
---|
592 | }
|
---|
593 | for (Widget iw : _widgets) {
|
---|
594 | java.awt.event.InputEvent e = respecInputEventForWidget(event, iw);
|
---|
595 |
|
---|
596 | forwardNativeInputToListener(iw, e);
|
---|
597 |
|
---|
598 | if (e != event && e.isConsumed()) {
|
---|
599 | event.consume();
|
---|
600 | }
|
---|
601 |
|
---|
602 | if (event.isConsumed()) {
|
---|
603 | return;
|
---|
604 | }
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | /** Modifies the event so that it is relative to the given widget. */
|
---|
609 | private java.awt.event.InputEvent respecInputEventForWidget(java.awt.event.InputEvent event, Widget iw) {
|
---|
610 | // TODO: Complete. cts16
|
---|
611 | return event;
|
---|
612 | }
|
---|
613 |
|
---|
614 | /** The timer which fires timeout events for the Swing input manager. */
|
---|
615 | private Timer _timer;
|
---|
616 |
|
---|
617 | @Override
|
---|
618 | protected void updateTimer(long nextTimeout) {
|
---|
619 | if (nextTimeout < 0) {
|
---|
620 | nextTimeout = 0;
|
---|
621 | }
|
---|
622 | _timer.stop();
|
---|
623 | _timer.setInitialDelay((int) nextTimeout);
|
---|
624 | _timer.start();
|
---|
625 |
|
---|
626 | }
|
---|
627 |
|
---|
628 | /**
|
---|
629 | * Obtained from stackoverflow.
|
---|
630 | * https://stackoverflow.com/questions/48799393/robot-mousemove-not-moving-to-specified-location-properly
|
---|
631 | * Fixes a (hopefully temporary) issue with the java.awt.Robot class on some
|
---|
632 | * specific systems (such as David's laptop) wherein the mouseMove function
|
---|
633 | * would move the mouse to odd locations.
|
---|
634 | *
|
---|
635 | * @author bln4
|
---|
636 | *
|
---|
637 | */
|
---|
638 | private class MouseCorrectRobot extends Robot {
|
---|
639 | final Dimension ScreenSize;// Primary Screen Size
|
---|
640 |
|
---|
641 | public MouseCorrectRobot() throws AWTException {
|
---|
642 | super();
|
---|
643 | ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
644 | }
|
---|
645 |
|
---|
646 | private double getTav(java.awt.Point a, java.awt.Point b) {
|
---|
647 | return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
|
---|
648 | }
|
---|
649 |
|
---|
650 | public void MoveMouseControlled(double xbe, double ybe)// Position of the cursor in [0,1] ranges. (0,0) is the
|
---|
651 | // upper
|
---|
652 | // left corner
|
---|
653 | {
|
---|
654 |
|
---|
655 | int xbepix = (int) (ScreenSize.width * xbe);
|
---|
656 | int ybepix = (int) (ScreenSize.height * ybe);
|
---|
657 |
|
---|
658 | int x = xbepix;
|
---|
659 | int y = ybepix;
|
---|
660 |
|
---|
661 | java.awt.Point mert = MouseInfo.getPointerInfo().getLocation();
|
---|
662 | java.awt.Point ElozoInitPont = new java.awt.Point(0, 0);
|
---|
663 |
|
---|
664 | int UgyanAztMeri = 0;
|
---|
665 | final int UgyanAZtMeriLimit = 30;
|
---|
666 |
|
---|
667 | int i = 0;
|
---|
668 | final int LepesLimit = 20000;
|
---|
669 | while ((mert.x != xbepix || mert.y != ybepix) && i < LepesLimit && UgyanAztMeri < UgyanAZtMeriLimit) {
|
---|
670 | ++i;
|
---|
671 | if (mert.x < xbepix) {
|
---|
672 | ++x;
|
---|
673 | } else {
|
---|
674 | --x;
|
---|
675 | }
|
---|
676 | if (mert.y < ybepix) {
|
---|
677 | ++y;
|
---|
678 | } else {
|
---|
679 | --y;
|
---|
680 | }
|
---|
681 | mouseMove(x, y);
|
---|
682 |
|
---|
683 | mert = MouseInfo.getPointerInfo().getLocation();
|
---|
684 |
|
---|
685 | if (getTav(ElozoInitPont, mert) < 5) {
|
---|
686 | ++UgyanAztMeri;
|
---|
687 | } else {
|
---|
688 | UgyanAztMeri = 0;
|
---|
689 | ElozoInitPont.x = mert.x;
|
---|
690 | ElozoInitPont.y = mert.y;
|
---|
691 | }
|
---|
692 |
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 | }
|
---|
697 |
|
---|
698 | @Override
|
---|
699 | public boolean dispatchKeyEvent(KeyEvent e) {
|
---|
700 | if (e.getKeyCode() == KeyEvent.VK_F10) {
|
---|
701 | if (KeyEvent.KEY_PRESSED == e.getID()) {
|
---|
702 | keyPressed(e);
|
---|
703 | } else if (KeyEvent.KEY_RELEASED == e.getID()) {
|
---|
704 | keyReleased(e);
|
---|
705 | } else if (KeyEvent.KEY_TYPED == e.getID()) {
|
---|
706 | keyTyped(e);
|
---|
707 | } else {
|
---|
708 | System.err.println("SwingInputManager::dispatchKeyEvent::Found Key that is not pressed, "
|
---|
709 | + "released or typed. What is it? " + e.getID());
|
---|
710 | }
|
---|
711 | return true;
|
---|
712 | }
|
---|
713 | return false;
|
---|
714 | }
|
---|
715 | }
|
---|