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