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         this._renderCmd.transform();
112     },
113 
114     /**
115      * Returns skin date of ccs.Skin.
116      * @returns {ccs.BaseData}
117      */
118     getSkinData: function () {
119         return this._skinData;
120     },
121 
122     /**
123      * Updates armature skin's transform with skin transform and bone's transform.
124      */
125     updateArmatureTransform: function () {
126         this._renderCmd.transform();
127     },
128 
129     /**
130      * Returns skin's world transform.
131      * @returns {cc.AffineTransform}
132      */
133     getNodeToWorldTransform: function(){
134         return this._renderCmd.getNodeToWorldTransform();
135     },
136 
137     getNodeToWorldTransformAR: function(){
138         return this._renderCmd.getNodeToWorldTransformAR();
139     },
140 
141     /**
142      * Sets the bone reference to ccs.Skin.
143      * @param bone
144      */
145     setBone: function (bone) {
146         this.bone = bone;
147         var armature = this.bone.getArmature();
148         if(armature)
149             this._armature = armature;
150     },
151 
152     /**
153      * Returns the bone reference of ccs.Skin.
154      * @returns {null}
155      */
156     getBone: function () {
157         return this.bone;
158     },
159 
160     /**
161      * display name getter
162      * @returns {String}
163      */
164     getDisplayName: function () {
165         return this._displayName;
166     },
167 
168     _createRenderCmd: function(){
169         if(cc._renderType === cc.game.RENDER_TYPE_CANVAS)
170             return new ccs.Skin.CanvasRenderCmd(this);
171         else
172             return new ccs.Skin.WebGLRenderCmd(this);
173     }
174 });
175 
176 var _p = ccs.Skin.prototype;
177 
178 // Extended properties
179 /** @expose */
180 _p.skinData;
181 cc.defineGetterSetter(_p, "skinData", _p.getSkinData, _p.setSkinData);
182 /** @expose */
183 _p.displayName;
184 cc.defineGetterSetter(_p, "displayName", _p.getDisplayName);
185 
186 _p = null;
187 
188 /**
189  * allocates and initializes a skin.
190  * @param {String} [fileName] fileName or sprite frame name
191  * @param {cc.Rect} [rect]
192  * @returns {ccs.Skin}
193  * @deprecated since v3.1, please use new construction instead
194  */
195 ccs.Skin.create = function (fileName, rect) {
196     return new ccs.Skin(fileName, rect);
197 };
198 
199 /**
200  * allocates and initializes a skin.
201  * @param {String} spriteFrameName
202  * @returns {ccs.Skin}
203  * @deprecated since v3.1, please use new construction instead
204  */
205 ccs.Skin.createWithSpriteFrameName = function (spriteFrameName) {
206     return new ccs.Skin("#" + spriteFrameName);
207 };
208