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