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