axmol/cocos2dx/base_nodes/CCNode.cpp

981 lines
21 KiB
C++
Raw Normal View History

2010-07-07 15:24:44 +08:00
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
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 "CCNode.h"
2010-08-25 10:19:20 +08:00
#include "CGPointExtension.h"
2010-07-21 17:40:10 +08:00
#include "support/TransformUtils.h"
#include "CCCamera.h"
#include "effects/CCGrid.h"
2010-07-30 16:35:07 +08:00
#include "CCDirector.h"
2010-08-04 18:03:15 +08:00
#include "CCScheduler.h"
2010-08-25 10:19:20 +08:00
#include "CCTouch.h"
2010-08-30 15:04:46 +08:00
#include "CCActionManager.h"
2010-07-07 15:24:44 +08:00
2010-07-20 14:29:18 +08:00
#if CC_COCOSNODE_RENDER_SUBPIXEL
#define RENDER_IN_SUBPIXEL
#else
#define RENDER_IN_SUBPIXEL (int)
#endif
2010-08-04 15:46:12 +08:00
namespace cocos2d {
2010-07-07 15:24:44 +08:00
CCNode::CCNode(void)
2010-07-08 16:23:06 +08:00
:m_bIsRunning(false)
,m_fRotation(0.0f)
,m_fScaleX(1.0f)
,m_fScaleY(1.0f)
2010-07-16 17:53:59 +08:00
,m_tPosition(CGPointZero)
,m_tAnchorPointInPixels(CGPointZero)
,m_tAnchorPoint(CGPointZero)
,m_tContentSize(CGSizeZero)
2010-07-08 16:23:06 +08:00
// "whole screen" objects. like Scenes and Layers, should set isRelativeAnchorPoint to false
,m_bIsRelativeAnchorPoint(true)
,m_bIsTransformDirty(true)
,m_bIsInverseDirty(true)
2010-07-20 14:29:18 +08:00
#ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
2010-07-08 16:23:06 +08:00
,m_bIsTransformGLDirty(true)
#endif
,m_fVertexZ(0.0f)
,m_pGrid(NULL)
,m_bIsVisible(true)
2010-07-14 11:18:05 +08:00
,m_nTag(kCCNodeTagInvalid)
,m_nZOrder(0)
2010-07-08 16:23:06 +08:00
// lazy alloc
,m_pCamera(NULL)
// children (lazy allocs)
,m_pChildren(NULL)
// userData is always inited as nil
,m_pUserData(NULL)
,m_pParent(NULL)
2010-07-08 14:00:51 +08:00
{
2010-07-09 16:22:02 +08:00
// nothing
2010-07-08 14:00:51 +08:00
}
CCNode::~CCNode()
{
2010-08-02 15:42:07 +08:00
CCLOGINFO( "cocos2d: deallocing" );
2010-07-14 10:36:26 +08:00
// attributes
CCX_SAFE_RELEASE(m_pCamera);
CCX_SAFE_RELEASE(m_pGrid);
2010-08-02 15:42:07 +08:00
if(m_pChildren && m_pChildren->count() > 0)
{
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
for( it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
{
2010-08-20 09:31:35 +08:00
if (*it)
{
(*it)->m_pParent = NULL;
}
2010-08-02 15:42:07 +08:00
}
}
2010-07-14 10:36:26 +08:00
// children
2010-08-02 15:42:07 +08:00
CCX_SAFE_RELEASE(m_pChildren);
2010-07-14 10:36:26 +08:00
}
2010-07-08 14:00:51 +08:00
void CCNode::arrayMakeObjectsPerformSelector(NSMutableArray<CCNode*> * pArray, callbackFunc func)
2010-07-14 10:36:26 +08:00
{
if(pArray && pArray->count() > 0)
{
CCNode* pNode;
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
2010-07-14 10:36:26 +08:00
for( it = pArray->begin(); it != pArray->end(); it++)
{
pNode = (*it);
2010-07-14 10:36:26 +08:00
2010-07-15 11:39:05 +08:00
if(pNode && func)
2010-07-14 10:36:26 +08:00
{
(pNode->*func)();
}
}
}
}
/// zOrder getter
int CCNode::getZOrder()
{
2010-07-14 11:18:05 +08:00
return m_nZOrder;
}
2010-07-14 10:36:26 +08:00
/// zOrder setter : private method
/// used internally to alter the zOrder variable. DON'T call this method manually
void CCNode::setZOrder(int z)
{
2010-07-14 11:18:05 +08:00
m_nZOrder = z;
2010-07-14 10:36:26 +08:00
}
/// ertexZ getter
float CCNode::getVertexZ()
{
return m_fVertexZ;
}
/// vertexZ setter
void CCNode::setVertexZ(float var)
{
m_fVertexZ = var;
}
/// rotation getter
2010-07-08 10:26:58 +08:00
float CCNode::getRotation()
2010-07-07 15:24:44 +08:00
{
return m_fRotation;
}
/// rotation setter
void CCNode::setRotation(float newRotation)
{
m_fRotation = newRotation;
m_bIsTransformDirty = m_bIsInverseDirty = true;
2010-07-20 14:29:18 +08:00
#ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
m_bIsTransformGLDirty = true;
#endif
}
/// scale getter
float CCNode::getScale(void)
{
2010-07-14 10:36:26 +08:00
NSAssert( m_fScaleX == m_fScaleY, "CCNode#scale. ScaleX != ScaleY. Don't know which one to return");
2010-08-19 15:29:38 +08:00
return m_fScaleX;
2010-07-07 15:24:44 +08:00
}
/// scale setter
void CCNode::setScale(float scale)
{
m_fScaleX = m_fScaleY = scale;
m_bIsTransformDirty = m_bIsInverseDirty = true;
2010-07-20 14:29:18 +08:00
#ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
m_bIsTransformGLDirty = true;
#endif
}
/// scaleX getter
2010-07-08 10:26:58 +08:00
float CCNode::getScaleX()
2010-07-07 15:24:44 +08:00
{
return m_fScaleX;
}
/// scaleX setter
void CCNode::setScaleX(float newScaleX)
{
m_fScaleX = newScaleX;
m_bIsTransformDirty = m_bIsInverseDirty = true;
2010-07-20 14:29:18 +08:00
#ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
m_bIsTransformGLDirty = true;
#endif
2010-07-07 15:24:44 +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;
}
/// scaleY setter
void CCNode::setScaleY(float newScaleY)
{
m_fScaleY = newScaleY;
m_bIsTransformDirty = m_bIsInverseDirty = true;
2010-07-20 14:29:18 +08:00
#ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
m_bIsTransformGLDirty = true;
#endif
}
/// position getter
CGPoint CCNode::getPosition()
{
return m_tPosition;
}
2010-07-07 15:24:44 +08:00
/// position setter
void CCNode::setPosition(CGPoint newPosition)
2010-07-07 15:24:44 +08:00
{
m_tPosition = newPosition;
m_bIsTransformDirty = m_bIsInverseDirty = true;
2010-07-20 14:29:18 +08:00
#ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
m_bIsTransformGLDirty = true;
#endif
2010-07-07 15:24:44 +08:00
}
/// children getter
NSMutableArray<CCNode*> * CCNode::getChildren()
2010-07-07 15:24:44 +08:00
{
return m_pChildren;
}
2010-07-14 10:36:26 +08:00
/// camera getter: lazy alloc
CCCamera* CCNode::getCamera()
{
2010-07-14 10:36:26 +08:00
if (!m_pCamera)
{
m_pCamera = new CCCamera();
}
return m_pCamera;
}
2010-07-14 10:36:26 +08:00
/// grid getter
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)
{
if(m_pGrid)
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
m_pGrid->release();
2010-08-02 15:42:07 +08:00
}
2010-07-14 10:36:26 +08:00
m_pGrid = pGrid;
2010-07-14 10:36:26 +08:00
if(m_pGrid)
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
m_pGrid->retain();
2010-08-02 15:42:07 +08:00
}
2010-07-14 10:36:26 +08:00
}
2010-07-07 15:24:44 +08:00
/// isVisible getter
bool CCNode::getIsVisible()
2010-07-07 15:24:44 +08:00
{
return m_bIsVisible;
2010-07-07 15:24:44 +08:00
}
/// isVisible setter
void CCNode::setIsVisible(bool var)
2010-07-07 15:24:44 +08:00
{
m_bIsVisible = var;
2010-07-07 15:24:44 +08:00
}
/// anchorPoint getter
CGPoint CCNode::getAnchorPoint()
2010-07-07 15:24:44 +08:00
{
return m_tAnchorPoint;
2010-07-07 15:24:44 +08:00
}
2010-07-08 10:26:58 +08:00
void CCNode::setAnchorPoint(CGPoint point)
{
2010-07-16 17:53:59 +08:00
if( ! CGPoint::CGPointEqualToPoint(point, m_tAnchorPoint) )
{
m_tAnchorPoint = point;
this->m_tAnchorPointInPixels = ccp( m_tContentSize.width * m_tAnchorPoint.x, m_tContentSize.height * m_tAnchorPoint.y );
m_bIsTransformDirty = m_bIsInverseDirty = true;
2010-07-20 14:29:18 +08:00
#ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
2010-07-16 17:53:59 +08:00
m_bIsTransformGLDirty = true;
#endif
}
}
/// anchorPointInPixels getter
CGPoint CCNode::getAnchorPointInPixels()
2010-07-07 15:24:44 +08:00
{
return m_tAnchorPointInPixels;
2010-07-07 15:24:44 +08:00
}
/// contentSize getter
CGSize CCNode::getContentSize()
{
return m_tContentSize;
}
2010-07-07 15:24:44 +08:00
void CCNode::setContentSize(CGSize size)
{
2010-07-16 17:53:59 +08:00
if( ! CGSize::CGSizeEqualToSize(size, m_tContentSize) )
{
m_tContentSize = size;
m_tAnchorPointInPixels = ccp( m_tContentSize.width * m_tAnchorPoint.x, m_tContentSize.height * m_tAnchorPoint.y );
m_bIsTransformDirty = m_bIsInverseDirty = true;
2010-07-20 14:29:18 +08:00
#ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
2010-07-16 17:53:59 +08:00
m_bIsTransformGLDirty = true;
#endif
}
2010-07-07 15:24:44 +08:00
}
/// isRunning getter
bool CCNode::getIsRunning()
2010-07-07 15:24:44 +08:00
{
return m_bIsRunning;
2010-07-07 15:24:44 +08:00
}
/// parent getter
CCNode * CCNode::getParent()
2010-07-07 15:24:44 +08:00
{
return m_pParent;
}
/// 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::getIsRelativeAnchorPoint()
2010-07-07 15:24:44 +08:00
{
return m_bIsRelativeAnchorPoint;
2010-07-07 15:24:44 +08:00
}
/// isRelativeAnchorPoint setter
void CCNode::setIsRelativeAnchorPoint(bool newValue)
2010-07-07 15:24:44 +08:00
{
m_bIsRelativeAnchorPoint = newValue;
m_bIsTransformDirty = m_bIsInverseDirty = true;
2010-07-20 14:29:18 +08:00
#ifdef CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
m_bIsTransformGLDirty = true;
#endif
}
/// tag getter
int CCNode::getTag()
{
2010-07-14 11:18:05 +08:00
return m_nTag;
}
/// tag setter
void CCNode::setTag(int var)
{
2010-07-14 11:18:05 +08:00
m_nTag = var;
}
/// 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-07-20 14:29:18 +08:00
CGRect CCNode::boundingBox()
{
CGRect rect = CGRectMake(0, 0, m_tContentSize.width, m_tContentSize.height);
return CGRectApplyAffineTransform(rect, nodeToParentTransform());
}
CCNode * CCNode::node(void)
{
2010-08-03 11:28:34 +08:00
CCNode * pRet = new CCNode();
pRet->autorelease();
return pRet;
}
2010-07-14 10:36:26 +08:00
void CCNode::cleanup()
{
// actions
2010-07-14 10:36:26 +08:00
this->stopAllActions();
this->unscheduleAllSelectors();
2010-08-02 14:46:06 +08:00
// timers
2010-07-14 10:36:26 +08:00
arrayMakeObjectsPerformSelector(m_pChildren, &CCNode::cleanup);
}
2010-07-14 10:36:26 +08:00
2010-07-15 11:39:05 +08:00
2010-08-06 16:05:19 +08:00
char * CCNode::description()
{
2010-08-06 16:05:19 +08:00
char *ret = new char[100] ;
sprintf_s(ret, 100, "<CCNode | Tag = %d>", m_nTag);
2010-07-15 11:39:05 +08:00
return ret;
}
// lazy allocs
void CCNode::childrenAlloc(void)
{
m_pChildren = new NSMutableArray<CCNode*>(4);
}
CCNode* CCNode::getChildByTag(int aTag)
{
2010-07-14 10:36:26 +08:00
NSAssert( aTag != kCCNodeTagInvalid, "Invalid tag");
2010-07-14 10:36:26 +08:00
if(m_pChildren && m_pChildren->count() > 0)
{
CCNode* pNode;
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
2010-07-14 10:36:26 +08:00
for( it = m_pChildren->begin(); it != m_pChildren->end(); it++)
{
pNode = (*it);
2010-07-14 11:18:05 +08:00
if(pNode && pNode->m_nTag == aTag)
2010-07-14 10:36:26 +08:00
return pNode;
}
}
return NULL;
}
/* "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
*/
2010-07-14 10:36:26 +08:00
CCNode * CCNode::addChild(CCNode *child, int zOrder, int tag)
{
2010-07-14 10:36:26 +08:00
NSAssert( child != NULL, "Argument must be non-nil");
NSAssert( child->m_pParent == NULL, "child already added. It can't be added again");
2010-07-14 10:36:26 +08:00
if( ! m_pChildren )
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
this->childrenAlloc();
2010-08-02 15:42:07 +08:00
}
2010-07-14 10:36:26 +08:00
this->insertChild(child, zOrder);
2010-07-14 11:18:05 +08:00
child->m_nTag = tag;
2010-07-14 10:36:26 +08:00
child->setParent(this);
2010-07-14 10:36:26 +08:00
if( m_bIsRunning )
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
child->onEnter();
2010-08-02 15:42:07 +08:00
}
2010-07-14 10:36:26 +08:00
return this;
}
2010-07-14 10:36:26 +08:00
CCNode * CCNode::addChild(CCNode *child, int zOrder)
{
2010-07-14 10:36:26 +08:00
NSAssert( child != NULL, "Argument must be non-nil");
2010-07-14 11:18:05 +08:00
return this->addChild(child, zOrder, child->m_nTag);
}
2010-07-14 10:36:26 +08:00
CCNode * CCNode::addChild(CCNode *child)
{
2010-07-14 10:36:26 +08:00
NSAssert( child != NULL, "Argument must be non-nil");
2010-07-14 11:18:05 +08:00
return this->addChild(child, child->m_nZOrder, child->m_nTag);
}
void CCNode::removeFromParentAndCleanup(bool cleanup)
{
2010-07-14 10:36:26 +08:00
this->m_pParent->removeChild(this,cleanup);
}
/* "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
2010-07-14 10:36:26 +08:00
if (m_pChildren == NULL)
2010-08-02 15:42:07 +08:00
{
return;
2010-08-02 15:42:07 +08:00
}
2010-07-14 10:36:26 +08:00
if ( m_pChildren->containsObject(child) )
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
this->detachChild(child,cleanup);
2010-08-02 15:42:07 +08:00
}
}
void CCNode::removeChildByTag(int tag, bool cleanup)
{
2010-07-14 10:36:26 +08:00
NSAssert( tag != kCCNodeTagInvalid, "Invalid tag");
2010-07-14 10:36:26 +08:00
CCNode *child = this->getChildByTag(tag);
2010-07-14 10:36:26 +08:00
if (child == NULL)
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
CCLOG("cocos2d: removeChildByTag: child not found!");
2010-08-02 15:42:07 +08:00
}
else
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
this->removeChild(child, cleanup);
2010-08-02 15:42:07 +08:00
}
}
void CCNode::removeAllChildrenWithCleanup(bool cleanup)
{
// not using detachChild improves speed here
2010-07-14 10:36:26 +08:00
if ( m_pChildren && m_pChildren->count() > 0 )
{
CCNode * pNode;
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
2010-07-14 10:36:26 +08:00
for ( it = m_pChildren->begin(); it!= m_pChildren->end(); it++ )
{
2010-07-21 17:40:10 +08:00
pNode = *it;
2010-07-14 10:36:26 +08:00
if (pNode)
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
2010-08-02 15:42:07 +08:00
if(m_bIsRunning)
2010-07-14 10:36:26 +08:00
{
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
2010-07-14 10:36:26 +08:00
if (m_bIsRunning)
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
child->onExit();
2010-08-02 15:42:07 +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)
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
child->cleanup();
2010-08-02 15:42:07 +08:00
}
2010-07-14 10:36:26 +08:00
// set parent nil at the end
child->setParent(NULL);
2010-07-14 10:36:26 +08:00
m_pChildren->removeObject(child);
}
// helper used by reorderChild & add
void CCNode::insertChild(CCNode* child, int z)
{
int index=0;
2010-07-14 10:36:26 +08:00
bool added = false;
if(m_pChildren && m_pChildren->count() > 0)
{
CCNode* pNode;
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
2010-07-14 10:36:26 +08:00
for( it = m_pChildren->begin(); it != m_pChildren->end(); it++)
{
pNode = (*it);
2010-07-14 10:36:26 +08:00
2010-07-14 11:18:05 +08:00
if ( pNode && pNode->m_nZOrder > z )
2010-07-14 10:36:26 +08:00
{
added = true;
m_pChildren->insertObjectAtIndex(child, index);
break;
}
index++;
}
}
if( ! added )
2010-08-02 15:42:07 +08:00
{
2010-07-14 10:36:26 +08:00
m_pChildren->addObject(child);
2010-08-02 15:42:07 +08:00
}
2010-07-14 10:36:26 +08:00
child->setZOrder(z);
}
void CCNode::reorderChild(CCNode *child, int zOrder)
{
2010-07-14 10:36:26 +08:00
NSAssert( child != NULL, "Child must be non-nil");
2010-07-14 10:36:26 +08:00
child->retain();
m_pChildren->removeObject(child);
2010-07-14 10:36:26 +08:00
insertChild(child, zOrder);
2010-08-02 15:44:41 +08:00
child->release();
}
void CCNode::draw()
{
//assert(0);
// override me
// Only use- this function to draw your staff.
// DON'T draw your stuff outside this method
}
void CCNode::visit()
{
if (!m_bIsVisible)
{
return;
}
glPushMatrix();
2010-08-17 14:25:41 +08:00
if (m_pGrid && m_pGrid->isActive())
{
m_pGrid->beforeDraw();
this->transformAncestors();
}
this->transform();
CCNode* pNode;
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
if(m_pChildren && m_pChildren->count() > 0)
{
for( it = m_pChildren->begin(); it != m_pChildren->end(); it++)
{
pNode = (*it);
if ( pNode && pNode->m_nZOrder < 0 )
{
pNode->visit();
}
else
{
break;
}
}
}
this->draw();
if (m_pChildren && m_pChildren->count() > 0)
{
for ( ; it!=m_pChildren->end(); it++ )
{
2010-07-21 17:40:10 +08:00
pNode = (*it);
if (pNode)
{
pNode->visit();
}
}
}
2010-08-17 14:25:41 +08:00
if (m_pGrid && m_pGrid->isActive())
{
m_pGrid->afterDraw(this);
}
glPopMatrix();
}
void CCNode::transformAncestors()
{
if( m_pParent != NULL )
{
m_pParent->transformAncestors();
m_pParent->transform();
}
}
void CCNode::transform()
{
// transformations
#if CC_NODE_TRANSFORM_USING_AFFINE_MATRIX
// BEGIN alternative -- using cached transform
//
2010-07-20 14:29:18 +08:00
if( m_bIsTransformGLDirty ) {
CGAffineTransform t = this->nodeToParentTransform();
CGAffineToGL(&t, m_pTransformGL);
m_bIsTransformGLDirty = false;
}
2010-07-20 14:29:18 +08:00
glMultMatrixf(m_pTransformGL);
if( m_fVertexZ )
2010-08-02 15:42:07 +08:00
{
2010-07-20 14:29:18 +08:00
glTranslatef(0, 0, m_fVertexZ);
2010-08-02 15:42:07 +08:00
}
// XXX: Expensive calls. Camera should be integrated into the cached affine matrix
2010-08-17 14:25:41 +08:00
if (m_pCamera && !(m_pGrid && m_pGrid->isActive())) {
2010-07-20 14:29:18 +08:00
bool translate = (m_tAnchorPointInPixels.x != 0.0f || m_tAnchorPointInPixels.y != 0.0f);
if( translate )
2010-08-02 15:42:07 +08:00
{
2010-07-20 14:29:18 +08:00
glTranslatef(RENDER_IN_SUBPIXEL(m_tAnchorPointInPixels.x), RENDER_IN_SUBPIXEL(m_tAnchorPointInPixels.y), 0);
2010-08-02 15:42:07 +08:00
}
2010-07-20 14:29:18 +08:00
m_pCamera->locate();
if( translate )
2010-08-02 15:42:07 +08:00
{
2010-07-20 14:29:18 +08:00
glTranslatef(RENDER_IN_SUBPIXEL(-m_tAnchorPointInPixels.x), RENDER_IN_SUBPIXEL(-m_tAnchorPointInPixels.y), 0);
2010-08-02 15:42:07 +08:00
}
}
// END alternative
#else
// BEGIN original implementation
//
// translate
2010-07-20 14:29:18 +08:00
if ( m_bIsRelativeAnchorPoint && (m_tAnchorPointInPixels.x != 0 || m_tAnchorPointInPixels.y != 0 ) )
glTranslatef( RENDER_IN_SUBPIXEL(-m_tAnchorPointInPixels.x), RENDER_IN_SUBPIXEL(-m_tAnchorPointInPixels.y), 0);
2010-07-20 14:29:18 +08:00
if (m_tAnchorPointInPixels.x != 0 || m_tAnchorPointInPixels.y != 0)
glTranslatef( RENDER_IN_SUBPIXEL(m_tPosition.x + m_tAnchorPointInPixels.x), RENDER_IN_SUBPIXEL(m_tPosition.y + m_tAnchorPointInPixels.y), m_fVertexZ);
else if ( m_tPosition.x !=0 || m_tPosition.y !=0 || m_fVertexZ != 0)
glTranslatef( RENDER_IN_SUBPIXEL(m_tPosition.x), RENDER_IN_SUBPIXEL(m_tPosition.y), m_fVertexZ );
// rotate
2010-07-20 14:29:18 +08:00
if (m_fRotation != 0.0f )
glRotatef( -m_fRotation, 0.0f, 0.0f, 1.0f );
// scale
2010-07-20 14:29:18 +08:00
if (m_fScaleX != 1.0f || m_fScaleY != 1.0f)
glScalef( m_fScaleX, m_fScaleY, 1.0f );
2010-08-17 14:25:41 +08:00
if ( m_pCamera && !(m_pGrid && m_pGrid->isActive()) )
2010-07-20 14:29:18 +08:00
m_pCamera->locate();
// restore and re-position point
2010-07-20 14:29:18 +08:00
if (m_tAnchorPointInPixels.x != 0.0f || m_tAnchorPointInPixels.y != 0.0f)
glTranslatef(RENDER_IN_SUBPIXEL(-m_tAnchorPointInPixels.x), RENDER_IN_SUBPIXEL(-m_tAnchorPointInPixels.y), 0);
//
// END original implementation
#endif
2010-07-20 14:29:18 +08:00
}
void CCNode::onEnter()
{
2010-07-14 10:36:26 +08:00
arrayMakeObjectsPerformSelector(m_pChildren, &CCNode::onEnter);
2010-07-14 10:36:26 +08:00
this->resumeSchedulerAndActions();
2010-07-14 10:36:26 +08:00
m_bIsRunning = true;
}
void CCNode::onEnterTransitionDidFinish()
{
2010-07-14 10:36:26 +08:00
arrayMakeObjectsPerformSelector(m_pChildren, &CCNode::onEnterTransitionDidFinish);
}
void CCNode::onExit()
{
2010-07-14 10:36:26 +08:00
this->pauseSchedulerAndActions();
m_bIsRunning = false;
arrayMakeObjectsPerformSelector(m_pChildren, &CCNode::onExit);
}
2010-08-06 09:46:56 +08:00
CCAction * CCNode::runAction(CCAction* action)
{
NSAssert( action != NULL, "Argument must be non-nil");
2010-08-06 09:46:56 +08:00
CCActionManager::getSharedManager()->addAction(action, this, !m_bIsRunning);
return action;
}
void CCNode::stopAllActions()
{
2010-08-06 09:46:56 +08:00
CCActionManager::getSharedManager()->removeAllActionsFromTarget(this);
}
2010-08-06 09:46:56 +08:00
void CCNode::stopAction(CCAction* action)
{
2010-08-06 09:46:56 +08:00
CCActionManager::getSharedManager()->removeAction(action);
}
void CCNode::stopActionByTag(int tag)
{
2010-08-06 09:46:56 +08:00
NSAssert( tag != kCCActionTagInvalid, "Invalid tag");
CCActionManager::getSharedManager()->removeActionByTag(tag, this);
}
2010-08-06 09:46:56 +08:00
CCAction * CCNode::getActionByTag(int tag)
{
2010-08-06 09:46:56 +08:00
NSAssert( tag != kCCActionTagInvalid, "Invalid tag");
return CCActionManager::getSharedManager()->getActionByTag(tag, this);
}
int CCNode::numberOfRunningActions()
{
2010-08-06 09:46:56 +08:00
return CCActionManager::getSharedManager()->numberOfRunningActionsInTarget(this);
}
// CCNode - Callbacks
void CCNode::scheduleUpdate()
{
scheduleUpdateWithPriority(0);
}
void CCNode::scheduleUpdateWithPriority(int priority)
{
CCScheduler::getSharedScheduler()->scheduleUpdateForTarget(this, priority, !m_bIsRunning);
}
void CCNode::unscheduleUpdate()
{
CCScheduler::getSharedScheduler()->unscheduleUpdateForTarget(this);
}
void CCNode::schedule(SEL_SCHEDULE selector)
{
this->schedule(selector, 0);
}
void CCNode::schedule(SEL_SCHEDULE selector, ccTime interval)
{
NSAssert( selector != NULL, "Argument must be non-nil");
NSAssert( interval >=0, "Argument must be positive");
CCScheduler::getSharedScheduler()->scheduleSelector(selector, this, interval, !m_bIsRunning);
}
void CCNode::unschedule(SEL_SCHEDULE selector)
{
// explicit nil handling
if (selector == NULL)
return;
CCScheduler::getSharedScheduler()->unscheduleSelector(selector, this);
}
void CCNode::unscheduleAllSelectors()
{
CCScheduler::getSharedScheduler()->unscheduleAllSelectorsForTarget(this);
}
void CCNode::resumeSchedulerAndActions()
{
2010-08-04 18:03:15 +08:00
CCScheduler::getSharedScheduler()->resumeTarget(this);
2010-08-06 09:46:56 +08:00
CCActionManager::getSharedManager()->resumeTarget(this);
}
void CCNode::pauseSchedulerAndActions()
{
2010-08-04 18:03:15 +08:00
CCScheduler::getSharedScheduler()->pauseTarget(this);
2010-08-06 09:46:56 +08:00
CCActionManager::getSharedManager()->pauseTarget(this);
}
2010-08-30 15:45:39 +08:00
void CCNode::selectorProtocolRetain(void)
{
retain();
}
void CCNode::selectorProtocolRelease(void)
{
release();
}
CGAffineTransform CCNode::nodeToParentTransform(void)
{
if ( m_bIsTransformDirty ) {
m_tTransform = CGAffineTransformIdentity;
if( ! m_bIsRelativeAnchorPoint && ! CGPoint::CGPointEqualToPoint(m_tAnchorPointInPixels, CGPointZero) )
m_tTransform = CGAffineTransformTranslate(m_tTransform, m_tAnchorPointInPixels.x, m_tAnchorPointInPixels.y);
if( ! CGPoint::CGPointEqualToPoint(m_tPosition, CGPointZero) )
m_tTransform = CGAffineTransformTranslate(m_tTransform, m_tPosition.x, m_tPosition.y);
if( m_fRotation != 0 )
m_tTransform = CGAffineTransformRotate(m_tTransform, -CC_DEGREES_TO_RADIANS(m_fRotation));
if( ! (m_fScaleX == 1 && m_fScaleY == 1) )
m_tTransform = CGAffineTransformScale(m_tTransform, m_fScaleX, m_fScaleY);
if( ! CGPoint::CGPointEqualToPoint(m_tAnchorPointInPixels, CGPointZero) )
m_tTransform = CGAffineTransformTranslate(m_tTransform, -m_tAnchorPointInPixels.x, -m_tAnchorPointInPixels.y);
m_bIsTransformDirty = false;
}
return m_tTransform;
}
CGAffineTransform CCNode::parentToNodeTransform(void)
{
if ( m_bIsInverseDirty ) {
m_tInverse = CGAffineTransformInvert(this->nodeToParentTransform());
m_bIsInverseDirty = false;
}
return m_tInverse;
}
CGAffineTransform CCNode::nodeToWorldTransform()
{
CGAffineTransform t = this->nodeToParentTransform();
for (CCNode *p = m_pParent; p != NULL; p = p->getParent())
t = CGAffineTransformConcat(t, p->nodeToParentTransform());
return t;
}
2010-07-21 17:40:10 +08:00
CGAffineTransform CCNode::worldToNodeTransform(void)
{
return CGAffineTransformInvert(this->nodeToWorldTransform());
}
CGPoint CCNode::convertToNodeSpace(CGPoint worldPoint)
{
return CGPointApplyAffineTransform(worldPoint, this->worldToNodeTransform());
}
CGPoint CCNode::convertToWorldSpace(CGPoint nodePoint)
{
return CGPointApplyAffineTransform(nodePoint, this->nodeToWorldTransform());
}
CGPoint CCNode::convertToNodeSpaceAR(CGPoint worldPoint)
{
CGPoint nodePoint = this->convertToNodeSpace(worldPoint);
return ccpSub(nodePoint, m_tAnchorPointInPixels);
}
CGPoint CCNode::convertToWorldSpaceAR(CGPoint nodePoint)
{
nodePoint = ccpAdd(nodePoint, m_tAnchorPointInPixels);
return this->convertToWorldSpace(nodePoint);
}
CGPoint CCNode::convertToWindowSpace(CGPoint nodePoint)
{
CGPoint worldPoint = this->convertToWorldSpace(nodePoint);
2010-08-02 16:00:01 +08:00
return CCDirector::getSharedDirector()->convertToUI(worldPoint);
}
// convenience methods which take a CCTouch instead of CGPoint
2010-07-30 16:35:07 +08:00
CGPoint CCNode::convertTouchToNodeSpace(CCTouch *touch)
{
2010-07-30 16:35:07 +08:00
CGPoint point = touch->locationInView(touch->view());
point = CCDirector::getSharedDirector()->convertToGL(point);
return this->convertToNodeSpace(point);
}
CGPoint CCNode::convertTouchToNodeSpaceAR(CCTouch *touch)
{
2010-07-30 16:35:07 +08:00
CGPoint point = touch->locationInView(touch->view());
point = CCDirector::getSharedDirector()->convertToGL(point);
return this->convertToNodeSpaceAR(point);
}
2010-07-30 16:35:07 +08:00
2010-08-04 15:46:12 +08:00
}//namespace cocos2d