/src/com/pblabs/rendering2D/SimpleShapeRenderer.as

https://github.com/joelhooks/PushButtonEngine
ActionScript | 189 lines | 115 code | 28 blank | 46 comment | 5 complexity | 522efd1926143b0400dd1cae4c11757d MD5 | raw file
  1. /*******************************************************************************
  2. * PushButton Engine
  3. * Copyright (C) 2009 PushButton Labs, LLC
  4. * For more information see http://www.pushbuttonengine.com
  5. *
  6. * This file is licensed under the terms of the MIT license, which is included
  7. * in the License.html file at the root directory of this SDK.
  8. ******************************************************************************/
  9. package com.pblabs.rendering2D
  10. {
  11. import com.pblabs.engine.debug.Logger;
  12. import flash.display.Graphics;
  13. import flash.display.Sprite;
  14. import flash.geom.Point;
  15. /**
  16. * Draw a simple shape, a box or a circle, with color.
  17. */
  18. public class SimpleShapeRenderer extends DisplayObjectRenderer
  19. {
  20. public function SimpleShapeRenderer()
  21. {
  22. super();
  23. // Initialize displayObject to be a Sprite.
  24. displayObject = new Sprite();
  25. // Initial draw.
  26. redraw();
  27. }
  28. private var _radius:Number = 50;
  29. private var _isSquare:Boolean = false;
  30. private var _isCircle:Boolean = true;
  31. private var _fillColor:uint = 0xFF00FF;
  32. private var _fillAlpha:Number = 0.5;
  33. private var _lineColor:uint = 0x00FF00;
  34. private var _lineSize:Number = 0.5;
  35. private var _lineAlpha:Number = 1;
  36. public function get lineAlpha():Number
  37. {
  38. return _lineAlpha;
  39. }
  40. /**
  41. * The opacity of the line.
  42. */
  43. public function set lineAlpha(value:Number):void
  44. {
  45. _lineAlpha = value;
  46. redraw();
  47. }
  48. public function get lineSize():Number
  49. {
  50. return _lineSize;
  51. }
  52. /**
  53. * Thickness of the line. If between 0 and 1 you get a hairline.
  54. */
  55. public function set lineSize(value:Number):void
  56. {
  57. _lineSize = value;
  58. redraw();
  59. }
  60. public function get lineColor():uint
  61. {
  62. return _lineColor;
  63. }
  64. /**
  65. * Color of the line.
  66. */
  67. public function set lineColor(value:uint):void
  68. {
  69. _lineColor = value;
  70. redraw();
  71. }
  72. public function get fillAlpha():Number
  73. {
  74. return _fillAlpha;
  75. }
  76. /**
  77. * Opacity for the shape fill.
  78. */
  79. public function set fillAlpha(value:Number):void
  80. {
  81. _fillAlpha = value;
  82. redraw();
  83. }
  84. public function get fillColor():uint
  85. {
  86. return _fillColor;
  87. }
  88. /**
  89. * Fill shape with color.
  90. */
  91. public function set fillColor(value:uint):void
  92. {
  93. _fillColor = value;
  94. redraw();
  95. }
  96. public function get isCircle():Boolean
  97. {
  98. return _isCircle;
  99. }
  100. /**
  101. * If set, draw a circle.
  102. */
  103. public function set isCircle(value:Boolean):void
  104. {
  105. _isCircle = value;
  106. redraw();
  107. }
  108. public function get isSquare():Boolean
  109. {
  110. return _isSquare;
  111. }
  112. /**
  113. * If set, draw a square.
  114. */
  115. public function set isSquare(value:Boolean):void
  116. {
  117. _isSquare = value;
  118. redraw();
  119. }
  120. public function get radius():Number
  121. {
  122. return _radius;
  123. }
  124. /**
  125. * The size of the shape in pixels.
  126. */
  127. public function set radius(value:Number):void
  128. {
  129. _radius = value;
  130. redraw();
  131. }
  132. /**
  133. * Automatically called, but redraws the Sprite based on the user's
  134. * settings.
  135. */
  136. public function redraw():void
  137. {
  138. // Get references.
  139. var s:Sprite = displayObject as Sprite;
  140. if(!s)
  141. throw new Error("displayObject null or not a Sprite!");
  142. var g:Graphics = s.graphics;
  143. // Don't forget to clear.
  144. g.clear();
  145. // Prep line/fill settings.
  146. g.lineStyle(_lineSize, _lineColor, _lineAlpha);
  147. g.beginFill(_fillColor, _fillAlpha);
  148. // Draw one or both shapes.
  149. if(isSquare)
  150. g.drawRect(-radius, -radius, radius*2, radius*2);
  151. if(isCircle)
  152. g.drawCircle(0, 0, radius);
  153. g.endFill();
  154. // Sanity check.
  155. if(!isCircle && !isSquare)
  156. {
  157. Logger.error(this, "redraw", "Neither square nor circle, what am I?");
  158. }
  159. }
  160. }
  161. }