2013-06-06 12:02:54 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-16 16:48:39 +08:00
|
|
|
#include "cocostudio/CCTransformHelp.h"
|
|
|
|
#include "cocostudio/CCUtilMath.h"
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
namespace cocostudio {
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
AffineTransform TransformHelp::helpMatrix1;
|
|
|
|
AffineTransform TransformHelp::helpMatrix2;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
Point TransformHelp::helpPoint1;
|
|
|
|
Point TransformHelp::helpPoint2;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
BaseData helpParentNode;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
TransformHelp::TransformHelp()
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void TransformHelp::transformFromParent(BaseData &node, const BaseData &parentNode)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
nodeToMatrix(node, helpMatrix1);
|
|
|
|
nodeToMatrix(parentNode, helpMatrix2);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
helpMatrix2 = AffineTransformInvert(helpMatrix2);
|
|
|
|
helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
matrixToNode(helpMatrix1, node);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void TransformHelp::transformToParent(BaseData &node, const BaseData &parentNode)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
nodeToMatrix(node, helpMatrix1);
|
|
|
|
nodeToMatrix(parentNode, helpMatrix2);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
matrixToNode(helpMatrix1, node);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void TransformHelp::transformFromParentWithoutScale(BaseData &node, const BaseData &parentNode)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
helpParentNode.copy(&parentNode);
|
|
|
|
helpParentNode.scaleX = 1;
|
|
|
|
helpParentNode.scaleY = 1;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
nodeToMatrix(node, helpMatrix1);
|
|
|
|
nodeToMatrix(helpParentNode, helpMatrix2);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
helpMatrix2 = AffineTransformInvert(helpMatrix2);
|
|
|
|
helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
matrixToNode(helpMatrix1, node);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void TransformHelp::transformToParentWithoutScale(BaseData &node, const BaseData &parentNode)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
helpParentNode.copy(&parentNode);
|
|
|
|
helpParentNode.scaleX = 1;
|
|
|
|
helpParentNode.scaleY = 1;
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
nodeToMatrix(node, helpMatrix1);
|
|
|
|
nodeToMatrix(helpParentNode, helpMatrix2);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
matrixToNode(helpMatrix1, node);
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void TransformHelp::nodeToMatrix(const BaseData &node, AffineTransform &matrix)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-10-30 09:41:40 +08:00
|
|
|
if (node.skewX == -node.skewY)
|
|
|
|
{
|
|
|
|
double sine = sin(node.skewX);
|
|
|
|
double cosine = cos(node.skewX);
|
|
|
|
|
|
|
|
matrix.a = node.scaleX * cosine;
|
|
|
|
matrix.b = node.scaleX * -sine;
|
|
|
|
matrix.c = node.scaleY * sine;
|
|
|
|
matrix.d = node.scaleY * cosine;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matrix.a = node.scaleX * cos(node.skewY);
|
|
|
|
matrix.b = node.scaleX * sin(node.skewY);
|
|
|
|
matrix.c = node.scaleY * sin(node.skewX);
|
|
|
|
matrix.d = node.scaleY * cos(node.skewX);
|
|
|
|
}
|
2013-06-06 12:02:54 +08:00
|
|
|
|
2013-06-07 10:52:32 +08:00
|
|
|
matrix.tx = node.x;
|
|
|
|
matrix.ty = node.y;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 03:07:15 +08:00
|
|
|
void TransformHelp::nodeToMatrix(const BaseData &node, kmMat4 &matrix)
|
|
|
|
{
|
|
|
|
kmMat4Identity(&matrix);
|
|
|
|
|
|
|
|
if (node.skewX == -node.skewY)
|
|
|
|
{
|
|
|
|
double sine = sin(node.skewX);
|
|
|
|
double cosine = cos(node.skewX);
|
|
|
|
|
|
|
|
matrix.mat[0] = node.scaleX * cosine;
|
|
|
|
matrix.mat[1] = node.scaleX * -sine;
|
|
|
|
matrix.mat[4] = node.scaleY * sine;
|
|
|
|
matrix.mat[5] = node.scaleY * cosine;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matrix.mat[0] = node.scaleX * cos(node.skewY);
|
|
|
|
matrix.mat[1] = node.scaleX * sin(node.skewY);
|
|
|
|
matrix.mat[4] = node.scaleY * sin(node.skewX);
|
|
|
|
matrix.mat[5] = node.scaleY * cos(node.skewX);
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix.mat[12] = node.x;
|
|
|
|
matrix.mat[13] = node.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void TransformHelp::matrixToNode(const AffineTransform &matrix, BaseData &node)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform
|
|
|
|
* but not used the tx, ty value. we simulate the function here
|
|
|
|
*/
|
2013-06-07 10:52:32 +08:00
|
|
|
helpPoint1.x = 0;
|
|
|
|
helpPoint1.y = 1;
|
2013-06-20 14:15:53 +08:00
|
|
|
helpPoint1 = PointApplyAffineTransform(helpPoint1, matrix);
|
2013-06-07 10:52:32 +08:00
|
|
|
helpPoint1.x -= matrix.tx;
|
|
|
|
helpPoint1.y -= matrix.ty;
|
|
|
|
|
|
|
|
helpPoint2.x = 1;
|
|
|
|
helpPoint2.y = 0;
|
2013-06-20 14:15:53 +08:00
|
|
|
helpPoint2 = PointApplyAffineTransform(helpPoint2, matrix);
|
2013-06-07 10:52:32 +08:00
|
|
|
helpPoint2.x -= matrix.tx;
|
|
|
|
helpPoint2.y -= matrix.ty;
|
|
|
|
|
|
|
|
node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f);
|
|
|
|
node.skewY = atan2f(helpPoint2.y, helpPoint2.x);
|
|
|
|
node.scaleX = sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
|
|
|
|
node.scaleY = sqrt(matrix.c * matrix.c + matrix.d * matrix.d);
|
|
|
|
node.x = matrix.tx;
|
|
|
|
node.y = matrix.ty;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 03:07:15 +08:00
|
|
|
void TransformHelp::matrixToNode(const kmMat4 &matrix, BaseData &node)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform
|
|
|
|
* but not used the tx, ty value. we simulate the function here
|
|
|
|
*/
|
|
|
|
helpPoint1.x = 0;
|
|
|
|
helpPoint1.y = 1;
|
|
|
|
helpPoint1 = PointApplyTransform(helpPoint1, matrix);
|
|
|
|
helpPoint1.x -= matrix.mat[12];
|
|
|
|
helpPoint1.y -= matrix.mat[13];
|
|
|
|
|
|
|
|
helpPoint2.x = 1;
|
|
|
|
helpPoint2.y = 0;
|
|
|
|
helpPoint2 = PointApplyTransform(helpPoint2, matrix);
|
|
|
|
helpPoint2.x -= matrix.mat[12];
|
|
|
|
helpPoint2.y -= matrix.mat[13];
|
|
|
|
|
|
|
|
node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f);
|
|
|
|
node.skewY = atan2f(helpPoint2.y, helpPoint2.x);
|
|
|
|
node.scaleX = sqrt(matrix.mat[0] * matrix.mat[0] + matrix.mat[1] * matrix.mat[1]);
|
|
|
|
node.scaleY = sqrt(matrix.mat[4] * matrix.mat[4] + matrix.mat[5] * matrix.mat[5]);
|
|
|
|
node.x = matrix.mat[12];
|
|
|
|
node.y = matrix.mat[13];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void TransformHelp::nodeConcat(BaseData &target, BaseData &source)
|
2013-06-06 12:02:54 +08:00
|
|
|
{
|
2013-06-07 10:52:32 +08:00
|
|
|
target.x += source.x;
|
|
|
|
target.y += source.y;
|
|
|
|
target.skewX += source.skewX;
|
|
|
|
target.skewY += source.skewY;
|
|
|
|
target.scaleX += source.scaleX;
|
|
|
|
target.scaleY += source.scaleY;
|
2013-06-06 12:02:54 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 19:08:45 +08:00
|
|
|
void TransformHelp::nodeSub(BaseData &target, BaseData &source)
|
2013-09-13 18:07:37 +08:00
|
|
|
{
|
|
|
|
target.x -= source.x;
|
|
|
|
target.y -= source.y;
|
|
|
|
target.skewX -= source.skewX;
|
|
|
|
target.skewY -= source.skewY;
|
|
|
|
target.scaleX -= source.scaleX;
|
|
|
|
target.scaleY -= source.scaleY;
|
|
|
|
}
|
|
|
|
|
2013-10-15 18:00:03 +08:00
|
|
|
}
|