/flash/src/playn/flash/FlashMouse.java

https://github.com/simensan/playn
Java | 86 lines | 57 code | 7 blank | 22 comment | 11 complexity | 425778afe576d1d1fc8b7693bbcb6e7e MD5 | raw file
  1. /**
  2. * Copyright 2011 The PlayN Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package playn.flash;
  17. import flash.events.MouseEvent;
  18. import flash.display.Sprite;
  19. import playn.core.PlayN;
  20. import playn.core.Mouse;
  21. class FlashMouse implements Mouse {
  22. private Listener listener;
  23. FlashMouse() {
  24. // Mouse handlers.
  25. FlashPlatform.captureEvent(Sprite.MOUSEDOWN, new EventHandler<MouseEvent>() {
  26. public void handleEvent(MouseEvent nativeEvent) {
  27. if (listener != null) {
  28. ButtonEvent.Impl event = new ButtonEvent.Impl(PlayN.currentTime(),
  29. nativeEvent.getStageX(), nativeEvent.getStageY(), getMouseButton(nativeEvent));
  30. listener.onMouseDown(event);
  31. if (event.getPreventDefault()) {
  32. nativeEvent.preventDefault();
  33. }
  34. }
  35. }
  36. });
  37. FlashPlatform.captureEvent(Sprite.MOUSEUP, new EventHandler<MouseEvent>() {
  38. public void handleEvent(MouseEvent nativeEvent) {
  39. if (listener != null) {
  40. ButtonEvent.Impl event = new ButtonEvent.Impl(PlayN.currentTime(),
  41. nativeEvent.getStageX(), nativeEvent.getStageY(), getMouseButton(nativeEvent));
  42. listener.onMouseUp(event);
  43. if (event.getPreventDefault()) {
  44. nativeEvent.preventDefault();
  45. }
  46. }
  47. }
  48. });
  49. FlashPlatform.captureEvent(Sprite.MOUSEMOVE, new EventHandler<MouseEvent>() {
  50. public void handleEvent(MouseEvent nativeEvent) {
  51. if (listener != null) {
  52. MotionEvent.Impl event = new MotionEvent.Impl(PlayN.currentTime(),
  53. nativeEvent.getStageX(), nativeEvent.getStageY());
  54. listener.onMouseMove(event);
  55. if (event.getPreventDefault()) {
  56. nativeEvent.preventDefault();
  57. }
  58. }
  59. }
  60. });
  61. }
  62. @Override
  63. public void setListener(Listener listener) {
  64. this.listener = listener;
  65. }
  66. /**
  67. * Return the {@link Mouse} button given a {@link MouseEvent}
  68. *
  69. * @param e MouseEvent
  70. * @return {@link Mouse} button corresponding to the event
  71. */
  72. protected static int getMouseButton(MouseEvent e) {
  73. if (e.isButtonDown()) {
  74. return Mouse.BUTTON_LEFT;
  75. } else {
  76. return Mouse.BUTTON_RIGHT;
  77. }
  78. }
  79. }