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         var localTransform = this.nodeToParentTransform();
 66         var skinTransform = this._skinTransform;
 67         skinTransform.a = localTransform.a;
 68         skinTransform.b = localTransform.b;
 69         skinTransform.c = localTransform.c;
 70         skinTransform.d = localTransform.d;
 71         skinTransform.tx = localTransform.tx;
 72         skinTransform.ty = localTransform.ty;
 73         this.updateArmatureTransform();
 74     },
 75 
 76     getSkinData:function () {
 77         return this._skinData;
 78     },
 79 
 80     setBone:function (bone) {
 81         this._bone = bone;
 82     },
 83 
 84     getBone:function () {
 85         return this._bone;
 86     },
 87 
 88     updateArmatureTransform:function () {
 89         this._transform = cc.AffineTransformConcat(this._skinTransform, this._bone.nodeToArmatureTransform());
 90         var locTransform = this._transform;
 91         var locArmature = this._armature;
 92         if (locArmature && locArmature.getBatchNode()) {
 93             this._transform = cc.AffineTransformConcat(locTransform, locTransform.nodeToParentTransform());
 94         }
 95     },
 96     /** returns a "local" axis aligned bounding box of the node. <br/>
 97      * The returned box is relative only to its parent.
 98      * @return {cc.Rect}
 99      */
100     getBoundingBox:function () {
101         var rect = cc.rect(0, 0, this._contentSize._width, this._contentSize._height);
102         var transForm = this.nodeToParentTransform();
103         return cc.RectApplyAffineTransform(rect, transForm);
104     },
105 
106     /**
107      * display name getter
108      * @returns {String}
109      */
110     getDisplayName:function(){
111         return this._displayName;
112     },
113 
114     nodeToWorldTransform: function () {
115         return cc.AffineTransformConcat(this._transform, this._bone.getArmature().nodeToWorldTransform());
116     },
117 
118 
119     nodeToWorldTransformAR: function () {
120         var displayTransform = this._transform;
121         var anchorPoint = this._anchorPointInPoints;
122 
123         anchorPoint = cc.PointApplyAffineTransform(anchorPoint, displayTransform);
124         displayTransform.tx = anchorPoint.x;
125         displayTransform.ty = anchorPoint.y;
126 
127         return cc.AffineTransformConcat(displayTransform, this._bone.getArmature().nodeToWorldTransform());
128     },
129     /**
130      * update blendType
131      * @param {ccs.BlendType} blendType
132      */
133     updateBlendType: function (blendType) {
134         var blendFunc = this._blend;
135         switch (blendType) {
136             case ccs.BlendType.normal:
137                 blendFunc.src = cc.BLEND_SRC;
138                 blendFunc.dst = cc.BLEND_DST;
139                 break;
140             case ccs.BlendType.add:
141                 blendFunc.src = gl.SRC_ALPHA;
142                 blendFunc.dst = gl.ONE;
143                 break;
144             case ccs.BlendType.multiply:
145                 blendFunc.src = gl.ONE_MINUS_SRC_ALPHA;
146                 blendFunc.dst = gl.ONE_MINUS_DST_COLOR;
147                 break;
148             case ccs.BlendType.screen:
149                 blendFunc.src = gl.ONE;
150                 blendFunc.dst = gl.ONE_MINUS_DST_COLOR;
151                 break;
152             default:
153                 break;
154         }
155         this.setBlendFunc(blendFunc);
156     }
157 });
158 
159 /**
160  * allocates and initializes a skin.
161  * @param {String} fileName
162  * @param {cc.Rect} rect
163  * @returns {ccs.Skin}
164  * @example
165  * // example
166  * var skin = ccs.Skin.create("res/test.png",cc.rect(0,0,50,50));
167  */
168 ccs.Skin.create = function (fileName, rect) {
169     var argnum = arguments.length;
170     var sprite = new ccs.Skin();
171     if (argnum === 0) {
172         if (sprite.init())
173             return sprite;
174     } else {
175         if (sprite && sprite.initWithFile(fileName, rect))
176             return sprite;
177     }
178     return null;
179 };
180 
181 /**
182  * allocates and initializes a skin.
183  * @param {String} pszSpriteFrameName
184  * @returns {ccs.Skin}
185  * @example
186  * // example
187  * var skin = ccs.Skin.createWithSpriteFrameName("test.png");
188  */
189 ccs.Skin.createWithSpriteFrameName = function (pszSpriteFrameName) {
190     var skin = new ccs.Skin();
191     if (skin && skin.initWithSpriteFrameName(pszSpriteFrameName)) {
192         return skin;
193     }
194     return null;
195 };
196