123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include "2d/CCParallaxNode.h"
- #include "base/ccCArray.h"
- NS_CC_BEGIN
- class PointObject : public Ref
- {
- public:
- static PointObject * create(Vec2 ratio, Vec2 offset)
- {
- PointObject *ret = new (std::nothrow) PointObject();
- ret->initWithPoint(ratio, offset);
- ret->autorelease();
- return ret;
- }
-
- bool initWithPoint(Vec2 ratio, Vec2 offset)
- {
- _ratio = ratio;
- _offset = offset;
- _child = nullptr;
- return true;
- }
-
- const Vec2& getRatio() const { return _ratio; }
- void setRatio(const Vec2& ratio) { _ratio = ratio; }
- const Vec2& getOffset() const { return _offset; }
- void setOffset(const Vec2& offset) { _offset = offset; }
-
- Node* getChild() const { return _child; }
- void setChild(Node* child) { _child = child; }
-
- private:
- Vec2 _ratio;
- Vec2 _offset;
- Node *_child;
- };
- ParallaxNode::ParallaxNode()
- {
- _parallaxArray = ccArrayNew(5);
- _lastPosition.set(-100.0f, -100.0f);
- }
- ParallaxNode::~ParallaxNode()
- {
- if( _parallaxArray )
- {
- ccArrayFree(_parallaxArray);
- _parallaxArray = nullptr;
- }
- }
- ParallaxNode * ParallaxNode::create()
- {
- ParallaxNode *ret = new (std::nothrow) ParallaxNode();
- ret->autorelease();
- return ret;
- }
- void ParallaxNode::addChild(Node* , int , int )
- {
- CCASSERT(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
- }
- void ParallaxNode::addChild(Node* , int , const std::string& )
- {
- CCASSERT(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
- }
- void ParallaxNode::addChild(Node *child, int z, const Vec2& ratio, const Vec2& offset)
- {
- CCASSERT( child != nullptr, "Argument must be non-nil");
- PointObject *obj = PointObject::create(ratio, offset);
- obj->setChild(child);
- ccArrayAppendObjectWithResize(_parallaxArray, (Ref*)obj);
- Vec2 pos = this->absolutePosition();
- pos.x = -pos.x + pos.x * ratio.x + offset.x;
- pos.y = -pos.y + pos.y * ratio.y + offset.y;
- child->setPosition(pos);
- Node::addChild(child, z, child->getName());
- }
- void ParallaxNode::removeChild(Node* child, bool cleanup)
- {
- for( int i=0;i < _parallaxArray->num;i++)
- {
- PointObject *point = (PointObject*)_parallaxArray->arr[i];
- if (point->getChild() == child)
- {
- ccArrayRemoveObjectAtIndex(_parallaxArray, i, true);
- break;
- }
- }
- Node::removeChild(child, cleanup);
- }
- void ParallaxNode::removeAllChildrenWithCleanup(bool cleanup)
- {
- ccArrayRemoveAllObjects(_parallaxArray);
- Node::removeAllChildrenWithCleanup(cleanup);
- }
- Vec2 ParallaxNode::absolutePosition()
- {
- Vec2 ret = _position;
- Node *cn = this;
- while (cn->getParent() != nullptr)
- {
- cn = cn->getParent();
- ret = ret + cn->getPosition();
- }
- return ret;
- }
- void ParallaxNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
- {
-
-
- Vec2 pos = this->absolutePosition();
- if( ! pos.equals(_lastPosition) )
- {
- for( int i=0; i < _parallaxArray->num; i++ )
- {
- PointObject *point = (PointObject*)_parallaxArray->arr[i];
- float x = -pos.x + pos.x * point->getRatio().x + point->getOffset().x;
- float y = -pos.y + pos.y * point->getRatio().y + point->getOffset().y;
- point->getChild()->setPosition(x,y);
- }
- _lastPosition = pos;
- }
- Node::visit(renderer, parentTransform, parentFlags);
- }
- NS_CC_END
|