2010-12-18 16:12:59 +08:00
|
|
|
/****************************************************************************
|
2012-06-14 15:13:16 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2009 Valentin Milea
|
2011-07-01 15:48:05 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2010-12-18 16:12:59 +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.
|
|
|
|
****************************************************************************/
|
2012-04-14 19:11:57 +08:00
|
|
|
#include "CCString.h"
|
2010-12-18 16:12:59 +08:00
|
|
|
#include "CCNode.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCPointExtension.h"
|
2010-12-18 16:12:59 +08:00
|
|
|
#include "support/TransformUtils.h"
|
|
|
|
#include "CCCamera.h"
|
|
|
|
#include "effects/CCGrid.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "CCScheduler.h"
|
|
|
|
#include "CCTouch.h"
|
|
|
|
#include "CCActionManager.h"
|
2012-02-09 14:07:11 +08:00
|
|
|
#include "CCScriptSupport.h"
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "CCGLProgram.h"
|
2012-04-04 21:58:04 +08:00
|
|
|
// externals
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "kazmath/GL/matrix.h"
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
|
2012-03-23 10:26:36 +08:00
|
|
|
#if CC_NODE_RENDER_SUBPIXEL
|
2010-07-20 14:29:18 +08:00
|
|
|
#define RENDER_IN_SUBPIXEL
|
|
|
|
#else
|
|
|
|
#define RENDER_IN_SUBPIXEL (int)
|
|
|
|
#endif
|
2010-12-18 16:12:59 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2010-12-18 16:12:59 +08:00
|
|
|
|
2012-04-04 21:58:04 +08:00
|
|
|
// XXX: Yes, nodes might have a sort problem once every 15 days if the game runs at 60 FPS and each frame sprites are reordered.
|
2012-03-14 14:55:17 +08:00
|
|
|
static int s_globalOrderOfArrival = 1;
|
|
|
|
|
2010-12-18 16:12:59 +08:00
|
|
|
CCNode::CCNode(void)
|
2011-07-06 20:48:56 +08:00
|
|
|
: m_nZOrder(0)
|
2011-06-10 17:51:37 +08:00
|
|
|
, m_fVertexZ(0.0f)
|
|
|
|
, m_fRotation(0.0f)
|
|
|
|
, m_fScaleX(1.0f)
|
|
|
|
, m_fScaleY(1.0f)
|
|
|
|
, m_tPosition(CCPointZero)
|
2012-03-14 14:55:17 +08:00
|
|
|
, m_fSkewX(0.0f)
|
|
|
|
, m_fSkewY(0.0f)
|
2011-06-10 17:51:37 +08:00
|
|
|
// children (lazy allocs)
|
|
|
|
, m_pChildren(NULL)
|
|
|
|
// lazy alloc
|
|
|
|
, m_pCamera(NULL)
|
|
|
|
, m_pGrid(NULL)
|
|
|
|
, m_bIsVisible(true)
|
|
|
|
, m_tAnchorPoint(CCPointZero)
|
2012-03-14 14:55:17 +08:00
|
|
|
, m_tAnchorPointInPoints(CCPointZero)
|
2011-06-10 17:51:37 +08:00
|
|
|
, m_tContentSize(CCSizeZero)
|
|
|
|
, m_bIsRunning(false)
|
|
|
|
, m_pParent(NULL)
|
2010-12-18 16:12:59 +08:00
|
|
|
// "whole screen" objects. like Scenes and Layers, should set isRelativeAnchorPoint to false
|
2012-06-08 14:17:39 +08:00
|
|
|
, m_bIgnoreAnchorPointForPosition(false)
|
2011-06-10 17:51:37 +08:00
|
|
|
, m_nTag(kCCNodeTagInvalid)
|
|
|
|
// userData is always inited as nil
|
|
|
|
, m_pUserData(NULL)
|
2012-03-16 13:42:53 +08:00
|
|
|
, m_pUserObject(NULL)
|
2011-06-10 17:51:37 +08:00
|
|
|
, m_bIsTransformDirty(true)
|
|
|
|
, m_bIsInverseDirty(true)
|
2012-02-07 11:43:29 +08:00
|
|
|
, m_nScriptHandler(0)
|
2012-03-14 14:55:17 +08:00
|
|
|
, m_pShaderProgram(NULL)
|
|
|
|
, m_nOrderOfArrival(0)
|
|
|
|
, m_glServerState(CC_GL_BLEND)
|
|
|
|
, m_bReorderChildDirty(false)
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// set default scheduler and actionManager
|
|
|
|
CCDirector *director = CCDirector::sharedDirector();
|
|
|
|
m_pActionManager = director->getActionManager();
|
|
|
|
m_pActionManager->retain();
|
|
|
|
m_pScheduler = director->getScheduler();
|
|
|
|
m_pScheduler->retain();
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
2012-03-14 18:11:25 +08:00
|
|
|
|
2012-01-09 23:21:31 +08:00
|
|
|
CCNode::~CCNode(void)
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOGINFO( "cocos2d: deallocing" );
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE(m_pActionManager);
|
|
|
|
CC_SAFE_RELEASE(m_pScheduler);
|
|
|
|
// attributes
|
|
|
|
CC_SAFE_RELEASE(m_pCamera);
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE(m_pGrid);
|
|
|
|
CC_SAFE_RELEASE(m_pShaderProgram);
|
|
|
|
CC_SAFE_RELEASE(m_pUserObject);
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if(m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2011-04-21 14:46:15 +08:00
|
|
|
CCObject* child;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, child)
|
|
|
|
{
|
|
|
|
CCNode* pChild = (CCNode*) child;
|
|
|
|
if (pChild)
|
|
|
|
{
|
|
|
|
pChild->m_pParent = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// children
|
|
|
|
CC_SAFE_RELEASE(m_pChildren);
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2011-07-01 15:48:05 +08:00
|
|
|
float CCNode::getSkewX()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_fSkewX;
|
2011-07-01 15:48:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::setSkewX(float newSkewX)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fSkewX = newSkewX;
|
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
2011-07-01 15:48:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float CCNode::getSkewY()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_fSkewY;
|
2011-07-01 15:48:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::setSkewY(float newSkewY)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fSkewY = newSkewY;
|
2011-07-01 15:48:05 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
2011-07-01 15:48:05 +08:00
|
|
|
}
|
|
|
|
|
2010-12-18 16:12:59 +08:00
|
|
|
/// zOrder getter
|
2011-07-06 20:48:56 +08:00
|
|
|
int CCNode::getZOrder()
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_nZOrder;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// zOrder setter : private method
|
2010-07-14 10:36:26 +08:00
|
|
|
/// used internally to alter the zOrder variable. DON'T call this method manually
|
2012-03-16 13:42:53 +08:00
|
|
|
void CCNode::_setZOrder(int z)
|
2010-07-14 10:36:26 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_nZOrder = z;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
void CCNode::setZOrder(int z)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
_setZOrder(z);
|
|
|
|
if (m_pParent)
|
|
|
|
{
|
|
|
|
m_pParent->reorderChild(this, z);
|
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2010-12-18 16:12:59 +08:00
|
|
|
/// ertexZ getter
|
|
|
|
float CCNode::getVertexZ()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_fVertexZ;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// vertexZ setter
|
|
|
|
void CCNode::setVertexZ(float var)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fVertexZ = var;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// rotation getter
|
|
|
|
float CCNode::getRotation()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_fRotation;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// rotation setter
|
2010-07-12 17:56:06 +08:00
|
|
|
void CCNode::setRotation(float newRotation)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fRotation = newRotation;
|
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// scale getter
|
|
|
|
float CCNode::getScale(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( m_fScaleX == m_fScaleY, "CCNode#scale. ScaleX != ScaleY. Don't know which one to return");
|
|
|
|
return m_fScaleX;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// scale setter
|
2010-07-12 17:56:06 +08:00
|
|
|
void CCNode::setScale(float scale)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fScaleX = m_fScaleY = scale;
|
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2010-12-18 16:12:59 +08:00
|
|
|
|
|
|
|
/// scaleX getter
|
2010-07-08 10:26:58 +08:00
|
|
|
float CCNode::getScaleX()
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_fScaleX;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// scaleX setter
|
2010-07-12 17:56:06 +08:00
|
|
|
void CCNode::setScaleX(float newScaleX)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fScaleX = newScaleX;
|
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
2010-12-18 16:12:59 +08:00
|
|
|
|
|
|
|
/// scaleY getter
|
2010-07-08 10:26:58 +08:00
|
|
|
float CCNode::getScaleY()
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_fScaleY;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
|
|
|
/// scaleY setter
|
|
|
|
void CCNode::setScaleY(float newScaleY)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_fScaleY = newScaleY;
|
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// position getter
|
2011-08-17 21:19:57 +08:00
|
|
|
const CCPoint& CCNode::getPosition()
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tPosition;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// position setter
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCNode::setPosition(const CCPoint& newPosition)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tPosition = newPosition;
|
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
2010-12-31 14:56:24 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 10:39:19 +08:00
|
|
|
const CCPoint& CCNode::getPositionLua(void)
|
|
|
|
{
|
|
|
|
return m_tPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::getPosition(float* x, float* y)
|
|
|
|
{
|
|
|
|
*x = m_tPosition.x;
|
|
|
|
*y = m_tPosition.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCNode::getPositionX(void)
|
|
|
|
{
|
|
|
|
return m_tPosition.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CCNode::getPositionY(void)
|
|
|
|
{
|
|
|
|
return m_tPosition.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::setPositionX(float x)
|
|
|
|
{
|
|
|
|
setPosition(ccp(x, m_tPosition.y));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::setPositionY(float y)
|
|
|
|
{
|
|
|
|
setPosition(ccp(m_tPosition.x, y));
|
|
|
|
}
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-02-02 10:39:19 +08:00
|
|
|
void CCNode::setPosition(float x, float y)
|
|
|
|
{
|
|
|
|
setPosition(ccp(x, y));
|
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// children getter
|
2011-04-21 14:46:15 +08:00
|
|
|
CCArray* CCNode::getChildren()
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pChildren;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2012-02-02 10:39:19 +08:00
|
|
|
unsigned int CCNode::getChildrenCount(void)
|
|
|
|
{
|
|
|
|
return m_pChildren ? m_pChildren->count() : 0;
|
|
|
|
}
|
|
|
|
|
2010-07-14 10:36:26 +08:00
|
|
|
/// camera getter: lazy alloc
|
|
|
|
CCCamera* CCNode::getCamera()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (!m_pCamera)
|
|
|
|
{
|
|
|
|
m_pCamera = new CCCamera();
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_pCamera;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2010-12-18 16:12:59 +08:00
|
|
|
/// grid getter
|
2010-07-14 10:36:26 +08:00
|
|
|
CCGridBase* CCNode::getGrid()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pGrid;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
2010-07-14 10:36:26 +08:00
|
|
|
/// grid setter
|
|
|
|
void CCNode::setGrid(CCGridBase* pGrid)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RETAIN(pGrid);
|
|
|
|
CC_SAFE_RELEASE(m_pGrid);
|
|
|
|
m_pGrid = pGrid;
|
2010-07-14 10:36:26 +08:00
|
|
|
}
|
2010-07-07 15:24:44 +08:00
|
|
|
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isVisible getter
|
|
|
|
bool CCNode::getIsVisible()
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_bIsVisible;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isVisible setter
|
|
|
|
void CCNode::setIsVisible(bool var)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bIsVisible = var;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2012-03-14 14:55:17 +08:00
|
|
|
const CCPoint& CCNode::getAnchorPointInPoints()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tAnchorPointInPoints;
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
|
|
|
/// anchorPoint getter
|
2011-08-17 21:19:57 +08:00
|
|
|
const CCPoint& CCNode::getAnchorPoint()
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tAnchorPoint;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCNode::setAnchorPoint(const CCPoint& point)
|
2010-07-08 10:26:58 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! CCPoint::CCPointEqualToPoint(point, m_tAnchorPoint) )
|
|
|
|
{
|
|
|
|
m_tAnchorPoint = point;
|
|
|
|
m_tAnchorPointInPoints = ccp( m_tContentSize.width * m_tAnchorPoint.x, m_tContentSize.height * m_tAnchorPoint.y );
|
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// contentSize getter
|
2011-08-17 21:19:57 +08:00
|
|
|
const CCSize& CCNode::getContentSize()
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tContentSize;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
void CCNode::setContentSize(const CCSize& size)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! CCSize::CCSizeEqualToSize(size, m_tContentSize) )
|
|
|
|
{
|
|
|
|
m_tContentSize = size;
|
2011-01-04 17:46:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tAnchorPointInPoints = ccp( m_tContentSize.width * m_tAnchorPoint.x, m_tContentSize.height * m_tAnchorPoint.y );
|
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
|
|
|
}
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2011-01-31 06:59:03 +08:00
|
|
|
// isRunning getter
|
2010-07-12 17:56:06 +08:00
|
|
|
bool CCNode::getIsRunning()
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_bIsRunning;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// parent getter
|
|
|
|
CCNode * CCNode::getParent()
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pParent;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
/// parent setter
|
|
|
|
void CCNode::setParent(CCNode * var)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pParent = var;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isRelativeAnchorPoint getter
|
2012-06-08 14:17:39 +08:00
|
|
|
bool CCNode::getIgnoreAnchorPointForPosition()
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-06-08 14:17:39 +08:00
|
|
|
return m_bIgnoreAnchorPointForPosition;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isRelativeAnchorPoint setter
|
2012-06-08 14:17:39 +08:00
|
|
|
void CCNode::setIgnoreAnchorPointForPosition(bool newValue)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-06-08 14:17:39 +08:00
|
|
|
if (newValue != m_bIgnoreAnchorPointForPosition)
|
|
|
|
{
|
|
|
|
m_bIgnoreAnchorPointForPosition = newValue;
|
|
|
|
m_bIsTransformDirty = m_bIsInverseDirty = true;
|
|
|
|
}
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// tag getter
|
|
|
|
int CCNode::getTag()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_nTag;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// tag setter
|
|
|
|
void CCNode::setTag(int var)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_nTag = var;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// userData getter
|
|
|
|
void * CCNode::getUserData()
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pUserData;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// userData setter
|
|
|
|
void CCNode::setUserData(void *var)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pUserData = var;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
2010-12-18 16:12:59 +08:00
|
|
|
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCRect CCNode::boundingBox()
|
2010-07-20 14:29:18 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCRect rect = CCRectMake(0, 0, m_tContentSize.width, m_tContentSize.height);
|
|
|
|
return CCRectApplyAffineTransform(rect, nodeToParentTransform());
|
2010-07-20 14:29:18 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
// CCNode * CCNode::node(void)
|
|
|
|
// {
|
|
|
|
// return CCNode::create();
|
|
|
|
// }
|
|
|
|
|
|
|
|
CCNode * CCNode::create(void)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCNode * pRet = new CCNode();
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::cleanup()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// actions
|
|
|
|
this->stopAllActions();
|
|
|
|
this->unscheduleAllSelectors();
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// timers
|
|
|
|
arrayMakeObjectsPerformSelector(m_pChildren, cleanup, CCNode*);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2010-07-15 11:39:05 +08:00
|
|
|
|
2012-04-14 19:11:57 +08:00
|
|
|
const char* CCNode::description()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return CCString::stringWithFormat("<CCNode | Tag = %d>", m_nTag)->getCString();
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// lazy allocs
|
|
|
|
void CCNode::childrenAlloc(void)
|
|
|
|
{
|
2011-04-21 14:46:15 +08:00
|
|
|
m_pChildren = CCArray::arrayWithCapacity(4);
|
|
|
|
m_pChildren->retain();
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:48:56 +08:00
|
|
|
CCNode* CCNode::getChildByTag(int aTag)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( aTag != kCCNodeTagInvalid, "Invalid tag");
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if(m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2011-04-21 14:46:15 +08:00
|
|
|
CCObject* child;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, child)
|
|
|
|
{
|
|
|
|
CCNode* pNode = (CCNode*) child;
|
2012-04-19 14:35:52 +08:00
|
|
|
if(pNode && pNode->m_nTag == aTag)
|
|
|
|
return pNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* "add" logic MUST only be on this method
|
|
|
|
* If a class want's to extend the 'addChild' behaviour it only needs
|
|
|
|
* to override this method
|
|
|
|
*/
|
2011-07-06 20:48:56 +08:00
|
|
|
void CCNode::addChild(CCNode *child, int zOrder, int tag)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAssert( child != NULL, "Argument must be non-nil");
|
|
|
|
CCAssert( child->m_pParent == NULL, "child already added. It can't be added again");
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( ! m_pChildren )
|
|
|
|
{
|
|
|
|
this->childrenAlloc();
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->insertChild(child, zOrder);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
child->m_nTag = tag;
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
child->setParent(this);
|
|
|
|
child->setOrderOfArrival(s_globalOrderOfArrival++);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( m_bIsRunning )
|
|
|
|
{
|
|
|
|
child->onEnter();
|
|
|
|
child->onEnterTransitionDidFinish();
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:48:56 +08:00
|
|
|
void CCNode::addChild(CCNode *child, int zOrder)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( child != NULL, "Argument must be non-nil");
|
|
|
|
this->addChild(child, zOrder, child->m_nTag);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2010-12-24 15:21:57 +08:00
|
|
|
void CCNode::addChild(CCNode *child)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( child != NULL, "Argument must be non-nil");
|
|
|
|
this->addChild(child, child->m_nZOrder, child->m_nTag);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::removeFromParentAndCleanup(bool cleanup)
|
|
|
|
{
|
2012-05-02 12:04:27 +08:00
|
|
|
if (m_pParent != NULL)
|
|
|
|
{
|
|
|
|
m_pParent->removeChild(this,cleanup);
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* "remove" logic MUST only be on this method
|
|
|
|
* If a class want's to extend the 'removeChild' behavior it only needs
|
|
|
|
* to override this method
|
|
|
|
*/
|
2010-07-14 10:36:26 +08:00
|
|
|
void CCNode::removeChild(CCNode* child, bool cleanup)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// explicit nil handling
|
|
|
|
if (m_pChildren == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( m_pChildren->containsObject(child) )
|
|
|
|
{
|
|
|
|
this->detachChild(child,cleanup);
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:48:56 +08:00
|
|
|
void CCNode::removeChildByTag(int tag, bool cleanup)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( tag != kCCNodeTagInvalid, "Invalid tag");
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCNode *child = this->getChildByTag(tag);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (child == NULL)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: removeChildByTag: child not found!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->removeChild(child, cleanup);
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::removeAllChildrenWithCleanup(bool cleanup)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// not using detachChild improves speed here
|
|
|
|
if ( m_pChildren && m_pChildren->count() > 0 )
|
|
|
|
{
|
2011-04-21 14:46:15 +08:00
|
|
|
CCObject* child;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, child)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2011-04-21 14:46:15 +08:00
|
|
|
CCNode* pNode = (CCNode*) child;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pNode)
|
|
|
|
{
|
|
|
|
// IMPORTANT:
|
|
|
|
// -1st do onExit
|
|
|
|
// -2nd cleanup
|
|
|
|
if(m_bIsRunning)
|
|
|
|
{
|
|
|
|
pNode->onExitTransitionDidStart();
|
|
|
|
pNode->onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cleanup)
|
|
|
|
{
|
|
|
|
pNode->cleanup();
|
|
|
|
}
|
|
|
|
// set parent nil at the end
|
|
|
|
pNode->setParent(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pChildren->removeAllObjects();
|
|
|
|
}
|
|
|
|
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::detachChild(CCNode *child, bool doCleanup)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// IMPORTANT:
|
|
|
|
// -1st do onExit
|
|
|
|
// -2nd cleanup
|
|
|
|
if (m_bIsRunning)
|
|
|
|
{
|
|
|
|
child->onExitTransitionDidStart();
|
|
|
|
child->onExit();
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// If you don't do cleanup, the child's actions will not get removed and the
|
|
|
|
// its scheduledSelectors_ dict will not get released!
|
|
|
|
if (doCleanup)
|
|
|
|
{
|
|
|
|
child->cleanup();
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// set parent nil at the end
|
|
|
|
child->setParent(NULL);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pChildren->removeObject(child);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// helper used by reorderChild & add
|
2011-07-06 20:48:56 +08:00
|
|
|
void CCNode::insertChild(CCNode* child, int z)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bReorderChildDirty = true;
|
|
|
|
ccArrayAppendObjectWithResize(m_pChildren->data, child);
|
2012-03-16 17:56:19 +08:00
|
|
|
child->_setZOrder(z);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:48:56 +08:00
|
|
|
void CCNode::reorderChild(CCNode *child, int zOrder)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( child != NULL, "Child must be non-nil");
|
|
|
|
m_bReorderChildDirty = true;
|
|
|
|
child->setOrderOfArrival(s_globalOrderOfArrival++);
|
|
|
|
child->_setZOrder(zOrder);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2012-04-04 21:58:04 +08:00
|
|
|
void CCNode::sortAllChildren()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_bReorderChildDirty)
|
|
|
|
{
|
|
|
|
int i,j,length = m_pChildren->data->num;
|
|
|
|
CCNode ** x = (CCNode**)m_pChildren->data->arr;
|
|
|
|
CCNode *tempItem;
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// insertion sort
|
|
|
|
for(i=1; i<length; i++)
|
|
|
|
{
|
|
|
|
tempItem = x[i];
|
|
|
|
j = i-1;
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller
|
|
|
|
while(j>=0 && ( tempItem->m_nZOrder < x[j]->m_nZOrder || ( tempItem->m_nZOrder== x[j]->m_nZOrder && tempItem->m_nOrderOfArrival < x[j]->m_nOrderOfArrival ) ) )
|
|
|
|
{
|
|
|
|
x[j+1] = x[j];
|
|
|
|
j = j-1;
|
|
|
|
}
|
|
|
|
x[j+1] = tempItem;
|
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//don't need to check children recursively, that's done in visit of each child
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bReorderChildDirty = false;
|
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
|
|
|
|
2010-12-18 16:12:59 +08:00
|
|
|
void CCNode::draw()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
//CCAssert(0);
|
|
|
|
// override me
|
|
|
|
// Only use- this function to draw your staff.
|
|
|
|
// DON'T draw your stuff outside this method
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
|
|
|
void CCNode::visit()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// quick return if not visible. children won't be drawn.
|
|
|
|
if (!m_bIsVisible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
kmGLPushMatrix();
|
2010-08-17 14:25:41 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_pGrid && m_pGrid->isActive())
|
|
|
|
{
|
|
|
|
m_pGrid->beforeDraw();
|
|
|
|
}
|
2010-12-18 16:12:59 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->transform();
|
2010-08-04 10:36:37 +08:00
|
|
|
|
2011-04-21 14:46:15 +08:00
|
|
|
CCNode* pNode = NULL;
|
|
|
|
unsigned int i = 0;
|
2010-08-04 10:36:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if(m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
// draw children zOrder < 0
|
2011-04-21 14:46:15 +08:00
|
|
|
ccArray *arrayData = m_pChildren->data;
|
|
|
|
for( ; i < arrayData->num; i++ )
|
|
|
|
{
|
|
|
|
pNode = (CCNode*) arrayData->arr[i];
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( pNode && pNode->m_nZOrder < 0 )
|
|
|
|
{
|
|
|
|
pNode->visit();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// self draw
|
|
|
|
this->draw();
|
|
|
|
|
|
|
|
for( ; i < arrayData->num; i++ )
|
|
|
|
{
|
|
|
|
pNode = (CCNode*) arrayData->arr[i];
|
|
|
|
if (pNode)
|
|
|
|
{
|
|
|
|
pNode->visit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset for next frame
|
|
|
|
m_nOrderOfArrival = 0;
|
|
|
|
|
|
|
|
if (m_pGrid && m_pGrid->isActive())
|
|
|
|
{
|
|
|
|
m_pGrid->afterDraw(this);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2010-08-17 14:25:41 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLPopMatrix();
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::transformAncestors()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( m_pParent != NULL )
|
|
|
|
{
|
|
|
|
m_pParent->transformAncestors();
|
|
|
|
m_pParent->transform();
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::transform()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
kmMat4 transfrom4x4;
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Convert 3x3 into 4x4 matrix
|
|
|
|
CCAffineTransform tmpAffine = this->nodeToParentTransform();
|
|
|
|
CGAffineToGL(&tmpAffine, transfrom4x4.mat);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Update Z vertex manually
|
|
|
|
transfrom4x4.mat[14] = m_fVertexZ;
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLMultMatrix( &transfrom4x4 );
|
2012-04-04 21:58:04 +08:00
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// XXX: Expensive calls. Camera should be integrated into the cached affine matrix
|
|
|
|
if ( m_pCamera != NULL && !(m_pGrid != NULL && m_pGrid->isActive()) )
|
|
|
|
{
|
|
|
|
bool translate = (m_tAnchorPointInPoints.x != 0.0f || m_tAnchorPointInPoints.y != 0.0f);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( translate )
|
|
|
|
kmGLTranslatef(RENDER_IN_SUBPIXEL(m_tAnchorPointInPoints.x), RENDER_IN_SUBPIXEL(m_tAnchorPointInPoints.y), 0 );
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pCamera->locate();
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if( translate )
|
|
|
|
kmGLTranslatef(RENDER_IN_SUBPIXEL(-m_tAnchorPointInPoints.x), RENDER_IN_SUBPIXEL(-m_tAnchorPointInPoints.y), 0 );
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCNode::onEnter()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
arrayMakeObjectsPerformSelector(m_pChildren, onEnter, CCNode*);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->resumeSchedulerAndActions();
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bIsRunning = true;
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
if (m_nScriptHandler)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
2012-02-09 14:07:11 +08:00
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeFunctionWithIntegerData(m_nScriptHandler, kCCNodeOnEnter);
|
2012-02-02 15:58:10 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::onEnterTransitionDidFinish()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
arrayMakeObjectsPerformSelector(m_pChildren, onEnterTransitionDidFinish, CCNode*);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2012-04-04 21:58:04 +08:00
|
|
|
void CCNode::onExitTransitionDidStart()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
arrayMakeObjectsPerformSelector(m_pChildren, onExitTransitionDidStart, CCNode*);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2010-07-09 18:24:08 +08:00
|
|
|
void CCNode::onExit()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->pauseSchedulerAndActions();
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bIsRunning = false;
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
if (m_nScriptHandler)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
2012-02-09 14:07:11 +08:00
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeFunctionWithIntegerData(m_nScriptHandler, kCCNodeOnExit);
|
2012-02-02 15:58:10 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
arrayMakeObjectsPerformSelector(m_pChildren, onExit, CCNode*);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2012-02-07 11:43:29 +08:00
|
|
|
void CCNode::registerScriptHandler(int nHandler)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
|
|
|
unregisterScriptHandler();
|
2012-02-07 11:43:29 +08:00
|
|
|
m_nScriptHandler = nHandler;
|
|
|
|
LUALOG("[LUA] Add CCNode event handler: %d", m_nScriptHandler);
|
2012-02-02 15:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::unregisterScriptHandler(void)
|
|
|
|
{
|
2012-02-07 11:43:29 +08:00
|
|
|
if (m_nScriptHandler)
|
2012-02-02 15:58:10 +08:00
|
|
|
{
|
2012-02-09 14:07:11 +08:00
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_nScriptHandler);
|
2012-02-07 11:43:29 +08:00
|
|
|
LUALOG("[LUA] Remove CCNode event handler: %d", m_nScriptHandler);
|
|
|
|
m_nScriptHandler = 0;
|
2012-02-02 15:58:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-04 21:58:04 +08:00
|
|
|
void CCNode::setActionManager(CCActionManager* actionManager)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( actionManager != m_pActionManager ) {
|
|
|
|
this->stopAllActions();
|
|
|
|
CC_SAFE_RETAIN(actionManager);
|
|
|
|
CC_SAFE_RELEASE(m_pActionManager);
|
|
|
|
m_pActionManager = actionManager;
|
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCActionManager* CCNode::getActionManager()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pActionManager;
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2010-08-06 09:46:56 +08:00
|
|
|
CCAction * CCNode::runAction(CCAction* action)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( action != NULL, "Argument must be non-nil");
|
|
|
|
m_pActionManager->addAction(action, this, !m_bIsRunning);
|
|
|
|
return action;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::stopAllActions()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pActionManager->removeAllActionsFromTarget(this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2010-08-06 09:46:56 +08:00
|
|
|
void CCNode::stopAction(CCAction* action)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pActionManager->removeAction(action);
|
2010-08-06 09:46:56 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2011-07-06 20:48:56 +08:00
|
|
|
void CCNode::stopActionByTag(int tag)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( tag != kCCActionTagInvalid, "Invalid tag");
|
|
|
|
m_pActionManager->removeActionByTag(tag, this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:48:56 +08:00
|
|
|
CCAction * CCNode::getActionByTag(int tag)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( tag != kCCActionTagInvalid, "Invalid tag");
|
|
|
|
return m_pActionManager->getActionByTag(tag, this);
|
2010-08-06 09:46:56 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2011-07-01 15:48:05 +08:00
|
|
|
unsigned int CCNode::numberOfRunningActions()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pActionManager->numberOfRunningActionsInTarget(this);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2010-07-14 16:30:25 +08:00
|
|
|
// CCNode - Callbacks
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-04 21:58:04 +08:00
|
|
|
void CCNode::setScheduler(CCScheduler* scheduler)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if( scheduler != m_pScheduler ) {
|
|
|
|
this->unscheduleAllSelectors();
|
|
|
|
CC_SAFE_RETAIN(scheduler);
|
|
|
|
CC_SAFE_RELEASE(m_pScheduler);
|
|
|
|
m_pScheduler = scheduler;
|
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCScheduler* CCNode::getScheduler()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_pScheduler;
|
2012-04-04 21:58:04 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2010-07-09 18:24:08 +08:00
|
|
|
void CCNode::scheduleUpdate()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
scheduleUpdateWithPriority(0);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-07-06 20:48:56 +08:00
|
|
|
void CCNode::scheduleUpdateWithPriority(int priority)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pScheduler->scheduleUpdateForTarget(this, priority, !m_bIsRunning);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::unscheduleUpdate()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pScheduler->unscheduleUpdateForTarget(this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2010-07-14 11:52:27 +08:00
|
|
|
void CCNode::schedule(SEL_SCHEDULE selector)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->schedule(selector, 0.0f, kCCRepeatForever, 0.0f);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void CCNode::schedule(SEL_SCHEDULE selector, float interval)
|
2012-04-04 21:58:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->schedule(selector, interval, kCCRepeatForever, 0.0f);
|
2010-07-14 11:52:27 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( selector, "Argument must be non-nil");
|
|
|
|
CCAssert( interval >=0, "Argument must be positive");
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pScheduler->scheduleSelector(selector, this, interval, !m_bIsRunning, repeat, delay);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void CCNode::scheduleOnce(SEL_SCHEDULE selector, float delay)
|
2012-04-04 21:58:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->schedule(selector, 0.0f, 0, delay);
|
2010-07-14 11:52:27 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2010-07-14 11:52:27 +08:00
|
|
|
void CCNode::unschedule(SEL_SCHEDULE selector)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// explicit nil handling
|
|
|
|
if (selector == 0)
|
|
|
|
return;
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pScheduler->unscheduleSelector(selector, this);
|
2010-07-14 11:52:27 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
|
|
|
void CCNode::unscheduleAllSelectors()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pScheduler->unscheduleAllSelectorsForTarget(this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::resumeSchedulerAndActions()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pScheduler->resumeTarget(this);
|
|
|
|
m_pActionManager->resumeTarget(this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCNode::pauseSchedulerAndActions()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pScheduler->pauseTarget(this);
|
|
|
|
m_pActionManager->pauseTarget(this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAffineTransform CCNode::nodeToParentTransform(void)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-06-08 14:17:39 +08:00
|
|
|
if (m_bIsTransformDirty)
|
|
|
|
{
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Translate values
|
|
|
|
float x = m_tPosition.x;
|
|
|
|
float y = m_tPosition.y;
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-06-08 14:17:39 +08:00
|
|
|
if (m_bIgnoreAnchorPointForPosition)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
x += m_tAnchorPointInPoints.x;
|
|
|
|
y += m_tAnchorPointInPoints.y;
|
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Rotation values
|
|
|
|
float c = 1, s = 0;
|
2012-06-08 14:17:39 +08:00
|
|
|
if (m_fRotation)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
float radians = -CC_DEGREES_TO_RADIANS(m_fRotation);
|
|
|
|
c = cosf(radians);
|
|
|
|
s = sinf(radians);
|
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
bool needsSkewMatrix = ( m_fSkewX || m_fSkewY );
|
2012-04-04 21:58:04 +08:00
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// optimization:
|
|
|
|
// inline anchor point calculation if skew is not needed
|
2012-06-08 14:17:39 +08:00
|
|
|
if (! needsSkewMatrix && !CCPoint::CCPointEqualToPoint(m_tAnchorPointInPoints, CCPointZero))
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
x += c * -m_tAnchorPointInPoints.x * m_fScaleX + -s * -m_tAnchorPointInPoints.y * m_fScaleY;
|
|
|
|
y += s * -m_tAnchorPointInPoints.x * m_fScaleX + c * -m_tAnchorPointInPoints.y * m_fScaleY;
|
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Build Transform Matrix
|
|
|
|
m_tTransform = CCAffineTransformMake( c * m_fScaleX, s * m_fScaleX,
|
|
|
|
-s * m_fScaleY, c * m_fScaleY,
|
|
|
|
x, y );
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// XXX: Try to inline skew
|
|
|
|
// If skew is needed, apply skew and then anchor point
|
2012-06-08 14:17:39 +08:00
|
|
|
if (needsSkewMatrix)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAffineTransform skewMatrix = CCAffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(m_fSkewY)),
|
|
|
|
tanf(CC_DEGREES_TO_RADIANS(m_fSkewX)), 1.0f,
|
|
|
|
0.0f, 0.0f );
|
|
|
|
m_tTransform = CCAffineTransformConcat(skewMatrix, m_tTransform);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// adjust anchor point
|
2012-06-08 14:17:39 +08:00
|
|
|
if (! CCPoint::CCPointEqualToPoint(m_tAnchorPointInPoints, CCPointZero))
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tTransform = CCAffineTransformTranslate(m_tTransform, -m_tAnchorPointInPoints.x, -m_tAnchorPointInPoints.y);
|
2012-06-08 14:17:39 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bIsTransformDirty = false;
|
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tTransform;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAffineTransform CCNode::parentToNodeTransform(void)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if ( m_bIsInverseDirty ) {
|
|
|
|
m_tInverse = CCAffineTransformInvert(this->nodeToParentTransform());
|
|
|
|
m_bIsInverseDirty = false;
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tInverse;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAffineTransform CCNode::nodeToWorldTransform()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAffineTransform t = this->nodeToParentTransform();
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
for (CCNode *p = m_pParent; p != NULL; p = p->getParent())
|
|
|
|
t = CCAffineTransformConcat(t, p->nodeToParentTransform());
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return t;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
2010-07-21 17:40:10 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
CCAffineTransform CCNode::worldToNodeTransform(void)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return CCAffineTransformInvert(this->nodeToWorldTransform());
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
CCPoint CCNode::convertToNodeSpace(const CCPoint& worldPoint)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint ret = CCPointApplyAffineTransform(worldPoint, worldToNodeTransform());
|
|
|
|
return ret;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
CCPoint CCNode::convertToWorldSpace(const CCPoint& nodePoint)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint ret = CCPointApplyAffineTransform(nodePoint, nodeToWorldTransform());
|
|
|
|
return ret;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
CCPoint CCNode::convertToNodeSpaceAR(const CCPoint& worldPoint)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint nodePoint = convertToNodeSpace(worldPoint);
|
|
|
|
return ccpSub(nodePoint, m_tAnchorPointInPoints);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
CCPoint CCNode::convertToWorldSpaceAR(const CCPoint& nodePoint)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint pt = ccpAdd(nodePoint, m_tAnchorPointInPoints);
|
|
|
|
return convertToWorldSpace(pt);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2011-08-17 21:19:57 +08:00
|
|
|
CCPoint CCNode::convertToWindowSpace(const CCPoint& nodePoint)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint worldPoint = this->convertToWorldSpace(nodePoint);
|
|
|
|
return CCDirector::sharedDirector()->convertToUI(worldPoint);
|
2010-07-20 13:49:13 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2011-03-07 17:11:57 +08:00
|
|
|
// convenience methods which take a CCTouch instead of CCPoint
|
|
|
|
CCPoint CCNode::convertTouchToNodeSpace(CCTouch *touch)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint point = touch->locationInView();
|
|
|
|
point = CCDirector::sharedDirector()->convertToGL(point);
|
|
|
|
return this->convertToNodeSpace(point);
|
2010-07-30 16:35:07 +08:00
|
|
|
}
|
2011-03-07 17:11:57 +08:00
|
|
|
CCPoint CCNode::convertTouchToNodeSpaceAR(CCTouch *touch)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint point = touch->locationInView();
|
|
|
|
point = CCDirector::sharedDirector()->convertToGL(point);
|
|
|
|
return this->convertToNodeSpaceAR(point);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
2010-07-30 16:35:07 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|
|
|
|
|