axmol/cocos2dx/base_nodes/CCNode.cpp

1250 lines
28 KiB
C++
Raw Normal View History

2010-12-18 16:12:59 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2009 Valentin Milea
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.
****************************************************************************/
#include "cocoa/CCString.h"
2010-12-18 16:12:59 +08:00
#include "CCNode.h"
#include "support/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 "touch_dispatcher/CCTouch.h"
#include "actions/CCActionManager.h"
#include "script_support/CCScriptSupport.h"
#include "shaders/CCGLProgram.h"
2012-04-04 21:58:04 +08:00
// externals
#include "kazmath/GL/matrix.h"
2012-02-02 15:58:10 +08:00
2012-04-14 19:11:57 +08:00
#if CC_NODE_RENDER_SUBPIXEL
2010-07-20 14:29:18 +08:00
#define RENDER_IN_SUBPIXEL
#else
2012-11-14 18:05:15 +08:00
#define RENDER_IN_SUBPIXEL (__ARGS__) (ceil(__ARGS__))
2010-07-20 14:29:18 +08:00
#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.
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)
2012-11-14 18:05:15 +08:00
, m_fRotationX(0.0f)
, m_fRotationY(0.0f)
2011-06-10 17:51:37 +08:00
, m_fScaleX(1.0f)
, m_fScaleY(1.0f)
2012-11-14 18:05:15 +08:00
, m_obPosition(CCPointZero)
, 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)
2012-11-14 18:05:15 +08:00
, m_bVisible(true)
, m_obAnchorPoint(CCPointZero)
, m_obAnchorPointInPoints(CCPointZero)
, m_obContentSize(CCSizeZero)
, m_bRunning(false)
2011-06-10 17:51:37 +08:00
, m_pParent(NULL)
// "whole screen" objects. like Scenes and Layers, should set m_bIgnoreAnchorPointForPosition 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)
, m_pUserObject(NULL)
2012-11-14 18:05:15 +08:00
, m_bTransformDirty(true)
, m_bInverseDirty(true)
2012-02-07 11:43:29 +08:00
, m_nScriptHandler(0)
, m_nUpdateScriptHandler(0)
, m_pShaderProgram(NULL)
2012-11-14 18:05:15 +08:00
, m_uOrderOfArrival(0)
, m_eGLServerState(ccGLServerState(0))
, m_bReorderChildDirty(false)
2010-12-18 16:12:59 +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();
CCScriptEngineProtocol* pEngine = CCScriptEngineManager::sharedManager()->getScriptEngine();
m_eScriptType = pEngine != NULL ? pEngine->getScriptType() : kScriptTypeNone;
2010-12-18 16:12:59 +08:00
}
CCNode::~CCNode(void)
2010-12-18 16:12:59 +08:00
{
CCLOGINFO( "cocos2d: deallocing" );
unregisterScriptHandler();
if (m_nUpdateScriptHandler)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nUpdateScriptHandler);
}
2010-07-14 10:36:26 +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
CC_SAFE_RELEASE(m_pGrid);
CC_SAFE_RELEASE(m_pShaderProgram);
CC_SAFE_RELEASE(m_pUserObject);
2010-07-14 10:36:26 +08:00
if(m_pChildren && m_pChildren->count() > 0)
{
CCObject* child;
CCARRAY_FOREACH(m_pChildren, child)
{
CCNode* pChild = (CCNode*) child;
if (pChild)
{
pChild->m_pParent = NULL;
}
}
}
2010-07-14 10:36:26 +08:00
// children
CC_SAFE_RELEASE(m_pChildren);
2010-12-18 16:12:59 +08:00
}
float CCNode::getSkewX()
{
return m_fSkewX;
}
void CCNode::setSkewX(float newSkewX)
{
m_fSkewX = newSkewX;
2012-11-14 18:05:15 +08:00
m_bTransformDirty = m_bInverseDirty = true;
}
float CCNode::getSkewY()
{
return m_fSkewY;
}
void CCNode::setSkewY(float newSkewY)
{
m_fSkewY = newSkewY;
2012-11-14 18:05:15 +08:00
m_bTransformDirty = m_bInverseDirty = true;
}
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
{
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
void CCNode::_setZOrder(int z)
2010-07-14 10:36:26 +08:00
{
m_nZOrder = z;
2010-12-18 16:12:59 +08:00
}
void CCNode::setZOrder(int z)
{
_setZOrder(z);
if (m_pParent)
{
m_pParent->reorderChild(this, z);
}
}
/// vertexZ getter
2010-12-18 16:12:59 +08:00
float CCNode::getVertexZ()
{
return m_fVertexZ;
2010-12-18 16:12:59 +08:00
}
/// vertexZ setter
void CCNode::setVertexZ(float var)
{
m_fVertexZ = var;
2010-12-18 16:12:59 +08:00
}
/// rotation getter
float CCNode::getRotation()
{
2012-11-14 18:05:15 +08:00
CCAssert(m_fRotationX == m_fRotationY, "CCNode#rotation. RotationX != RotationY. Don't know which one to return");
return m_fRotationX;
2010-12-18 16:12:59 +08:00
}
/// rotation setter
void CCNode::setRotation(float newRotation)
{
2012-11-14 18:05:15 +08:00
m_fRotationX = m_fRotationY = newRotation;
m_bTransformDirty = m_bInverseDirty = true;
}
float CCNode::getRotationX()
{
return m_fRotationX;
}
void CCNode::setRotationX(float fRotationX)
{
m_fRotationX = fRotationX;
m_bTransformDirty = m_bInverseDirty = true;
}
float CCNode::getRotationY()
{
return m_fRotationY;
}
void CCNode::setRotationY(float fRotationY)
{
m_fRotationY = fRotationY;
m_bTransformDirty = m_bInverseDirty = true;
2010-12-18 16:12:59 +08:00
}
/// scale getter
float CCNode::getScale(void)
{
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
void CCNode::setScale(float scale)
{
m_fScaleX = m_fScaleY = scale;
2012-11-14 18:05:15 +08:00
m_bTransformDirty = m_bInverseDirty = true;
}
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
{
return m_fScaleX;
2010-12-18 16:12:59 +08:00
}
/// scaleX setter
void CCNode::setScaleX(float newScaleX)
{
m_fScaleX = newScaleX;
2012-11-14 18:05:15 +08:00
m_bTransformDirty = m_bInverseDirty = 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
{
return m_fScaleY;
2010-12-18 16:12:59 +08:00
}
/// scaleY setter
void CCNode::setScaleY(float newScaleY)
{
m_fScaleY = newScaleY;
2012-11-14 18:05:15 +08:00
m_bTransformDirty = m_bInverseDirty = true;
2010-12-18 16:12:59 +08:00
}
/// position getter
CCPoint CCNode::getPosition()
2010-12-18 16:12:59 +08:00
{
2012-11-14 18:05:15 +08:00
return m_obPosition;
2010-12-18 16:12:59 +08:00
}
/// position setter
void CCNode::setPosition(const CCPoint& newPosition)
2010-07-07 15:24:44 +08:00
{
2012-11-14 18:05:15 +08:00
m_obPosition = newPosition;
m_bTransformDirty = m_bInverseDirty = true;
}
const CCPoint& CCNode::getPositionLua(void)
{
2012-11-14 18:05:15 +08:00
return m_obPosition;
}
void CCNode::getPosition(float* x, float* y)
{
2012-11-14 18:05:15 +08:00
*x = m_obPosition.x;
*y = m_obPosition.y;
}
float CCNode::getPositionX(void)
{
2012-11-14 18:05:15 +08:00
return m_obPosition.x;
}
float CCNode::getPositionY(void)
{
2012-11-14 18:05:15 +08:00
return m_obPosition.y;
}
void CCNode::setPositionX(float x)
{
2012-11-14 18:05:15 +08:00
setPosition(ccp(x, m_obPosition.y));
}
void CCNode::setPositionY(float y)
{
2012-11-14 18:05:15 +08:00
setPosition(ccp(m_obPosition.x, y));
}
2012-02-02 15:58:10 +08:00
void CCNode::setPosition(float x, float y)
{
setPosition(ccp(x, y));
}
/// children getter
CCArray* CCNode::getChildren()
2010-07-07 15:24:44 +08:00
{
return m_pChildren;
}
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()
{
if (!m_pCamera)
{
m_pCamera = new CCCamera();
}
return m_pCamera;
}
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()
{
return m_pGrid;
2010-07-07 15:24:44 +08:00
}
2010-07-14 10:36:26 +08:00
/// grid setter
void CCNode::setGrid(CCGridBase* pGrid)
{
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
/// isVisible getter
bool CCNode::isVisible()
2010-07-07 15:24:44 +08:00
{
2012-11-14 18:05:15 +08:00
return m_bVisible;
2010-07-07 15:24:44 +08:00
}
/// isVisible setter
void CCNode::setVisible(bool var)
2010-07-07 15:24:44 +08:00
{
2012-11-14 18:05:15 +08:00
m_bVisible = var;
2010-07-07 15:24:44 +08:00
}
CCPoint CCNode::getAnchorPointInPoints()
{
2012-11-14 18:05:15 +08:00
return m_obAnchorPointInPoints;
}
/// anchorPoint getter
CCPoint CCNode::getAnchorPoint()
2010-07-07 15:24:44 +08:00
{
2012-11-14 18:05:15 +08:00
return m_obAnchorPoint;
2010-12-18 16:12:59 +08:00
}
void CCNode::setAnchorPoint(const CCPoint& point)
2010-07-08 10:26:58 +08:00
{
2012-11-14 18:05:15 +08:00
if( ! point.equals(m_obAnchorPoint))
{
2012-11-14 18:05:15 +08:00
m_obAnchorPoint = point;
m_obAnchorPointInPoints = ccp(m_obContentSize.width * m_obAnchorPoint.x, m_obContentSize.height * m_obAnchorPoint.y );
m_bTransformDirty = m_bInverseDirty = true;
}
}
/// contentSize getter
CCSize CCNode::getContentSize()
{
2012-11-14 18:05:15 +08:00
return m_obContentSize;
}
void CCNode::setContentSize(const CCSize & size)
2010-07-07 15:24:44 +08:00
{
2012-11-14 18:05:15 +08:00
if ( ! size.equals(m_obContentSize))
{
2012-11-14 18:05:15 +08:00
m_obContentSize = size;
2011-01-04 17:46:23 +08:00
2012-11-14 18:05:15 +08:00
m_obAnchorPointInPoints = ccp(m_obContentSize.width * m_obAnchorPoint.x, m_obContentSize.height * m_obAnchorPoint.y );
m_bTransformDirty = m_bInverseDirty = true;
}
2010-07-07 15:24:44 +08:00
}
2011-01-31 06:59:03 +08:00
// isRunning getter
bool CCNode::isRunning()
2010-07-07 15:24:44 +08:00
{
2012-11-14 18:05:15 +08:00
return m_bRunning;
2010-07-07 15:24:44 +08:00
}
/// parent getter
CCNode * CCNode::getParent()
2010-07-07 15:24:44 +08:00
{
return m_pParent;
2010-07-07 15:24:44 +08:00
}
/// parent setter
void CCNode::setParent(CCNode * var)
2010-07-07 15:24:44 +08:00
{
m_pParent = var;
2010-07-07 15:24:44 +08:00
}
/// isRelativeAnchorPoint getter
bool CCNode::isIgnoreAnchorPointForPosition()
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
}
/// isRelativeAnchorPoint setter
void CCNode::ignoreAnchorPointForPosition(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;
2012-11-14 18:05:15 +08:00
m_bTransformDirty = m_bInverseDirty = true;
2012-06-08 14:17:39 +08:00
}
2010-12-18 16:12:59 +08:00
}
/// tag getter
int CCNode::getTag()
{
return m_nTag;
2010-12-18 16:12:59 +08:00
}
/// tag setter
void CCNode::setTag(int var)
{
m_nTag = var;
2010-12-18 16:12:59 +08:00
}
/// userData getter
void * CCNode::getUserData()
2010-07-07 15:24:44 +08:00
{
return m_pUserData;
2010-07-07 15:24:44 +08:00
}
/// userData setter
void CCNode::setUserData(void *var)
2010-07-07 15:24:44 +08:00
{
m_pUserData = var;
2010-07-07 15:24:44 +08:00
}
2010-12-18 16:12:59 +08:00
2012-11-14 18:05:15 +08:00
unsigned int CCNode::getOrderOfArrival()
{
return m_uOrderOfArrival;
}
void CCNode::setOrderOfArrival(unsigned int uOrderOfArrival)
{
m_uOrderOfArrival = uOrderOfArrival;
}
CCGLProgram* CCNode::getShaderProgram()
{
return m_pShaderProgram;
}
CCObject* CCNode::getUserObject()
{
return m_pUserObject;
}
ccGLServerState CCNode::getGLServerState()
{
return m_eGLServerState;
}
void CCNode::setGLServerState(ccGLServerState glServerState)
{
m_eGLServerState = glServerState;
}
void CCNode::setUserObject(CCObject *pUserObject)
{
CC_SAFE_RELEASE(m_pUserObject);
CC_SAFE_RETAIN(pUserObject);
2012-11-14 18:05:15 +08:00
m_pUserObject = pUserObject;
}
void CCNode::setShaderProgram(CCGLProgram *pShaderProgram)
{
CC_SAFE_RELEASE(m_pShaderProgram);
m_pShaderProgram = pShaderProgram;
CC_SAFE_RETAIN(m_pShaderProgram);
}
2010-12-18 16:12:59 +08:00
CCRect CCNode::boundingBox()
2010-07-20 14:29:18 +08:00
{
2012-11-14 18:05:15 +08:00
CCRect rect = CCRectMake(0, 0, m_obContentSize.width, m_obContentSize.height);
return CCRectApplyAffineTransform(rect, nodeToParentTransform());
2010-07-20 14:29:18 +08:00
}
CCNode * CCNode::create(void)
{
CCNode * pRet = new CCNode();
pRet->autorelease();
return pRet;
}
void CCNode::cleanup()
{
// actions
this->stopAllActions();
this->unscheduleAllSelectors();
if ( m_eScriptType != kScriptTypeNone)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kCCNodeOnCleanup);
}
// timers
arrayMakeObjectsPerformSelector(m_pChildren, cleanup, CCNode*);
}
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()
{
return CCString::createWithFormat("<CCNode | Tag = %d>", m_nTag)->getCString();
}
// lazy allocs
void CCNode::childrenAlloc(void)
{
m_pChildren = CCArray::createWithCapacity(4);
m_pChildren->retain();
}
2011-07-06 20:48:56 +08:00
CCNode* CCNode::getChildByTag(int aTag)
{
CCAssert( aTag != kCCNodeTagInvalid, "Invalid tag");
if(m_pChildren && m_pChildren->count() > 0)
{
CCObject* child;
CCARRAY_FOREACH(m_pChildren, child)
{
CCNode* pNode = (CCNode*) child;
if(pNode && pNode->m_nTag == aTag)
return pNode;
}
}
return NULL;
}
/* "add" logic MUST only be on this method
* If a class want's to extend the 'addChild' behavior it only needs
* to override this method
*/
2011-07-06 20:48:56 +08:00
void CCNode::addChild(CCNode *child, int zOrder, int tag)
{
CCAssert( child != NULL, "Argument must be non-nil");
CCAssert( child->m_pParent == NULL, "child already added. It can't be added again");
if( ! m_pChildren )
{
this->childrenAlloc();
}
this->insertChild(child, zOrder);
child->m_nTag = tag;
child->setParent(this);
child->setOrderOfArrival(s_globalOrderOfArrival++);
2012-11-14 18:05:15 +08:00
if( m_bRunning )
{
child->onEnter();
child->onEnterTransitionDidFinish();
}
}
2011-07-06 20:48:56 +08:00
void CCNode::addChild(CCNode *child, int zOrder)
{
CCAssert( child != NULL, "Argument must be non-nil");
this->addChild(child, zOrder, child->m_nTag);
}
void CCNode::addChild(CCNode *child)
{
CCAssert( child != NULL, "Argument must be non-nil");
this->addChild(child, child->m_nZOrder, child->m_nTag);
}
2012-11-09 12:08:18 +08:00
void CCNode::removeFromParent()
{
this->removeFromParentAndCleanup(true);
}
void CCNode::removeFromParentAndCleanup(bool cleanup)
{
if (m_pParent != NULL)
{
m_pParent->removeChild(this,cleanup);
}
}
void CCNode::removeChild(CCNode* child)
{
this->removeChild(child, true);
}
/* "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)
{
// explicit nil handling
if (m_pChildren == NULL)
{
return;
}
if ( m_pChildren->containsObject(child) )
{
this->detachChild(child,cleanup);
}
}
void CCNode::removeChildByTag(int tag)
{
this->removeChildByTag(tag, true);
}
2011-07-06 20:48:56 +08:00
void CCNode::removeChildByTag(int tag, bool cleanup)
{
CCAssert( tag != kCCNodeTagInvalid, "Invalid tag");
CCNode *child = this->getChildByTag(tag);
if (child == NULL)
{
CCLOG("cocos2d: removeChildByTag: child not found!");
}
else
{
this->removeChild(child, cleanup);
}
}
void CCNode::removeAllChildren()
{
this->removeAllChildrenWithCleanup(true);
}
void CCNode::removeAllChildrenWithCleanup(bool cleanup)
{
// not using detachChild improves speed here
if ( m_pChildren && m_pChildren->count() > 0 )
{
CCObject* child;
CCARRAY_FOREACH(m_pChildren, child)
{
CCNode* pNode = (CCNode*) child;
if (pNode)
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
2012-11-14 18:05:15 +08:00
if(m_bRunning)
{
pNode->onExitTransitionDidStart();
pNode->onExit();
}
if (cleanup)
{
pNode->cleanup();
}
// set parent nil at the end
pNode->setParent(NULL);
}
}
m_pChildren->removeAllObjects();
}
}
void CCNode::detachChild(CCNode *child, bool doCleanup)
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
2012-11-14 18:05:15 +08:00
if (m_bRunning)
{
child->onExitTransitionDidStart();
child->onExit();
}
// 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();
}
// set parent nil at the end
child->setParent(NULL);
m_pChildren->removeObject(child);
}
// helper used by reorderChild & add
2011-07-06 20:48:56 +08:00
void CCNode::insertChild(CCNode* child, int z)
{
m_bReorderChildDirty = true;
ccArrayAppendObjectWithResize(m_pChildren->data, child);
child->_setZOrder(z);
}
2011-07-06 20:48:56 +08:00
void CCNode::reorderChild(CCNode *child, int zOrder)
{
CCAssert( child != NULL, "Child must be non-nil");
m_bReorderChildDirty = true;
child->setOrderOfArrival(s_globalOrderOfArrival++);
child->_setZOrder(zOrder);
}
2012-04-04 21:58:04 +08:00
void CCNode::sortAllChildren()
{
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
// insertion sort
for(i=1; i<length; i++)
{
tempItem = x[i];
j = i-1;
2012-04-04 21:58:04 +08:00
//continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller
2012-11-14 18:05:15 +08:00
while(j>=0 && ( tempItem->m_nZOrder < x[j]->m_nZOrder || ( tempItem->m_nZOrder== x[j]->m_nZOrder && tempItem->m_uOrderOfArrival < x[j]->m_uOrderOfArrival ) ) )
{
x[j+1] = x[j];
j = j-1;
}
x[j+1] = tempItem;
}
2012-04-04 21:58:04 +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
m_bReorderChildDirty = false;
}
2012-04-04 21:58:04 +08:00
}
2010-12-18 16:12:59 +08:00
void CCNode::draw()
{
//CCAssert(0);
// override me
// Only use- this function to draw your stuff.
// DON'T draw your stuff outside this method
2010-12-18 16:12:59 +08:00
}
void CCNode::visit()
{
// quick return if not visible. children won't be drawn.
2012-11-14 18:05:15 +08:00
if (!m_bVisible)
{
return;
}
kmGLPushMatrix();
2010-08-17 14:25:41 +08:00
if (m_pGrid && m_pGrid->isActive())
{
m_pGrid->beforeDraw();
}
2010-12-18 16:12:59 +08:00
this->transform();
CCNode* pNode = NULL;
unsigned int i = 0;
if(m_pChildren && m_pChildren->count() > 0)
{
sortAllChildren();
// draw children zOrder < 0
ccArray *arrayData = m_pChildren->data;
for( ; i < arrayData->num; i++ )
{
pNode = (CCNode*) arrayData->arr[i];
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
2012-11-14 18:05:15 +08:00
m_uOrderOfArrival = 0;
if (m_pGrid && m_pGrid->isActive())
{
m_pGrid->afterDraw(this);
}
2010-08-17 14:25:41 +08:00
kmGLPopMatrix();
}
void CCNode::transformAncestors()
{
if( m_pParent != NULL )
{
m_pParent->transformAncestors();
m_pParent->transform();
}
}
void CCNode::transform()
{
kmMat4 transfrom4x4;
2012-04-04 21:58:04 +08:00
// Convert 3x3 into 4x4 matrix
CCAffineTransform tmpAffine = this->nodeToParentTransform();
CGAffineToGL(&tmpAffine, transfrom4x4.mat);
2012-04-04 21:58:04 +08:00
// Update Z vertex manually
transfrom4x4.mat[14] = m_fVertexZ;
2012-04-04 21:58:04 +08:00
kmGLMultMatrix( &transfrom4x4 );
2012-04-04 21:58:04 +08:00
// XXX: Expensive calls. Camera should be integrated into the cached affine matrix
if ( m_pCamera != NULL && !(m_pGrid != NULL && m_pGrid->isActive()) )
{
2012-11-14 18:05:15 +08:00
bool translate = (m_obAnchorPointInPoints.x != 0.0f || m_obAnchorPointInPoints.y != 0.0f);
2012-04-04 21:58:04 +08:00
if( translate )
2012-11-14 18:05:15 +08:00
kmGLTranslatef(RENDER_IN_SUBPIXEL(m_obAnchorPointInPoints.x), RENDER_IN_SUBPIXEL(m_obAnchorPointInPoints.y), 0 );
2012-04-04 21:58:04 +08:00
m_pCamera->locate();
2012-04-04 21:58:04 +08:00
if( translate )
2012-11-14 18:05:15 +08:00
kmGLTranslatef(RENDER_IN_SUBPIXEL(-m_obAnchorPointInPoints.x), RENDER_IN_SUBPIXEL(-m_obAnchorPointInPoints.y), 0 );
}
}
void CCNode::onEnter()
{
arrayMakeObjectsPerformSelector(m_pChildren, onEnter, CCNode*);
this->resumeSchedulerAndActions();
2012-11-14 18:05:15 +08:00
m_bRunning = true;
2012-02-02 15:58:10 +08:00
if (m_eScriptType != kScriptTypeNone)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kCCNodeOnEnter);
2012-02-02 15:58:10 +08:00
}
}
void CCNode::onEnterTransitionDidFinish()
{
arrayMakeObjectsPerformSelector(m_pChildren, onEnterTransitionDidFinish, CCNode*);
if (m_eScriptType == kScriptTypeJavascript)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kCCNodeOnEnterTransitionDidFinish);
}
}
2012-04-04 21:58:04 +08:00
void CCNode::onExitTransitionDidStart()
{
arrayMakeObjectsPerformSelector(m_pChildren, onExitTransitionDidStart, CCNode*);
if (m_eScriptType == kScriptTypeJavascript)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kCCNodeOnExitTransitionDidStart);
}
}
void CCNode::onExit()
{
this->pauseSchedulerAndActions();
2010-07-14 10:36:26 +08:00
2012-11-14 18:05:15 +08:00
m_bRunning = false;
2010-07-14 10:36:26 +08:00
if ( m_eScriptType != kScriptTypeNone)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeNodeEvent(this, kCCNodeOnExit);
}
2012-02-02 15:58:10 +08:00
arrayMakeObjectsPerformSelector(m_pChildren, onExit, CCNode*);
}
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
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(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
}
}
void CCNode::scheduleUpdateWithPriorityLua(int nHandler, int priority)
{
unscheduleUpdate();
m_nUpdateScriptHandler = nHandler;
m_pScheduler->scheduleUpdateForTarget(this, priority, !m_bRunning);
}
2012-04-04 21:58:04 +08:00
void CCNode::setActionManager(CCActionManager* actionManager)
{
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()
{
return m_pActionManager;
}
2010-08-06 09:46:56 +08:00
CCAction * CCNode::runAction(CCAction* action)
{
CCAssert( action != NULL, "Argument must be non-nil");
2012-11-14 18:05:15 +08:00
m_pActionManager->addAction(action, this, !m_bRunning);
return action;
}
void CCNode::stopAllActions()
{
m_pActionManager->removeAllActionsFromTarget(this);
}
2010-08-06 09:46:56 +08:00
void CCNode::stopAction(CCAction* action)
{
m_pActionManager->removeAction(action);
2010-08-06 09:46:56 +08:00
}
2011-07-06 20:48:56 +08:00
void CCNode::stopActionByTag(int tag)
{
CCAssert( tag != kCCActionTagInvalid, "Invalid tag");
m_pActionManager->removeActionByTag(tag, this);
}
2011-07-06 20:48:56 +08:00
CCAction * CCNode::getActionByTag(int tag)
{
CCAssert( tag != kCCActionTagInvalid, "Invalid tag");
return m_pActionManager->getActionByTag(tag, this);
2010-08-06 09:46:56 +08:00
}
unsigned int CCNode::numberOfRunningActions()
{
return m_pActionManager->numberOfRunningActionsInTarget(this);
}
// CCNode - Callbacks
2012-04-04 21:58:04 +08:00
void CCNode::setScheduler(CCScheduler* scheduler)
{
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()
{
return m_pScheduler;
2012-04-04 21:58:04 +08:00
}
void CCNode::scheduleUpdate()
{
scheduleUpdateWithPriority(0);
}
2011-07-06 20:48:56 +08:00
void CCNode::scheduleUpdateWithPriority(int priority)
{
2012-11-14 18:05:15 +08:00
m_pScheduler->scheduleUpdateForTarget(this, priority, !m_bRunning);
}
void CCNode::unscheduleUpdate()
{
m_pScheduler->unscheduleUpdateForTarget(this);
if (m_nUpdateScriptHandler)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nUpdateScriptHandler);
m_nUpdateScriptHandler = 0;
}
}
void CCNode::schedule(SEL_SCHEDULE selector)
{
this->schedule(selector, 0.0f, kCCRepeatForever, 0.0f);
}
2012-06-08 13:55:28 +08:00
void CCNode::schedule(SEL_SCHEDULE selector, float interval)
2012-04-04 21:58:04 +08:00
{
this->schedule(selector, interval, kCCRepeatForever, 0.0f);
}
2012-06-08 13:55:28 +08:00
void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
{
CCAssert( selector, "Argument must be non-nil");
CCAssert( interval >=0, "Argument must be positive");
m_pScheduler->scheduleSelector(selector, this, interval , repeat, delay, !m_bRunning);
}
2012-06-08 13:55:28 +08:00
void CCNode::scheduleOnce(SEL_SCHEDULE selector, float delay)
2012-04-04 21:58:04 +08:00
{
this->schedule(selector, 0.0f, 0, delay);
}
void CCNode::unschedule(SEL_SCHEDULE selector)
{
// explicit nil handling
if (selector == 0)
return;
m_pScheduler->unscheduleSelector(selector, this);
}
void CCNode::unscheduleAllSelectors()
{
2012-11-14 18:05:15 +08:00
m_pScheduler->unscheduleAllForTarget(this);
}
void CCNode::resumeSchedulerAndActions()
{
m_pScheduler->resumeTarget(this);
m_pActionManager->resumeTarget(this);
}
void CCNode::pauseSchedulerAndActions()
{
m_pScheduler->pauseTarget(this);
m_pActionManager->pauseTarget(this);
}
2012-11-14 18:05:15 +08:00
// override me
void CCNode::update(float fDelta)
{
if (m_nUpdateScriptHandler)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeSchedule(m_nUpdateScriptHandler, fDelta);
}
2012-11-14 18:05:15 +08:00
}
CCAffineTransform CCNode::nodeToParentTransform(void)
{
2012-11-14 18:05:15 +08:00
if (m_bTransformDirty)
2012-06-08 14:17:39 +08:00
{
2012-04-04 21:58:04 +08:00
// Translate values
2012-11-14 18:05:15 +08:00
float x = m_obPosition.x;
float y = m_obPosition.y;
2012-04-04 21:58:04 +08:00
2012-06-08 14:17:39 +08:00
if (m_bIgnoreAnchorPointForPosition)
{
2012-11-14 18:05:15 +08:00
x += m_obAnchorPointInPoints.x;
y += m_obAnchorPointInPoints.y;
}
2012-04-04 21:58:04 +08:00
// Rotation values
2012-11-14 18:05:15 +08:00
// Change rotation code to handle X and Y
// If we skew with the exact same value for both x and y then we're simply just rotating
float cx = 1, sx = 0, cy = 1, sy = 0;
if (m_fRotationX || m_fRotationY)
2012-06-08 14:17:39 +08:00
{
2012-11-14 18:05:15 +08:00
float radiansX = -CC_DEGREES_TO_RADIANS(m_fRotationX);
float radiansY = -CC_DEGREES_TO_RADIANS(m_fRotationY);
cx = cosf(radiansX);
sx = sinf(radiansX);
cy = cosf(radiansY);
sy = sinf(radiansY);
}
2012-04-04 21:58:04 +08:00
bool needsSkewMatrix = ( m_fSkewX || m_fSkewY );
2012-04-04 21:58:04 +08:00
// optimization:
// inline anchor point calculation if skew is not needed
2012-11-14 18:05:15 +08:00
// Adjusted transform calculation for rotational skew
if (! needsSkewMatrix && !m_obAnchorPointInPoints.equals(CCPointZero))
2012-06-08 14:17:39 +08:00
{
2012-11-14 18:05:15 +08:00
x += cy * -m_obAnchorPointInPoints.x * m_fScaleX + -sx * -m_obAnchorPointInPoints.y * m_fScaleY;
y += sy * -m_obAnchorPointInPoints.x * m_fScaleX + cx * -m_obAnchorPointInPoints.y * m_fScaleY;
}
2012-04-04 21:58:04 +08:00
// Build Transform Matrix
2012-11-14 18:05:15 +08:00
// Adjusted transform calculation for rotational skew
m_sTransform = CCAffineTransformMake( cy * m_fScaleX, sy * m_fScaleX,
-sx * m_fScaleY, cx * m_fScaleY,
x, y );
2012-04-04 21:58:04 +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)
{
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 );
2012-11-14 18:05:15 +08:00
m_sTransform = CCAffineTransformConcat(skewMatrix, m_sTransform);
2012-04-04 21:58:04 +08:00
// adjust anchor point
2012-11-14 18:05:15 +08:00
if (!m_obAnchorPointInPoints.equals(CCPointZero))
2012-06-08 14:17:39 +08:00
{
2012-11-14 18:05:15 +08:00
m_sTransform = CCAffineTransformTranslate(m_sTransform, -m_obAnchorPointInPoints.x, -m_obAnchorPointInPoints.y);
2012-06-08 14:17:39 +08:00
}
}
2012-04-04 21:58:04 +08:00
2012-11-14 18:05:15 +08:00
m_bTransformDirty = false;
}
2012-04-04 21:58:04 +08:00
2012-11-14 18:05:15 +08:00
return m_sTransform;
}
CCAffineTransform CCNode::parentToNodeTransform(void)
{
2012-11-14 18:05:15 +08:00
if ( m_bInverseDirty ) {
m_sInverse = CCAffineTransformInvert(this->nodeToParentTransform());
m_bInverseDirty = false;
}
2012-11-14 18:05:15 +08:00
return m_sInverse;
}
CCAffineTransform CCNode::nodeToWorldTransform()
{
CCAffineTransform t = this->nodeToParentTransform();
for (CCNode *p = m_pParent; p != NULL; p = p->getParent())
t = CCAffineTransformConcat(t, p->nodeToParentTransform());
return t;
}
2010-07-21 17:40:10 +08:00
CCAffineTransform CCNode::worldToNodeTransform(void)
{
return CCAffineTransformInvert(this->nodeToWorldTransform());
}
CCPoint CCNode::convertToNodeSpace(const CCPoint& worldPoint)
{
CCPoint ret = CCPointApplyAffineTransform(worldPoint, worldToNodeTransform());
return ret;
}
CCPoint CCNode::convertToWorldSpace(const CCPoint& nodePoint)
{
CCPoint ret = CCPointApplyAffineTransform(nodePoint, nodeToWorldTransform());
return ret;
}
CCPoint CCNode::convertToNodeSpaceAR(const CCPoint& worldPoint)
{
CCPoint nodePoint = convertToNodeSpace(worldPoint);
2012-11-14 18:05:15 +08:00
return ccpSub(nodePoint, m_obAnchorPointInPoints);
}
CCPoint CCNode::convertToWorldSpaceAR(const CCPoint& nodePoint)
{
2012-11-14 18:05:15 +08:00
CCPoint pt = ccpAdd(nodePoint, m_obAnchorPointInPoints);
return convertToWorldSpace(pt);
}
CCPoint CCNode::convertToWindowSpace(const CCPoint& nodePoint)
{
CCPoint worldPoint = this->convertToWorldSpace(nodePoint);
return CCDirector::sharedDirector()->convertToUI(worldPoint);
}
// convenience methods which take a CCTouch instead of CCPoint
CCPoint CCNode::convertTouchToNodeSpace(CCTouch *touch)
{
CCPoint point = touch->getLocation();
return this->convertToNodeSpace(point);
2010-07-30 16:35:07 +08:00
}
CCPoint CCNode::convertTouchToNodeSpaceAR(CCTouch *touch)
{
CCPoint point = touch->getLocation();
return this->convertToNodeSpaceAR(point);
}
2010-07-30 16:35:07 +08:00
2012-11-09 12:08:18 +08:00
// MARMALADE ADDED
void CCNode::updateTransform()
{
// Recursively iterate over children
arrayMakeObjectsPerformSelector(m_pChildren, updateTransform, CCNode*);
}
2012-04-18 18:43:45 +08:00
NS_CC_END