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