1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 4 http://www.cocos2d-x.org 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 ****************************************************************************/ 24 25 /** 26 * @ignore 27 */ 28 ccs.TransformHelp = ccs.TransformHelp || ccs.Class.extend({}); 29 30 ccs.TransformHelp.helpMatrix1 = cc.AffineTransformMake(1, 0, 0, 1, 0, 0); 31 ccs.TransformHelp.helpMatrix2 = cc.AffineTransformMake(1, 0, 0, 1, 0, 0); 32 ccs.TransformHelp.helpPoint1 = cc.p(0, 0); 33 ccs.TransformHelp.helpPoint2 = cc.p(0, 0); 34 35 /** 36 * @function 37 * @param {ccs.BaseData} bone 38 * @return {cc.AffineTransform} 39 * Constructor 40 */ 41 ccs.TransformHelp.transformFromParent = function (bone, parentBone) { 42 this.nodeToMatrix(bone, this.helpMatrix1); 43 this.nodeToMatrix(parentBone, this.helpMatrix2); 44 45 this.helpMatrix2 = cc.AffineTransformInvert(this.helpMatrix2); 46 this.helpMatrix1 = cc.AffineTransformConcat(this.helpMatrix1, this.helpMatrix2); 47 48 this.matrixToNode(this.helpMatrix1, bone); 49 }; 50 51 /** 52 * @function 53 * @param {ccs.BaseData} node 54 * @param {cc.AffineTransform} matrix 55 */ 56 ccs.TransformHelp.nodeToMatrix = function (node, matrix) { 57 if (node.skewX == -node.skewY) { 58 var sine = Math.sin(node.skewX); 59 var cosine = Math.cos(node.skewX); 60 matrix.a = node.scaleX * cosine; 61 matrix.b = node.scaleX * -sine; 62 matrix.c = node.scaleY * sine; 63 matrix.d = node.scaleY * cosine; 64 } else { 65 matrix.a = node.scaleX * Math.cos(node.skewY); 66 matrix.b = node.scaleX * Math.sin(node.skewY); 67 matrix.c = node.scaleY * Math.sin(node.skewX); 68 matrix.d = node.scaleY * Math.cos(node.skewY); 69 } 70 matrix.tx = node.x; 71 matrix.ty = node.y; 72 }; 73 74 /** 75 * @function 76 * @param {cc.AffineTransform} matrix 77 * @param {ccs.BaseData} node 78 */ 79 ccs.TransformHelp.matrixToNode = function (matrix, node) { 80 /* 81 * In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform 82 * but not used the tx, ty value. we simulate the function here 83 */ 84 this.helpPoint1.x = 0; 85 this.helpPoint1.y = 1; 86 this.helpPoint1 = cc.PointApplyAffineTransform(this.helpPoint1, matrix); 87 this.helpPoint1.x -= matrix.tx; 88 this.helpPoint1.y -= matrix.ty; 89 90 this.helpPoint2.x = 1; 91 this.helpPoint2.y = 0; 92 this.helpPoint2 = cc.PointApplyAffineTransform(this.helpPoint2, matrix); 93 this.helpPoint2.x -= matrix.tx; 94 this.helpPoint2.y -= matrix.ty; 95 96 node.skewX = -(Math.atan2(this.helpPoint1.y, this.helpPoint1.x) - 1.5707964); //todo 97 //node.skewX = -Math.atan2(this.helpPoint2.y, this.helpPoint2.x); 98 node.skewY = Math.atan2(this.helpPoint2.y, this.helpPoint2.x); 99 node.scaleX = Math.sqrt(matrix.a * matrix.a + matrix.b * matrix.b); 100 node.scaleY = Math.sqrt(matrix.c * matrix.c + matrix.d * matrix.d); 101 node.x = matrix.tx; 102 node.y = matrix.ty; 103 }; 104 105 /** 106 * @function 107 * @param {ccs.BaseData} target 108 * @param {ccs.BaseData} source 109 */ 110 ccs.TransformHelp.nodeConcat = function (target, source) { 111 target.x += source.x; 112 target.y += source.y; 113 target.skewX += source.skewX; 114 target.skewY += source.skewY; 115 target.scaleX += source.scaleX; 116 target.scaleY += source.scaleY; 117 }; 118 119 /** 120 * @function 121 * @param {ccs.BaseData} target 122 * @param {ccs.BaseData} source 123 */ 124 ccs.TransformHelp.nodeSub = function (target, source) { 125 target.x -= source.x; 126 target.y -= source.y; 127 target.skewX -= source.skewX; 128 target.skewY -= source.skewY; 129 target.scaleX -= source.scaleX; 130 target.scaleY -= source.scaleY; 131 };