PageRenderTime 844ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/as3-f10/src/net/badimon/five3D/display/Sprite2D.as

http://five3d.googlecode.com/
ActionScript | 262 lines | 140 code | 30 blank | 92 comment | 15 complexity | 172c7585c069a871aa5d73126f74493b MD5 | raw file
  1. /*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. FIVe3D
  3. Flash Interactive Vector-based 3D
  4. Mathieu Badimon | five3d@mathieu-badimon.com
  5. http://five3D.mathieu-badimon.com | http://five3d.mathieu-badimon.com/archives/ | http://code.google.com/p/five3d/
  6. /*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  7. package net.badimon.five3D.display {
  8. import net.badimon.five3D.utils.InternalUtils;
  9. import flash.display.DisplayObject;
  10. import flash.display.Sprite;
  11. import flash.geom.Matrix3D;
  12. import flash.geom.Rectangle;
  13. /**
  14. * The Sprite2D class is the equivalent in the FIVe3D package of the Sprite class in the Flash package but contrary to the Sprite3D class, the Sprite2D class always faces the screen.
  15. *
  16. * @see Sprite3D
  17. * @see http://help.adobe.com/en_US/AS3LCR/Flash_10.0/flash/display/Sprite.html
  18. */
  19. public class Sprite2D extends Sprite implements IObject3D {
  20. private var __visible:Boolean = true;
  21. private var __x:Number = 0;
  22. private var __y:Number = 0;
  23. private var __z:Number = 0;
  24. private var __scaleX:Number = 1;
  25. private var __scaleY:Number = 1;
  26. private var __matrix:Matrix3D;
  27. private var __concatenatedMatrix:Matrix3D;
  28. private var __scaled:Boolean = true;
  29. private var __render:Boolean = true;
  30. private var __renderScaling:Boolean = true;
  31. private var __scaling:Boolean = false;
  32. // Calculation
  33. private var __vectorIn:Vector.<Number>;
  34. private var __vectorOut:Vector.<Number>;
  35. private var __perspective:Number;
  36. /**
  37. * Creates a new Sprite2D instance.
  38. */
  39. public function Sprite2D() {
  40. super.visible = false;
  41. __matrix = new Matrix3D();
  42. __concatenatedMatrix = new Matrix3D();
  43. initVectors();
  44. }
  45. private function initVectors():void {
  46. __vectorIn = new Vector.<Number>(3, true);
  47. __vectorOut = new Vector.<Number>(3, true);
  48. }
  49. //----------------------------------------------------------------------------------------------------
  50. // Properties (from normal "Sprite" class)
  51. //----------------------------------------------------------------------------------------------------
  52. /**
  53. * Whether or not the Sprite2D instance is visible. When the Sprite2D instance is not visible, 3D calculation and rendering are not executed.
  54. * Any change of this property takes effect the next time the instance is being rendered.
  55. *
  56. * <p>This property has the same behavior than the normal Sprite <code>visible</code> property.</p>
  57. */
  58. override public function get visible():Boolean {
  59. return __visible;
  60. }
  61. override public function set visible(value:Boolean):void {
  62. __visible = value;
  63. }
  64. /**
  65. * Indicates the x coordinate along the x-axis of the Sprite2D instance relative to the local coordinate system of the 3D parent container.
  66. *
  67. * <p>This property has the same behavior than the normal Sprite <code>x</code> property.</p>
  68. */
  69. override public function get x():Number {
  70. return __x;
  71. }
  72. override public function set x(value:Number):void {
  73. __x = value;
  74. askRendering();
  75. }
  76. /**
  77. * Indicates the y coordinate along the y-axis of the Sprite2D instance relative to the local coordinate system of the 3D parent container.
  78. *
  79. * <p>This property has the same behavior than the normal Sprite <code>y</code> property.</p>
  80. */
  81. override public function get y():Number {
  82. return __y;
  83. }
  84. override public function set y(value:Number):void {
  85. __y = value;
  86. askRendering();
  87. }
  88. /**
  89. * Indicates the z coordinate along the z-axis of the Sprite2D instance relative to the local coordinate system of the 3D parent container.
  90. *
  91. * <p>This property has the same behavior than the normal Sprite <code>z</code> property.</p>
  92. */
  93. override public function get z():Number {
  94. return __z;
  95. }
  96. override public function set z(value:Number):void {
  97. __z = value;
  98. askRendering();
  99. }
  100. /**
  101. * Indicates the x-axis scale (percentage) of the Sprite2D instance from its registration point relative or not to the local coordinate system of the 3D parent container depending on the <code>scaled</code> property setting.
  102. * A value of 0.0 equals a 0% scale and a value of 1.0 equals a 100% scale.
  103. *
  104. * <p>This property has the same behavior than the normal Sprite <code>scaleX</code> property.</p>
  105. *
  106. * @see #scaled
  107. */
  108. override public function get scaleX():Number {
  109. return __scaleX;
  110. }
  111. override public function set scaleX(value:Number):void {
  112. __scaleX = value;
  113. if (__scaled) __renderScaling = true;
  114. else super.scaleX = __scaleX;
  115. }
  116. /**
  117. * Indicates the y-axis scale (percentage) of the Sprite2D instance from its registration point relative or not to the local coordinate system of the 3D parent container depending on the <code>scaled</code> property setting.
  118. * A value of 0.0 equals a 0% scale and a value of 1.0 equals a 100% scale.
  119. *
  120. * <p>This property has the same behavior than the normal Sprite <code>scaleY</code> property.</p>
  121. *
  122. * @see #scaled
  123. */
  124. override public function get scaleY():Number {
  125. return __scaleY;
  126. }
  127. override public function set scaleY(value:Number):void {
  128. __scaleY = value;
  129. if (__scaled) __renderScaling = true;
  130. else super.scaleY = __scaleY;
  131. }
  132. //----------------------------------------------------------------------------------------------------
  133. // Properties (new)
  134. //----------------------------------------------------------------------------------------------------
  135. /**
  136. * A Matrix3D object representing the combined transformation matrixes of the Sprite2D instance and all of its parent 3D objects, back to the scene level.
  137. */
  138. public function get concatenatedMatrix():Matrix3D {
  139. return __concatenatedMatrix;
  140. }
  141. /**
  142. * Wether or not the Sprite2D instance scale is being affected by its depth in the 3D scene independently from the <code>scaleX</code> and <code>scaleY</code> properties.
  143. */
  144. public function get scaled():Boolean {
  145. return __scaled;
  146. }
  147. public function set scaled(value:Boolean):void {
  148. __scaled = value;
  149. if (__scaled) {
  150. __renderScaling = true;
  151. } else {
  152. __renderScaling = false;
  153. if (__scaling) removeScaling();
  154. }
  155. }
  156. //----------------------------------------------------------------------------------------------------
  157. // Workflow
  158. //----------------------------------------------------------------------------------------------------
  159. /**
  160. * @private
  161. */
  162. public function askRendering():void {
  163. __render = true;
  164. if (__scaled) __renderScaling = true;
  165. }
  166. /**
  167. * @private
  168. */
  169. public function askRenderingShading():void {
  170. }
  171. /**
  172. * @private
  173. */
  174. public function render(scene:Scene3D):void {
  175. if (!__visible && super.visible) super.visible = false;
  176. else if (__visible) {
  177. if (!super.visible) super.visible = true;
  178. if (__render) {
  179. var viewDistance:Number = scene.viewDistance;
  180. InternalUtils.setMatrixPosition(__matrix, __x, __y, __z);
  181. InternalUtils.setConcatenatedMatrix(__concatenatedMatrix, parent, __matrix);
  182. __concatenatedMatrix.transformVectors(__vectorIn, __vectorOut);
  183. __perspective = viewDistance / (__vectorOut[2] + viewDistance);
  184. setPlacement();
  185. __render = false;
  186. }
  187. if (__renderScaling) {
  188. applyScaling();
  189. __renderScaling = false;
  190. }
  191. }
  192. }
  193. private function setPlacement():void {
  194. super.x = __vectorOut[0] * __perspective;
  195. super.y = __vectorOut[1] * __perspective;
  196. }
  197. private function applyScaling():void {
  198. super.scaleX = __scaleX * __perspective;
  199. super.scaleY = __scaleY * __perspective;
  200. __scaling = true;
  201. }
  202. private function removeScaling():void {
  203. super.scaleX = __scaleX;
  204. super.scaleY = __scaleY;
  205. __scaling = false;
  206. }
  207. //----------------------------------------------------------------------------------------------------
  208. // Errors
  209. //----------------------------------------------------------------------------------------------------
  210. /**
  211. * @private
  212. */
  213. override public function get dropTarget():DisplayObject {
  214. throw new Error("The Sprite2D class does not implement this property or method.");
  215. }
  216. /**
  217. * @private
  218. */
  219. override public function startDrag(lockCenter:Boolean = false, bounds:Rectangle = null):void {
  220. throw new Error("The Sprite2D class does not implement this property or method.");
  221. }
  222. /**
  223. * @private
  224. */
  225. override public function stopDrag():void {
  226. throw new Error("The Sprite2D class does not implement this property or method.");
  227. }
  228. }
  229. }