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