2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
2019-11-24 23:15:56 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-10-21 10:12:00 +08:00
|
|
|
#include "CCTransformHelp.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocostudio
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
AffineTransform TransformHelp::helpMatrix1;
|
|
|
|
AffineTransform TransformHelp::helpMatrix2;
|
|
|
|
|
|
|
|
Vec2 TransformHelp::helpPoint1;
|
|
|
|
Vec2 TransformHelp::helpPoint2;
|
|
|
|
|
|
|
|
BaseData helpParentNode;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TransformHelp::TransformHelp() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::transformFromParent(BaseData& node, const BaseData& parentNode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
nodeToMatrix(node, helpMatrix1);
|
|
|
|
nodeToMatrix(parentNode, helpMatrix2);
|
|
|
|
|
|
|
|
helpMatrix2 = AffineTransformInvert(helpMatrix2);
|
|
|
|
helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
|
|
|
|
|
|
|
|
matrixToNode(helpMatrix1, node);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::transformToParent(BaseData& node, const BaseData& parentNode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
nodeToMatrix(node, helpMatrix1);
|
|
|
|
nodeToMatrix(parentNode, helpMatrix2);
|
|
|
|
|
|
|
|
helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
|
|
|
|
|
|
|
|
matrixToNode(helpMatrix1, node);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::transformFromParentWithoutScale(BaseData& node, const BaseData& parentNode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
helpParentNode.copy(&parentNode);
|
|
|
|
helpParentNode.scaleX = 1;
|
|
|
|
helpParentNode.scaleY = 1;
|
|
|
|
|
|
|
|
nodeToMatrix(node, helpMatrix1);
|
|
|
|
nodeToMatrix(helpParentNode, helpMatrix2);
|
|
|
|
|
|
|
|
helpMatrix2 = AffineTransformInvert(helpMatrix2);
|
|
|
|
helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
|
|
|
|
|
|
|
|
matrixToNode(helpMatrix1, node);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::transformToParentWithoutScale(BaseData& node, const BaseData& parentNode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
helpParentNode.copy(&parentNode);
|
|
|
|
helpParentNode.scaleX = 1;
|
|
|
|
helpParentNode.scaleY = 1;
|
|
|
|
|
|
|
|
nodeToMatrix(node, helpMatrix1);
|
|
|
|
nodeToMatrix(helpParentNode, helpMatrix2);
|
|
|
|
|
|
|
|
helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
|
|
|
|
|
|
|
|
matrixToNode(helpMatrix1, node);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::nodeToMatrix(const BaseData& node, AffineTransform& matrix)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (node.skewX == -node.skewY)
|
|
|
|
{
|
|
|
|
double sine = sin(node.skewX);
|
|
|
|
double cosine = cos(node.skewX);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix.tx = node.x;
|
|
|
|
matrix.ty = node.y;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::nodeToMatrix(const BaseData& node, Mat4& matrix)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
matrix = Mat4::IDENTITY;
|
|
|
|
|
|
|
|
if (node.skewX == -node.skewY)
|
|
|
|
{
|
|
|
|
double sine = sin(node.skewX);
|
|
|
|
double cosine = cos(node.skewX);
|
|
|
|
|
|
|
|
matrix.m[0] = node.scaleX * cosine;
|
|
|
|
matrix.m[1] = node.scaleX * -sine;
|
|
|
|
matrix.m[4] = node.scaleY * sine;
|
|
|
|
matrix.m[5] = node.scaleY * cosine;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matrix.m[0] = node.scaleX * cos(node.skewY);
|
|
|
|
matrix.m[1] = node.scaleX * sin(node.skewY);
|
|
|
|
matrix.m[4] = node.scaleY * sin(node.skewX);
|
|
|
|
matrix.m[5] = node.scaleY * cos(node.skewX);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
matrix.m[12] = node.x;
|
|
|
|
matrix.m[13] = node.y;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::matrixToNode(const AffineTransform& matrix, BaseData& node)
|
2019-11-23 20:27:39 +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
|
|
|
|
*/
|
|
|
|
helpPoint1.x = 0;
|
|
|
|
helpPoint1.y = 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
helpPoint1 = PointApplyAffineTransform(helpPoint1, matrix);
|
2019-11-23 20:27:39 +08:00
|
|
|
helpPoint1.x -= matrix.tx;
|
|
|
|
helpPoint1.y -= matrix.ty;
|
|
|
|
|
|
|
|
helpPoint2.x = 1;
|
|
|
|
helpPoint2.y = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
helpPoint2 = PointApplyAffineTransform(helpPoint2, matrix);
|
2019-11-23 20:27:39 +08:00
|
|
|
helpPoint2.x -= matrix.tx;
|
|
|
|
helpPoint2.y -= matrix.ty;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f);
|
|
|
|
node.skewY = atan2f(helpPoint2.y, helpPoint2.x);
|
2019-11-23 20:27:39 +08:00
|
|
|
node.scaleX = sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
|
|
|
|
node.scaleY = sqrt(matrix.c * matrix.c + matrix.d * matrix.d);
|
2021-12-25 10:04:45 +08:00
|
|
|
node.x = matrix.tx;
|
|
|
|
node.y = matrix.ty;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::matrixToNode(const Mat4& matrix, BaseData& node)
|
2019-11-23 20:27:39 +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
|
|
|
|
*/
|
|
|
|
helpPoint1.x = 0;
|
|
|
|
helpPoint1.y = 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
helpPoint1 = PointApplyTransform(helpPoint1, matrix);
|
2019-11-23 20:27:39 +08:00
|
|
|
helpPoint1.x -= matrix.m[12];
|
|
|
|
helpPoint1.y -= matrix.m[13];
|
|
|
|
|
|
|
|
helpPoint2.x = 1;
|
|
|
|
helpPoint2.y = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
helpPoint2 = PointApplyTransform(helpPoint2, matrix);
|
2019-11-23 20:27:39 +08:00
|
|
|
helpPoint2.x -= matrix.m[12];
|
|
|
|
helpPoint2.y -= matrix.m[13];
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f);
|
|
|
|
node.skewY = atan2f(helpPoint2.y, helpPoint2.x);
|
2019-11-23 20:27:39 +08:00
|
|
|
node.scaleX = sqrt(matrix.m[0] * matrix.m[0] + matrix.m[1] * matrix.m[1]);
|
|
|
|
node.scaleY = sqrt(matrix.m[4] * matrix.m[4] + matrix.m[5] * matrix.m[5]);
|
2021-12-25 10:04:45 +08:00
|
|
|
node.x = matrix.m[12];
|
|
|
|
node.y = matrix.m[13];
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::nodeConcat(BaseData& target, BaseData& source)
|
2019-11-23 20:27:39 +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;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TransformHelp::nodeSub(BaseData& target, BaseData& source)
|
2019-11-23 20:27:39 +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;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocostudio
|