1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3 
  4  http://www.cocos2d-x.org
  5 
  6  Permission is hereby granted, free of charge, to any person obtaining a copy
  7  of this software and associated documentation files (the "Software"), to deal
  8  in the Software without restriction, including without limitation the rights
  9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  copies of the Software, and to permit persons to whom the Software is
 11  furnished to do so, subject to the following conditions:
 12 
 13  The above copyright notice and this permission notice shall be included in
 14  all copies or substantial portions of the Software.
 15 
 16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22  THE SOFTWARE.
 23  ****************************************************************************/
 24 
 25 /**
 26  * BlendType
 27  * @type Object
 28  */
 29 ccs.BlendType = {
 30     normal: 0,
 31     layer: 1,
 32     darken: 2,
 33     multiply: 3,
 34     lighten: 4,
 35     screen: 5,
 36     overlay: 6,
 37     highlight: 7,
 38     add: 8,
 39     subtract: 9,
 40     difference: 10,
 41     invert: 11,
 42     alpha: 12,
 43     erase: 13
 44 };
 45 /**
 46  * DisplayType
 47  * @type Object
 48  */
 49 ccs.DisplayType = {
 50     sprite: 0,
 51     armature: 1,
 52     particle: 2,
 53     max: 3
 54 };
 55 
 56 /**
 57  * Base class for ccs.BaseData objects.
 58  * @class
 59  * @extends ccs.Class
 60  */
 61 ccs.BaseData = ccs.Class.extend(/** @lends ccs.BaseData# */{
 62     x:0,
 63     y:0,
 64     zOrder:0,
 65     skewX:0,
 66     skewY:0,
 67     scaleX:1,
 68     scaleY:1,
 69     tweenRotate:0,
 70     isUseColorInfo:false,
 71     r:255,
 72     g:255,
 73     b:255,
 74     a:255,
 75 
 76     ctor:function () {
 77         this.x = 0;
 78         this.y = 0;
 79         this.zOrder = 0;
 80         this.skewX = 0;
 81         this.skewY = 0;
 82         this.scaleX = 1;
 83         this.scaleY = 1;
 84         this.tweenRotate = 0;
 85         this.isUseColorInfo = false;
 86         this.color = cc.c4f(1, 1, 1, 1);
 87     },
 88 
 89 
 90     /**
 91      * Copy data from node
 92      * @param {ccs.BaseData} node
 93      */
 94     copy:function (node) {
 95         this.x = node.x;
 96         this.y = node.y;
 97         this.zOrder = node.zOrder;
 98         this.scaleX = node.scaleX;
 99         this.scaleY = node.scaleY;
100         this.skewX = node.skewX;
101         this.skewY = node.skewY;
102         this.tweenRotate = node.tweenRotate;
103         this.isUseColorInfo = node.isUseColorInfo;
104         this.r = node.r;
105         this.g = node.g;
106         this.b = node.b;
107         this.a = node.a;
108     },
109 
110     /**
111      * color setter
112      * @param {cc.Color4B} color
113      */
114     setColor:function(color){
115         this.r = color.r;
116         this.g = color.g;
117         this.b = color.b;
118         this.a = color.a;
119     },
120 
121     /**
122      * color getter
123      * @returns {cc.Color4B}
124      */
125     getColor:function(){
126         return cc.c4b(this.r, this.g, this.b, this.a);
127     },
128 
129     /**
130      * Calculate two baseData's between value(to - from) and set to self
131      * @param {ccs.BaseData} from
132      * @param {ccs.BaseData} to
133      * @param {Boolean} limit
134      */
135     subtract:function (from, to, limit) {
136         this.x = to.x - from.x;
137         this.y = to.y - from.y;
138         this.scaleX = to.scaleX - from.scaleX;
139         this.scaleY = to.scaleY - from.scaleY;
140         this.skewX = to.skewX - from.skewX;
141         this.skewY = to.skewY - from.skewY;
142 
143         if (this.isUseColorInfo || from.isUseColorInfo || to.isUseColorInfo) {
144             this.a = to.a - from.a;
145             this.r = to.r - from.r;
146             this.g = to.g - from.g;
147             this.b = to.b - from.b;
148             this.isUseColorInfo = true;
149         } else {
150             this.a = this.r = this.g = this.b = 0;
151             this.isUseColorInfo = false;
152         }
153 
154         if (limit) {
155             if (this.skewX > cc.PI) {
156                 this.skewX -= ccs.M_PI_X_2;
157             }
158             if (this.skewX < -cc.PI) {
159                 this.skewX += ccs.M_PI_X_2;
160             }
161             if (this.skewY > cc.PI) {
162                 this.skewY -= ccs.M_PI_X_2;
163             }
164             if (this.skewY < -cc.PI) {
165                 this.skewY += ccs.M_PI_X_2;
166             }
167         }
168 
169         if (to.tweenRotate) {
170             this.skewX += to.tweenRotate * ccs.M_PI_X_2;
171             this.skewY -= to.tweenRotate * ccs.M_PI_X_2;
172         }
173     }
174 });
175 
176 /**
177  * Base class for ccs.DisplayData objects.
178  * @class
179  * @extends ccs.Class
180  */
181 ccs.DisplayData = ccs.Class.extend(/** @lends ccs.DisplayData# */{
182     displayType:ccs.DisplayType.max,
183     displayName:"",
184     ctor:function () {
185         this.displayType = ccs.DisplayType.max;
186     },
187     /**
188      * change display name to texture type
189      * @param {String} displayName
190      * @returns {String}
191      */
192     changeDisplayToTexture:function (displayName) {
193         // remove .xxx
194         var textureName = displayName;
195         var startPos = textureName.lastIndexOf(".");
196 
197         if (startPos != -1) {
198             textureName = textureName.substring(0, startPos);
199         }
200         return textureName;
201     },
202     /**
203      * copy data
204      * @param {ccs.DisplayData} displayData
205      */
206     copy:function (displayData) {
207         this.displayName = displayData.displayName;
208         this.displayType = displayData.displayType;
209     }
210 });
211 
212 /**
213  * Base class for ccs.SpriteDisplayData objects.
214  * @class
215  * @extends ccs.DisplayData
216  */
217 ccs.SpriteDisplayData = ccs.DisplayData.extend(/** @lends ccs.SpriteDisplayData# */{
218     skinData:null,
219     ctor:function () {
220         this.skinData = new ccs.BaseData();
221         this.displayType = ccs.DisplayType.sprite;
222     },
223     /**
224      * copy data
225      * @param {ccs.SpriteDisplayData} displayData
226      */
227     copy:function (displayData) {
228         ccs.DisplayData.prototype.copy.call(this,displayData);
229         this.skinData = displayData.skinData;
230     }
231 });
232 
233 /**
234  * Base class for ccs.ArmatureDisplayData objects.
235  * @class
236  * @extends ccs.DisplayData
237  */
238 ccs.ArmatureDisplayData = ccs.DisplayData.extend(/** @lends ccs.ArmatureDisplayData# */{
239     displayName:"",
240     ctor:function () {
241         this.displayName = "";
242         this.displayType = ccs.DisplayType.armature;
243     }
244 });
245 
246 /**
247  * Base class for ccs.ParticleDisplayData objects.
248  * @class
249  * @extends ccs.DisplayData
250  */
251 ccs.ParticleDisplayData = ccs.DisplayData.extend(/** @lends ccs.ParticleDisplayData# */{
252     ctor:function () {
253         this.displayType = ccs.DisplayType.particle;
254     }
255 });
256 
257 /**
258  * Base class for ccs.BoneData objects.
259  * @class
260  * @extends ccs.BaseData
261  */
262 ccs.BoneData = ccs.BaseData.extend(/** @lends ccs.BoneData# */{
263     displayDataList:null,
264     name:"",
265     parentName:"",
266     boneDataTransform:null,
267     ctor:function () {
268         this.displayDataList = [];
269         this.name = "";
270         this.parentName = "";
271         this.boneDataTransform = null;
272 
273     },
274     init:function () {
275 
276     },
277     /**
278      * add display data
279      * @param {ccs.DisplayData} displayData
280      */
281     addDisplayData:function (displayData) {
282         this.displayDataList.push(displayData);
283     },
284 
285     /**
286      * get display data
287      * @param {Number} index
288      * @returns {ccs.DisplayData}
289      */
290     getDisplayData:function (index) {
291         return this.displayDataList[index];
292     }
293 });
294 
295 /**
296  * Base class for ccs.ArmatureData objects.
297  * @class
298  * @extends ccs.Class
299  */
300 ccs.ArmatureData = ccs.Class.extend(/** @lends ccs.ArmatureData# */{
301     boneDataDic:null,
302     name:"",
303     dataVersion:0.1,
304     ctor:function () {
305         this.boneDataDic = {};
306         this.name = "";
307         this.dataVersion = 0.1;
308     },
309     init:function () {
310         return true;
311     },
312     /**
313      * add bone data
314      * @param {ccs.BoneData} boneData
315      */
316     addBoneData:function (boneData) {
317         this.boneDataDic[boneData.name] = boneData;
318     },
319     /**
320      * get bone datas
321      * @returns {Object}
322      */
323     getBoneDataDic:function () {
324         return this.boneDataDic;
325     },
326     /**
327      * get bone data by bone name
328      * @param {String} boneName
329      * @returns {ccs.BoneData}
330      */
331     getBoneData:function (boneName) {
332         return this.boneDataDic[boneName];
333     }
334 });
335 
336 /**
337  * Base class for ccs.FrameData objects.
338  * @class
339  * @extends ccs.BaseData
340  */
341 ccs.FrameData = ccs.BaseData.extend(/** @lends ccs.FrameData# */{
342         duration:0,
343         tweenEasing:0,
344         easingParamNumber: 0,
345         easingParams: null,
346         displayIndex:-1,
347         movement:"",
348         event:"",
349         sound:"",
350         soundEffect:"",
351         blendFunc:0,
352         frameID:0,
353         isTween:true,
354         ctor:function () {
355             ccs.BaseData.prototype.ctor.call(this);
356             this.duration = 1;
357             this.tweenEasing = ccs.TweenType.linear;
358             this.easingParamNumber = 0;
359             this.easingParams = [];
360             this.displayIndex = 0;
361             this.movement = "";
362             this.event = "";
363             this.sound = "";
364             this.soundEffect = "";
365             this.blendFunc = new cc.BlendFunc(cc.BLEND_SRC, cc.BLEND_DST);
366             this.frameID = 0;
367             this.isTween = true;
368         },
369 
370         /**
371          * copy data
372          * @param frameData
373          */
374         copy:function (frameData) {
375             ccs.BaseData.prototype.copy.call(this, frameData);
376             this.duration = frameData.duration;
377             this.tweenEasing = frameData.tweenEasing;
378             this.displayIndex = frameData.displayIndex;
379             this.movement = frameData.movement;
380             this.event = frameData.event;
381             this.sound = frameData.sound;
382             this.soundEffect = frameData.soundEffect;
383             this.blendFunc = frameData.blendFunc;
384             this.isTween = frameData.isTween;
385 
386             this.easingParamNumber = frameData.easingParamNumber;
387             this.easingParams = [];
388             if (this.easingParamNumber != 0)            {
389                 for (var i = 0; i<this.easingParamNumber; i++)                {
390                     this.easingParams[i] = frameData.easingParams[i];
391                 }
392             }
393         }
394     }
395 );
396 
397 /**
398  * Base class for ccs.MovementBoneData objects.
399  * @class
400  * @extends ccs.Class
401  */
402 ccs.MovementBoneData = ccs.Class.extend(/** @lends ccs.MovementBoneData# */{
403     delay:0,
404     scale:1,
405     duration:0,
406     frameList:null,
407     name:"",
408     ctor:function () {
409         this.delay = 0;
410         this.scale = 1;
411         this.duration = 0;
412         this.frameList = [];
413         this.name = "";
414     },
415     init:function () {
416         this.frameList = [];
417     },
418     /**
419      * add frame data
420      * @param {ccs.FrameData} frameData
421      */
422     addFrameData:function (frameData) {
423         this.frameList.push(frameData);
424     },
425     /**
426      * get frame data
427      * @param {Number} index
428      * @returns {ccs.FrameData}
429      */
430     getFrameData:function (index) {
431         return this.frameList[index];
432     }
433 });
434 
435 /**
436  * Base class for ccs.MovementData objects.
437  * @class
438  * @extends ccs.Class
439  */
440 ccs.MovementData = ccs.Class.extend(/** @lends ccs.MovementData# */{
441     movBoneDataDic:null,
442     duration:0,
443     scale:1,
444     durationTo:0,
445     durationTween:ccs.TweenType.linear,
446     loop:true,
447     tweenEasing:2,
448     name:"",
449     ctor:function () {
450         this.name = "";
451         this.duration = 0;
452         this.scale = 1;
453         this.durationTo = 0;
454         this.durationTween = 0;
455         this.loop = true;
456         this.tweenEasing = ccs.TweenType.linear;
457         this.movBoneDataDic = {};
458     },
459 
460     /**
461      * add movement bone data
462      * @param {ccs.MovementBoneData} movBoneData
463      */
464     addMovementBoneData:function (movBoneData) {
465         this.movBoneDataDic[ movBoneData.name] = movBoneData;
466     },
467 
468     /**
469      * get movement bone data
470      * @param {String} boneName
471      * @returns {ccs.MovementBoneData}
472      */
473     getMovementBoneData:function (boneName) {
474         return  this.movBoneDataDic[boneName];
475     }
476 });
477 
478 /**
479  * Base class for ccs.AnimationData objects.
480  * @class
481  * @extends ccs.Class
482  */
483 ccs.AnimationData = ccs.Class.extend(/** @lends ccs.AnimationData# */{
484     moveDataDic:null,
485     movementNames:null,
486     name:"",
487     ctor:function () {
488         this.moveDataDic = {};
489         this.movementNames = [];
490     },
491     /**
492      * add movement data
493      * @param {ccs.MovementData} moveData
494      */
495     addMovement:function (moveData) {
496         this.moveDataDic[moveData.name] = moveData;
497         this.movementNames.push(moveData.name);
498     },
499     /**
500      * get movement data
501      * @param {String} moveName
502      * @returns {ccs.MovementData}
503      */
504     getMovement:function (moveName) {
505         return this.moveDataDic[moveName];
506     },
507     /**
508      *
509      * @returns {Number}
510      */
511     getMovementCount:function () {
512         return Object.keys(this.moveDataDic).length;
513     }
514 });
515 
516 /**
517  * contour vertex
518  * @param {Number} x
519  * @param {Number} y
520  * @constructor
521  */
522 ccs.ContourVertex2 = function (x, y) {
523     this.x = x || 0;
524     this.y = y || 0;
525 };
526 
527 /**
528  * Base class for ccs.ContourData objects.
529  * @class
530  * @extends ccs.Class
531  */
532 ccs.ContourData = ccs.Class.extend({
533     vertexList:null,
534     ctor:function () {
535         this.vertexList = [];
536     },
537 
538     init:function () {
539         this.vertexList = [];
540         return true;
541     },
542 
543     /**
544      * add vertex
545      * @param {cc.Point} p
546      */
547     addVertex: function (p) {
548        var v = ccs.ContourVertex2(p.x, p.y);
549        this.vertexList.push(v);
550     }
551 });
552 
553 /**
554  * Base class for ccs.TextureData objects.
555  * @class
556  * @extends ccs.Class
557  */
558 ccs.TextureData = ccs.Class.extend(/** @lends ccs.TextureData# */{
559     height:0,
560     width:0,
561     pivotX:0,
562     pivotY:0,
563     name:"",
564     contourDataList:null,
565     ctor:function () {
566         this.height = 0;
567         this.width = 0;
568         this.pivotX = 0.5;
569         this.pivotY = 0.5;
570         this.name = "";
571         this.contourDataList = [];
572     },
573 
574     init:function () {
575         this.contourDataList = [];
576     },
577 
578     /**
579      * set contourData
580      * @param {ccs.ContourData} contourData
581      */
582     addContourData:function (contourData) {
583         this.contourDataList.push(contourData);
584     },
585 
586     /**
587      * get contourData
588      * @param {Number} index
589      * @returns {ccs.ContourData}
590      */
591     getContourData:function (index) {
592         return this.contourDataList[index];
593     }
594 });
595