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  * ccs.Bone uses ccs.Skin to displays on screen.
 28  * @class
 29  * @extends ccs.Sprite
 30  *
 31  * @param {String} [fileName]
 32  * @param {cc.Rect} [rect]
 33  *
 34  * @property {Object}   skinData    - The data of the skin
 35  * @property {ccs.Bone} bone        - The bone of the skin
 36  * @property {String}   displayName - <@readonly> The displayed name of skin
 37  *
 38  */
 39 ccs.Skin = ccs.Sprite.extend(/** @lends ccs.Skin# */{
 40     _skinData: null,
 41     bone: null,
 42     _skinTransform: null,
 43     _displayName: "",
 44     _armature: null,
 45     _className: "Skin",
 46 
 47     ctor: function (fileName, rect) {
 48         cc.Sprite.prototype.ctor.call(this);
 49         this._skinData = null;
 50         this.bone = null;
 51         this._displayName = "";
 52         this._skinTransform = cc.affineTransformIdentity();
 53         this._armature = null;
 54 
 55         if (fileName == null || fileName === "") {
 56             ccs.Skin.prototype.init.call(this);
 57         } else {
 58             if(fileName[0] === "#"){
 59                 ccs.Skin.prototype.initWithSpriteFrameName.call(this, fileName.substr(1));
 60             } else {
 61                 ccs.Skin.prototype.initWithFile.call(this, fileName, rect);
 62             }
 63         }
 64     },
 65 
 66     /**
 67      * Initializes with sprite frame name
 68      * @param {String} spriteFrameName
 69      * @returns {Boolean}
 70      */
 71     initWithSpriteFrameName: function (spriteFrameName) {
 72         if(spriteFrameName === "")
 73             return false;
 74         var pFrame = cc.spriteFrameCache.getSpriteFrame(spriteFrameName);
 75         var ret = true;
 76         if(pFrame)
 77             this.initWithSpriteFrame(pFrame);
 78         else{
 79             cc.log("Can't find CCSpriteFrame with %s. Please check your .plist file", spriteFrameName);
 80             ret = false;
 81         }
 82         this._displayName = spriteFrameName;
 83         return ret;
 84     },
 85 
 86     /**
 87      * Initializes with texture file name.
 88      * @param {String} fileName
 89      * @param {cc.Rect} rect
 90      * @returns {Boolean}
 91      */
 92     initWithFile: function (fileName, rect) {
 93         var ret = rect ? cc.Sprite.prototype.initWithFile.call(this, fileName, rect)
 94                        : cc.Sprite.prototype.initWithFile.call(this, fileName);
 95         this._displayName = fileName;
 96         return ret;
 97     },
 98 
 99     /**
100      * Sets skin data to ccs.Skin.
101      * @param {ccs.BaseData} skinData
102      */
103     setSkinData: function (skinData) {
104         this._skinData = skinData;
105         this.setScaleX(skinData.scaleX);
106         this.setScaleY(skinData.scaleY);
107         this.setRotationX(cc.radiansToDegrees(skinData.skewX));
108         this.setRotationY(cc.radiansToDegrees(-skinData.skewY));
109         this.setPosition(skinData.x, skinData.y);
110 
111         var localTransform = this.getNodeToParentTransform ? this.getNodeToParentTransform() : this.nodeToParentTransform();
112         var skinTransform = this._skinTransform;
113         skinTransform.a = localTransform.a;
114         skinTransform.b = localTransform.b;
115         skinTransform.c = localTransform.c;
116         skinTransform.d = localTransform.d;
117         skinTransform.tx = localTransform.tx;
118         skinTransform.ty = localTransform.ty;
119         this.updateArmatureTransform();
120     },
121 
122     /**
123      * Returns skin date of ccs.Skin.
124      * @returns {ccs.BaseData}
125      */
126     getSkinData: function () {
127         return this._skinData;
128     },
129 
130     /**
131      * Updates armature skin's transform with skin transform and bone's transform.
132      */
133     updateArmatureTransform: function () {
134         this._renderCmd.updateArmatureTransform();
135     },
136 
137     /**
138      * Returns skin's world transform.
139      * @returns {cc.AffineTransform}
140      */
141     getNodeToWorldTransform: function(){
142         return this._renderCmd.getNodeToWorldTransform();
143     },
144 
145     getNodeToWorldTransformAR: function(){
146         return this._renderCmd.getNodeToWorldTransformAR();
147     },
148 
149     /**
150      * Sets the bone reference to ccs.Skin.
151      * @param bone
152      */
153     setBone: function (bone) {
154         this.bone = bone;
155         var armature = this.bone.getArmature();
156         if(armature)
157             this._armature = armature;
158     },
159 
160     /**
161      * Returns the bone reference of ccs.Skin.
162      * @returns {null}
163      */
164     getBone: function () {
165         return this.bone;
166     },
167 
168     /**
169      * display name getter
170      * @returns {String}
171      */
172     getDisplayName: function () {
173         return this._displayName;
174     },
175 
176     _createRenderCmd: function(){
177         if(cc._renderType === cc.game.RENDER_TYPE_CANVAS)
178             return new ccs.Skin.CanvasRenderCmd(this);
179         else
180             return new ccs.Skin.WebGLRenderCmd(this);
181     }
182 });
183 
184 var _p = ccs.Skin.prototype;
185 
186 // Extended properties
187 /** @expose */
188 _p.skinData;
189 cc.defineGetterSetter(_p, "skinData", _p.getSkinData, _p.setSkinData);
190 /** @expose */
191 _p.displayName;
192 cc.defineGetterSetter(_p, "displayName", _p.getDisplayName);
193 
194 _p = null;
195 
196 /**
197  * allocates and initializes a skin.
198  * @param {String} [fileName] fileName or sprite frame name
199  * @param {cc.Rect} [rect]
200  * @returns {ccs.Skin}
201  * @deprecated since v3.1, please use new construction instead
202  */
203 ccs.Skin.create = function (fileName, rect) {
204     return new ccs.Skin(fileName, rect);
205 };
206 
207 /**
208  * allocates and initializes a skin.
209  * @param {String} spriteFrameName
210  * @returns {ccs.Skin}
211  * @deprecated since v3.1, please use new construction instead
212  */
213 ccs.Skin.createWithSpriteFrameName = function (spriteFrameName) {
214     return new ccs.Skin("#" + spriteFrameName);
215 };
216