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