From c4d8ebc78090df2c098c13502edf1bfacc8664f9 Mon Sep 17 00:00:00 2001 From: andyque Date: Mon, 20 Jul 2015 10:03:15 +0800 Subject: [PATCH] add Two APIs to Node. 1. add getNodeToParentTransform(Node* ancestor) 2. add getNodeToParentAffineTransform(Node* ancestor) --- cocos/2d/CCNode.cpp | 38 ++++++++++++++++++++++++-------------- cocos/2d/CCNode.h | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/cocos/2d/CCNode.cpp b/cocos/2d/CCNode.cpp index 94a868d8de..3a28eabc2a 100644 --- a/cocos/2d/CCNode.cpp +++ b/cocos/2d/CCNode.cpp @@ -1761,6 +1761,28 @@ AffineTransform Node::getNodeToParentAffineTransform() const return ret; } + +Mat4 Node::getNodeToParentTransform(Node* ancestor) const +{ + Mat4 t(this->getNodeToParentTransform()); + + for (Node *p = _parent; p != nullptr && p != ancestor ; p = p->getParent()) + { + t = p->getNodeToParentTransform() * t; + } + + return t; +} + +AffineTransform Node::getNodeToParentAffineTransform(Node* ancestor) const +{ + AffineTransform t(this->getNodeToParentAffineTransform()); + + for (Node *p = _parent; p != nullptr && p != ancestor; p = p->getParent()) + t = AffineTransformConcat(t, p->getNodeToParentAffineTransform()); + + return t; +} const Mat4& Node::getNodeToParentTransform() const { if (_transformDirty) @@ -1916,24 +1938,12 @@ const Mat4& Node::getParentToNodeTransform() const AffineTransform Node::getNodeToWorldAffineTransform() const { - AffineTransform t(this->getNodeToParentAffineTransform()); - - for (Node *p = _parent; p != nullptr; p = p->getParent()) - t = AffineTransformConcat(t, p->getNodeToParentAffineTransform()); - - return t; + return this->getNodeToParentAffineTransform(nullptr); } Mat4 Node::getNodeToWorldTransform() const { - Mat4 t(this->getNodeToParentTransform()); - - for (Node *p = _parent; p != nullptr; p = p->getParent()) - { - t = p->getNodeToParentTransform() * t; - } - - return t; + return this->getNodeToParentTransform(nullptr); } AffineTransform Node::getWorldToNodeAffineTransform() const diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 59d8cd2c94..98873d52e1 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -1488,6 +1488,29 @@ public: virtual const Mat4& getNodeToParentTransform() const; virtual AffineTransform getNodeToParentAffineTransform() const; + /** + * Returns the matrix that transform the node's (local) space coordinates into the parent's space coordinates. + * The matrix is in Pixels. + * Note: If acenstor is not a valid ancestor of the node, the API would return the same value as @see getNodeToWorldTransform + * + * @param ancestor The parent's node pointer. + * @since v3.7 + * @return The transformation matrix. + */ + virtual Mat4 getNodeToParentTransform(Node* ancestor) const; + + /** + * Returns the affline transform matrix that transform the node's (local) space coordinates into the parent's space coordinates. + * The matrix is in Pixels. + * + * Note: If acenstor is not a valid ancestor of the node, the API would return the same value as @see getNodeToWorldAffineTransform + * + * @param ancestor The parent's node pointer. + * @since v3.7 + * @return The affline transformation matrix. + */ + virtual AffineTransform getNodeToParentAffineTransform(Node* ancestor) const; + /** * Sets the transformation matrix manually. *