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