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