PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/org/flixel/system/input/Mouse.as

http://github.com/AdamAtomic/flixel
ActionScript | 338 lines | 171 code | 27 blank | 140 comment | 43 complexity | 853f4a6b4a31d4a32183766adecd4ded MD5 | raw file
  1. package org.flixel.system.input
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.Sprite;
  5. import flash.events.MouseEvent;
  6. import org.flixel.FlxCamera;
  7. import org.flixel.FlxG;
  8. import org.flixel.FlxPoint;
  9. import org.flixel.FlxSprite;
  10. import org.flixel.FlxU;
  11. import org.flixel.system.replay.MouseRecord;
  12. /**
  13. * This class helps contain and track the mouse pointer in your game.
  14. * Automatically accounts for parallax scrolling, etc.
  15. *
  16. * @author Adam Atomic
  17. */
  18. public class Mouse extends FlxPoint
  19. {
  20. [Embed(source="../../data/cursor.png")] protected var ImgDefaultCursor:Class;
  21. /**
  22. * Current "delta" value of mouse wheel. If the wheel was just scrolled up, it will have a positive value. If it was just scrolled down, it will have a negative value. If it wasn't just scroll this frame, it will be 0.
  23. */
  24. public var wheel:int;
  25. /**
  26. * Current X position of the mouse pointer on the screen.
  27. */
  28. public var screenX:int;
  29. /**
  30. * Current Y position of the mouse pointer on the screen.
  31. */
  32. public var screenY:int;
  33. /**
  34. * Helper variable for tracking whether the mouse was just pressed or just released.
  35. */
  36. protected var _current:int;
  37. /**
  38. * Helper variable for tracking whether the mouse was just pressed or just released.
  39. */
  40. protected var _last:int;
  41. /**
  42. * A display container for the mouse cursor.
  43. * This container is a child of FlxGame and sits at the right "height".
  44. */
  45. protected var _cursorContainer:Sprite;
  46. /**
  47. * This is just a reference to the current cursor image, if there is one.
  48. */
  49. protected var _cursor:Bitmap;
  50. /**
  51. * Helper variables for recording purposes.
  52. */
  53. protected var _lastX:int;
  54. protected var _lastY:int;
  55. protected var _lastWheel:int;
  56. protected var _point:FlxPoint;
  57. protected var _globalScreenPosition:FlxPoint;
  58. /**
  59. * Constructor.
  60. */
  61. public function Mouse(CursorContainer:Sprite)
  62. {
  63. super();
  64. _cursorContainer = CursorContainer;
  65. _lastX = screenX = 0;
  66. _lastY = screenY = 0;
  67. _lastWheel = wheel = 0;
  68. _current = 0;
  69. _last = 0;
  70. _cursor = null;
  71. _point = new FlxPoint();
  72. _globalScreenPosition = new FlxPoint();
  73. }
  74. /**
  75. * Clean up memory.
  76. */
  77. public function destroy():void
  78. {
  79. _cursorContainer = null;
  80. _cursor = null;
  81. _point = null;
  82. _globalScreenPosition = null;
  83. }
  84. /**
  85. * Either show an existing cursor or load a new one.
  86. *
  87. * @param Graphic The image you want to use for the cursor.
  88. * @param Scale Change the size of the cursor. Default = 1, or native size. 2 = 2x as big, 0.5 = half size, etc.
  89. * @param XOffset The number of pixels between the mouse's screen position and the graphic's top left corner.
  90. * @param YOffset The number of pixels between the mouse's screen position and the graphic's top left corner.
  91. */
  92. public function show(Graphic:Class=null,Scale:Number=1,XOffset:int=0,YOffset:int=0):void
  93. {
  94. _cursorContainer.visible = true;
  95. if(Graphic != null)
  96. load(Graphic,Scale,XOffset,YOffset);
  97. else if(_cursor == null)
  98. load();
  99. }
  100. /**
  101. * Hides the mouse cursor
  102. */
  103. public function hide():void
  104. {
  105. _cursorContainer.visible = false;
  106. }
  107. /**
  108. * Read only, check visibility of mouse cursor.
  109. */
  110. public function get visible():Boolean
  111. {
  112. return _cursorContainer.visible;
  113. }
  114. /**
  115. * Load a new mouse cursor graphic
  116. *
  117. * @param Graphic The image you want to use for the cursor.
  118. * @param Scale Change the size of the cursor.
  119. * @param XOffset The number of pixels between the mouse's screen position and the graphic's top left corner.
  120. * @param YOffset The number of pixels between the mouse's screen position and the graphic's top left corner.
  121. */
  122. public function load(Graphic:Class=null,Scale:Number=1,XOffset:int=0,YOffset:int=0):void
  123. {
  124. if(_cursor != null)
  125. _cursorContainer.removeChild(_cursor);
  126. if(Graphic == null)
  127. Graphic = ImgDefaultCursor;
  128. _cursor = new Graphic();
  129. _cursor.x = XOffset;
  130. _cursor.y = YOffset;
  131. _cursor.scaleX = Scale;
  132. _cursor.scaleY = Scale;
  133. _cursorContainer.addChild(_cursor);
  134. }
  135. /**
  136. * Unload the current cursor graphic. If the current cursor is visible,
  137. * then the default system cursor is loaded up to replace the old one.
  138. */
  139. public function unload():void
  140. {
  141. if(_cursor != null)
  142. {
  143. if(_cursorContainer.visible)
  144. load();
  145. else
  146. {
  147. _cursorContainer.removeChild(_cursor)
  148. _cursor = null;
  149. }
  150. }
  151. }
  152. /**
  153. * Called by the internal game loop to update the mouse pointer's position in the game world.
  154. * Also updates the just pressed/just released flags.
  155. *
  156. * @param X The current X position of the mouse in the window.
  157. * @param Y The current Y position of the mouse in the window.
  158. * @param XScroll The amount the game world has scrolled horizontally.
  159. * @param YScroll The amount the game world has scrolled vertically.
  160. */
  161. public function update(X:int,Y:int):void
  162. {
  163. _globalScreenPosition.x = X;
  164. _globalScreenPosition.y = Y;
  165. updateCursor();
  166. if((_last == -1) && (_current == -1))
  167. _current = 0;
  168. else if((_last == 2) && (_current == 2))
  169. _current = 1;
  170. _last = _current;
  171. }
  172. /**
  173. * Internal function for helping to update the mouse cursor and world coordinates.
  174. */
  175. protected function updateCursor():void
  176. {
  177. //actually position the flixel mouse cursor graphic
  178. _cursorContainer.x = _globalScreenPosition.x;
  179. _cursorContainer.y = _globalScreenPosition.y;
  180. //update the x, y, screenX, and screenY variables based on the default camera.
  181. //This is basically a combination of getWorldPosition() and getScreenPosition()
  182. var camera:FlxCamera = FlxG.camera;
  183. screenX = (_globalScreenPosition.x - camera.x)/camera.zoom;
  184. screenY = (_globalScreenPosition.y - camera.y)/camera.zoom;
  185. x = screenX + camera.scroll.x;
  186. y = screenY + camera.scroll.y;
  187. }
  188. /**
  189. * Fetch the world position of the mouse on any given camera.
  190. * NOTE: Mouse.x and Mouse.y also store the world position of the mouse cursor on the main camera.
  191. *
  192. * @param Camera If unspecified, first/main global camera is used instead.
  193. * @param Point An existing point object to store the results (if you don't want a new one created).
  194. *
  195. * @return The mouse's location in world space.
  196. */
  197. public function getWorldPosition(Camera:FlxCamera=null,Point:FlxPoint=null):FlxPoint
  198. {
  199. if(Camera == null)
  200. Camera = FlxG.camera;
  201. if(Point == null)
  202. Point = new FlxPoint();
  203. getScreenPosition(Camera,_point);
  204. Point.x = _point.x + Camera.scroll.x;
  205. Point.y = _point.y + Camera.scroll.y;
  206. return Point;
  207. }
  208. /**
  209. * Fetch the screen position of the mouse on any given camera.
  210. * NOTE: Mouse.screenX and Mouse.screenY also store the screen position of the mouse cursor on the main camera.
  211. *
  212. * @param Camera If unspecified, first/main global camera is used instead.
  213. * @param Point An existing point object to store the results (if you don't want a new one created).
  214. *
  215. * @return The mouse's location in screen space.
  216. */
  217. public function getScreenPosition(Camera:FlxCamera=null,Point:FlxPoint=null):FlxPoint
  218. {
  219. if(Camera == null)
  220. Camera = FlxG.camera;
  221. if(Point == null)
  222. Point = new FlxPoint();
  223. Point.x = (_globalScreenPosition.x - Camera.x)/Camera.zoom;
  224. Point.y = (_globalScreenPosition.y - Camera.y)/Camera.zoom;
  225. return Point;
  226. }
  227. /**
  228. * Resets the just pressed/just released flags and sets mouse to not pressed.
  229. */
  230. public function reset():void
  231. {
  232. _current = 0;
  233. _last = 0;
  234. }
  235. /**
  236. * Check to see if the mouse is pressed.
  237. *
  238. * @return Whether the mouse is pressed.
  239. */
  240. public function pressed():Boolean { return _current > 0; }
  241. /**
  242. * Check to see if the mouse was just pressed.
  243. *
  244. * @return Whether the mouse was just pressed.
  245. */
  246. public function justPressed():Boolean { return _current == 2; }
  247. /**
  248. * Check to see if the mouse was just released.
  249. *
  250. * @return Whether the mouse was just released.
  251. */
  252. public function justReleased():Boolean { return _current == -1; }
  253. /**
  254. * Event handler so FlxGame can update the mouse.
  255. *
  256. * @param FlashEvent A <code>MouseEvent</code> object.
  257. */
  258. public function handleMouseDown(FlashEvent:MouseEvent):void
  259. {
  260. if(_current > 0) _current = 1;
  261. else _current = 2;
  262. }
  263. /**
  264. * Event handler so FlxGame can update the mouse.
  265. *
  266. * @param FlashEvent A <code>MouseEvent</code> object.
  267. */
  268. public function handleMouseUp(FlashEvent:MouseEvent):void
  269. {
  270. if(_current > 0) _current = -1;
  271. else _current = 0;
  272. }
  273. /**
  274. * Event handler so FlxGame can update the mouse.
  275. *
  276. * @param FlashEvent A <code>MouseEvent</code> object.
  277. */
  278. public function handleMouseWheel(FlashEvent:MouseEvent):void
  279. {
  280. wheel = FlashEvent.delta;
  281. }
  282. /**
  283. * If the mouse changed state or is pressed, return that info now
  284. *
  285. * @return An array of key state data. Null if there is no data.
  286. */
  287. public function record():MouseRecord
  288. {
  289. if((_lastX == _globalScreenPosition.x) && (_lastY == _globalScreenPosition.y) && (_current == 0) && (_lastWheel == wheel))
  290. return null;
  291. _lastX = _globalScreenPosition.x;
  292. _lastY = _globalScreenPosition.y;
  293. _lastWheel = wheel;
  294. return new MouseRecord(_lastX,_lastY,_current,_lastWheel);
  295. }
  296. /**
  297. * Part of the keystroke recording system.
  298. * Takes data about key presses and sets it into array.
  299. *
  300. * @param KeyStates Array of data about key states.
  301. */
  302. public function playback(Record:MouseRecord):void
  303. {
  304. _current = Record.button;
  305. wheel = Record.wheel;
  306. _globalScreenPosition.x = Record.x;
  307. _globalScreenPosition.y = Record.y;
  308. updateCursor();
  309. }
  310. }
  311. }