2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
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) 2009-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCParallaxNode.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "support/CCPointExtension.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "support/data_support/ccCArray.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
class PointObject : Object
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_SYNTHESIZE(Point, _ratio, Ratio)
|
|
|
|
CC_SYNTHESIZE(Point, _offset, Offset)
|
|
|
|
CC_SYNTHESIZE(Node *,_child, Child) // weak ref
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2013-05-23 15:06:57 +08:00
|
|
|
public:
|
2013-06-20 14:13:12 +08:00
|
|
|
static PointObject * pointWithPoint(Point ratio, Point offset)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
PointObject *pRet = new PointObject();
|
|
|
|
pRet->initWithPoint(ratio, offset);
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
bool initWithPoint(Point ratio, Point offset)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_ratio = ratio;
|
|
|
|
_offset = offset;
|
|
|
|
_child = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
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);
|
|
|
|
_lastPosition = CCPointMake(-100,-100);
|
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
|
|
|
if( _parallaxArray )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccArrayFree(_parallaxArray);
|
|
|
|
_parallaxArray = NULL;
|
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
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ParallaxNode *pRet = new ParallaxNode();
|
2012-04-19 14:35:52 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParallaxNode::addChild(Node * child, unsigned int zOrder, int tag)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(zOrder);
|
|
|
|
CC_UNUSED_PARAM(child);
|
|
|
|
CC_UNUSED_PARAM(tag);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(0,"ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParallaxNode::addChild(Node *child, unsigned int z, const Point& ratio, const Point& offset)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( child != NULL, "Argument must be non-nil");
|
2013-06-20 14:13:12 +08:00
|
|
|
PointObject *obj = PointObject::pointWithPoint(ratio, offset);
|
2012-04-19 14:35:52 +08:00
|
|
|
obj->setChild(child);
|
2013-06-20 14:13:12 +08:00
|
|
|
ccArrayAppendObjectWithResize(_parallaxArray, (Object*)obj);
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Point pos = _position;
|
2012-04-19 14:35:52 +08:00
|
|
|
pos.x = pos.x * ratio.x + offset.x;
|
|
|
|
pos.y = pos.y * ratio.y + offset.y;
|
|
|
|
child->setPosition(pos);
|
2010-08-26 11:30:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::addChild(child, z, child->getTag());
|
2012-04-18 18:43:45 +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-06-15 14:03:30 +08:00
|
|
|
for( unsigned 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
|
|
|
if( point->getChild()->isEqual(child))
|
|
|
|
{
|
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
|
|
|
}
|
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
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
Point ParallaxNode::absolutePosition()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Point ret = _position;
|
|
|
|
Node *cn = this;
|
2012-04-19 14:35:52 +08:00
|
|
|
while (cn->getParent() != NULL)
|
|
|
|
{
|
|
|
|
cn = cn->getParent();
|
|
|
|
ret = ccpAdd( ret, cn->getPosition());
|
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParallaxNode::visit()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
// Point pos = position_;
|
|
|
|
// Point pos = [self convertToWorldSpace:PointZero];
|
|
|
|
Point pos = this->absolutePosition();
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! pos.equals(_lastPosition) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for(unsigned 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;
|
|
|
|
point->getChild()->setPosition(ccp(x,y));
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_lastPosition = pos;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::visit();
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_END
|