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  *  use to calculate the matrix of node from parent node
 28  * @class ccs.TransformHelp
 29  * @extend ccs.Class
 30  */
 31 ccs.TransformHelp = ccs.TransformHelp || ccs.Class.extend({});
 32 
 33 ccs.TransformHelp.helpMatrix1 = cc.affineTransformMake(1, 0, 0, 1, 0, 0);
 34 ccs.TransformHelp.helpMatrix2 = cc.affineTransformMake(1, 0, 0, 1, 0, 0);
 35 ccs.TransformHelp.helpPoint1 = cc.p(0, 0);
 36 ccs.TransformHelp.helpPoint2 = cc.p(0, 0);
 37 ccs.TransformHelp.helpParentNode = {};
 38 
 39 /**
 40  * Calculate a BaseData's transform matrix from parent node.
 41  * @function
 42  * @static
 43  * @param {ccs.BaseData} bone
 44  * Constructor
 45  */
 46 ccs.TransformHelp.transformFromParent = function (bone, parentNode) {
 47     this.nodeToMatrix(bone, this.helpMatrix1);
 48     this.nodeToMatrix(parentNode, this.helpMatrix2);
 49 
 50     this.helpMatrix2 = cc.affineTransformInvert(this.helpMatrix2);
 51     this.helpMatrix1 = cc.affineTransformConcat(this.helpMatrix1, this.helpMatrix2);
 52 
 53     this.matrixToNode(this.helpMatrix1, bone);
 54 };
 55 
 56 ccs.TransformHelp.transformToParent = function(node, parentNode){
 57     this.nodeToMatrix(node, this.helpMatrix1);
 58     this.nodeToMatrix(parentNode, this.helpMatrix2);
 59 
 60     this.helpMatrix1 = cc.affineTransformConcat(this.helpMatrix1, this.helpMatrix2);
 61 
 62     this.matrixToNode(this.helpMatrix1, node);
 63 };
 64 
 65 ccs.TransformHelp.transformFromParentWithoutScale = function(node, parentNode){
 66 //    this.helpParentNode.copy(&parentNode);
 67 
 68     for(var p in parentNode){
 69         this.helpParentNode[p] = parentNode[p];
 70     }
 71     this.helpParentNode.scaleX = 1;
 72     this.helpParentNode.scaleY = 1;
 73 
 74     this.nodeToMatrix(node, this.helpMatrix1);
 75     this.nodeToMatrix(this.helpParentNode, this.helpMatrix2);
 76 
 77     this.helpMatrix2 = cc.affineTransformInvert(this.helpMatrix2);
 78     this.helpMatrix1 = cc.affineTransformConcat(this.helpMatrix1, this.helpMatrix2);
 79 
 80     this.matrixToNode(this.helpMatrix1, node);
 81 };
 82 
 83 ccs.TransformHelp.transformToParentWithoutScale = function(node, parentNode){
 84     for(var p in parentNode){
 85         this.helpParentNode[p] = parentNode[p];
 86     }
 87     this.helpParentNode.scaleX = 1;
 88     this.helpParentNode.scaleY = 1;
 89 
 90     this.nodeToMatrix(node, this.helpMatrix1);
 91     this.nodeToMatrix(this.helpParentNode, this.helpMatrix2);
 92 
 93     this.helpMatrix1 = cc.affineTransformConcat(this.helpMatrix1, this.helpMatrix2);
 94 
 95     this.matrixToNode(this.helpMatrix1, node);
 96 
 97 };
 98 
 99 /**
100  * @function
101  * @static
102  * @param {ccs.BaseData} node
103  * @param {cc.AffineTransform} matrix
104  */
105 ccs.TransformHelp.nodeToMatrix = function (node, matrix) {
106     if (node.skewX === -node.skewY) {
107         var sine = Math.sin(node.skewX);
108         var cosine = Math.cos(node.skewX);
109         matrix.a = node.scaleX * cosine;
110         matrix.b = node.scaleX * -sine;
111         matrix.c = node.scaleY * sine;
112         matrix.d = node.scaleY * cosine;
113     } else {
114         matrix.a = node.scaleX * Math.cos(node.skewY);
115         matrix.b = node.scaleX * Math.sin(node.skewY);
116         matrix.c = node.scaleY * Math.sin(node.skewX);
117         matrix.d = node.scaleY * Math.cos(node.skewX);
118     }
119     matrix.tx = node.x;
120     matrix.ty = node.y;
121 };
122 
123 /**
124  * @function
125  * @static
126  * @param {cc.AffineTransform} matrix
127  * @param {ccs.BaseData} node
128  */
129 ccs.TransformHelp.matrixToNode = function (matrix, node) {
130     /*
131      *  In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform
132      *  but not used the tx, ty value. we simulate the function here
133      */
134     this.helpPoint1.x = 0;
135     this.helpPoint1.y = 1;
136     this.helpPoint1 = cc.pointApplyAffineTransform(this.helpPoint1, matrix);
137     this.helpPoint1.x -= matrix.tx;
138     this.helpPoint1.y -= matrix.ty;
139 
140     this.helpPoint2.x = 1;
141     this.helpPoint2.y = 0;
142     this.helpPoint2 = cc.pointApplyAffineTransform(this.helpPoint2, matrix);
143     this.helpPoint2.x -= matrix.tx;
144     this.helpPoint2.y -= matrix.ty;
145 
146     node.skewX = -(Math.atan2(this.helpPoint1.y, this.helpPoint1.x) - 1.5707964); //todo
147     //node.skewX = -Math.atan2(this.helpPoint2.y, this.helpPoint2.x);
148     node.skewY = Math.atan2(this.helpPoint2.y, this.helpPoint2.x);
149     node.scaleX = Math.sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
150     node.scaleY = Math.sqrt(matrix.c * matrix.c + matrix.d * matrix.d);
151     node.x = matrix.tx;
152     node.y = matrix.ty;
153 };
154 
155 /**
156  * @function
157  * @static
158  * @param {ccs.BaseData} target
159  * @param {ccs.BaseData} source
160  */
161 ccs.TransformHelp.nodeConcat = function (target, source) {
162     target.x += source.x;
163     target.y += source.y;
164     target.skewX += source.skewX;
165     target.skewY += source.skewY;
166     target.scaleX += source.scaleX;
167     target.scaleY += source.scaleY;
168 };
169 
170 /**
171  * @function
172  * @static
173  * @param {ccs.BaseData} target
174  * @param {ccs.BaseData} source
175  */
176 ccs.TransformHelp.nodeSub = function (target, source) {
177     target.x -= source.x;
178     target.y -= source.y;
179     target.skewX -= source.skewX;
180     target.skewY -= source.skewY;
181     target.scaleX -= source.scaleX;
182     target.scaleY -= source.scaleY;
183 };