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 //DisplayType 126 /** 127 * The Sprite flag of display render type. 128 * @constant 129 * @type Number 130 */ 131 ccs.DISPLAY_TYPE_SPRITE = 0; 132 /** 133 * The Armature flag of display render type. 134 * @constant 135 * @type Number 136 */ 137 ccs.DISPLAY_TYPE_ARMATURE = 1; 138 /** 139 * The Particle flag of display render type. 140 * @constant 141 * @type Number 142 */ 143 ccs.DISPLAY_TYPE_PARTICLE = 2; 144 ccs.DISPLAY_TYPE_MAX = 3; 145 146 /** 147 * <p> 148 * The base data class for Armature. it contains position, zOrder, skew, scale, color datas. <br/> 149 * x y skewX skewY scaleX scaleY used to calculate transform matrix <br/> 150 * skewX, skewY can have rotation effect <br/> 151 * To get more matrix information, you can have a look at this pape : http://www.senocular.com/flash/tutorials/transformmatrix/ <br/> 152 * </p> 153 * @class 154 * @extends ccs.Class 155 * 156 * @property {Number} x - x 157 * @property {Number} y - y 158 * @property {Number} zOrder - zOrder 159 * @property {Number} skewX - skewX 160 * @property {Number} skewY - skewY 161 * @property {Number} scaleX - scaleX 162 * @property {Number} scaleY - scaleY 163 * @property {Number} tweenRotate - tween Rotate 164 * @property {Number} isUseColorInfo - is Use Color Info 165 * @property {Number} r - r of color 166 * @property {Number} g - g of color 167 * @property {Number} b - b of color 168 * @property {Number} a - a of color 169 */ 170 ccs.BaseData = ccs.Class.extend(/** @lends ccs.BaseData# */{ 171 x:0, 172 y:0, 173 zOrder:0, 174 skewX:0, 175 skewY:0, 176 scaleX:1, 177 scaleY:1, 178 tweenRotate:0, //! SkewX, SkewY, and TweenRotate effect the rotation 179 isUseColorInfo:false, //! Whether or not this frame have the color changed Info 180 r:255, 181 g:255, 182 b:255, 183 a:255, 184 185 /** 186 * Construction of ccs.BaseData 187 */ 188 ctor:function () { 189 this.x = 0; 190 this.y = 0; 191 this.zOrder = 0; 192 this.skewX = 0; 193 this.skewY = 0; 194 this.scaleX = 1; 195 this.scaleY = 1; 196 this.tweenRotate = 0; 197 this.isUseColorInfo = false; 198 this.r = 255; 199 this.g = 255; 200 this.b = 255; 201 this.a = 255; 202 }, 203 204 /** 205 * Copy data from node 206 * @function 207 * @param {ccs.BaseData} node 208 */ 209 copy:function (node) { 210 this.x = node.x; 211 this.y = node.y; 212 this.zOrder = node.zOrder; 213 214 this.scaleX = node.scaleX; 215 this.scaleY = node.scaleY; 216 this.skewX = node.skewX; 217 this.skewY = node.skewY; 218 219 this.tweenRotate = node.tweenRotate; 220 221 this.isUseColorInfo = node.isUseColorInfo; 222 this.r = node.r; 223 this.g = node.g; 224 this.b = node.b; 225 this.a = node.a; 226 }, 227 228 /** 229 * Sets color to base data. 230 * @function 231 * @param {cc.Color} color 232 */ 233 setColor:function(color){ 234 this.r = color.r; 235 this.g = color.g; 236 this.b = color.b; 237 this.a = color.a; 238 }, 239 240 /** 241 * Returns the color of ccs.BaseData 242 * @function 243 * @returns {cc.Color} 244 */ 245 getColor:function(){ 246 return cc.color(this.r, this.g, this.b, this.a); 247 }, 248 249 /** 250 * Calculate two baseData's between value(to - from) and set to self 251 * @function 252 * @param {ccs.BaseData} from 253 * @param {ccs.BaseData} to 254 * @param {Boolean} limit 255 */ 256 subtract:function (from, to, limit) { 257 this.x = to.x - from.x; 258 this.y = to.y - from.y; 259 this.scaleX = to.scaleX - from.scaleX; 260 this.scaleY = to.scaleY - from.scaleY; 261 this.skewX = to.skewX - from.skewX; 262 this.skewY = to.skewY - from.skewY; 263 264 if (this.isUseColorInfo || from.isUseColorInfo || to.isUseColorInfo) { 265 this.a = to.a - from.a; 266 this.r = to.r - from.r; 267 this.g = to.g - from.g; 268 this.b = to.b - from.b; 269 this.isUseColorInfo = true; 270 } else { 271 this.a = this.r = this.g = this.b = 0; 272 this.isUseColorInfo = false; 273 } 274 275 if (limit) { 276 if (this.skewX > ccs.M_PI) 277 this.skewX -= ccs.DOUBLE_PI; 278 if (this.skewX < -ccs.M_PI) 279 this.skewX += ccs.DOUBLE_PI; 280 if (this.skewY > ccs.M_PI) 281 this.skewY -= ccs.DOUBLE_PI; 282 if (this.skewY < -ccs.M_PI) 283 this.skewY += ccs.DOUBLE_PI; 284 } 285 286 if (to.tweenRotate) { 287 this.skewX += to.tweenRotate * ccs.PI * 2; 288 this.skewY -= to.tweenRotate * ccs.PI * 2; 289 } 290 } 291 }); 292 293 /** 294 * The class use for save display data. 295 * @class 296 * @extends ccs.Class 297 * 298 * @property {Number} displayType - the display type 299 * @property {String} displayName - the display name 300 */ 301 ccs.DisplayData = ccs.Class.extend(/** @lends ccs.DisplayData# */{ 302 displayType: ccs.DISPLAY_TYPE_MAX, 303 displayName: "", 304 305 /** 306 * Construction of ccs.DisplayData 307 */ 308 ctor: function () { 309 this.displayType = ccs.DISPLAY_TYPE_MAX; 310 }, 311 /** 312 * Changes display name to texture type 313 * @function 314 * @param {String} displayName 315 * @returns {String} 316 */ 317 changeDisplayToTexture:function (displayName) { 318 // remove .xxx 319 var textureName = displayName; 320 var startPos = textureName.lastIndexOf("."); 321 322 if (startPos !== -1) 323 textureName = textureName.substring(0, startPos); 324 return textureName; 325 }, 326 327 /** 328 * copy data 329 * @function 330 * @param {ccs.DisplayData} displayData 331 */ 332 copy:function (displayData) { 333 this.displayName = displayData.displayName; 334 this.displayType = displayData.displayType; 335 } 336 }); 337 338 /** 339 * The sprite display data class. 340 * @class 341 * @extends ccs.DisplayData 342 * 343 * @property {ccs.BaseData} skinData - the skin data 344 */ 345 ccs.SpriteDisplayData = ccs.DisplayData.extend(/** @lends ccs.SpriteDisplayData# */{ 346 skinData:null, 347 348 /** 349 * Construction of ccs.SpriteDisplayData 350 */ 351 ctor:function () { 352 this.skinData = new ccs.BaseData(); 353 this.displayType = ccs.DISPLAY_TYPE_SPRITE; 354 }, 355 /** 356 * copy data 357 * @function 358 * @param {ccs.SpriteDisplayData} displayData 359 */ 360 copy:function (displayData) { 361 ccs.DisplayData.prototype.copy.call(this,displayData); 362 this.skinData = displayData.skinData; 363 } 364 }); 365 366 /** 367 * The armature display data class 368 * @class ccs.ArmatureDisplayData 369 * @extends ccs.DisplayData 370 */ 371 ccs.ArmatureDisplayData = ccs.DisplayData.extend(/** @lends ccs.ArmatureDisplayData# */{ 372 /** 373 * Construction of ccs.ArmatureDisplayData 374 */ 375 ctor:function () { 376 this.displayName = ""; 377 this.displayType = ccs.DISPLAY_TYPE_ARMATURE; 378 } 379 }); 380 381 /** 382 * The particle display data class. 383 * @class ccs.ParticleDisplayData 384 * @extends ccs.DisplayData 385 */ 386 ccs.ParticleDisplayData = ccs.DisplayData.extend(/** @lends ccs.ParticleDisplayData# */{ 387 /** 388 * Construction of ccs.ParticleDisplayData 389 */ 390 ctor:function () { 391 this.displayType = ccs.DISPLAY_TYPE_PARTICLE; 392 } 393 }); 394 395 /** 396 * <p> 397 * BoneData used to init a Bone. <br/> 398 * BoneData keeps a DisplayData list, a Bone can have many display to change. <br/> 399 * The display information saved in the DisplayData <br/> 400 * </p> 401 * @class ccs.BoneData 402 * @extends ccs.BaseData 403 * 404 * @property {Array} displayDataList - the display data list 405 * @property {String} name - the name of Bone 406 * @property {String} parentName - the parent name of bone 407 * @property {cc.AffineTransform} boneDataTransform - the bone transform data 408 */ 409 ccs.BoneData = ccs.BaseData.extend(/** @lends ccs.BoneData# */{ 410 displayDataList: null, 411 name: "", 412 parentName: "", 413 boneDataTransform: null, 414 415 /** 416 * Construction of ccs.BoneData 417 */ 418 ctor: function () { 419 this.displayDataList = []; 420 this.name = ""; 421 this.parentName = ""; 422 this.boneDataTransform = null; 423 }, 424 425 /** 426 * Initializes a ccs.BoneData 427 * @returns {boolean} 428 */ 429 init: function () { 430 this.displayDataList.length = 0; 431 return true; 432 }, 433 /** 434 * Adds display data to list 435 * @function 436 * @param {ccs.DisplayData} displayData 437 */ 438 addDisplayData:function (displayData) { 439 this.displayDataList.push(displayData); 440 }, 441 442 /** 443 * Returns display data with index. 444 * @function 445 * @param {Number} index 446 * @returns {ccs.DisplayData} 447 */ 448 getDisplayData:function (index) { 449 return this.displayDataList[index]; 450 } 451 }); 452 453 /** 454 * <p> 455 * ArmatureData saved the Armature name and BoneData needed for the CCBones in this Armature <br/> 456 * When we create a Armature, we need to get each Bone's BoneData as it's init information. <br/> 457 * So we can get a BoneData from the Dictionary saved in the ArmatureData. <br/> 458 * </p> 459 * @class ccs.ArmatureData 460 * @extends ccs.Class 461 * 462 * @property {Object} boneDataDic - the bone data dictionary 463 * @property {String} name - the name of armature data 464 * @property {Number} dataVersion - the data version of armature data 465 */ 466 ccs.ArmatureData = ccs.Class.extend(/** @lends ccs.ArmatureData# */{ 467 boneDataDic:null, 468 name:"", 469 dataVersion:0.1, 470 471 /** 472 * Construction of ccs.ArmatureData 473 */ 474 ctor:function () { 475 this.boneDataDic = {}; 476 this.name = ""; 477 this.dataVersion = 0.1; 478 }, 479 480 /** 481 * Initializes a ccs.ArmatureData 482 * @returns {boolean} 483 */ 484 init:function () { 485 return true; 486 }, 487 488 /** 489 * Adds bone data to dictionary 490 * @param {ccs.BoneData} boneData 491 */ 492 addBoneData:function (boneData) { 493 this.boneDataDic[boneData.name] = boneData; 494 }, 495 496 /** 497 * Gets bone data dictionary 498 * @returns {Object} 499 */ 500 getBoneDataDic:function () { 501 return this.boneDataDic; 502 }, 503 /** 504 * Gets bone data by bone name 505 * @function 506 * @param {String} boneName 507 * @returns {ccs.BoneData} 508 */ 509 getBoneData:function (boneName) { 510 return this.boneDataDic[boneName]; 511 } 512 }); 513 514 /** 515 * FrameData saved the frame data needed for armature animation in this Armature. 516 * @class ccs.FrameData 517 * @extends ccs.BaseData 518 * 519 * @property {Number} duration - the duration of frame 520 * @property {Number} tweenEasing - the easing type of frame 521 * @property {Number} easingParamNumber - the count of easing parameters. 522 * @property {Object} easingParams - the dictionary of easing parameters. 523 * @property {Number} displayIndex - the display renderer index. 524 * @property {String} movement - the movement name. 525 * @property {String} event - the event name 526 * @property {String} sound - the sound path. 527 * @property {String} soundEffect - the sound effect path. 528 * @property {Object} blendFunc - the blendFunc of frame. 529 * @property {Number} frameID - the frame ID of frame 530 * @property {Boolean} isTween - the flag which frame whether is tween. 531 */ 532 ccs.FrameData = ccs.BaseData.extend(/** @lends ccs.FrameData# */{ 533 duration:0, 534 tweenEasing:0, 535 easingParamNumber: 0, 536 easingParams: null, 537 displayIndex:-1, 538 movement:"", 539 event:"", 540 sound:"", 541 soundEffect:"", 542 blendFunc:null, 543 frameID:0, 544 isTween:true, 545 546 /** 547 * Construction of ccs.FrameData. 548 */ 549 ctor:function () { 550 ccs.BaseData.prototype.ctor.call(this); 551 this.duration = 1; 552 this.tweenEasing = ccs.TweenType.LINEAR; 553 this.easingParamNumber = 0; 554 this.easingParams = []; 555 this.displayIndex = 0; 556 this.movement = ""; 557 this.event = ""; 558 this.sound = ""; 559 this.soundEffect = ""; 560 this.blendFunc = new cc.BlendFunc(cc.BLEND_SRC, cc.BLEND_DST); 561 this.frameID = 0; 562 this.isTween = true; 563 }, 564 565 /** 566 * copy data 567 * @function 568 * @param frameData 569 */ 570 copy:function (frameData) { 571 ccs.BaseData.prototype.copy.call(this, frameData); 572 this.duration = frameData.duration; 573 this.displayIndex = frameData.displayIndex; 574 575 this.tweenEasing = frameData.tweenEasing; 576 this.easingParamNumber = frameData.easingParamNumber; 577 578 // this.movement = frameData.movement; 579 // this.event = frameData.event; 580 // this.sound = frameData.sound; 581 // this.soundEffect = frameData.soundEffect; 582 // this.easingParams.length = 0; 583 if (this.easingParamNumber !== 0){ 584 this.easingParams.length = 0; 585 for (var i = 0; i<this.easingParamNumber; i++){ 586 this.easingParams[i] = frameData.easingParams[i]; 587 } 588 } 589 this.blendFunc = frameData.blendFunc; 590 this.isTween = frameData.isTween; 591 592 } 593 } 594 ); 595 596 /** 597 * MovementBoneData saved the name, delay, frame list of Bone's movement. 598 * @class ccs.MovementBoneData 599 * @extends ccs.Class 600 * 601 * @property {Number} delay - the delay of bone's movement. 602 * @property {Number} scale - the scale of bone's movement. 603 * @property {Number} duration - the duration of bone's movement. 604 * @property {Array} frameList - the frame list of bone's movement. 605 * @property {String} name - the name of bone's movement. 606 */ 607 ccs.MovementBoneData = ccs.Class.extend(/** @lends ccs.MovementBoneData# */{ 608 delay:0, 609 scale:1, 610 duration:0, 611 frameList:null, 612 name:"", 613 614 /** 615 * Construction of ccs.MovementBoneData. 616 */ 617 ctor:function () { 618 this.delay = 0; 619 this.scale = 1; 620 this.duration = 0; 621 this.frameList = []; 622 this.name = ""; 623 }, 624 625 /** 626 * Initializes a ccs.MovementBoneData. 627 * @returns {boolean} 628 */ 629 init:function () { 630 return true; 631 }, 632 /** 633 * Adds frame data to frame list. 634 * @param {ccs.FrameData} frameData 635 */ 636 addFrameData:function (frameData) { 637 this.frameList.push(frameData); 638 }, 639 /** 640 * Gets frame data by Index. 641 * @function 642 * @param {Number} index 643 * @returns {ccs.FrameData} 644 */ 645 getFrameData:function (index) { 646 return this.frameList[index]; 647 } 648 }); 649 650 /** 651 * The movement data information of Cocos Armature. 652 * @class ccs.MovementData 653 * @constructor 654 */ 655 ccs.MovementData = function(){ 656 this.name = ""; 657 this.duration = 0; 658 this.scale = 1; 659 /** 660 * Change to this movement will last durationTo frames. Use this effect can avoid too suddenly changing. 661 * 662 * Example : current movement is "stand", we want to change to "run", then we fill durationTo frames before 663 * change to "run" instead of changing to "run" directly. 664 */ 665 this.durationTo = 0; 666 /** 667 * This is different from duration, durationTween contain tween effect. 668 * duration is the raw time that the animation will last, it's the same with the time you edit in the Action Editor. 669 * durationTween is the actual time you want this animation last. 670 * Example : If we edit 10 frames in the flash, then duration is 10. When we set durationTween to 50, the movement will last 50 frames, the extra 40 frames will auto filled with tween effect 671 */ 672 this.durationTween = 0; 673 this.loop = true; //! whether the movement was looped 674 /** 675 * Which tween easing effect the movement use 676 * TWEEN_EASING_MAX : use the value from MovementData get from flash design panel 677 */ 678 this.tweenEasing = ccs.TweenType.LINEAR; 679 this.movBoneDataDic = {}; 680 }; 681 682 /** 683 * add a movement bone data to dictionary 684 * @param {ccs.MovementBoneData} movBoneData 685 */ 686 ccs.MovementData.prototype.addMovementBoneData = function(movBoneData){ 687 this.movBoneDataDic[ movBoneData.name] = movBoneData; 688 }; 689 690 /** 691 * add a movement bone data from dictionary by name 692 * @param boneName 693 * @returns {ccs.MovementBoneData} 694 */ 695 ccs.MovementData.prototype.getMovementBoneData = function(boneName){ 696 return this.movBoneDataDic[boneName]; 697 }; 698 699 /** 700 * <p> 701 * The animation data information of Cocos Armature. It include all movement information for the Armature. <br/> 702 * The struct is AnimationData -> MovementData -> MovementBoneData -> FrameData <br/> 703 * -> MovementFrameData <br/> 704 * </p> 705 * @class ccs.AnimationData 706 * @extends ccs.Class 707 */ 708 ccs.AnimationData = function(){ 709 this.movementDataDic = {}; 710 this.movementNames = []; 711 this.name = ""; 712 }; 713 714 /** 715 * adds movement data to the movement data dictionary 716 * @param {ccs.MovementData} moveData 717 */ 718 ccs.AnimationData.prototype.addMovement = function(moveData){ 719 this.movementDataDic[moveData.name] = moveData; 720 this.movementNames.push(moveData.name); 721 }; 722 723 /** 724 * gets movement data from movement data dictionary 725 * @param {String} moveName 726 * @returns {ccs.MovementData} 727 */ 728 ccs.AnimationData.prototype.getMovement = function(moveName){ 729 return this.movementDataDic[moveName]; 730 }; 731 732 /** 733 * gets the count of movement data dictionary 734 * @returns {Number} 735 */ 736 ccs.AnimationData.prototype.getMovementCount = function(){ 737 return Object.keys(this.movementDataDic).length; 738 }; 739 740 /** 741 * contour vertex 742 * @class ccs.ContourVertex2 743 * @param {Number} x 744 * @param {Number} y 745 * @constructor 746 */ 747 ccs.ContourVertex2 = function (x, y) { 748 this.x = x || 0; 749 this.y = y || 0; 750 }; 751 752 /** 753 * The Contour data information of Cocos Armature. 754 * @class ccs.ContourData 755 * @constructor 756 */ 757 ccs.ContourData = function(){ 758 this.vertexList = []; 759 }; 760 761 ccs.ContourData.prototype.init = function(){ 762 this.vertexList.length = 0; 763 return true; 764 }; 765 766 /** 767 * add a vertex object to vertex list 768 * @param {cc.Point} p 769 */ 770 ccs.ContourData.prototype.addVertex = function(p){ 771 //var v = new ccs.ContourVertex2(p.x, p.y); //ccs.ContourVertex2 is same as cc.Point, so we needn't create a ccs.ContourVertex2 object 772 this.vertexList.push(p); 773 }; 774 775 /** 776 * The texture data information of Cocos Armature 777 * @class ccs.TextureData 778 */ 779 ccs.TextureData = function(){ 780 this.height = 0; 781 this.width = 0; 782 this.pivotX = 0.5; 783 this.pivotY = 0.5; 784 this.name = ""; 785 this.contourDataList = []; 786 }; 787 788 ccs.TextureData.prototype.init = function(){ 789 this.contourDataList.length = 0; 790 }; 791 792 /** 793 * Adds a contourData to contourDataList 794 * @param {ccs.ContourData} contourData 795 */ 796 ccs.TextureData.prototype.addContourData = function(contourData){ 797 this.contourDataList.push(contourData); 798 }; 799 800 /** 801 * gets a contourData from contourDataList by index 802 * @param {Number} index 803 * @returns {ccs.ContourData} 804 */ 805 ccs.TextureData.prototype.getContourData = function(index){ 806 return this.contourDataList[index]; 807 }; 808