/src/net/flashpunk/Screen.as

https://github.com/jakevsrobots/Infinite-Friends
ActionScript | 213 lines | 130 code | 20 blank | 63 comment | 18 complexity | 8097dcbd230e4dcc90a2a4d2fe7d972f MD5 | raw file
  1. package net.flashpunk
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.PixelSnapping;
  6. import flash.display.Sprite;
  7. import flash.geom.Matrix;
  8. import flash.geom.Point;
  9. import flash.geom.Transform;
  10. /**
  11. * Container for the main screen buffer. Can be used to transform the screen.
  12. */
  13. public class Screen
  14. {
  15. /**
  16. * Constructor.
  17. */
  18. public function Screen()
  19. {
  20. // create screen buffers
  21. _bitmap[0] = new Bitmap(new BitmapData(FP.width, FP.height, false, 0), PixelSnapping.NEVER);
  22. _bitmap[1] = new Bitmap(new BitmapData(FP.width, FP.height, false, 0), PixelSnapping.NEVER);
  23. FP.engine.addChild(_sprite);
  24. _sprite.addChild(_bitmap[0]).visible = true;
  25. _sprite.addChild(_bitmap[1]).visible = false;
  26. FP.buffer = _bitmap[0].bitmapData;
  27. _width = FP.width;
  28. _height = FP.height;
  29. update();
  30. }
  31. /**
  32. * Swaps screen buffers.
  33. */
  34. public function swap():void
  35. {
  36. _current = 1 - _current;
  37. FP.buffer = _bitmap[_current].bitmapData;
  38. }
  39. /**
  40. * Refreshes the screen.
  41. */
  42. public function refresh():void
  43. {
  44. // refreshes the screen
  45. FP.buffer.fillRect(FP.bounds, _color);
  46. }
  47. /**
  48. * Redraws the screen.
  49. */
  50. public function redraw():void
  51. {
  52. // refresh the buffers
  53. _bitmap[_current].visible = true;
  54. _bitmap[1 - _current].visible = false;
  55. }
  56. /** @private Re-applies transformation matrix. */
  57. public function update():void
  58. {
  59. _matrix.b = _matrix.c = 0;
  60. _matrix.a = _scaleX * _scale;
  61. _matrix.d = _scaleY * _scale;
  62. _matrix.tx = -_originX * _matrix.a;
  63. _matrix.ty = -_originY * _matrix.d;
  64. if (_angle != 0) _matrix.rotate(_angle);
  65. _matrix.tx += _originX * _scaleX * _scale + _x;
  66. _matrix.ty += _originY * _scaleX * _scale + _y;
  67. _sprite.transform.matrix = _matrix;
  68. }
  69. /**
  70. * Refresh color of the screen.
  71. */
  72. public function get color():uint { return _color; }
  73. public function set color(value:uint):void { _color = 0xFF000000 | value; }
  74. /**
  75. * X offset of the screen.
  76. */
  77. public function get x():int { return _x; }
  78. public function set x(value:int):void
  79. {
  80. if (_x == value) return;
  81. _x = value;
  82. update();
  83. }
  84. /**
  85. * Y offset of the screen.
  86. */
  87. public function get y():int { return _y; }
  88. public function set y(value:int):void
  89. {
  90. if (_y == value) return;
  91. _y = value;
  92. update();
  93. }
  94. /**
  95. * X origin of transformations.
  96. */
  97. public function get originX():int { return _originX; }
  98. public function set originX(value:int):void
  99. {
  100. if (_originX == value) return;
  101. _originX = value;
  102. update();
  103. }
  104. /**
  105. * Y origin of transformations.
  106. */
  107. public function get originY():int { return _originY; }
  108. public function set originY(value:int):void
  109. {
  110. if (_originY == value) return;
  111. _originY = value;
  112. update();
  113. }
  114. /**
  115. * X scale of the screen.
  116. */
  117. public function get scaleX():Number { return _scaleX; }
  118. public function set scaleX(value:Number):void
  119. {
  120. if (_scaleX == value) return;
  121. _scaleX = value;
  122. update();
  123. }
  124. /**
  125. * Y scale of the screen.
  126. */
  127. public function get scaleY():Number { return _scaleY; }
  128. public function set scaleY(value:Number):void
  129. {
  130. if (_scaleY == value) return;
  131. _scaleY = value;
  132. update();
  133. }
  134. /**
  135. * Scale factor of the screen. Final scale is scaleX * scale by scaleY * scale, so
  136. * you can use this factor to scale the screen both horizontally and vertically.
  137. */
  138. public function get scale():Number { return _scale; }
  139. public function set scale(value:Number):void
  140. {
  141. if (_scale == value) return;
  142. _scale = value;
  143. update();
  144. }
  145. /**
  146. * Rotation of the screen, in degrees.
  147. */
  148. public function get angle():Number { return _angle * FP.DEG; }
  149. public function set angle(value:Number):void
  150. {
  151. if (_angle == value) return;
  152. _angle = value * FP.RAD;
  153. update();
  154. }
  155. /**
  156. * Whether screen smoothing should be used or not.
  157. */
  158. public function get smoothing():Boolean { return _bitmap[0].smoothing; }
  159. public function set smoothing(value:Boolean):void { _bitmap[0].smoothing = _bitmap[1].smoothing = value; }
  160. /**
  161. * Width of the screen.
  162. */
  163. public function get width():uint { return _width; }
  164. /**
  165. * Height of the screen.
  166. */
  167. public function get height():uint { return _height; }
  168. /**
  169. * X position of the mouse on the screen.
  170. */
  171. public function get mouseX():int { return (FP.stage.mouseX - _x) / (_scaleX * _scale); }
  172. /**
  173. * Y position of the mouse on the screen.
  174. */
  175. public function get mouseY():int { return (FP.stage.mouseY - _y) / (_scaleY * _scale); }
  176. // Screen infromation.
  177. /** @private */ private var _sprite:Sprite = new Sprite;
  178. /** @private */ private var _bitmap:Vector.<Bitmap> = new Vector.<Bitmap>(2);
  179. /** @private */ private var _current:int = 0;
  180. /** @private */ private var _matrix:Matrix = new Matrix;
  181. /** @private */ private var _x:int;
  182. /** @private */ private var _y:int;
  183. /** @private */ private var _width:uint;
  184. /** @private */ private var _height:uint;
  185. /** @private */ private var _originX:int;
  186. /** @private */ private var _originY:int;
  187. /** @private */ private var _scaleX:Number = 1;
  188. /** @private */ private var _scaleY:Number = 1;
  189. /** @private */ private var _scale:Number = 1;
  190. /** @private */ private var _angle:Number = 0;
  191. /** @private */ private var _color:uint = 0x202020;
  192. }
  193. }