/src/QuickB2/misc/qb2Mouse.as

http://quickb2.googlecode.com/ · ActionScript · 147 lines · 101 code · 21 blank · 25 comment · 15 complexity · 0333d0ef56b711f6d67824e2366c7881 MD5 · raw file

  1. /**
  2. * Copyright (c) 2010 Johnson Center for Simulation at Pine Technical College
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. package QuickB2.misc
  23. {
  24. import As3Math.geo2d.*;
  25. import flash.display.*;
  26. import flash.events.*;
  27. import QuickB2.events.*;
  28. import revent.rEventDispatcher;
  29. import revent.rIEventDispatcher;
  30. [Event(name="mouseDown", type="flash.events.MouseEvent")]
  31. [Event(name="mouseUp", type="flash.events.MouseEvent")]
  32. [Event(name="mouseOut", type="flash.events.MouseEvent")]
  33. [Event(name="mouseOver", type="flash.events.MouseEvent")]
  34. [Event(name="click", type="flash.events.MouseEvent")]
  35. /**
  36. * ...
  37. * @author Doug Koellmer
  38. */
  39. public class qb2Mouse extends EventDispatcher
  40. {
  41. public var suppressEvents:Boolean = false;
  42. public var mouseWentDown:Function;
  43. public var mouseWentUp:Function;
  44. public function qb2Mouse(initInteractiveSource:InteractiveObject)
  45. {
  46. interactiveSource = initInteractiveSource;
  47. }
  48. private function mouseEvent(evt:MouseEvent):void
  49. {
  50. if ( evt.type == MouseEvent.MOUSE_DOWN )
  51. {
  52. if ( !suppressEvents )
  53. {
  54. dispatchEvent(evt);
  55. _isDown = true;
  56. if ( mouseWentDown != null ) mouseWentDown.call();
  57. }
  58. }
  59. else if ( evt.type == MouseEvent.MOUSE_UP )
  60. {
  61. if ( !suppressEvents )
  62. {
  63. dispatchEvent(evt);
  64. _isDown = false;
  65. if ( mouseWentUp != null ) mouseWentUp.call();
  66. }
  67. }
  68. else
  69. {
  70. if ( !suppressEvents )
  71. {
  72. dispatchEvent(evt);
  73. }
  74. }
  75. _lastEventType = evt.type;
  76. _lastEventTarget = evt.target;
  77. }
  78. public static function makeSingleton(interactiveSource:InteractiveObject):void
  79. {
  80. if ( !_singleton )
  81. _singleton = new qb2Mouse(interactiveSource);
  82. }
  83. public function get lastEventType():String
  84. { return _lastEventType; }
  85. private var _lastEventType:String = "";
  86. public function get lastEventTarget():Object
  87. { return _lastEventTarget; }
  88. private var _lastEventTarget:Object = null;
  89. public function get isDown():Boolean
  90. { return _isDown; }
  91. private var _isDown:Boolean = false;
  92. public function get position():amPoint2d
  93. { return new amPoint2d(_interactiveSource.mouseX, _interactiveSource.mouseY); }
  94. public function get mouseX():Number
  95. { return _interactiveSource.mouseX; }
  96. public function get mouseY():Number
  97. { return _interactiveSource.mouseY; }
  98. public function set interactiveSource(source:InteractiveObject):void
  99. {
  100. if ( _interactiveSource )
  101. {
  102. _interactiveSource.removeEventListener(MouseEvent.MOUSE_DOWN, mouseEvent);
  103. _interactiveSource.removeEventListener(MouseEvent.MOUSE_UP, mouseEvent);
  104. _interactiveSource.removeEventListener(MouseEvent.MOUSE_OUT, mouseEvent);
  105. _interactiveSource.removeEventListener(MouseEvent.MOUSE_OVER, mouseEvent);
  106. _interactiveSource.removeEventListener(MouseEvent.CLICK, mouseEvent);
  107. }
  108. _interactiveSource = source;
  109. if ( _interactiveSource )
  110. {
  111. _interactiveSource.addEventListener(MouseEvent.MOUSE_DOWN, mouseEvent, false, 0, true );
  112. _interactiveSource.addEventListener(MouseEvent.MOUSE_UP, mouseEvent, false, 0, true );
  113. _interactiveSource.addEventListener(MouseEvent.MOUSE_OUT, mouseEvent, false, 0, true );
  114. _interactiveSource.addEventListener(MouseEvent.MOUSE_OVER, mouseEvent, false, 0, true );
  115. _interactiveSource.addEventListener(MouseEvent.CLICK, mouseEvent, false, 0, true );
  116. }
  117. _isDown = false;
  118. }
  119. public function get interactiveSource():InteractiveObject
  120. { return _interactiveSource; }
  121. private var _interactiveSource:InteractiveObject;
  122. public static function get singleton():qb2Mouse
  123. { return _singleton; }
  124. private static var _singleton:qb2Mouse;
  125. }
  126. }