PageRenderTime 143ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/nid/xfl/core/Layer.as

https://bitbucket.org/nidinthb/as3-xfl-compiler
ActionScript | 315 lines | 243 code | 39 blank | 33 comment | 35 complexity | d260b10aa38d74df5950af6e245bc0ed MD5 | raw file
  1. package nid.xfl.core
  2. {
  3. import flash.display.Sprite;
  4. import flash.geom.Matrix;
  5. import flash.geom.Point;
  6. import flash.geom.Transform;
  7. import flash.utils.Dictionary;
  8. import nid.geom.DMatrix;
  9. import nid.utils.MatrixConvertor;
  10. import nid.xfl.compiler.swf.data.SWFButtonRecord;
  11. import nid.xfl.compiler.swf.tags.ITag;
  12. import nid.xfl.dom.DOMFrame;
  13. import nid.xfl.dom.DOMLayer;
  14. import nid.xfl.dom.elements.DOMSymbolInstance;
  15. import nid.xfl.interfaces.IXFilter;
  16. import nid.xfl.motion.EasingEquations;
  17. import nid.xfl.motion.TweenTypes;
  18. /**
  19. * ...
  20. * @author Nidin P Vinayakan
  21. */
  22. public class Layer extends Sprite
  23. {
  24. public var totalFrames:int = 0;
  25. public var layerType:String;
  26. public var hasParentLayer:Boolean;
  27. public var parentLayerIndex:int;
  28. public var clipDepth:int;
  29. public var frames:Vector.<Frame>;
  30. public var timeline:Dictionary = new Dictionary();
  31. public var currentIndex:int = 0;
  32. public var stop:Boolean;
  33. public var depthOffset:int = 0;
  34. public var isPublished:Boolean;
  35. public var isScaned:Boolean;
  36. private var display:Sprite;
  37. public function Layer(data:DOMLayer=null)
  38. {
  39. frames = new Vector.<Frame>();
  40. if (data != null)
  41. {
  42. construct(data);
  43. }
  44. }
  45. public function construct(data:DOMLayer):void
  46. {
  47. name = data.name;
  48. layerType = data.layerType;
  49. parentLayerIndex = data.parentLayerIndex;
  50. hasParentLayer = data.hasParentLayer;
  51. var frame:Frame;
  52. for (var f:int = 0; f < data.domframes.length; f++)
  53. {
  54. if (data.domframes[f].duration > 0)
  55. {
  56. for (var i:int = data.domframes[f].index; i < data.domframes[f].index + data.domframes[f].duration; i++)
  57. {
  58. if (i > data.domframes[f].index && data.domframes.length > f)
  59. {
  60. //frame = frames[i - 1].clone(f == (data.domframes.length - 1));
  61. frame = frames[i - 1].clone();
  62. applyProperties(frame, data, i, f);
  63. }
  64. else
  65. {
  66. frame = new Frame(data.domframes[f]);
  67. //trace('alpha:'+frame.elements[0].alpha);
  68. }
  69. addFrameAt(frame, i, i > data.domframes[f].index);
  70. frames.push(frame);
  71. frame.sid = frames.length;
  72. totalFrames++;
  73. }
  74. }
  75. else
  76. {
  77. frame = new Frame(data.domframes[f]);
  78. addFrameAt(frame, data.domframes[f].index);
  79. frames.push(frame);
  80. frame.sid = frames.length;
  81. totalFrames++;
  82. }
  83. }
  84. }
  85. internal function applyProperties(frame:Frame, data:DOMLayer, i:int, f:int):void
  86. {
  87. /**
  88. * Apply Tween
  89. */
  90. switch(data.domframes[f].tweenType)
  91. {
  92. case TweenTypes.SHAPE:
  93. {
  94. //TODO: MorphCurves
  95. }
  96. break;
  97. case TweenTypes.MOTION:
  98. {
  99. /**
  100. * Classic Motion Tween
  101. */
  102. var mat:Matrix = frame.elements[0].display.transform.matrix;
  103. if (frame.elements[0].isTimeline)
  104. {
  105. var bcpx:Number = frame.elements[0].timeline.centerPoint3DX;
  106. var bcpy:Number = frame.elements[0].timeline.centerPoint3DY;
  107. var bcpz:Number = frame.elements[0].timeline.centerPoint3DZ;
  108. }
  109. if (data.domframes.length > f)
  110. {
  111. if (data.domframes[f + 1].elements[0] is DOMSymbolInstance)
  112. {
  113. var symbol:DOMSymbolInstance = DOMSymbolInstance(data.domframes[f + 1].elements[0]);
  114. var ccpx:Number = symbol.centerPoint3DX - bcpx;
  115. var ccpy:Number = symbol.centerPoint3DY - bcpy;
  116. var ccpz:Number = symbol.centerPoint3DZ - bcpz;
  117. var iftrs:Vector.<IXFilter> = symbol._filters;
  118. }
  119. if (data.domframes[f].elements[0] is DOMSymbolInstance)
  120. {
  121. var bftrs:Vector.<IXFilter> = DOMSymbolInstance(data.domframes[f].elements[0])._filters;
  122. }
  123. var bmat:DMatrix = MatrixConvertor.convert(data.domframes[f].tweenMatrix);
  124. var imat:DMatrix = MatrixConvertor.convert(data.domframes[f + 1].tweenMatrix);
  125. var cmat:DMatrix = new DMatrix();
  126. var t:Number = i + 1 - data.domframes[f].index;
  127. var d:Number = data.domframes[f].duration + 1;
  128. var acc:Number = data.domframes[f].acceleration;
  129. var tp:Point = data.domframes[f].transformationPoint;
  130. cmat.tx = imat.tx - bmat.tx;
  131. cmat.ty = imat.ty - bmat.ty;
  132. cmat.scaleX = imat.scaleX - bmat.scaleX;
  133. cmat.scaleY = imat.scaleY - bmat.scaleY;
  134. cmat.rotation = imat.rotation - bmat.rotation;
  135. var bcpt:Point = new Point(bcpx, bcpy);
  136. var ccpt:Point = new Point(ccpx, ccpy);
  137. EasingEquations.easeMatrix(mat, bmat, cmat, tp, bcpt, ccpt, t, d, acc);
  138. //mat.tx = EasingEquations.ease(t, bx, cx, d, acc);
  139. //mat.ty = EasingEquations.ease(t, by, cy, d, acc);
  140. var alpha_b:Number = data.domframes[f].color.alphaMultiplier;
  141. var alpha_c:Number = data.domframes[f + 1].color.alphaMultiplier - data.domframes[f].color.alphaMultiplier;
  142. /**
  143. * Apply Color Transform
  144. */
  145. if (frame.hasColorTransform)
  146. {
  147. if (data.domframes[f + 1].color.alphaMultiplier == data.domframes[f].color.alphaMultiplier)
  148. {
  149. frame.colorTransform.alphaMultiplier = data.domframes[f].color.alphaMultiplier;
  150. }
  151. else
  152. {
  153. frame.colorTransform.alphaMultiplier = EasingEquations.ease(t, alpha_b, alpha_c, d, acc);
  154. }
  155. }
  156. /**
  157. * Apply Filters
  158. */
  159. if (iftrs != null)
  160. {
  161. frame.hasFilter = true;
  162. frame._filters = EasingEquations.easeFilters(t, bftrs, iftrs , d, acc);
  163. }
  164. }
  165. frame.matrix = mat;
  166. }
  167. break;
  168. case TweenTypes.MOTION_OBJECT:
  169. {
  170. }
  171. break;
  172. }
  173. }
  174. public function addFrame(frame:Frame, extension:Boolean = false):void
  175. {
  176. timeline[++currentIndex] = frame;
  177. timeline[currentIndex].extension = extension;
  178. }
  179. public function addFrameAt(frame:Frame, index:int = 0, extension:Boolean = false):void
  180. {
  181. timeline[index] = frame;
  182. timeline[index].extension = extension;
  183. }
  184. public function removeFrame(frame:Frame):void
  185. {
  186. timeline[currentIndex] = null;
  187. delete timeline[currentIndex--];
  188. }
  189. public function removeFrameAt(index:int = 0):void
  190. {
  191. timeline[index] = null;
  192. delete timeline[index];
  193. }
  194. public function gotoAndStop(frame:Object,depth:int):void
  195. {
  196. flush();
  197. if (frame is String)
  198. {
  199. //TODO: jump to label
  200. }
  201. else if (frame is Number)
  202. {
  203. var _fi:int = int(frame) - 1;
  204. var _fo:Frame = timeline[_fi];
  205. if (_fo != null)
  206. {
  207. display.addChild(_fo);
  208. _fo.updateDisplay();
  209. stop = _fo.stop == true?true:stop;
  210. }
  211. else if (totalFrames <= _fi && timeline[totalFrames - 1] != null)
  212. {
  213. _fo = timeline[totalFrames - 1];
  214. display.addChild(_fo);
  215. _fo.updateDisplay();
  216. stop = _fo.stop == true?true:stop;
  217. }
  218. else
  219. {
  220. //trace('null frame:', int(frame), 'depth:' + depth);
  221. }
  222. }
  223. }
  224. internal function flush():void
  225. {
  226. if (display != null && this.contains(display))
  227. {
  228. removeChild(display);
  229. }
  230. display = null;
  231. display = new Sprite();
  232. addChild(display);
  233. }
  234. /**
  235. * Scan layer for depth offset calculation
  236. */
  237. public function scan(property:Object):void
  238. {
  239. var _property:Object = { depth:0 }
  240. for (var i:int = 0; i < frames.length; i++)
  241. {
  242. frames[i].scan(_property, layerType);
  243. }
  244. //trace('parent:' + property.parent);
  245. //trace('_property.depth:' + _property.depth);
  246. depthOffset = property.depth + _property.depth;
  247. property.depth = depthOffset;
  248. if (hasParentLayer)
  249. {
  250. //if (property.clipDepths == undefined) property.clipDepths = new Object();
  251. property.clipDepths[parentLayerIndex] = depthOffset;
  252. }
  253. isScaned = true;
  254. //trace('depthOffset:' + depthOffset);
  255. }
  256. public function publish(f:int, tags:Vector.<ITag>, property:Object, sub_tags:Vector.<ITag> = null, isButton:Boolean = false, characters:Vector.<SWFButtonRecord> = null ):void
  257. {
  258. if (frames.length > f)
  259. {
  260. isPublished = true;
  261. property.depthOffset = depthOffset;
  262. frames[f].publish(tags, property, sub_tags, isButton, characters, layerType, clipDepth);
  263. }
  264. }
  265. /**
  266. * Save modifications
  267. */
  268. public function save(f:int):void
  269. {
  270. if (frames.length > f)
  271. {
  272. frames[f].save();
  273. }
  274. }
  275. }
  276. }