/net/flashpunk/Screen.as

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