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