2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2012-09-24 21:22:20 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "2d/CCParallaxNode.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/ccCArray.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
class PointObject : public Ref
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-05-23 15:06:57 +08:00
|
|
|
public:
|
2014-05-15 01:07:09 +08:00
|
|
|
static PointObject * create(Vec2 ratio, Vec2 offset)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
PointObject *ret = new (std::nothrow) PointObject();
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->initWithPoint(ratio, offset);
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
bool initWithPoint(Vec2 ratio, Vec2 offset)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ratio = ratio;
|
|
|
|
_offset = offset;
|
2013-12-18 17:47:20 +08:00
|
|
|
_child = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2016-09-12 09:44:21 +08:00
|
|
|
const Vec2& getRatio() const { return _ratio; }
|
|
|
|
void setRatio(const Vec2& ratio) { _ratio = ratio; }
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2016-09-12 09:44:21 +08:00
|
|
|
const Vec2& getOffset() const { return _offset; }
|
|
|
|
void setOffset(const Vec2& offset) { _offset = offset; }
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2016-09-12 09:44:21 +08:00
|
|
|
Node* getChild() const { return _child; }
|
|
|
|
void setChild(Node* child) { _child = child; }
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
|
|
private:
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 _ratio;
|
|
|
|
Vec2 _offset;
|
2013-07-23 18:26:26 +08:00
|
|
|
Node *_child; // weak ref
|
2012-04-18 18:43:45 +08:00
|
|
|
};
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParallaxNode::ParallaxNode()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_parallaxArray = ccArrayNew(5);
|
2015-04-20 01:40:52 +08:00
|
|
|
_lastPosition.set(-100.0f, -100.0f);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParallaxNode::~ParallaxNode()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _parallaxArray )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccArrayFree(_parallaxArray);
|
2013-12-18 17:47:20 +08:00
|
|
|
_parallaxArray = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParallaxNode * ParallaxNode::create()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
ParallaxNode *ret = new (std::nothrow) ParallaxNode();
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void ParallaxNode::addChild(Node* /*child*/, int /*zOrder*/, int /*tag*/)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2013-07-17 14:32:10 +08:00
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void ParallaxNode::addChild(Node* /*child*/, int /*zOrder*/, const std::string& /*name*/)
|
2014-06-25 11:27:48 +08:00
|
|
|
{
|
|
|
|
CCASSERT(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ParallaxNode::addChild(Node *child, int z, const Vec2& ratio, const Vec2& offset)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT( child != nullptr, "Argument must be non-nil");
|
2013-07-23 18:26:26 +08:00
|
|
|
PointObject *obj = PointObject::create(ratio, offset);
|
2012-04-19 14:35:52 +08:00
|
|
|
obj->setChild(child);
|
2014-02-20 10:53:49 +08:00
|
|
|
ccArrayAppendObjectWithResize(_parallaxArray, (Ref*)obj);
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 pos = this->absolutePosition();
|
2014-01-21 16:25:35 +08:00
|
|
|
pos.x = -pos.x + pos.x * ratio.x + offset.x;
|
|
|
|
pos.y = -pos.y + pos.y * ratio.y + offset.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
child->setPosition(pos);
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2014-06-25 11:27:48 +08:00
|
|
|
Node::addChild(child, z, child->getName());
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2014-02-20 10:53:49 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParallaxNode::removeChild(Node* child, bool cleanup)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-09-08 11:26:38 +08:00
|
|
|
for( int i=0;i < _parallaxArray->num;i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
PointObject *point = (PointObject*)_parallaxArray->arr[i];
|
2014-02-20 10:53:49 +08:00
|
|
|
if (point->getChild() == child)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccArrayRemoveObjectAtIndex(_parallaxArray, i, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::removeChild(child, cleanup);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2014-02-20 10:53:49 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParallaxNode::removeAllChildrenWithCleanup(bool cleanup)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccArrayRemoveAllObjects(_parallaxArray);
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::removeAllChildrenWithCleanup(cleanup);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2014-02-20 10:53:49 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 ParallaxNode::absolutePosition()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 ret = _position;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node *cn = this;
|
2013-12-18 17:47:20 +08:00
|
|
|
while (cn->getParent() != nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
cn = cn->getParent();
|
2013-07-11 16:38:58 +08:00
|
|
|
ret = ret + cn->getPosition();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
return ret;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
/*
|
|
|
|
The positions are updated at visit because:
|
|
|
|
- using a timer is not guaranteed that it will called after all the positions were updated
|
|
|
|
- overriding "draw" will only precise if the children have a z > 0
|
|
|
|
*/
|
2014-05-31 07:42:05 +08:00
|
|
|
void ParallaxNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
// Vec2 pos = position_;
|
|
|
|
// Vec2 pos = [self convertToWorldSpace:Vec2::ZERO];
|
|
|
|
Vec2 pos = this->absolutePosition();
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! pos.equals(_lastPosition) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-08 11:26:38 +08:00
|
|
|
for( int i=0; i < _parallaxArray->num; i++ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
PointObject *point = (PointObject*)_parallaxArray->arr[i];
|
2012-04-19 14:35:52 +08:00
|
|
|
float x = -pos.x + pos.x * point->getRatio().x + point->getOffset().x;
|
|
|
|
float y = -pos.y + pos.y * point->getRatio().y + point->getOffset().y;
|
2014-08-28 11:41:18 +08:00
|
|
|
point->getChild()->setPosition(x,y);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_lastPosition = pos;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2014-05-31 07:42:05 +08:00
|
|
|
Node::visit(renderer, parentTransform, parentFlags);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_END
|