Changeset 1169 for trunk/src/org/expeditee/gio/swing/SwingInputManager.java
- Timestamp:
- 09/27/18 17:05:54 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/gio/swing/SwingInputManager.java
r1144 r1169 36 36 import org.expeditee.items.widgets.Widget; 37 37 38 public class SwingInputManager extends InputManager 39 implements ComponentListener, 40 WindowListener, 41 WindowStateListener, 42 KeyListener, 43 MouseListener, 44 MouseMotionListener, 45 MouseWheelListener 46 { 38 public class SwingInputManager extends InputManager implements ComponentListener, WindowListener, WindowStateListener, 39 KeyListener, MouseListener, MouseMotionListener, MouseWheelListener { 47 40 /** Singleton instance. */ 48 41 private static SwingInputManager _instance; 49 42 50 43 /** Singleton constructor. */ 51 public static SwingInputManager getInstance() 52 { 44 public static SwingInputManager getInstance() { 53 45 if (_instance == null) { 54 46 try { … … 64 56 } 65 57 } 66 58 67 59 return _instance; 68 60 } 69 61 70 62 /** TODO: What functionality does this object provide? */ 71 63 private MouseEventRouter _mouseEventRouter; 72 64 73 65 /** For robotically controlling the mouse position. */ 74 // private Robot _robot;66 // private Robot _robot; 75 67 private MouseCorrectRobot _robot; 76 77 private SwingInputManager() 78 { 68 69 private SwingInputManager() { 79 70 SwingInputManager swingInputManager = this; 80 71 81 72 // Sign up to receive input as events 82 73 SwingGraphicsManager graphicsManager = SwingMiscManager.getIfUsingSwingGraphicsManager(); … … 87 78 graphicsManager.addKeyListener(swingInputManager); // TODO: ...both of these? cts16 88 79 graphicsManager.addComponentListener(swingInputManager); 89 80 90 81 // Sign up to receive mouse events, from the SwingGraphicsManager 91 82 // TODO: Decide if we will keep using MouseEventRouter. cts16 … … 94 85 _mouseEventRouter.addExpediteeMouseMotionListener(swingInputManager); 95 86 _mouseEventRouter.addExpediteeMouseWheelListener(swingInputManager); 96 87 97 88 // enable the glasspane-for capturing all mouse events 98 89 graphicsManager.setGlassPane(_mouseEventRouter); … … 101 92 graphicsManager.getContentPane().setFocusTraversalKeysEnabled(false); 102 93 } 103 94 104 95 // Set up the robot (for controlling the mouse) 105 96 // TODO: What to do if robot throws exception??? cts16 … … 109 100 e.printStackTrace(); 110 101 } 111 102 112 103 // Create the timer for firing timeout events 113 104 Timer.setLogTimers(false); … … 120 111 _timer.setRepeats(false); 121 112 } 122 123 @Override 124 protected Point getRealCursorPosition() 125 { 113 114 @Override 115 protected Point getRealCursorPosition() { 126 116 Point mouseScreenPos = SwingConversions.fromSwingPoint(MouseInfo.getPointerInfo().getLocation()); 127 117 return screenToWindowPosition(mouseScreenPos); … … 129 119 130 120 @Override 131 public void setCursorPosition(Point position) 132 { 133 //System.err.println("Move to position; Moving to: " + position); 121 public void setCursorPosition(Point position) { 122 // System.err.println("Move to position; Moving to: " + position); 134 123 Point screenPosition = windowToScreenPosition(position); 135 //System.err.println("Move to position: window adjusted; Moving to: " + screenPosition); 136 //_robot.mouseMove(screenPosition.x, screenPosition.y); 124 // System.err.println("Move to position: window adjusted; Moving to: " + 125 // screenPosition); 126 // _robot.mouseMove(screenPosition.x, screenPosition.y); 137 127 final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 138 _robot.MoveMouseControlled(screenPosition.getX() / screenSize.getWidth(), screenPosition.getY() / screenSize.getHeight()); 128 _robot.MoveMouseControlled(screenPosition.getX() / screenSize.getWidth(), 129 screenPosition.getY() / screenSize.getHeight()); 139 130 updateCursorPosition(position.getX(), position.getY()); 140 131 } 141 132 142 133 @Override 143 public void windowStateChanged(WindowEvent e) 144 { 145 // Does nothing. cts16 146 } 147 148 @Override 149 public void windowOpened(WindowEvent e) 150 { 151 // Does nothing. cts16 152 } 153 154 @Override 155 public void windowClosing(WindowEvent e) 156 { 157 // Does nothing. cts16 158 } 159 160 @Override 161 public void windowClosed(WindowEvent e) 162 { 134 public void windowStateChanged(WindowEvent e) { 135 // Does nothing. cts16 136 } 137 138 @Override 139 public void windowOpened(WindowEvent e) { 140 // Does nothing. cts16 141 } 142 143 @Override 144 public void windowClosing(WindowEvent e) { 145 // Does nothing. cts16 146 } 147 148 @Override 149 public void windowClosed(WindowEvent e) { 163 150 SwingGraphicsManager g = SwingMiscManager.getIfUsingSwingGraphicsManager(); 164 151 165 152 if (g == null || !g.isFullscreenTransitionPending()) { 166 153 distributeWindowEvent(WindowEventType.WINDOW_CLOSED); … … 169 156 170 157 @Override 171 public void windowIconified(WindowEvent e) 172 { 173 // Does nothing. cts16 174 } 175 176 @Override 177 public void windowDeiconified(WindowEvent e) 178 { 179 // Does nothing. cts16 180 } 181 182 @Override 183 public void windowActivated(WindowEvent e) 184 { 185 // Does nothing. cts16 186 } 187 188 @Override 189 public void windowDeactivated(WindowEvent e) 190 { 191 // Does nothing. cts16 192 } 193 194 @Override 195 public void componentResized(ComponentEvent e) 196 { 158 public void windowIconified(WindowEvent e) { 159 // Does nothing. cts16 160 } 161 162 @Override 163 public void windowDeiconified(WindowEvent e) { 164 // Does nothing. cts16 165 } 166 167 @Override 168 public void windowActivated(WindowEvent e) { 169 // Does nothing. cts16 170 } 171 172 @Override 173 public void windowDeactivated(WindowEvent e) { 174 // Does nothing. cts16 175 } 176 177 @Override 178 public void componentResized(ComponentEvent e) { 197 179 SwingGraphicsManager g = SwingMiscManager.getIfUsingSwingGraphicsManager(); 198 if (g != null) g.refreshRootSurface(); 199 180 if (g != null) { 181 g.refreshRootSurface(); 182 } 183 200 184 distributeWindowEvent(WindowEventType.WINDOW_RESIZED); 201 185 } 202 186 203 187 @Override 204 public void componentMoved(ComponentEvent e) 205 { 206 // Does nothing. cts16 207 208 } 209 210 @Override 211 public void componentShown(ComponentEvent e) 212 { 188 public void componentMoved(ComponentEvent e) { 189 // Does nothing. cts16 190 191 } 192 193 @Override 194 public void componentShown(ComponentEvent e) { 213 195 SwingGraphicsManager g = SwingMiscManager.getIfUsingSwingGraphicsManager(); 214 215 if (g != null) g.finishFullscreenTransition(); 216 } 217 218 @Override 219 public void componentHidden(ComponentEvent e) 220 { 221 // Does nothing. cts16 222 } 223 196 197 if (g != null) { 198 g.finishFullscreenTransition(); 199 } 200 } 201 202 @Override 203 public void componentHidden(ComponentEvent e) { 204 // Does nothing. cts16 205 } 206 224 207 /** Converts a location given in screen-space to window-space. */ 225 private Point screenToWindowPosition(Point screenPosition) 226 { 208 private Point screenToWindowPosition(Point screenPosition) { 227 209 Point windowTopLeft = EcosystemManager.getGraphicsManager().getWindowLocation(); 228 210 229 211 return Point.difference(screenPosition, windowTopLeft); 230 212 } 231 213 232 214 /** Converts a location given in window-space to screen-space. */ 233 private Point windowToScreenPosition(Point windowPosition) 234 { 215 private Point windowToScreenPosition(Point windowPosition) { 235 216 Point windowTopLeft = EcosystemManager.getGraphicsManager().getWindowLocation(); 236 217 237 218 return windowPosition.clone().add(windowTopLeft); 238 219 } 239 220 240 221 @Override 241 public void mouseWheelMoved(MouseWheelEvent e) 242 { 222 public void mouseWheelMoved(MouseWheelEvent e) { 243 223 // Give widgets first whack at input 244 224 distributeNativeInput(e); 245 if (e.isConsumed()) return; 246 225 if (e.isConsumed()) { 226 return; 227 } 228 247 229 try { 248 230 // Create an input event 249 231 Integer rotation = new Integer(e.getWheelRotation()); 250 232 InputEvent event = new KBMInputEvent(KBMInputEvent.EventType.MOUSE_WHEEL_SCROLL, rotation); 251 233 252 234 // Translate and perform the input 253 235 distributeInputEvent(event); … … 258 240 259 241 @Override 260 public void mouseDragged(MouseEvent e) 261 { 242 public void mouseDragged(MouseEvent e) { 262 243 mouseMoved(e); 263 244 } 264 245 265 246 @Override 266 public void mouseMoved(MouseEvent e) 267 { 247 public void mouseMoved(MouseEvent e) { 268 248 // Update the cursor position 269 249 updateCursorPosition(e.getX(), e.getY()); 270 250 271 251 // Give widgets first whack at input 272 252 distributeNativeInput(e); 273 if (e.isConsumed()) return; 274 253 if (e.isConsumed()) { 254 return; 255 } 256 275 257 try { 276 258 // Create an input event … … 278 260 Point movedTo = new Point(e.getX(), e.getY()); 279 261 InputEvent event = new KBMInputEvent(KBMInputEvent.EventType.MOUSE_MOVE, new Line(movedFrom, movedTo)); 280 262 281 263 // Translate and perform the input 282 264 distributeInputEvent(event); … … 287 269 288 270 @Override 289 public void mouseClicked(MouseEvent e) 290 { 271 public void mouseClicked(MouseEvent e) { 291 272 // Give widgets first whack at input 292 273 distributeNativeInput(e); 293 if (e.isConsumed()) return; 294 274 if (e.isConsumed()) { 275 return; 276 } 277 295 278 // Do nothing else (handled by mousePressed and mouseReleased) 296 279 } 297 280 298 281 @Override 299 public void mousePressed(MouseEvent e) 300 { 282 public void mousePressed(MouseEvent e) { 301 283 mouseAction(e, true); 302 284 } 303 285 304 286 @Override 305 public void mouseReleased(MouseEvent e) 306 { 287 public void mouseReleased(MouseEvent e) { 307 288 mouseAction(e, false); 308 289 } 309 290 310 291 /** Handles mouse pressed/released events. */ 311 private void mouseAction(MouseEvent e, boolean down) 312 { 292 private void mouseAction(MouseEvent e, boolean down) { 313 293 // Give widgets first whack at input 314 294 distributeNativeInput(e); 315 if (e.isConsumed()) return; 316 295 if (e.isConsumed()) { 296 return; 297 } 298 317 299 // Work out which button was pressed 318 300 KBMInputEvent.MouseButton button; 319 301 switch (e.getButton()) { 320 321 322 323 324 325 326 327 328 329 330 331 } 332 302 case MouseEvent.BUTTON1: 303 button = KBMInputEvent.MouseButton.LEFT; 304 break; 305 case MouseEvent.BUTTON2: 306 button = KBMInputEvent.MouseButton.MIDDLE; 307 break; 308 case MouseEvent.BUTTON3: 309 button = KBMInputEvent.MouseButton.RIGHT; 310 break; 311 default: 312 return; 313 } 314 333 315 try { 334 316 // Create an input event 335 KBMInputEvent.EventType type = down ? KBMInputEvent.EventType.MOUSE_BUTTON_DOWN : KBMInputEvent.EventType.MOUSE_BUTTON_UP; 317 KBMInputEvent.EventType type = down ? KBMInputEvent.EventType.MOUSE_BUTTON_DOWN 318 : KBMInputEvent.EventType.MOUSE_BUTTON_UP; 336 319 InputEvent event = new KBMInputEvent(type, button); 337 320 338 321 // Translate and perform the input 339 322 distributeInputEvent(event); … … 343 326 } 344 327 345 /** Prevents forwarding mouse-entered-window events when coming back from a widget. */ 328 /** 329 * Prevents forwarding mouse-entered-window events when coming back from a 330 * widget. 331 */ 346 332 private boolean _inWindow = false; 347 348 @Override 349 public void mouseEntered(MouseEvent e) 350 { 333 334 @Override 335 public void mouseEntered(MouseEvent e) { 351 336 if (!_inWindow) { 352 337 _inWindow = true; … … 356 341 357 342 @Override 358 public void mouseExited(MouseEvent e) 359 { 343 public void mouseExited(MouseEvent e) { 360 344 _inWindow = false; 361 345 distributeWindowEvent(WindowEventType.MOUSE_EXITED_WINDOW); … … 363 347 364 348 @Override 365 public void keyTyped(KeyEvent e) 366 { 349 public void keyTyped(KeyEvent e) { 367 350 // Give widgets first whack at input 368 351 distributeNativeInput(e); 369 if (e.isConsumed()) return; 370 352 if (e.isConsumed()) { 353 return; 354 } 355 371 356 try { 372 357 // Ignore escape character and control characters 373 if (e.getKeyChar() == KeyEvent.VK_ESCAPE || e.isControlDown() || e.isAltDown()) return; 374 358 if (e.getKeyChar() == KeyEvent.VK_ESCAPE || e.isControlDown() || e.isAltDown()) { 359 return; 360 } 361 375 362 // Create an input event 376 363 Character character = new Character(e.getKeyChar()); 377 364 InputEvent event = new KBMInputEvent(KBMInputEvent.EventType.CHAR_TYPED, character); 378 365 379 366 // Translate and perform the input 380 367 distributeInputEvent(event); … … 385 372 386 373 @Override 387 public void keyPressed(KeyEvent e) 388 { 374 public void keyPressed(KeyEvent e) { 389 375 keyAction(e, true); 390 376 } 391 377 392 378 @Override 393 public void keyReleased(KeyEvent e) 394 { 379 public void keyReleased(KeyEvent e) { 395 380 keyAction(e, false); 396 381 } 397 382 398 383 /** Handles key pressed/released events. */ 399 private void keyAction(KeyEvent e, boolean down) 400 { 384 private void keyAction(KeyEvent e, boolean down) { 401 385 // Give widgets first whack at input 402 386 distributeNativeInput(e); 403 if (e.isConsumed()) return; 404 387 if (e.isConsumed()) { 388 return; 389 } 390 405 391 KBMInputEvent.Key key = SwingConversions.fromSwingVirtualKey(e.getKeyCode()); 406 if (key == null) return; 407 392 if (key == null) { 393 return; 394 } 395 408 396 try { 409 397 // Create an input event 410 398 KBMInputEvent.EventType type = down ? KBMInputEvent.EventType.KEY_DOWN : KBMInputEvent.EventType.KEY_UP; 411 399 InputEvent event = new KBMInputEvent(type, key); 412 400 413 401 // Translate and perform the input 414 402 distributeInputEvent(event); … … 417 405 } 418 406 } 419 420 @Override 421 protected final boolean isInputTypeSupported(InputType type) 422 { 407 408 @Override 409 protected final boolean isInputTypeSupported(InputType type) { 423 410 // Only keyboard/mouse input is currently supported 424 if (type == InputEvent.InputType.KBM) return true; 425 411 if (type == InputEvent.InputType.KBM) { 412 return true; 413 } 414 426 415 return false; 427 416 } 428 417 429 418 /** Forwards the given input event to the listener if it is listening for it. */ 430 private void forwardNativeInputToListener(Object listener, java.awt.event.InputEvent event) 431 { 432 if (listener == null || event == null) return; 433 419 private void forwardNativeInputToListener(Object listener, java.awt.event.InputEvent event) { 420 if (listener == null || event == null) { 421 return; 422 } 423 434 424 if (listener instanceof KeyListener && event instanceof KeyEvent) { 435 425 436 426 KeyListener keyListener = (KeyListener) listener; 437 427 KeyEvent ke = (KeyEvent) event; 438 428 439 429 switch (event.getID()) { 440 441 442 443 444 445 446 447 448 449 } 450 430 case KeyEvent.KEY_PRESSED: 431 keyListener.keyPressed(ke); 432 return; 433 case KeyEvent.KEY_RELEASED: 434 keyListener.keyReleased(ke); 435 return; 436 case KeyEvent.KEY_TYPED: 437 keyListener.keyTyped(ke); 438 return; 439 } 440 451 441 } else if (event instanceof MouseEvent) { 452 442 453 443 MouseEvent me = (MouseEvent) event; 454 444 455 445 if (listener instanceof MouseListener) { 456 446 MouseListener mouseListener = (MouseListener) listener; 457 447 switch (event.getID()) { 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 448 case MouseEvent.MOUSE_CLICKED: 449 mouseListener.mouseClicked(me); 450 return; 451 case MouseEvent.MOUSE_PRESSED: 452 mouseListener.mousePressed(me); 453 return; 454 case MouseEvent.MOUSE_RELEASED: 455 mouseListener.mouseReleased(me); 456 return; 457 case MouseEvent.MOUSE_ENTERED: 458 mouseListener.mouseEntered(me); 459 return; 460 case MouseEvent.MOUSE_EXITED: 461 mouseListener.mouseExited(me); 462 return; 473 463 } 474 464 } 475 465 476 466 if (listener instanceof MouseMotionListener) { 477 467 MouseMotionListener motionListener = (MouseMotionListener) listener; 478 468 switch (event.getID()) { 479 480 481 482 483 484 469 case MouseEvent.MOUSE_MOVED: 470 motionListener.mouseMoved(me); 471 return; 472 case MouseEvent.MOUSE_DRAGGED: 473 motionListener.mouseDragged(me); 474 return; 485 475 } 486 476 } 487 477 488 478 if (listener instanceof MouseWheelListener && me instanceof MouseWheelEvent) { 489 479 MouseWheelListener wheelListener = (MouseWheelListener) listener; 490 480 MouseWheelEvent mwe = (MouseWheelEvent) me; 491 481 switch (event.getID()) { 492 493 494 482 case MouseEvent.MOUSE_WHEEL: 483 wheelListener.mouseWheelMoved(mwe); 484 return; 495 485 } 496 486 } 497 487 } 498 488 } 499 489 500 490 /** Distributes the native input to all pop-ups and widgets. */ 501 private void distributeNativeInput(java.awt.event.InputEvent event) 502 { 491 private void distributeNativeInput(java.awt.event.InputEvent event) { 503 492 // Distribute to pop-ups first 504 493 distributeNativeInputToPopups(event); 505 if (event.isConsumed()) return; 506 494 if (event.isConsumed()) { 495 return; 496 } 497 507 498 // Then distribute to widgets 508 499 distributeNativeInputToWidgets(event); 509 500 } 510 501 511 502 /** Distributes the input event to registered widgets. */ 512 private void distributeNativeInputToPopups(java.awt.event.InputEvent event) 513 { 514 if (event == null) return; 515 503 private void distributeNativeInputToPopups(java.awt.event.InputEvent event) { 504 if (event == null) { 505 return; 506 } 507 516 508 for (Popup popup : PopupManager.getInstance().getPopups()) { 517 509 forwardNativeInputToListener(popup, event); 518 519 if (event.isConsumed()) return; 520 } 521 } 522 510 511 if (event.isConsumed()) { 512 return; 513 } 514 } 515 } 516 523 517 /** Distributes the input event to registered widgets. */ 524 private void distributeNativeInputToWidgets(java.awt.event.InputEvent event) 525 { 526 if (event == null) return; 527 518 private void distributeNativeInputToWidgets(java.awt.event.InputEvent event) { 519 if (event == null) { 520 return; 521 } 522 528 523 for (Widget iw : _widgets) { 529 524 java.awt.event.InputEvent e = respecInputEventForWidget(event, iw); 530 525 531 526 forwardNativeInputToListener(iw, e); 532 533 if (e != event && e.isConsumed()) event.consume(); 534 535 if (event.isConsumed()) return; 536 } 537 } 538 527 528 if (e != event && e.isConsumed()) { 529 event.consume(); 530 } 531 532 if (event.isConsumed()) { 533 return; 534 } 535 } 536 } 537 539 538 /** Modifies the event so that it is relative to the given widget. */ 540 private java.awt.event.InputEvent respecInputEventForWidget(java.awt.event.InputEvent event, Widget iw) 541 { 539 private java.awt.event.InputEvent respecInputEventForWidget(java.awt.event.InputEvent event, Widget iw) { 542 540 // TODO: Complete. cts16 543 541 return event; … … 546 544 /** The timer which fires timeout events for the Swing input manager. */ 547 545 private Timer _timer; 548 549 @Override 550 protected void updateTimer(long nextTimeout) 551 { 552 if (nextTimeout < 0) nextTimeout = 0; 546 547 @Override 548 protected void updateTimer(long nextTimeout) { 549 if (nextTimeout < 0) { 550 nextTimeout = 0; 551 } 553 552 _timer.stop(); 554 553 _timer.setInitialDelay((int) nextTimeout); 555 554 _timer.start(); 556 557 } 558 555 556 } 557 559 558 /** 560 * Obtained from stackoverflow. https://stackoverflow.com/questions/48799393/robot-mousemove-not-moving-to-specified-location-properly 561 * Fixes a (hopefully temporary) issue with the java.awt.Robot class on some specific systems (such as David's laptop) wherein 562 * the mouseMove function would move the mouse to odd locations. 559 * Obtained from stackoverflow. 560 * https://stackoverflow.com/questions/48799393/robot-mousemove-not-moving-to-specified-location-properly 561 * Fixes a (hopefully temporary) issue with the java.awt.Robot class on some 562 * specific systems (such as David's laptop) wherein the mouseMove function 563 * would move the mouse to odd locations. 564 * 563 565 * @author bln4 564 566 * … … 573 575 574 576 private double getTav(java.awt.Point a, java.awt.Point b) { 575 return Math.sqrt((double) ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))); 576 } 577 578 public void MoveMouseControlled(double xbe, double ybe)// Position of the cursor in [0,1] ranges. (0,0) is the upper 577 return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); 578 } 579 580 public void MoveMouseControlled(double xbe, double ybe)// Position of the cursor in [0,1] ranges. (0,0) is the 581 // upper 579 582 // left corner 580 583 { … … 596 599 while ((mert.x != xbepix || mert.y != ybepix) && i < LepesLimit && UgyanAztMeri < UgyanAZtMeriLimit) { 597 600 ++i; 598 if (mert.x < xbepix) 601 if (mert.x < xbepix) { 599 602 ++x; 600 else603 } else { 601 604 --x; 602 if (mert.y < ybepix) 605 } 606 if (mert.y < ybepix) { 603 607 ++y; 604 else608 } else { 605 609 --y; 610 } 606 611 mouseMove(x, y); 607 612 608 613 mert = MouseInfo.getPointerInfo().getLocation(); 609 614 610 if (getTav(ElozoInitPont, mert) < 5) 615 if (getTav(ElozoInitPont, mert) < 5) { 611 616 ++UgyanAztMeri; 612 else {617 } else { 613 618 UgyanAztMeri = 0; 614 619 ElozoInitPont.x = mert.x;
Note:
See TracChangeset
for help on using the changeset viewer.