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  * @class ccs.TransformHelp
 28  * @extend ccs.Class
 29  */
 30 ccs.TransformHelp = ccs.TransformHelp || ccs.Class.extend({});
 31 
 32 ccs.TransformHelp.helpMatrix1 = cc.AffineTransformMake(1, 0, 0, 1, 0, 0);
 33 ccs.TransformHelp.helpMatrix2 = cc.AffineTransformMake(1, 0, 0, 1, 0, 0);
 34 ccs.TransformHelp.helpPoint1 = cc.p(0, 0);
 35 ccs.TransformHelp.helpPoint2 = cc.p(0, 0);
 36 
 37 /**
 38  * @function
 39  * @static
 40  * @param {ccs.BaseData} bone
 41  * @return {cc.AffineTransform}
 42  * Constructor
 43  */
 44 ccs.TransformHelp.transformFromParent = function (bone, parentBone) {
 45     this.nodeToMatrix(bone, this.helpMatrix1);
 46     this.nodeToMatrix(parentBone, this.helpMatrix2);
 47 
 48     this.helpMatrix2 = cc.AffineTransformInvert(this.helpMatrix2);
 49     this.helpMatrix1 = cc.AffineTransformConcat(this.helpMatrix1, this.helpMatrix2);
 50 
 51     this.matrixToNode(this.helpMatrix1, bone);
 52 };
 53 
 54 /**
 55  * @function
 56  * @static
 57  * @param {ccs.BaseData} node
 58  * @param {cc.AffineTransform} matrix
 59  */
 60 ccs.TransformHelp.nodeToMatrix = function (node, matrix) {
 61     if (node.skewX == -node.skewY) {
 62         var sine = Math.sin(node.skewX);
 63         var cosine = Math.cos(node.skewX);
 64         matrix.a = node.scaleX * cosine;
 65         matrix.b = node.scaleX * -sine;
 66         matrix.c = node.scaleY * sine;
 67         matrix.d = node.scaleY * cosine;
 68     } else {
 69         matrix.a = node.scaleX * Math.cos(node.skewY);
 70         matrix.b = node.scaleX * Math.sin(node.skewY);
 71         matrix.c = node.scaleY * Math.sin(node.skewX);
 72         matrix.d = node.scaleY * Math.cos(node.skewY);
 73     }
 74     matrix.tx = node.x;
 75     matrix.ty = node.y;
 76 };
 77 
 78 /**
 79  * @function
 80  * @static
 81  * @param {cc.AffineTransform} matrix
 82  * @param {ccs.BaseData} node
 83  */
 84 ccs.TransformHelp.matrixToNode = function (matrix, node) {
 85     /*
 86      *  In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform
 87      *  but not used the tx, ty value. we simulate the function here
 88      */
 89     this.helpPoint1.x = 0;
 90     this.helpPoint1.y = 1;
 91     this.helpPoint1 = cc.PointApplyAffineTransform(this.helpPoint1, matrix);
 92     this.helpPoint1.x -= matrix.tx;
 93     this.helpPoint1.y -= matrix.ty;
 94 
 95     this.helpPoint2.x = 1;
 96     this.helpPoint2.y = 0;
 97     this.helpPoint2 = cc.PointApplyAffineTransform(this.helpPoint2, matrix);
 98     this.helpPoint2.x -= matrix.tx;
 99     this.helpPoint2.y -= matrix.ty;
100 
101     node.skewX = -(Math.atan2(this.helpPoint1.y, this.helpPoint1.x) - 1.5707964); //todo
102     //node.skewX = -Math.atan2(this.helpPoint2.y, this.helpPoint2.x);
103     node.skewY = Math.atan2(this.helpPoint2.y, this.helpPoint2.x);
104     node.scaleX = Math.sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
105     node.scaleY = Math.sqrt(matrix.c * matrix.c + matrix.d * matrix.d);
106     node.x = matrix.tx;
107     node.y = matrix.ty;
108 };
109 
110 /**
111  * @function
112  * @static
113  * @param {ccs.BaseData} target
114  * @param {ccs.BaseData} source
115  */
116 ccs.TransformHelp.nodeConcat = function (target, source) {
117     target.x += source.x;
118     target.y += source.y;
119     target.skewX += source.skewX;
120     target.skewY += source.skewY;
121     target.scaleX += source.scaleX;
122     target.scaleY += source.scaleY;
123 };
124 
125 /**
126  * @function
127  * @static
128  * @param {ccs.BaseData} target
129  * @param {ccs.BaseData} source
130  */
131 ccs.TransformHelp.nodeSub = function (target, source) {
132     target.x -= source.x;
133     target.y -= source.y;
134     target.skewX -= source.skewX;
135     target.skewY -= source.skewY;
136     target.scaleX -= source.scaleX;
137     target.scaleY -= source.scaleY;
138 };