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 /** 27 * Base class for ccs.Skin 28 * @class 29 * @extends ccs.Sprite 30 * 31 * @property {Object} skinData - The data of the skin 32 * @property {ccs.Bone} bone - The bone of the skin 33 * @property {String} displayName - <@readonly> The displayed name of skin 34 * 35 */ 36 ccs.Skin = ccs.Sprite.extend(/** @lends ccs.Skin# */{ 37 _skinData: null, 38 bone: null, 39 _skinTransform: null, 40 _displayName: "", 41 _armature: null, 42 _className: "Skin", 43 ctor: function () { 44 cc.Sprite.prototype.ctor.call(this); 45 this._skinData = null; 46 this.bone = null; 47 this._displayName = ""; 48 this._skinTransform = cc.AffineTransformIdentity(); 49 this._armature = null; 50 }, 51 initWithSpriteFrameName: function (spriteFrameName) { 52 var ret = cc.Sprite.prototype.initWithSpriteFrameName.call(this, spriteFrameName); 53 this._displayName = spriteFrameName; 54 return ret; 55 }, 56 initWithFile: function (fileName) { 57 var ret = cc.Sprite.prototype.initWithFile.call(this, fileName); 58 this._displayName = fileName; 59 return ret; 60 }, 61 setSkinData: function (skinData) { 62 this._skinData = skinData; 63 64 this.setScaleX(skinData.scaleX); 65 this.setScaleY(skinData.scaleY); 66 this.setRotationX(cc.radiansToDegrees(skinData.skewX)); 67 this.setRotationY(cc.radiansToDegrees(-skinData.skewY)); 68 this.setPosition(skinData.x, skinData.y); 69 70 var localTransform = this.nodeToParentTransform(); 71 var skinTransform = this._skinTransform; 72 skinTransform.a = localTransform.a; 73 skinTransform.b = localTransform.b; 74 skinTransform.c = localTransform.c; 75 skinTransform.d = localTransform.d; 76 skinTransform.tx = localTransform.tx; 77 skinTransform.ty = localTransform.ty; 78 this.updateArmatureTransform(); 79 }, 80 81 getSkinData: function () { 82 return this._skinData; 83 }, 84 85 setBone: function (bone) { 86 this.bone = bone; 87 }, 88 89 getBone: function () { 90 return this.bone; 91 }, 92 93 updateArmatureTransform: function () { 94 this._transform = cc.AffineTransformConcat(this._skinTransform, this.bone.nodeToArmatureTransform()); 95 var locTransform = this._transform; 96 var locArmature = this._armature; 97 if (locArmature && locArmature.getBatchNode()) { 98 this._transform = cc.AffineTransformConcat(locTransform, locArmature.nodeToParentTransform()); 99 } 100 if (cc._renderType === cc._RENDER_TYPE_CANVAS) { 101 locTransform = this._transform 102 locTransform.b *= -1; 103 locTransform.c *= -1; 104 var tempB = locTransform.b; 105 locTransform.b = locTransform.c; 106 locTransform.c = tempB; 107 } 108 }, 109 /** returns a "local" axis aligned bounding box of the node. <br/> 110 * The returned box is relative only to its parent. 111 * @return {cc.Rect} 112 */ 113 getBoundingBox: function () { 114 var rect = cc.rect(0, 0, this._contentSize.width, this._contentSize.height); 115 var transForm = this.nodeToParentTransform(); 116 return cc.RectApplyAffineTransform(rect, transForm); 117 }, 118 119 /** 120 * display name getter 121 * @returns {String} 122 */ 123 getDisplayName: function () { 124 return this._displayName; 125 }, 126 127 nodeToWorldTransform: function () { 128 return cc.AffineTransformConcat(this._transform, this.bone.getArmature().nodeToWorldTransform()); 129 }, 130 131 132 nodeToWorldTransformAR: function () { 133 var displayTransform = this._transform; 134 var anchorPoint = this._anchorPointInPoints; 135 136 anchorPoint = cc.PointApplyAffineTransform(anchorPoint, displayTransform); 137 displayTransform.tx = anchorPoint.x; 138 displayTransform.ty = anchorPoint.y; 139 140 return cc.AffineTransformConcat(displayTransform, this.bone.getArmature().nodeToWorldTransform()); 141 } 142 }); 143 ccs.Skin.prototype.nodeToParentTransform = cc.Node.prototype._nodeToParentTransformForWebGL; 144 145 var _p = ccs.Skin.prototype; 146 147 // Extended properties 148 /** @expose */ 149 _p.skinData; 150 cc.defineGetterSetter(_p, "skinData", _p.getSkinData, _p.setSkinData); 151 /** @expose */ 152 _p.displayName; 153 cc.defineGetterSetter(_p, "displayName", _p.getDisplayName); 154 155 _p = null; 156 157 /** 158 * allocates and initializes a skin. 159 * @param {String} fileName 160 * @param {cc.Rect} rect 161 * @returns {ccs.Skin} 162 * @example 163 * // example 164 * var skin = ccs.Skin.create("res/test.png",cc.rect(0,0,50,50)); 165 */ 166 ccs.Skin.create = function (fileName, rect) { 167 var argnum = arguments.length; 168 var sprite = new ccs.Skin(); 169 if (argnum === 0) { 170 if (sprite.init()) 171 return sprite; 172 } else { 173 if (sprite && sprite.initWithFile(fileName, rect)) 174 return sprite; 175 } 176 return null; 177 }; 178 179 /** 180 * allocates and initializes a skin. 181 * @param {String} pszSpriteFrameName 182 * @returns {ccs.Skin} 183 * @example 184 * // example 185 * var skin = ccs.Skin.createWithSpriteFrameName("test.png"); 186 */ 187 ccs.Skin.createWithSpriteFrameName = function (pszSpriteFrameName) { 188 var skin = new ccs.Skin(); 189 if (skin && skin.initWithSpriteFrameName(pszSpriteFrameName)) { 190 return skin; 191 } 192 return null; 193 }; 194