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