axmol/cocos2dx/base_nodes/CCNode.cpp

1450 lines
34 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.
****************************************************************************/
2013-08-26 01:50:29 +08:00
2010-12-18 16:12:59 +08:00
#include "CCNode.h"
2013-08-26 01:50:29 +08:00
#include <algorithm>
#include "cocoa/CCString.h"
#include "support/data_support/ccCArray.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 "event_dispatcher/CCTouch.h"
#include "actions/CCActionManager.h"
#include "script_support/CCScriptSupport.h"
#include "shaders/CCGLProgram.h"
#include "event_dispatcher/CCEventDispatcher.h"
#include "event_dispatcher/CCEvent.h"
#include "event_dispatcher/CCTouchEvent.h"
2012-04-04 21:58:04 +08:00
// externals
#include "kazmath/GL/matrix.h"
2013-06-04 17:38:43 +08:00
#include "support/component/CCComponent.h"
#include "support/component/CCComponentContainer.h"
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
2013-04-13 15:18:54 +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
#if CC_USE_ARRAY_VECTOR
bool nodeComparisonLess(const RCPtr<Object>& pp1, const RCPtr<Object>& pp2)
{
Object *p1 = static_cast<Object*>(pp1);
Object *p2 = static_cast<Object*>(pp2);
Node *n1 = static_cast<Node*>(p1);
Node *n2 = static_cast<Node*>(p2);
return( n1->getZOrder() < n2->getZOrder() ||
( n1->getZOrder() == n2->getZOrder() && n1->getOrderOfArrival() < n2->getOrderOfArrival() )
);
}
#else
bool nodeComparisonLess(Object* p1, Object* p2)
{
Node *n1 = static_cast<Node*>(p1);
Node *n2 = static_cast<Node*>(p2);
return( n1->getZOrder() < n2->getZOrder() ||
( n1->getZOrder() == n2->getZOrder() && n1->getOrderOfArrival() < n2->getOrderOfArrival() )
);
}
#endif
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;
static int _globalEventPriorityIndex = 0;
Node::Node(void)
: _rotationX(0.0f)
, _rotationY(0.0f)
, _scaleX(1.0f)
, _scaleY(1.0f)
, _vertexZ(0.0f)
, _position(Point::ZERO)
, _skewX(0.0f)
, _skewY(0.0f)
, _anchorPointInPoints(Point::ZERO)
, _anchorPoint(Point::ZERO)
, _contentSize(Size::ZERO)
, _additionalTransform(AffineTransform::IDENTITY)
, _transform(AffineTransform::IDENTITY)
, _inverse(AffineTransform::IDENTITY)
, _additionalTransformDirty(false)
, _transformDirty(true)
, _inverseDirty(true)
, _camera(NULL)
2011-06-10 17:51:37 +08:00
// children (lazy allocs)
// lazy alloc
, _grid(NULL)
, _ZOrder(0)
, _children(NULL)
, _parent(NULL)
// "whole screen" objects. like Scenes and Layers, should set _ignoreAnchorPointForPosition to true
, _tag(Node::INVALID_TAG)
2011-06-10 17:51:37 +08:00
// userData is always inited as nil
, _userData(NULL)
, _userObject(NULL)
, _shaderProgram(NULL)
, _orderOfArrival(0)
, _running(false)
, _visible(true)
, _ignoreAnchorPointForPosition(false)
, _reorderChildDirty(false)
, _isTransitionFinished(false)
, _updateScriptHandler(0)
, _componentContainer(NULL)
2010-12-18 16:12:59 +08:00
{
// set default scheduler and actionManager
Director *director = Director::getInstance();
_actionManager = director->getActionManager();
_actionManager->retain();
_scheduler = director->getScheduler();
_scheduler->retain();
ScriptEngineProtocol* pEngine = ScriptEngineManager::getInstance()->getScriptEngine();
_scriptType = pEngine != NULL ? pEngine->getScriptType() : kScriptTypeNone;
2010-12-18 16:12:59 +08:00
}
Node::~Node()
2010-12-18 16:12:59 +08:00
{
CCLOGINFO( "deallocing Node: %p - tag: %i", this, _tag );
if (_updateScriptHandler)
{
ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler);
}
2010-07-14 10:36:26 +08:00
CC_SAFE_RELEASE(_actionManager);
CC_SAFE_RELEASE(_scheduler);
// attributes
CC_SAFE_RELEASE(_camera);
2010-07-14 10:36:26 +08:00
CC_SAFE_RELEASE(_grid);
CC_SAFE_RELEASE(_shaderProgram);
CC_SAFE_RELEASE(_userObject);
2010-07-14 10:36:26 +08:00
if(_children && _children->count() > 0)
{
Object* child;
CCARRAY_FOREACH(_children, child)
{
Node* node = static_cast<Node*>(child);
if (node)
{
node->_parent = NULL;
}
}
}
2010-07-14 10:36:26 +08:00
// children
CC_SAFE_RELEASE(_children);
2013-06-04 17:38:43 +08:00
removeAllComponents();
CC_SAFE_DELETE(_componentContainer);
for (auto iter = _eventIds.begin(); iter != _eventIds.end(); ++iter)
{
EventDispatcher::getInstance()->unregisterEventListener(*iter);
}
2010-12-18 16:12:59 +08:00
}
bool Node::init()
2013-02-27 09:38:30 +08:00
{
return true;
}
float Node::getSkewX() const
{
return _skewX;
}
void Node::setSkewX(float newSkewX)
{
_skewX = newSkewX;
_transformDirty = _inverseDirty = true;
}
float Node::getSkewY() const
{
return _skewY;
}
void Node::setSkewY(float newSkewY)
{
_skewY = newSkewY;
_transformDirty = _inverseDirty = true;
}
2010-12-18 16:12:59 +08:00
/// zOrder getter
int Node::getZOrder() const
2010-12-18 16:12:59 +08:00
{
return _ZOrder;
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 Node::_setZOrder(int z)
2010-07-14 10:36:26 +08:00
{
_ZOrder = z;
2010-12-18 16:12:59 +08:00
}
void Node::setZOrder(int z)
{
_setZOrder(z);
if (_parent)
{
_parent->reorderChild(this, z);
}
}
/// vertexZ getter
float Node::getVertexZ() const
2010-12-18 16:12:59 +08:00
{
return _vertexZ;
2010-12-18 16:12:59 +08:00
}
/// vertexZ setter
void Node::setVertexZ(float var)
2010-12-18 16:12:59 +08:00
{
_vertexZ = var;
2010-12-18 16:12:59 +08:00
}
/// rotation getter
float Node::getRotation() const
2010-12-18 16:12:59 +08:00
{
CCASSERT(_rotationX == _rotationY, "CCNode#rotation. RotationX != RotationY. Don't know which one to return");
return _rotationX;
2010-12-18 16:12:59 +08:00
}
/// rotation setter
void Node::setRotation(float newRotation)
{
_rotationX = _rotationY = newRotation;
_transformDirty = _inverseDirty = true;
2012-11-14 18:05:15 +08:00
}
float Node::getRotationX() const
2012-11-14 18:05:15 +08:00
{
return _rotationX;
2012-11-14 18:05:15 +08:00
}
void Node::setRotationX(float fRotationX)
2012-11-14 18:05:15 +08:00
{
_rotationX = fRotationX;
_transformDirty = _inverseDirty = true;
2012-11-14 18:05:15 +08:00
}
float Node::getRotationY() const
2012-11-14 18:05:15 +08:00
{
return _rotationY;
2012-11-14 18:05:15 +08:00
}
void Node::setRotationY(float fRotationY)
2012-11-14 18:05:15 +08:00
{
_rotationY = fRotationY;
_transformDirty = _inverseDirty = true;
2010-12-18 16:12:59 +08:00
}
/// scale getter
float Node::getScale(void) const
{
CCASSERT( _scaleX == _scaleY, "CCNode#scale. ScaleX != ScaleY. Don't know which one to return");
return _scaleX;
2010-12-18 16:12:59 +08:00
}
/// scale setter
void Node::setScale(float scale)
{
_scaleX = _scaleY = scale;
_transformDirty = _inverseDirty = true;
}
2010-12-18 16:12:59 +08:00
/// scaleX getter
float Node::getScaleX() const
2010-07-07 15:24:44 +08:00
{
return _scaleX;
2010-12-18 16:12:59 +08:00
}
/// scaleX setter
void Node::setScaleX(float newScaleX)
{
_scaleX = newScaleX;
_transformDirty = _inverseDirty = true;
2010-07-07 15:24:44 +08:00
}
2010-12-18 16:12:59 +08:00
/// scaleY getter
float Node::getScaleY() const
2010-07-07 15:24:44 +08:00
{
return _scaleY;
2010-12-18 16:12:59 +08:00
}
/// scaleY setter
void Node::setScaleY(float newScaleY)
{
_scaleY = newScaleY;
_transformDirty = _inverseDirty = true;
2010-12-18 16:12:59 +08:00
}
/// position getter
const Point& Node::getPosition() const
2010-12-18 16:12:59 +08:00
{
return _position;
2010-12-18 16:12:59 +08:00
}
/// position setter
void Node::setPosition(const Point& newPosition)
2010-07-07 15:24:44 +08:00
{
_position = newPosition;
_transformDirty = _inverseDirty = true;
}
void Node::getPosition(float* x, float* y) const
{
*x = _position.x;
*y = _position.y;
}
void Node::setPosition(float x, float y)
{
2013-07-12 14:11:55 +08:00
setPosition(Point(x, y));
}
float Node::getPositionX() const
{
return _position.x;
}
float Node::getPositionY() const
{
return _position.y;
}
void Node::setPositionX(float x)
{
2013-07-12 14:11:55 +08:00
setPosition(Point(x, _position.y));
}
void Node::setPositionY(float y)
{
2013-07-12 14:11:55 +08:00
setPosition(Point(_position.x, y));
}
unsigned int Node::getChildrenCount() const
{
return _children ? _children->count() : 0;
}
2010-07-14 10:36:26 +08:00
/// camera getter: lazy alloc
Camera* Node::getCamera()
{
if (!_camera)
{
_camera = new Camera();
}
return _camera;
}
2010-07-14 10:36:26 +08:00
/// grid setter
void Node::setGrid(GridBase* pGrid)
2010-07-14 10:36:26 +08:00
{
CC_SAFE_RETAIN(pGrid);
CC_SAFE_RELEASE(_grid);
_grid = pGrid;
2010-07-14 10:36:26 +08:00
}
2010-07-07 15:24:44 +08:00
/// isVisible getter
bool Node::isVisible() const
2010-07-07 15:24:44 +08:00
{
return _visible;
2010-07-07 15:24:44 +08:00
}
/// isVisible setter
void Node::setVisible(bool var)
2010-07-07 15:24:44 +08:00
{
_visible = var;
2010-07-07 15:24:44 +08:00
}
const Point& Node::getAnchorPointInPoints() const
{
return _anchorPointInPoints;
}
/// anchorPoint getter
const Point& Node::getAnchorPoint() const
2010-07-07 15:24:44 +08:00
{
return _anchorPoint;
2010-12-18 16:12:59 +08:00
}
void Node::setAnchorPoint(const Point& point)
2010-07-08 10:26:58 +08:00
{
if( ! point.equals(_anchorPoint))
{
_anchorPoint = point;
2013-07-12 14:11:55 +08:00
_anchorPointInPoints = Point(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y );
_transformDirty = _inverseDirty = true;
}
}
/// contentSize getter
const Size& Node::getContentSize() const
{
return _contentSize;
}
void Node::setContentSize(const Size & size)
2010-07-07 15:24:44 +08:00
{
if ( ! size.equals(_contentSize))
{
_contentSize = size;
2011-01-04 17:46:23 +08:00
2013-07-12 14:11:55 +08:00
_anchorPointInPoints = Point(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y );
_transformDirty = _inverseDirty = true;
}
2010-07-07 15:24:44 +08:00
}
2011-01-31 06:59:03 +08:00
// isRunning getter
bool Node::isRunning() const
2010-07-07 15:24:44 +08:00
{
return _running;
2010-07-07 15:24:44 +08:00
}
/// parent setter
void Node::setParent(Node * var)
2010-07-07 15:24:44 +08:00
{
_parent = var;
2010-07-07 15:24:44 +08:00
}
/// isRelativeAnchorPoint getter
bool Node::isIgnoreAnchorPointForPosition() const
2010-07-07 15:24:44 +08:00
{
return _ignoreAnchorPointForPosition;
2010-07-07 15:24:44 +08:00
}
/// isRelativeAnchorPoint setter
void Node::ignoreAnchorPointForPosition(bool newValue)
2010-07-07 15:24:44 +08:00
{
if (newValue != _ignoreAnchorPointForPosition)
2012-06-08 14:17:39 +08:00
{
_ignoreAnchorPointForPosition = newValue;
_transformDirty = _inverseDirty = true;
2012-06-08 14:17:39 +08:00
}
2010-12-18 16:12:59 +08:00
}
/// tag getter
int Node::getTag() const
2010-12-18 16:12:59 +08:00
{
return _tag;
2010-12-18 16:12:59 +08:00
}
/// tag setter
void Node::setTag(int var)
2010-12-18 16:12:59 +08:00
{
_tag = var;
2010-12-18 16:12:59 +08:00
}
/// userData setter
void Node::setUserData(void *var)
2010-07-07 15:24:44 +08:00
{
_userData = var;
2010-07-07 15:24:44 +08:00
}
2010-12-18 16:12:59 +08:00
int Node::getOrderOfArrival() const
2012-11-14 18:05:15 +08:00
{
return _orderOfArrival;
2012-11-14 18:05:15 +08:00
}
void Node::setOrderOfArrival(int orderOfArrival)
2012-11-14 18:05:15 +08:00
{
CCASSERT(orderOfArrival >=0, "Invalid orderOfArrival");
_orderOfArrival = orderOfArrival;
2012-11-14 18:05:15 +08:00
}
void Node::setUserObject(Object *pUserObject)
2012-11-14 18:05:15 +08:00
{
CC_SAFE_RETAIN(pUserObject);
CC_SAFE_RELEASE(_userObject);
_userObject = pUserObject;
2012-11-14 18:05:15 +08:00
}
void Node::setShaderProgram(GLProgram *pShaderProgram)
2012-11-14 18:05:15 +08:00
{
CC_SAFE_RETAIN(pShaderProgram);
CC_SAFE_RELEASE(_shaderProgram);
_shaderProgram = pShaderProgram;
2012-11-14 18:05:15 +08:00
}
2010-12-18 16:12:59 +08:00
Rect Node::getBoundingBox() const
2010-07-20 14:29:18 +08:00
{
Rect rect = Rect(0, 0, _contentSize.width, _contentSize.height);
return RectApplyAffineTransform(rect, getNodeToParentTransform());
2010-07-20 14:29:18 +08:00
}
Node * Node::create(void)
{
Node * pRet = new Node();
2013-02-27 09:38:30 +08:00
if (pRet && pRet->init())
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
void Node::cleanup()
{
// actions
this->stopAllActions();
this->unscheduleAllSelectors();
if ( _scriptType != kScriptTypeNone)
{
int action = kNodeOnCleanup;
BasicScriptData data(this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
}
// timers
arrayMakeObjectsPerformSelector(_children, cleanup, Node*);
}
2010-07-14 10:36:26 +08:00
2010-07-15 11:39:05 +08:00
const char* Node::description() const
{
return String::createWithFormat("<Node | Tag = %d>", _tag)->getCString();
}
// lazy allocs
void Node::childrenAlloc(void)
{
_children = Array::createWithCapacity(4);
_children->retain();
}
Node* Node::getChildByTag(int aTag)
{
CCASSERT( aTag != Node::INVALID_TAG, "Invalid tag");
if(_children && _children->count() > 0)
{
Object* child;
CCARRAY_FOREACH(_children, child)
{
Node* pNode = static_cast<Node*>(child);
if(pNode && pNode->_tag == 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
*/
void Node::addChild(Node *child, int zOrder, int tag)
{
CCASSERT( child != NULL, "Argument must be non-nil");
CCASSERT( child->_parent == NULL, "child already added. It can't be added again");
if( ! _children )
{
this->childrenAlloc();
}
this->insertChild(child, zOrder);
child->_tag = tag;
child->setParent(this);
child->setOrderOfArrival(s_globalOrderOfArrival++);
if( _running )
{
child->onEnter();
// prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
if (_isTransitionFinished) {
child->onEnterTransitionDidFinish();
}
}
}
void Node::addChild(Node *child, int zOrder)
{
CCASSERT( child != NULL, "Argument must be non-nil");
this->addChild(child, zOrder, child->_tag);
}
void Node::addChild(Node *child)
{
CCASSERT( child != NULL, "Argument must be non-nil");
this->addChild(child, child->_ZOrder, child->_tag);
}
void Node::removeFromParent()
2012-11-09 12:08:18 +08:00
{
this->removeFromParentAndCleanup(true);
}
void Node::removeFromParentAndCleanup(bool cleanup)
{
if (_parent != NULL)
{
_parent->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
*/
void Node::removeChild(Node* child, bool cleanup /* = true */)
{
// explicit nil handling
if (_children == NULL)
{
return;
}
int index = _children->getIndexOfObject(child);
if( index != CC_INVALID_INDEX )
this->detachChild( child, index, cleanup );
}
void Node::removeChildByTag(int tag, bool cleanup/* = true */)
{
CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
Node *child = this->getChildByTag(tag);
if (child == NULL)
{
CCLOG("cocos2d: removeChildByTag(tag = %d): child not found!", tag);
}
else
{
this->removeChild(child, cleanup);
}
}
void Node::removeAllChildren()
{
this->removeAllChildrenWithCleanup(true);
}
void Node::removeAllChildrenWithCleanup(bool cleanup)
{
// not using detachChild improves speed here
if ( _children && _children->count() > 0 )
{
Object* child;
CCARRAY_FOREACH(_children, child)
{
Node* pNode = static_cast<Node*>(child);
if (pNode)
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
if(_running)
{
pNode->onExitTransitionDidStart();
pNode->onExit();
}
if (cleanup)
{
pNode->cleanup();
}
// set parent nil at the end
pNode->setParent(NULL);
}
}
_children->removeAllObjects();
}
}
void Node::detachChild(Node *child, int childIndex, bool doCleanup)
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
if (_running)
{
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);
_children->removeObjectAtIndex(childIndex);
}
// helper used by reorderChild & add
void Node::insertChild(Node* child, int z)
{
_reorderChildDirty = true;
_children->addObject(child);
child->_setZOrder(z);
}
void Node::reorderChild(Node *child, int zOrder)
{
CCASSERT( child != NULL, "Child must be non-nil");
_reorderChildDirty = true;
child->setOrderOfArrival(s_globalOrderOfArrival++);
child->_setZOrder(zOrder);
}
void Node::sortAllChildren()
2012-04-04 21:58:04 +08:00
{
2013-09-01 06:37:35 +08:00
#if 0
if (_reorderChildDirty)
{
int i,j,length = _children->count();
2012-04-04 21:58:04 +08:00
// insertion sort
for(i=1; i<length; i++)
{
j = i-1;
2013-08-20 08:27:31 +08:00
auto tempI = static_cast<Node*>( _children->getObjectAtIndex(i) );
auto tempJ = static_cast<Node*>( _children->getObjectAtIndex(j) );
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
2013-08-20 08:27:31 +08:00
while(j>=0 && ( tempI->_ZOrder < tempJ->_ZOrder || ( tempI->_ZOrder == tempJ->_ZOrder && tempI->_orderOfArrival < tempJ->_orderOfArrival ) ) )
{
_children->fastSetObject( tempJ, j+1 );
j = j-1;
2013-08-20 08:27:31 +08:00
if(j>=0)
tempJ = static_cast<Node*>( _children->getObjectAtIndex(j) );
}
_children->fastSetObject(tempI, j+1);
}
2012-04-04 21:58:04 +08:00
//don't need to check children recursively, that's done in visit of each child
2013-08-20 08:27:31 +08:00
_reorderChildDirty = false;
}
#else
if( _reorderChildDirty ) {
std::sort( std::begin(*_children), std::end(*_children), nodeComparisonLess );
_reorderChildDirty = false;
}
#endif
2012-04-04 21:58:04 +08:00
}
void Node::draw()
2010-12-18 16:12:59 +08:00
{
//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 Node::visit()
{
// quick return if not visible. children won't be drawn.
if (!_visible)
{
return;
}
kmGLPushMatrix();
2010-08-17 14:25:41 +08:00
if (_grid && _grid->isActive())
{
_grid->beforeDraw();
}
2010-12-18 16:12:59 +08:00
this->transform();
2013-09-08 11:26:38 +08:00
int i = 0;
if(_children && _children->count() > 0)
{
sortAllChildren();
// draw children zOrder < 0
for( ; i < _children->count(); i++ )
{
2013-08-20 08:27:31 +08:00
auto node = static_cast<Node*>( _children->getObjectAtIndex(i) );
2013-08-20 08:27:31 +08:00
if ( node && node->_ZOrder < 0 )
node->visit();
else
break;
}
// self draw
this->draw();
this->updateEventPriorityIndex();
for( ; i < _children->count(); i++ )
{
2013-08-20 08:27:31 +08:00
auto node = static_cast<Node*>( _children->getObjectAtIndex(i) );
if (node)
node->visit();
}
}
else
{
this->draw();
this->updateEventPriorityIndex();
}
// reset for next frame
_orderOfArrival = 0;
if (_grid && _grid->isActive())
{
_grid->afterDraw(this);
}
2010-08-17 14:25:41 +08:00
kmGLPopMatrix();
}
void Node::transformAncestors()
{
if( _parent != NULL )
{
_parent->transformAncestors();
_parent->transform();
}
}
void Node::transform()
{
kmMat4 transfrom4x4;
2012-04-04 21:58:04 +08:00
// Convert 3x3 into 4x4 matrix
CGAffineToGL(this->getNodeToParentTransform(), transfrom4x4.mat);
2012-04-04 21:58:04 +08:00
// Update Z vertex manually
transfrom4x4.mat[14] = _vertexZ;
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 ( _camera != NULL && !(_grid != NULL && _grid->isActive()) )
{
bool translate = (_anchorPointInPoints.x != 0.0f || _anchorPointInPoints.y != 0.0f);
2012-04-04 21:58:04 +08:00
if( translate )
kmGLTranslatef(RENDER_IN_SUBPIXEL(_anchorPointInPoints.x), RENDER_IN_SUBPIXEL(_anchorPointInPoints.y), 0 );
2012-04-04 21:58:04 +08:00
_camera->locate();
2012-04-04 21:58:04 +08:00
if( translate )
kmGLTranslatef(RENDER_IN_SUBPIXEL(-_anchorPointInPoints.x), RENDER_IN_SUBPIXEL(-_anchorPointInPoints.y), 0 );
}
}
void Node::onEnter()
{
_isTransitionFinished = false;
arrayMakeObjectsPerformSelector(_children, onEnter, Node*);
this->resumeSchedulerAndActions();
_running = true;
2012-02-02 15:58:10 +08:00
if (_scriptType != kScriptTypeNone)
{
int action = kNodeOnEnter;
BasicScriptData data(this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
}
}
void Node::onEnterTransitionDidFinish()
{
_isTransitionFinished = true;
arrayMakeObjectsPerformSelector(_children, onEnterTransitionDidFinish, Node*);
if (_scriptType != kScriptTypeNone)
{
int action = kNodeOnEnterTransitionDidFinish;
BasicScriptData data(this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
}
}
void Node::onExitTransitionDidStart()
2012-04-04 21:58:04 +08:00
{
arrayMakeObjectsPerformSelector(_children, onExitTransitionDidStart, Node*);
if (_scriptType != kScriptTypeNone)
{
int action = kNodeOnExitTransitionDidStart;
BasicScriptData data(this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
}
}
void Node::onExit()
{
this->pauseSchedulerAndActions();
2010-07-14 10:36:26 +08:00
_running = false;
if (_scriptType != kScriptTypeNone)
{
int action = kNodeOnExit;
BasicScriptData data(this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
}
2012-02-02 15:58:10 +08:00
arrayMakeObjectsPerformSelector(_children, onExit, Node*);
}
2012-02-02 15:58:10 +08:00
void Node::setActionManager(ActionManager* actionManager)
2012-04-04 21:58:04 +08:00
{
if( actionManager != _actionManager ) {
this->stopAllActions();
CC_SAFE_RETAIN(actionManager);
CC_SAFE_RELEASE(_actionManager);
_actionManager = actionManager;
}
2012-04-04 21:58:04 +08:00
}
Action * Node::runAction(Action* action)
{
CCASSERT( action != NULL, "Argument must be non-nil");
_actionManager->addAction(action, this, !_running);
return action;
}
void Node::stopAllActions()
{
_actionManager->removeAllActionsFromTarget(this);
}
void Node::stopAction(Action* action)
{
_actionManager->removeAction(action);
2010-08-06 09:46:56 +08:00
}
void Node::stopActionByTag(int tag)
{
CCASSERT( tag != Action::INVALID_TAG, "Invalid tag");
_actionManager->removeActionByTag(tag, this);
}
Action * Node::getActionByTag(int tag)
{
CCASSERT( tag != Action::INVALID_TAG, "Invalid tag");
return _actionManager->getActionByTag(tag, this);
2010-08-06 09:46:56 +08:00
}
unsigned int Node::getNumberOfRunningActions() const
{
return _actionManager->getNumberOfRunningActionsInTarget(this);
}
// Node - Callbacks
void Node::setScheduler(Scheduler* scheduler)
2012-04-04 21:58:04 +08:00
{
if( scheduler != _scheduler ) {
this->unscheduleAllSelectors();
CC_SAFE_RETAIN(scheduler);
CC_SAFE_RELEASE(_scheduler);
_scheduler = scheduler;
}
2012-04-04 21:58:04 +08:00
}
bool Node::isScheduled(SEL_SCHEDULE selector)
{
return _scheduler->isScheduledForTarget(selector, this);
}
void Node::scheduleUpdate()
{
scheduleUpdateWithPriority(0);
}
void Node::scheduleUpdateWithPriority(int priority)
{
_scheduler->scheduleUpdateForTarget(this, priority, !_running);
}
void Node::scheduleUpdateWithPriorityLua(int nHandler, int priority)
2013-01-17 11:20:25 +08:00
{
unscheduleUpdate();
_updateScriptHandler = nHandler;
_scheduler->scheduleUpdateForTarget(this, priority, !_running);
2013-01-17 11:20:25 +08:00
}
void Node::unscheduleUpdate()
{
_scheduler->unscheduleUpdateForTarget(this);
if (_updateScriptHandler)
{
ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler);
_updateScriptHandler = 0;
}
}
void Node::schedule(SEL_SCHEDULE selector)
{
this->schedule(selector, 0.0f, kRepeatForever, 0.0f);
}
void Node::schedule(SEL_SCHEDULE selector, float interval)
2012-04-04 21:58:04 +08:00
{
this->schedule(selector, interval, kRepeatForever, 0.0f);
}
void Node::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");
_scheduler->scheduleSelector(selector, this, interval , repeat, delay, !_running);
}
void Node::scheduleOnce(SEL_SCHEDULE selector, float delay)
2012-04-04 21:58:04 +08:00
{
this->schedule(selector, 0.0f, 0, delay);
}
void Node::unschedule(SEL_SCHEDULE selector)
{
// explicit nil handling
if (selector == 0)
return;
_scheduler->unscheduleSelector(selector, this);
}
void Node::unscheduleAllSelectors()
{
_scheduler->unscheduleAllForTarget(this);
}
void Node::resumeSchedulerAndActions()
{
_scheduler->resumeTarget(this);
_actionManager->resumeTarget(this);
}
void Node::pauseSchedulerAndActions()
{
_scheduler->pauseTarget(this);
_actionManager->pauseTarget(this);
}
2012-11-14 18:05:15 +08:00
// override me
void Node::update(float fDelta)
2012-11-14 18:05:15 +08:00
{
if (0 != _updateScriptHandler)
{
//only lua use
SchedulerScriptData data(_updateScriptHandler,fDelta);
ScriptEvent event(kScheduleEvent,&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
2013-06-04 17:38:43 +08:00
if (_componentContainer && !_componentContainer->isEmpty())
2013-06-04 17:38:43 +08:00
{
_componentContainer->visit(fDelta);
2013-06-04 17:38:43 +08:00
}
2012-11-14 18:05:15 +08:00
}
const AffineTransform& Node::getNodeToParentTransform() const
{
if (_transformDirty)
2012-06-08 14:17:39 +08:00
{
2012-04-04 21:58:04 +08:00
// Translate values
float x = _position.x;
float y = _position.y;
2012-04-04 21:58:04 +08:00
if (_ignoreAnchorPointForPosition)
2012-06-08 14:17:39 +08:00
{
x += _anchorPointInPoints.x;
y += _anchorPointInPoints.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 (_rotationX || _rotationY)
2012-06-08 14:17:39 +08:00
{
float radiansX = -CC_DEGREES_TO_RADIANS(_rotationX);
float radiansY = -CC_DEGREES_TO_RADIANS(_rotationY);
2012-11-14 18:05:15 +08:00
cx = cosf(radiansX);
sx = sinf(radiansX);
cy = cosf(radiansY);
sy = sinf(radiansY);
}
2012-04-04 21:58:04 +08:00
bool needsSkewMatrix = ( _skewX || _skewY );
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 && !_anchorPointInPoints.equals(Point::ZERO))
2012-06-08 14:17:39 +08:00
{
x += cy * -_anchorPointInPoints.x * _scaleX + -sx * -_anchorPointInPoints.y * _scaleY;
y += sy * -_anchorPointInPoints.x * _scaleX + cx * -_anchorPointInPoints.y * _scaleY;
}
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
_transform = AffineTransformMake( cy * _scaleX, sy * _scaleX,
-sx * _scaleY, cx * _scaleY,
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)
{
AffineTransform skewMatrix = AffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(_skewY)),
tanf(CC_DEGREES_TO_RADIANS(_skewX)), 1.0f,
0.0f, 0.0f );
_transform = AffineTransformConcat(skewMatrix, _transform);
2012-04-04 21:58:04 +08:00
// adjust anchor point
if (!_anchorPointInPoints.equals(Point::ZERO))
2012-06-08 14:17:39 +08:00
{
_transform = AffineTransformTranslate(_transform, -_anchorPointInPoints.x, -_anchorPointInPoints.y);
2012-06-08 14:17:39 +08:00
}
}
if (_additionalTransformDirty)
{
_transform = AffineTransformConcat(_transform, _additionalTransform);
_additionalTransformDirty = false;
}
_transformDirty = false;
}
2012-04-04 21:58:04 +08:00
return _transform;
}
void Node::setAdditionalTransform(const AffineTransform& additionalTransform)
{
_additionalTransform = additionalTransform;
_transformDirty = true;
_additionalTransformDirty = true;
}
const AffineTransform& Node::getParentToNodeTransform() const
{
if ( _inverseDirty ) {
_inverse = AffineTransformInvert(this->getNodeToParentTransform());
_inverseDirty = false;
}
return _inverse;
}
AffineTransform Node::getNodeToWorldTransform() const
{
AffineTransform t = this->getNodeToParentTransform();
for (Node *p = _parent; p != NULL; p = p->getParent())
t = AffineTransformConcat(t, p->getNodeToParentTransform());
return t;
}
2010-07-21 17:40:10 +08:00
AffineTransform Node::getWorldToNodeTransform() const
{
return AffineTransformInvert(this->getNodeToWorldTransform());
}
Point Node::convertToNodeSpace(const Point& worldPoint) const
{
Point ret = PointApplyAffineTransform(worldPoint, getWorldToNodeTransform());
return ret;
}
Point Node::convertToWorldSpace(const Point& nodePoint) const
{
Point ret = PointApplyAffineTransform(nodePoint, getNodeToWorldTransform());
return ret;
}
Point Node::convertToNodeSpaceAR(const Point& worldPoint) const
{
Point nodePoint = convertToNodeSpace(worldPoint);
return nodePoint - _anchorPointInPoints;
}
Point Node::convertToWorldSpaceAR(const Point& nodePoint) const
{
Point pt = nodePoint + _anchorPointInPoints;
return convertToWorldSpace(pt);
}
Point Node::convertToWindowSpace(const Point& nodePoint) const
{
Point worldPoint = this->convertToWorldSpace(nodePoint);
return Director::getInstance()->convertToUI(worldPoint);
}
// convenience methods which take a Touch instead of Point
Point Node::convertTouchToNodeSpace(Touch *touch) const
{
Point point = touch->getLocation();
return this->convertToNodeSpace(point);
2010-07-30 16:35:07 +08:00
}
Point Node::convertTouchToNodeSpaceAR(Touch *touch) const
{
Point point = touch->getLocation();
return this->convertToNodeSpaceAR(point);
}
2010-07-30 16:35:07 +08:00
void Node::updateTransform()
2012-11-09 12:08:18 +08:00
{
// Recursively iterate over children
arrayMakeObjectsPerformSelector(_children, updateTransform, Node*);
}
Component* Node::getComponent(const char *pName)
2013-06-04 17:38:43 +08:00
{
if( _componentContainer )
return _componentContainer->get(pName);
return nullptr;
2013-06-04 17:38:43 +08:00
}
bool Node::addComponent(Component *pComponent)
2013-06-04 17:38:43 +08:00
{
// lazy alloc
if( !_componentContainer )
_componentContainer = new ComponentContainer(this);
return _componentContainer->add(pComponent);
2013-06-04 17:38:43 +08:00
}
bool Node::removeComponent(const char *pName)
2013-06-04 17:38:43 +08:00
{
if( _componentContainer )
return _componentContainer->remove(pName);
return false;
2013-06-04 17:38:43 +08:00
}
void Node::removeAllComponents()
2013-06-04 17:38:43 +08:00
{
if( _componentContainer )
_componentContainer->removeAll();
2013-06-04 17:38:43 +08:00
}
void Node::resetEventPriorityIndex()
{
_globalEventPriorityIndex = 0;
}
void Node::updateEventPriorityIndex()
{
_eventPriority = ++_globalEventPriorityIndex;
}
void Node::addEventId(int eventId)
{
_eventIds.insert(eventId);
}
void Node::removeEventId(int eventId)
{
_eventIds.erase(eventId);
}
// NodeRGBA
NodeRGBA::NodeRGBA()
: _displayedOpacity(255)
2013-02-28 11:55:36 +08:00
, _realOpacity(255)
, _displayedColor(Color3B::WHITE)
, _realColor(Color3B::WHITE)
2013-02-27 14:48:19 +08:00
, _cascadeColorEnabled(false)
, _cascadeOpacityEnabled(false)
{}
NodeRGBA::~NodeRGBA() {}
2013-02-27 09:38:30 +08:00
bool NodeRGBA::init()
2013-02-27 09:38:30 +08:00
{
if (Node::init())
2013-02-27 09:38:30 +08:00
{
2013-02-27 14:48:19 +08:00
_displayedOpacity = _realOpacity = 255;
_displayedColor = _realColor = Color3B::WHITE;
2013-02-27 14:48:19 +08:00
_cascadeOpacityEnabled = _cascadeColorEnabled = false;
2013-02-27 09:38:30 +08:00
return true;
}
return false;
}
GLubyte NodeRGBA::getOpacity(void) const
2013-02-27 09:38:30 +08:00
{
2013-02-27 14:48:19 +08:00
return _realOpacity;
2013-02-27 09:38:30 +08:00
}
GLubyte NodeRGBA::getDisplayedOpacity(void) const
2013-02-27 09:38:30 +08:00
{
2013-02-27 14:48:19 +08:00
return _displayedOpacity;
2013-02-27 09:38:30 +08:00
}
void NodeRGBA::setOpacity(GLubyte opacity)
2013-02-27 09:38:30 +08:00
{
2013-02-27 14:48:19 +08:00
_displayedOpacity = _realOpacity = opacity;
2013-02-27 09:38:30 +08:00
2013-02-27 14:48:19 +08:00
if (_cascadeOpacityEnabled)
{
2013-02-27 09:38:30 +08:00
GLubyte parentOpacity = 255;
RGBAProtocol* pParent = dynamic_cast<RGBAProtocol*>(_parent);
2013-02-27 09:38:30 +08:00
if (pParent && pParent->isCascadeOpacityEnabled())
{
parentOpacity = pParent->getDisplayedOpacity();
}
this->updateDisplayedOpacity(parentOpacity);
}
}
void NodeRGBA::updateDisplayedOpacity(GLubyte parentOpacity)
2013-02-27 09:38:30 +08:00
{
2013-02-27 14:48:19 +08:00
_displayedOpacity = _realOpacity * parentOpacity/255.0;
2013-02-27 09:38:30 +08:00
2013-02-27 18:21:35 +08:00
if (_cascadeOpacityEnabled)
2013-02-27 14:48:19 +08:00
{
Object* pObj;
CCARRAY_FOREACH(_children, pObj)
2013-02-27 14:48:19 +08:00
{
RGBAProtocol* item = dynamic_cast<RGBAProtocol*>(pObj);
2013-02-27 14:48:19 +08:00
if (item)
{
item->updateDisplayedOpacity(_displayedOpacity);
2013-02-27 09:38:30 +08:00
}
}
}
}
bool NodeRGBA::isCascadeOpacityEnabled(void) const
2013-02-27 15:30:49 +08:00
{
return _cascadeOpacityEnabled;
}
void NodeRGBA::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
2013-02-27 15:30:49 +08:00
{
_cascadeOpacityEnabled = cascadeOpacityEnabled;
}
const Color3B& NodeRGBA::getColor(void) const
2013-02-27 09:38:30 +08:00
{
return _realColor;
}
const Color3B& NodeRGBA::getDisplayedColor() const
2013-02-27 09:38:30 +08:00
{
return _displayedColor;
}
void NodeRGBA::setColor(const Color3B& color)
2013-02-27 09:38:30 +08:00
{
_displayedColor = _realColor = color;
2013-02-27 14:48:19 +08:00
if (_cascadeColorEnabled)
{
Color3B parentColor = Color3B::WHITE;
RGBAProtocol *parent = dynamic_cast<RGBAProtocol*>(_parent);
2013-02-27 14:48:19 +08:00
if (parent && parent->isCascadeColorEnabled())
{
2013-02-27 18:21:35 +08:00
parentColor = parent->getDisplayedColor();
2013-02-27 14:48:19 +08:00
}
2013-02-27 18:21:35 +08:00
2013-02-28 11:55:36 +08:00
updateDisplayedColor(parentColor);
2013-02-27 09:38:30 +08:00
}
}
void NodeRGBA::updateDisplayedColor(const Color3B& parentColor)
2013-02-27 09:38:30 +08:00
{
_displayedColor.r = _realColor.r * parentColor.r/255.0;
_displayedColor.g = _realColor.g * parentColor.g/255.0;
_displayedColor.b = _realColor.b * parentColor.b/255.0;
2013-02-27 14:48:19 +08:00
if (_cascadeColorEnabled)
{
Object *obj = NULL;
CCARRAY_FOREACH(_children, obj)
2013-02-27 14:48:19 +08:00
{
RGBAProtocol *item = dynamic_cast<RGBAProtocol*>(obj);
2013-02-27 14:48:19 +08:00
if (item)
{
item->updateDisplayedColor(_displayedColor);
2013-02-27 09:38:30 +08:00
}
}
}
}
2013-01-17 11:20:25 +08:00
bool NodeRGBA::isCascadeColorEnabled(void) const
2013-02-27 15:30:49 +08:00
{
return _cascadeColorEnabled;
}
void NodeRGBA::setCascadeColorEnabled(bool cascadeColorEnabled)
2013-02-27 15:30:49 +08:00
{
_cascadeColorEnabled = cascadeColorEnabled;
}
2012-04-18 18:43:45 +08:00
NS_CC_END