CCTransformHelp.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /****************************************************************************
  2. Copyright (c) 2013-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "editor-support/cocostudio/CCTransformHelp.h"
  21. #include "editor-support/cocostudio/CCUtilMath.h"
  22. using namespace cocos2d;
  23. namespace cocostudio {
  24. AffineTransform TransformHelp::helpMatrix1;
  25. AffineTransform TransformHelp::helpMatrix2;
  26. Vec2 TransformHelp::helpPoint1;
  27. Vec2 TransformHelp::helpPoint2;
  28. BaseData helpParentNode;
  29. TransformHelp::TransformHelp()
  30. {
  31. }
  32. void TransformHelp::transformFromParent(BaseData &node, const BaseData &parentNode)
  33. {
  34. nodeToMatrix(node, helpMatrix1);
  35. nodeToMatrix(parentNode, helpMatrix2);
  36. helpMatrix2 = AffineTransformInvert(helpMatrix2);
  37. helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
  38. matrixToNode(helpMatrix1, node);
  39. }
  40. void TransformHelp::transformToParent(BaseData &node, const BaseData &parentNode)
  41. {
  42. nodeToMatrix(node, helpMatrix1);
  43. nodeToMatrix(parentNode, helpMatrix2);
  44. helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
  45. matrixToNode(helpMatrix1, node);
  46. }
  47. void TransformHelp::transformFromParentWithoutScale(BaseData &node, const BaseData &parentNode)
  48. {
  49. helpParentNode.copy(&parentNode);
  50. helpParentNode.scaleX = 1;
  51. helpParentNode.scaleY = 1;
  52. nodeToMatrix(node, helpMatrix1);
  53. nodeToMatrix(helpParentNode, helpMatrix2);
  54. helpMatrix2 = AffineTransformInvert(helpMatrix2);
  55. helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
  56. matrixToNode(helpMatrix1, node);
  57. }
  58. void TransformHelp::transformToParentWithoutScale(BaseData &node, const BaseData &parentNode)
  59. {
  60. helpParentNode.copy(&parentNode);
  61. helpParentNode.scaleX = 1;
  62. helpParentNode.scaleY = 1;
  63. nodeToMatrix(node, helpMatrix1);
  64. nodeToMatrix(helpParentNode, helpMatrix2);
  65. helpMatrix1 = AffineTransformConcat(helpMatrix1, helpMatrix2);
  66. matrixToNode(helpMatrix1, node);
  67. }
  68. void TransformHelp::nodeToMatrix(const BaseData &node, AffineTransform &matrix)
  69. {
  70. if (node.skewX == -node.skewY)
  71. {
  72. double sine = sin(node.skewX);
  73. double cosine = cos(node.skewX);
  74. matrix.a = node.scaleX * cosine;
  75. matrix.b = node.scaleX * -sine;
  76. matrix.c = node.scaleY * sine;
  77. matrix.d = node.scaleY * cosine;
  78. }
  79. else
  80. {
  81. matrix.a = node.scaleX * cos(node.skewY);
  82. matrix.b = node.scaleX * sin(node.skewY);
  83. matrix.c = node.scaleY * sin(node.skewX);
  84. matrix.d = node.scaleY * cos(node.skewX);
  85. }
  86. matrix.tx = node.x;
  87. matrix.ty = node.y;
  88. }
  89. void TransformHelp::nodeToMatrix(const BaseData &node, Mat4 &matrix)
  90. {
  91. matrix = Mat4::IDENTITY;
  92. if (node.skewX == -node.skewY)
  93. {
  94. double sine = sin(node.skewX);
  95. double cosine = cos(node.skewX);
  96. matrix.m[0] = node.scaleX * cosine;
  97. matrix.m[1] = node.scaleX * -sine;
  98. matrix.m[4] = node.scaleY * sine;
  99. matrix.m[5] = node.scaleY * cosine;
  100. }
  101. else
  102. {
  103. matrix.m[0] = node.scaleX * cos(node.skewY);
  104. matrix.m[1] = node.scaleX * sin(node.skewY);
  105. matrix.m[4] = node.scaleY * sin(node.skewX);
  106. matrix.m[5] = node.scaleY * cos(node.skewX);
  107. }
  108. matrix.m[12] = node.x;
  109. matrix.m[13] = node.y;
  110. }
  111. void TransformHelp::matrixToNode(const AffineTransform &matrix, BaseData &node)
  112. {
  113. /*
  114. * In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform
  115. * but not used the tx, ty value. we simulate the function here
  116. */
  117. helpPoint1.x = 0;
  118. helpPoint1.y = 1;
  119. helpPoint1 = PointApplyAffineTransform(helpPoint1, matrix);
  120. helpPoint1.x -= matrix.tx;
  121. helpPoint1.y -= matrix.ty;
  122. helpPoint2.x = 1;
  123. helpPoint2.y = 0;
  124. helpPoint2 = PointApplyAffineTransform(helpPoint2, matrix);
  125. helpPoint2.x -= matrix.tx;
  126. helpPoint2.y -= matrix.ty;
  127. node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f);
  128. node.skewY = atan2f(helpPoint2.y, helpPoint2.x);
  129. node.scaleX = sqrt(matrix.a * matrix.a + matrix.b * matrix.b);
  130. node.scaleY = sqrt(matrix.c * matrix.c + matrix.d * matrix.d);
  131. node.x = matrix.tx;
  132. node.y = matrix.ty;
  133. }
  134. void TransformHelp::matrixToNode(const Mat4 &matrix, BaseData &node)
  135. {
  136. /*
  137. * In as3 language, there is a function called "deltaTransformPoint", it calculate a point used give Transform
  138. * but not used the tx, ty value. we simulate the function here
  139. */
  140. helpPoint1.x = 0;
  141. helpPoint1.y = 1;
  142. helpPoint1 = PointApplyTransform(helpPoint1, matrix);
  143. helpPoint1.x -= matrix.m[12];
  144. helpPoint1.y -= matrix.m[13];
  145. helpPoint2.x = 1;
  146. helpPoint2.y = 0;
  147. helpPoint2 = PointApplyTransform(helpPoint2, matrix);
  148. helpPoint2.x -= matrix.m[12];
  149. helpPoint2.y -= matrix.m[13];
  150. node.skewX = -(atan2f(helpPoint1.y, helpPoint1.x) - 1.5707964f);
  151. node.skewY = atan2f(helpPoint2.y, helpPoint2.x);
  152. node.scaleX = sqrt(matrix.m[0] * matrix.m[0] + matrix.m[1] * matrix.m[1]);
  153. node.scaleY = sqrt(matrix.m[4] * matrix.m[4] + matrix.m[5] * matrix.m[5]);
  154. node.x = matrix.m[12];
  155. node.y = matrix.m[13];
  156. }
  157. void TransformHelp::nodeConcat(BaseData &target, BaseData &source)
  158. {
  159. target.x += source.x;
  160. target.y += source.y;
  161. target.skewX += source.skewX;
  162. target.skewY += source.skewY;
  163. target.scaleX += source.scaleX;
  164. target.scaleY += source.scaleY;
  165. }
  166. void TransformHelp::nodeSub(BaseData &target, BaseData &source)
  167. {
  168. target.x -= source.x;
  169. target.y -= source.y;
  170. target.skewX -= source.skewX;
  171. target.skewY -= source.skewY;
  172. target.scaleX -= source.scaleX;
  173. target.scaleY -= source.scaleY;
  174. }
  175. }