PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/cocos/scripting/js-bindings/script/studio/parsers/action-2.x.js

https://github.com/dumganhar/cocos2d-x
JavaScript | 316 lines | 266 code | 24 blank | 26 comment | 28 complexity | f5db2388f5d48292b621703aa49bb0cc MD5 | raw file
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. (function(load, baseParser){
  22. var cache = {};
  23. var Parser = baseParser.extend({
  24. getNodeJson: function(json){
  25. return json["Content"]["Content"]["Animation"];
  26. },
  27. parseNode: function(json, resourcePath, file){
  28. if(!json)
  29. return null;
  30. if(cache[file])
  31. return cache[file].clone();
  32. var self = this,
  33. action = new ccs.ActionTimeline();
  34. action.setDuration(json["Duration"]);
  35. action.setTimeSpeed(json["Speed"] || 1);
  36. //The process of analysis
  37. var timelines = json["Timelines"];
  38. timelines.forEach(function(timeline){
  39. var parser = self.parsers[timeline["Property"]];
  40. var frame;
  41. if(parser)
  42. frame = parser.call(self, timeline, resourcePath);
  43. else
  44. cc.log("parser does not exist : %s", timeline["Property"]);
  45. if(frame)
  46. action.addTimeline(frame);
  47. });
  48. cache[file] = action;
  49. cache[file].retain();
  50. return action.clone();
  51. },
  52. deferred: function(json, resourcePath, action, file){
  53. var animationlist = json["Content"]["Content"]["AnimationList"];
  54. var length = animationlist ? animationlist.length : 0;
  55. for (var i = 0; i < length; i++){
  56. var animationdata = animationlist[i];
  57. var info = { name: null, startIndex: null, endIndex: null };
  58. info.name = animationdata["Name"];
  59. info.startIndex = animationdata["StartIndex"];
  60. info.endIndex = animationdata["EndIndex"];
  61. action.addAnimationInfo(info);
  62. }
  63. }
  64. });
  65. var parser = new Parser();
  66. var frameList = [
  67. {
  68. name: "Position",
  69. handle: function(options){
  70. var frame = new ccs.PositionFrame();
  71. var x = options["X"];
  72. var y = options["Y"];
  73. frame.setPosition(cc.p(x,y));
  74. return frame;
  75. }
  76. },
  77. {
  78. name: "VisibleForFrame",
  79. handle: function(options){
  80. var frame = new ccs.VisibleFrame();
  81. var visible = options["Value"];
  82. frame.setVisible(visible);
  83. return frame;
  84. }
  85. },
  86. {
  87. name: "Scale",
  88. handle: function(options){
  89. var frame = new ccs.ScaleFrame();
  90. var scalex = options["X"];
  91. var scaley = options["Y"];
  92. frame.setScaleX(scalex);
  93. frame.setScaleY(scaley);
  94. return frame;
  95. }
  96. },
  97. {
  98. name: "Rotation",
  99. handle: function(options){
  100. var frame = new ccs.RotationFrame();
  101. var rotation = options["Rotation"] || options["Value"] || 0;
  102. frame.setRotation(rotation);
  103. return frame;
  104. }
  105. },
  106. {
  107. name: "Skew",
  108. handle: function(options){
  109. var frame = new ccs.SkewFrame();
  110. var skewx = options["X"];
  111. var skewy = options["Y"];
  112. frame.setSkewX(skewx);
  113. frame.setSkewY(skewy);
  114. return frame;
  115. }
  116. },
  117. {
  118. name: "RotationSkew",
  119. handle: function(options){
  120. var frame = new ccs.RotationSkewFrame();
  121. var skewx = options["X"];
  122. var skewy = options["Y"];
  123. frame.setSkewX(skewx);
  124. frame.setSkewY(skewy);
  125. return frame;
  126. }
  127. },
  128. {
  129. name: "Anchor",
  130. handle: function(options){
  131. var frame = new ccs.AnchorPointFrame();
  132. var anchorx = options["X"];
  133. var anchory = options["Y"];
  134. frame.setAnchorPoint(cc.p(anchorx, anchory));
  135. return frame;
  136. }
  137. },{
  138. name: "AnchorPoint",
  139. handle: function(options){
  140. var frame = new ccs.AnchorPointFrame();
  141. var anchorx = options["X"];
  142. var anchory = options["Y"];
  143. frame.setAnchorPoint(cc.p(anchorx, anchory));
  144. return frame;
  145. }
  146. },{
  147. name: "InnerAction",
  148. handle: function(options){
  149. var frame = new ccs.InnerActionFrame();
  150. var type = options["InnerActionType"];
  151. var startFrame = options["StartFrame"];
  152. frame.setInnerActionType(type);
  153. frame.setStartFrameIndex(startFrame);
  154. return frame;
  155. }
  156. },
  157. {
  158. name: "CColor",
  159. handle: function(options){
  160. var frame = new ccs.ColorFrame();
  161. var color = options["Color"];
  162. if(!color) color = {};
  163. color["R"] = color["R"] === undefined ? 255 : color["R"];
  164. color["G"] = color["G"] === undefined ? 255 : color["G"];
  165. color["B"] = color["B"] === undefined ? 255 : color["B"];
  166. frame.setColor(cc.color(color["R"], color["G"], color["B"]));
  167. return frame;
  168. }
  169. },
  170. {
  171. name: "Alpha",
  172. handle: function(options){
  173. var frame = new ccs.AlphaFrame();
  174. var alpha = options["Value"];
  175. frame.setAlpha(alpha);
  176. return frame;
  177. }
  178. },
  179. {
  180. name: "FileData",
  181. handle: function(options, resourcePath){
  182. var frame, texture, plist, path, spriteFrame;
  183. frame = new ccs.TextureFrame();
  184. texture = options["TextureFile"];
  185. if(texture != null) {
  186. plist = texture["Plist"];
  187. path = texture["Path"];
  188. spriteFrame = cc.spriteFrameCache.getSpriteFrame(path);
  189. if(!spriteFrame && plist){
  190. if(cc.loader.getRes(resourcePath + plist)){
  191. cc.spriteFrameCache.addSpriteFrames(resourcePath + plist);
  192. spriteFrame = cc.spriteFrameCache.getSpriteFrame(path);
  193. }else{
  194. cc.log("%s need to be preloaded", resourcePath + plist);
  195. }
  196. }
  197. if(spriteFrame == null){
  198. path = resourcePath + path;
  199. }
  200. frame.setTextureName(path);
  201. }
  202. return frame;
  203. }
  204. },
  205. {
  206. name: "FrameEvent",
  207. handle: function(options){
  208. var frame = new ccs.EventFrame();
  209. var evnt = options["Value"];
  210. if(evnt != null)
  211. frame.setEvent(evnt);
  212. return frame;
  213. }
  214. },
  215. {
  216. name: "ZOrder",
  217. handle: function(options){
  218. var frame = new ccs.ZOrderFrame();
  219. var zorder = options["Value"];
  220. frame.setZOrder(zorder);
  221. return frame;
  222. }
  223. },
  224. {
  225. name: "ActionValue",
  226. handle: function (options) {
  227. var frame = new ccs.InnerActionFrame();
  228. var innerActionType = options["InnerActionType"];
  229. var currentAnimationFrame = options["CurrentAniamtionName"];
  230. var singleFrameIndex = options["SingleFrameIndex"];
  231. var frameIndex = options["FrameIndex"];
  232. if(frameIndex !== undefined)
  233. frame.setFrameIndex(frameIndex);
  234. frame.setInnerActionType(ccs.InnerActionType[innerActionType]);
  235. frame.setSingleFrameIndex(singleFrameIndex);
  236. frame.setEnterWithName(true);
  237. if (currentAnimationFrame)
  238. frame.setAnimationName(currentAnimationFrame);
  239. return frame;
  240. }
  241. },
  242. {
  243. name: "BlendFunc",
  244. handle: function(options){
  245. var frame = new ccs.BlendFuncFrame();
  246. var blendFunc = options["BlendFunc"];
  247. if(blendFunc && blendFunc["Src"] !== undefined && blendFunc["Dst"] !== undefined)
  248. frame.setBlendFunc(new cc.BlendFunc(blendFunc["Src"], blendFunc["Dst"]));
  249. return frame;
  250. }
  251. }
  252. ];
  253. var loadEasingDataWithFlatBuffers = function(frame, options){
  254. var type = options["Type"];
  255. frame.setTweenType(type);
  256. var points = options["Points"];
  257. var result = [];
  258. if(points){
  259. points.forEach(function(p){
  260. result.push(p["X"]);
  261. result.push(p["Y"]);
  262. });
  263. frame.setEasingParams(result);
  264. }
  265. };
  266. frameList.forEach(function(item){
  267. parser.registerParser(item.name, function(options, resourcePath){
  268. var timeline = new ccs.Timeline();
  269. timeline.setActionTag(options["ActionTag"]);
  270. var frames = options["Frames"];
  271. if(frames && frames.length){
  272. frames.forEach(function(frameData){
  273. var frame = item.handle(frameData, resourcePath);
  274. frame.setFrameIndex(frameData["FrameIndex"]);
  275. var tween = frameData["Tween"] != null ? frameData["Tween"] : true;
  276. frame.setTween(tween);
  277. //https://github.com/cocos2d/cocos2d-x/pull/11388/files
  278. var easingData = frameData["EasingData"];
  279. if(easingData)
  280. loadEasingDataWithFlatBuffers(frame, easingData);
  281. timeline.addFrame(frame);
  282. });
  283. }
  284. return timeline;
  285. });
  286. });
  287. load.registerParser("action", "*", parser);
  288. })(ccs._load, ccs._parser);