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