1
0

CCParallaxNode.cpp 5.1 KB

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