CCParallaxNode.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  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. #include "2d/CCParallaxNode.h"
  25. #include "base/ccCArray.h"
  26. NS_CC_BEGIN
  27. class PointObject : public Ref
  28. {
  29. public:
  30. static PointObject * create(Vec2 ratio, Vec2 offset)
  31. {
  32. PointObject *ret = new (std::nothrow) PointObject();
  33. ret->initWithPoint(ratio, offset);
  34. ret->autorelease();
  35. return ret;
  36. }
  37. bool initWithPoint(Vec2 ratio, Vec2 offset)
  38. {
  39. _ratio = ratio;
  40. _offset = offset;
  41. _child = nullptr;
  42. return true;
  43. }
  44. const Vec2& getRatio() const { return _ratio; }
  45. void setRatio(const Vec2& ratio) { _ratio = ratio; }
  46. const Vec2& getOffset() const { return _offset; }
  47. void setOffset(const Vec2& offset) { _offset = offset; }
  48. Node* getChild() const { return _child; }
  49. void setChild(Node* child) { _child = child; }
  50. private:
  51. Vec2 _ratio;
  52. Vec2 _offset;
  53. Node *_child; // weak ref
  54. };
  55. ParallaxNode::ParallaxNode()
  56. {
  57. _parallaxArray = ccArrayNew(5);
  58. _lastPosition.set(-100.0f, -100.0f);
  59. }
  60. ParallaxNode::~ParallaxNode()
  61. {
  62. if( _parallaxArray )
  63. {
  64. ccArrayFree(_parallaxArray);
  65. _parallaxArray = nullptr;
  66. }
  67. }
  68. ParallaxNode * ParallaxNode::create()
  69. {
  70. ParallaxNode *ret = new (std::nothrow) ParallaxNode();
  71. ret->autorelease();
  72. return ret;
  73. }
  74. void ParallaxNode::addChild(Node* /*child*/, int /*zOrder*/, int /*tag*/)
  75. {
  76. CCASSERT(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
  77. }
  78. void ParallaxNode::addChild(Node* /*child*/, int /*zOrder*/, const std::string& /*name*/)
  79. {
  80. CCASSERT(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
  81. }
  82. void ParallaxNode::addChild(Node *child, int z, const Vec2& ratio, const Vec2& offset)
  83. {
  84. CCASSERT( child != nullptr, "Argument must be non-nil");
  85. PointObject *obj = PointObject::create(ratio, offset);
  86. obj->setChild(child);
  87. ccArrayAppendObjectWithResize(_parallaxArray, (Ref*)obj);
  88. Vec2 pos = this->absolutePosition();
  89. pos.x = -pos.x + pos.x * ratio.x + offset.x;
  90. pos.y = -pos.y + pos.y * ratio.y + offset.y;
  91. child->setPosition(pos);
  92. Node::addChild(child, z, child->getName());
  93. }
  94. void ParallaxNode::removeChild(Node* child, bool cleanup)
  95. {
  96. for( int i=0;i < _parallaxArray->num;i++)
  97. {
  98. PointObject *point = (PointObject*)_parallaxArray->arr[i];
  99. if (point->getChild() == child)
  100. {
  101. ccArrayRemoveObjectAtIndex(_parallaxArray, i, true);
  102. break;
  103. }
  104. }
  105. Node::removeChild(child, cleanup);
  106. }
  107. void ParallaxNode::removeAllChildrenWithCleanup(bool cleanup)
  108. {
  109. ccArrayRemoveAllObjects(_parallaxArray);
  110. Node::removeAllChildrenWithCleanup(cleanup);
  111. }
  112. Vec2 ParallaxNode::absolutePosition()
  113. {
  114. Vec2 ret = _position;
  115. Node *cn = this;
  116. while (cn->getParent() != nullptr)
  117. {
  118. cn = cn->getParent();
  119. ret = ret + cn->getPosition();
  120. }
  121. return ret;
  122. }
  123. /*
  124. The positions are updated at visit because:
  125. - using a timer is not guaranteed that it will called after all the positions were updated
  126. - overriding "draw" will only precise if the children have a z > 0
  127. */
  128. void ParallaxNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  129. {
  130. // Vec2 pos = position_;
  131. // Vec2 pos = [self convertToWorldSpace:Vec2::ZERO];
  132. Vec2 pos = this->absolutePosition();
  133. if( ! pos.equals(_lastPosition) )
  134. {
  135. for( int i=0; i < _parallaxArray->num; i++ )
  136. {
  137. PointObject *point = (PointObject*)_parallaxArray->arr[i];
  138. float x = -pos.x + pos.x * point->getRatio().x + point->getOffset().x;
  139. float y = -pos.y + pos.y * point->getRatio().y + point->getOffset().y;
  140. point->getChild()->setPosition(x,y);
  141. }
  142. _lastPosition = pos;
  143. }
  144. Node::visit(renderer, parentTransform, parentFlags);
  145. }
  146. NS_CC_END