axmol/cocos/2d/CCNode.cpp

1832 lines
42 KiB
C++
Raw Normal View History

2010-12-18 16:12:59 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2009 Valentin Milea
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies 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
#include "2d/CCNode.h"
2013-08-26 01:50:29 +08:00
#include <algorithm>
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h"
#include "2d/ccCArray.h"
#include "TransformUtils.h"
#include "CCGrid.h"
#include "2d/CCDirector.h"
2010-12-18 16:12:59 +08:00
#include "CCScheduler.h"
#include "2d/CCTouch.h"
#include "2d/CCActionManager.h"
2014-04-27 01:35:57 +08:00
#include "2d/CCScriptSupport.h"
#include "2d/CCGLProgram.h"
#include "2d/CCEventDispatcher.h"
#include "2d/CCEvent.h"
#include "2d/CCEventTouch.h"
#include "2d/CCLayer.h"
#if CC_USE_PHYSICS
2014-04-27 01:35:57 +08:00
#include "physics/CCPhysicsBody.h"
#endif
2012-04-04 21:58:04 +08:00
// externals
#include "CCComponent.h"
#include "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
bool nodeComparisonLess(Node* n1, Node* n2)
{
return( n1->getLocalZOrder() < n2->getLocalZOrder() ||
( n1->getLocalZOrder() == n2->getLocalZOrder() && n1->getOrderOfArrival() < n2->getOrderOfArrival() )
);
}
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.
int Node::s_globalOrderOfArrival = 1;
Node::Node(void)
: _rotationX(0.0f)
, _rotationY(0.0f)
2014-02-23 11:16:42 +08:00
, _rotationZ_X(0.0f)
, _rotationZ_Y(0.0f)
, _scaleX(1.0f)
, _scaleY(1.0f)
2014-02-23 11:16:42 +08:00
, _scaleZ(1.0f)
, _positionZ(0.0f)
, _position(Vector2::ZERO)
, _skewX(0.0f)
, _skewY(0.0f)
, _anchorPointInPoints(Vector2::ZERO)
, _anchorPoint(Vector2::ZERO)
, _contentSize(Size::ZERO)
, _useAdditionalTransform(false)
, _transformDirty(true)
, _inverseDirty(true)
, _transformUpdated(true)
2011-06-10 17:51:37 +08:00
// children (lazy allocs)
// lazy alloc
, _localZOrder(0)
, _globalZOrder(0)
, _parent(nullptr)
// "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(nullptr)
, _userObject(nullptr)
, _shaderProgram(nullptr)
, _orderOfArrival(0)
, _running(false)
, _visible(true)
, _ignoreAnchorPointForPosition(false)
, _reorderChildDirty(false)
, _isTransitionFinished(false)
#if CC_ENABLE_SCRIPT_BINDING
, _updateScriptHandler(0)
#endif
, _componentContainer(nullptr)
#if CC_USE_PHYSICS
, _physicsBody(nullptr)
#endif
, _displayedOpacity(255)
, _realOpacity(255)
, _displayedColor(Color3B::WHITE)
, _realColor(Color3B::WHITE)
, _cascadeColorEnabled(false)
, _cascadeOpacityEnabled(false)
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();
_eventDispatcher = director->getEventDispatcher();
_eventDispatcher->retain();
#if CC_ENABLE_SCRIPT_BINDING
ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine();
_scriptType = engine != nullptr ? engine->getScriptType() : kScriptTypeNone;
#endif
2014-04-08 23:05:33 +08:00
_transform = _inverse = _additionalTransform = Matrix::identity();
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 CC_ENABLE_SCRIPT_BINDING
if (_updateScriptHandler)
{
ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler);
}
#endif
2010-07-14 10:36:26 +08:00
// User object has to be released before others, since userObject may have a weak reference of this node
// It may invoke `node->stopAllAction();` while `_actionManager` is null if the next line is after `CC_SAFE_RELEASE_NULL(_actionManager)`.
CC_SAFE_RELEASE_NULL(_userObject);
// attributes
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
CC_SAFE_RELEASE_NULL(_shaderProgram);
2010-07-14 10:36:26 +08:00
for (auto& child : _children)
{
child->_parent = nullptr;
}
2010-07-14 10:36:26 +08:00
removeAllComponents();
CC_SAFE_DELETE(_componentContainer);
#if CC_USE_PHYSICS
setPhysicsBody(nullptr);
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
#endif
CC_SAFE_RELEASE_NULL(_actionManager);
CC_SAFE_RELEASE_NULL(_scheduler);
_eventDispatcher->removeEventListenersForTarget(this);
#if CC_NODE_DEBUG_VERIFY_EVENT_LISTENERS && COCOS2D_DEBUG > 0
_eventDispatcher->debugCheckNodeHasNoEventListenersOnDestruction(this);
#endif
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
CCASSERT(!_running, "Node still marked as running on node destruction! Was base class onExit() called in derived class onExit() implementations?");
Fix crashes in event dispatch other related safety checks/fixes 1: Add a fix to prevent events from being sent to scene nodes which are destroyed. Previously when a node was being destroyed it would try to unregister any event listeners it would have. This process would fail however in the case where event dispatching was already underway because we can't modify the listeners list while we are iterating through it. This is a pretty common occurrence since we often tear down & switch scenes in response to button clicks etc. Fix the problem by nulling out the slot for the listener we are removing in this case, so that it's node no longer receives any events after it is destroyed. The event dispatching code will cleanup this unused/nulled slot once the event processing loop has terminated. 2: When removing an event listener in cocos2d::EventDispatcher, ensure immediately it gets removed from the node priority map and dirty nodes set in order to ensure we don't inadvertently access any dangling pointers to nodes. 3: Add debug checks to ensure nodes have no associated event listeners after they are destroyed. This check will catch the problem fixed by (1) if it is still present. 4: Add some extra debug sanity checks in the event dispatcher to ensure nodes are not added to the lists twice and that they are being properly removed from the lists under certain conditions. 5: In cocos2d::Node's destructor NULL out members after releasing them in case they somehow happen to be accessed during the destructor. Move the release of the event dispatcher to the very end, since we need to check against it in debug.
2014-03-27 04:12:44 +08:00
CC_SAFE_RELEASE(_eventDispatcher);
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;
}
2014-02-27 15:12:57 +08:00
void Node::setSkewX(float skewX)
{
2014-02-27 15:12:57 +08:00
if (_skewX == skewX)
return;
_skewX = skewX;
_transformUpdated = _transformDirty = _inverseDirty = true;
}
float Node::getSkewY() const
{
return _skewY;
}
2014-02-27 15:12:57 +08:00
void Node::setSkewY(float skewY)
{
2014-02-27 15:12:57 +08:00
if (_skewY == skewY)
return;
_skewY = skewY;
_transformUpdated = _transformDirty = _inverseDirty = true;
}
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::_setLocalZOrder(int z)
2010-07-14 10:36:26 +08:00
{
_localZOrder = z;
2010-12-18 16:12:59 +08:00
}
void Node::setLocalZOrder(int z)
{
2014-02-27 15:12:57 +08:00
if (_localZOrder == z)
return;
_localZOrder = z;
if (_parent)
{
_parent->reorderChild(this, z);
}
_eventDispatcher->setDirtyForNode(this);
}
void Node::setGlobalZOrder(float globalZOrder)
{
if (_globalZOrder != globalZOrder)
{
_globalZOrder = globalZOrder;
_eventDispatcher->setDirtyForNode(this);
}
}
2010-12-18 16:12:59 +08:00
/// rotation getter
float Node::getRotation() const
2010-12-18 16:12:59 +08:00
{
2014-02-23 11:16:42 +08:00
CCASSERT(_rotationZ_X == _rotationZ_Y, "CCNode#rotation. RotationX != RotationY. Don't know which one to return");
return _rotationZ_X;
2010-12-18 16:12:59 +08:00
}
/// rotation setter
2014-02-27 15:12:57 +08:00
void Node::setRotation(float rotation)
{
2014-02-27 15:12:57 +08:00
if (_rotationZ_X == rotation)
return;
_rotationZ_X = _rotationZ_Y = rotation;
_transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS
if (_physicsBody && !_physicsBody->_rotationResetTag)
{
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
updatePhysicsBodyRotation(layer);
}
#endif
2012-11-14 18:05:15 +08:00
}
2014-02-23 11:16:42 +08:00
float Node::getRotationSkewX() const
{
return _rotationZ_X;
}
2014-04-02 15:06:16 +08:00
void Node::setRotation3D(const Vector3& rotation)
2014-02-23 11:16:42 +08:00
{
2014-02-27 15:12:57 +08:00
if (_rotationX == rotation.x &&
_rotationY == rotation.y &&
_rotationZ_X == rotation.z)
return;
_transformUpdated = _transformDirty = _inverseDirty = true;
2014-02-23 11:16:42 +08:00
_rotationX = rotation.x;
_rotationY = rotation.y;
// rotation Z is decomposed in 2 to simulate Skew for Flash animations
_rotationZ_Y = _rotationZ_X = rotation.z;
#if CC_USE_PHYSICS
if (_physicsBody)
{
2014-04-21 13:32:47 +08:00
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
updatePhysicsBodyRotation(layer);
2014-02-23 11:16:42 +08:00
}
#endif
}
2014-04-02 15:06:16 +08:00
Vector3 Node::getRotation3D() const
2012-11-14 18:05:15 +08:00
{
2014-02-23 11:16:42 +08:00
// rotation Z is decomposed in 2 to simulate Skew for Flash animations
CCASSERT(_rotationZ_X == _rotationZ_Y, "_rotationZ_X != _rotationZ_Y");
2014-04-02 15:06:16 +08:00
return Vector3(_rotationX,_rotationY,_rotationZ_X);
2012-11-14 18:05:15 +08:00
}
2014-02-27 15:12:57 +08:00
void Node::setRotationSkewX(float rotationX)
2012-11-14 18:05:15 +08:00
{
2014-02-27 15:12:57 +08:00
if (_rotationZ_X == rotationX)
return;
_rotationZ_X = rotationX;
_transformUpdated = _transformDirty = _inverseDirty = true;
2012-11-14 18:05:15 +08:00
}
2014-02-23 11:16:42 +08:00
float Node::getRotationSkewY() const
2012-11-14 18:05:15 +08:00
{
2014-02-23 11:16:42 +08:00
return _rotationZ_Y;
2012-11-14 18:05:15 +08:00
}
2014-02-23 11:16:42 +08:00
void Node::setRotationSkewY(float rotationY)
2012-11-14 18:05:15 +08:00
{
2014-02-27 15:12:57 +08:00
if (_rotationZ_Y == rotationY)
return;
2014-02-23 11:16:42 +08:00
_rotationZ_Y = rotationY;
_transformUpdated = _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)
{
2014-02-27 15:12:57 +08:00
if (_scaleX == scale)
return;
2014-03-01 05:48:00 +08:00
_scaleX = _scaleY = _scaleZ = scale;
_transformUpdated = _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
}
/// scale setter
void Node::setScale(float scaleX,float scaleY)
{
2014-02-27 15:12:57 +08:00
if (_scaleX == scaleX && _scaleY == scaleY)
return;
_scaleX = scaleX;
_scaleY = scaleY;
_transformUpdated = _transformDirty = _inverseDirty = true;
}
2010-12-18 16:12:59 +08:00
/// scaleX setter
2014-02-27 15:12:57 +08:00
void Node::setScaleX(float scaleX)
{
2014-02-27 15:12:57 +08:00
if (_scaleX == scaleX)
return;
_scaleX = scaleX;
_transformUpdated = _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
}
2014-02-23 11:16:42 +08:00
/// scaleY setter
2014-02-27 15:12:57 +08:00
void Node::setScaleZ(float scaleZ)
2014-02-23 11:16:42 +08:00
{
2014-02-27 15:12:57 +08:00
if (_scaleZ == scaleZ)
return;
_scaleZ = scaleZ;
_transformUpdated = _transformDirty = _inverseDirty = true;
2014-02-23 11:16:42 +08:00
}
/// scaleY getter
float Node::getScaleZ() const
{
return _scaleZ;
}
/// scaleY setter
2014-02-27 15:12:57 +08:00
void Node::setScaleY(float scaleY)
{
2014-02-27 15:12:57 +08:00
if (_scaleY == scaleY)
return;
_scaleY = scaleY;
_transformUpdated = _transformDirty = _inverseDirty = true;
2010-12-18 16:12:59 +08:00
}
2014-02-23 11:16:42 +08:00
2010-12-18 16:12:59 +08:00
/// position getter
const Vector2& Node::getPosition() const
2010-12-18 16:12:59 +08:00
{
return _position;
2010-12-18 16:12:59 +08:00
}
/// position setter
2014-04-15 16:16:14 +08:00
void Node::setPosition(const Vector2& position)
2010-07-07 15:24:44 +08:00
{
2014-02-27 15:12:57 +08:00
if (_position.equals(position))
return;
_position = position;
_transformUpdated = _transformDirty = _inverseDirty = true;
#if CC_USE_PHYSICS
if (_physicsBody != nullptr && !_physicsBody->_positionResetTag)
{
Layer* layer = _physicsBody->getWorld() != nullptr ? &_physicsBody->getWorld()->getLayer() : nullptr;
updatePhysicsBodyPosition(layer);
}
#endif
}
void Node::getPosition(float* x, float* y) const
{
*x = _position.x;
*y = _position.y;
}
void Node::setPosition(float x, float y)
{
setPosition(Vector2(x, y));
}
2014-04-02 15:06:16 +08:00
void Node::setPosition3D(const Vector3& position)
{
2014-02-23 11:16:42 +08:00
_positionZ = position.z;
setPosition(Vector2(position.x, position.y));
}
2014-04-02 15:06:16 +08:00
Vector3 Node::getPosition3D() const
{
2014-04-02 15:06:16 +08:00
Vector3 ret;
2014-02-23 11:16:42 +08:00
ret.x = _position.x;
ret.y = _position.y;
ret.z = _positionZ;
return ret;
}
float Node::getPositionX() const
{
return _position.x;
}
void Node::setPositionX(float x)
{
setPosition(Vector2(x, _position.y));
}
2014-02-23 11:16:42 +08:00
float Node::getPositionY() const
{
return _position.y;
}
void Node::setPositionY(float y)
{
setPosition(Vector2(_position.x, y));
}
2014-02-23 11:16:42 +08:00
float Node::getPositionZ() const
{
return _positionZ;
}
void Node::setPositionZ(float positionZ)
{
2014-02-27 15:12:57 +08:00
if (_positionZ == positionZ)
return;
_transformUpdated = _transformDirty = _inverseDirty = true;
2014-02-23 11:16:42 +08:00
_positionZ = positionZ;
// XXX BUG
// Global Z Order should based on the modelViewTransform
setGlobalZOrder(positionZ);
}
2013-12-12 12:07:20 +08:00
ssize_t Node::getChildrenCount() const
{
return _children.size();
}
/// 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
{
2014-03-07 10:03:34 +08:00
if(var != _visible)
{
_visible = var;
if(_visible) _transformUpdated = _transformDirty = _inverseDirty = true;
}
2010-07-07 15:24:44 +08:00
}
const Vector2& Node::getAnchorPointInPoints() const
{
return _anchorPointInPoints;
}
/// anchorPoint getter
const Vector2& Node::getAnchorPoint() const
2010-07-07 15:24:44 +08:00
{
return _anchorPoint;
2010-12-18 16:12:59 +08:00
}
2014-04-15 16:47:29 +08:00
void Node::setAnchorPoint(const Vector2& point)
2010-07-08 10:26:58 +08:00
{
#if CC_USE_PHYSICS
if (_physicsBody != nullptr && !point.equals(Vector2::ANCHOR_MIDDLE))
{
CCLOG("Node warning: This node has a physics body, the anchor must be in the middle, you cann't change this to other value.");
return;
}
#endif
if( ! point.equals(_anchorPoint))
{
_anchorPoint = point;
_anchorPointInPoints = Vector2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y );
_transformUpdated = _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
_anchorPointInPoints = Vector2(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y );
_transformUpdated = _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;
_transformUpdated = _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(Ref *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
Scene* Node::getScene()
{
if(!_parent)
return nullptr;
2014-02-27 15:12:57 +08:00
return _parent->getScene();
}
Rect Node::getBoundingBox() const
2010-07-20 14:29:18 +08:00
{
Rect rect = Rect(0, 0, _contentSize.width, _contentSize.height);
return RectApplyAffineTransform(rect, getNodeToParentAffineTransform());
2010-07-20 14:29:18 +08:00
}
Node * Node::create(void)
{
Node * ret = new Node();
if (ret && ret->init())
2013-02-27 09:38:30 +08:00
{
ret->autorelease();
2013-02-27 09:38:30 +08:00
}
else
{
CC_SAFE_DELETE(ret);
2013-02-27 09:38:30 +08:00
}
return ret;
}
void Node::cleanup()
{
// actions
this->stopAllActions();
this->unscheduleAllSelectors();
#if CC_ENABLE_SCRIPT_BINDING
if ( _scriptType != kScriptTypeNone)
{
int action = kNodeOnCleanup;
BasicScriptData data(this,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
}
#endif // #if CC_ENABLE_SCRIPT_BINDING
// timers
for( const auto &child: _children)
child->cleanup();
}
2010-07-14 10:36:26 +08:00
2010-07-15 11:39:05 +08:00
std::string Node::getDescription() const
{
return StringUtils::format("<Node | Tag = %d", _tag);
}
// lazy allocs
void Node::childrenAlloc(void)
{
_children.reserve(4);
}
Node* Node::getChildByTag(int tag)
{
CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
for (auto& child : _children)
{
if(child && child->_tag == tag)
return child;
}
return nullptr;
}
/* "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 != nullptr, "Argument must be non-nil");
CCASSERT( child->_parent == nullptr, "child already added. It can't be added again");
if (_children.empty())
{
this->childrenAlloc();
}
this->insertChild(child, zOrder);
child->_tag = tag;
child->setParent(this);
child->setOrderOfArrival(s_globalOrderOfArrival++);
#if CC_USE_PHYSICS
// Recursive add children with which have physics body.
for (Node* node = this; node != nullptr; node = node->getParent())
{
Layer* layer = dynamic_cast<Layer*>(node);
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
{
layer->addChildToPhysicsWorld(child);
break;
}
}
#endif
if( _running )
{
child->onEnter();
// prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
if (_isTransitionFinished) {
child->onEnterTransitionDidFinish();
}
}
if (_cascadeColorEnabled)
{
updateCascadeColor();
}
if (_cascadeOpacityEnabled)
{
updateCascadeOpacity();
}
}
void Node::addChild(Node *child, int zOrder)
{
CCASSERT( child != nullptr, "Argument must be non-nil");
this->addChild(child, zOrder, child->_tag);
}
void Node::addChild(Node *child)
{
CCASSERT( child != nullptr, "Argument must be non-nil");
this->addChild(child, child->_localZOrder, child->_tag);
}
void Node::removeFromParent()
2012-11-09 12:08:18 +08:00
{
this->removeFromParentAndCleanup(true);
}
void Node::removeFromParentAndCleanup(bool cleanup)
{
if (_parent != nullptr)
{
_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.empty())
{
return;
}
2013-12-12 12:07:20 +08:00
ssize_t index = _children.getIndex(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 == nullptr)
{
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
for (auto& child : _children)
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
if(_running)
{
child->onExitTransitionDidStart();
child->onExit();
}
#if CC_USE_PHYSICS
if (child->_physicsBody != nullptr)
{
child->_physicsBody->removeFromWorld();
}
#endif
if (cleanup)
{
child->cleanup();
}
// set parent nil at the end
child->setParent(nullptr);
}
_children.clear();
}
2013-12-12 12:07:20 +08:00
void Node::detachChild(Node *child, ssize_t childIndex, bool doCleanup)
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
if (_running)
{
child->onExitTransitionDidStart();
child->onExit();
}
#if CC_USE_PHYSICS
if (child->_physicsBody != nullptr)
{
child->_physicsBody->removeFromWorld();
}
#endif
// 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(nullptr);
_children.erase(childIndex);
}
// helper used by reorderChild & add
void Node::insertChild(Node* child, int z)
{
_reorderChildDirty = true;
_children.pushBack(child);
child->_setLocalZOrder(z);
}
void Node::reorderChild(Node *child, int zOrder)
{
CCASSERT( child != nullptr, "Child must be non-nil");
_reorderChildDirty = true;
child->setOrderOfArrival(s_globalOrderOfArrival++);
child->_setLocalZOrder(zOrder);
}
void Node::sortAllChildren()
2012-04-04 21:58:04 +08:00
{
if( _reorderChildDirty ) {
std::sort( std::begin(_children), std::end(_children), nodeComparisonLess );
_reorderChildDirty = false;
}
2012-04-04 21:58:04 +08:00
}
void Node::draw()
{
auto renderer = Director::getInstance()->getRenderer();
draw(renderer, _modelViewTransform, true);
}
2014-04-08 22:07:35 +08:00
void Node::draw(Renderer* renderer, const Matrix &transform, bool transformUpdated)
{
}
void Node::visit()
{
auto renderer = Director::getInstance()->getRenderer();
Matrix parentTransform = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2014-04-04 18:13:10 +08:00
visit(renderer, parentTransform, true);
}
2014-04-08 21:45:54 +08:00
void Node::visit(Renderer* renderer, const Matrix &parentTransform, bool parentTransformUpdated)
{
// quick return if not visible. children won't be drawn.
if (!_visible)
{
return;
}
2010-08-17 14:25:41 +08:00
bool dirty = _transformUpdated || parentTransformUpdated;
if(dirty)
_modelViewTransform = this->transform(parentTransform);
_transformUpdated = false;
// IMPORTANT:
2014-04-10 11:14:46 +08:00
// To ease the migration to v3.0, we still support the Matrix stack,
// but it is deprecated and your code should not rely on it
Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when seting matrix stack");
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
2013-12-05 17:19:01 +08:00
int i = 0;
if(!_children.empty())
{
sortAllChildren();
// draw children zOrder < 0
for( ; i < _children.size(); i++ )
{
auto node = _children.at(i);
if ( node && node->_localZOrder < 0 )
node->visit(renderer, _modelViewTransform, dirty);
else
break;
}
// self draw
this->draw(renderer, _modelViewTransform, dirty);
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
(*it)->visit(renderer, _modelViewTransform, dirty);
}
else
{
this->draw(renderer, _modelViewTransform, dirty);
}
// reset for next frame
_orderOfArrival = 0;
2010-08-17 14:25:41 +08:00
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
2014-04-08 23:05:33 +08:00
Matrix Node::transform(const Matrix& parentTransform)
{
2014-04-08 23:05:33 +08:00
Matrix ret = this->getNodeToParentTransform();
ret = parentTransform * ret;
return ret;
}
#if CC_ENABLE_SCRIPT_BINDING
static bool sendNodeEventToJS(Node* node, int action)
{
auto scriptEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (scriptEngine->isCalledFromScript())
{
scriptEngine->setCalledFromScript(false);
}
else
{
BasicScriptData data(node,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
if (scriptEngine->sendEvent(&scriptEvent))
return true;
}
return false;
}
static void sendNodeEventToLua(Node* node, int action)
{
auto scriptEngine = ScriptEngineManager::getInstance()->getScriptEngine();
BasicScriptData data(node,(void*)&action);
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
scriptEngine->sendEvent(&scriptEvent);
}
#endif
void Node::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (sendNodeEventToJS(this, kNodeOnEnter))
return;
}
#endif
_isTransitionFinished = false;
for( const auto &child: _children)
child->onEnter();
this->resume();
_running = true;
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua)
{
sendNodeEventToLua(this, kNodeOnEnter);
}
#endif
}
void Node::onEnterTransitionDidFinish()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (sendNodeEventToJS(this, kNodeOnEnterTransitionDidFinish))
return;
}
#endif
_isTransitionFinished = true;
for( const auto &child: _children)
child->onEnterTransitionDidFinish();
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua)
{
sendNodeEventToLua(this, kNodeOnEnterTransitionDidFinish);
}
#endif
}
void Node::onExitTransitionDidStart()
2012-04-04 21:58:04 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (sendNodeEventToJS(this, kNodeOnExitTransitionDidStart))
return;
}
#endif
for( const auto &child: _children)
child->onExitTransitionDidStart();
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua)
{
sendNodeEventToLua(this, kNodeOnExitTransitionDidStart);
}
#endif
}
void Node::onExit()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (sendNodeEventToJS(this, kNodeOnExit))
return;
}
#endif
this->pause();
_running = false;
for( const auto &child: _children)
child->onExit();
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeLua)
{
sendNodeEventToLua(this, kNodeOnExit);
}
#endif
}
2012-02-02 15:58:10 +08:00
void Node::setEventDispatcher(EventDispatcher* dispatcher)
{
if (dispatcher != _eventDispatcher)
{
_eventDispatcher->removeEventListenersForTarget(this);
CC_SAFE_RETAIN(dispatcher);
CC_SAFE_RELEASE(_eventDispatcher);
_eventDispatcher = dispatcher;
}
}
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 != nullptr, "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
}
2013-12-12 12:07:20 +08:00
ssize_t 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->isScheduled(selector, this);
}
void Node::scheduleUpdate()
{
scheduleUpdateWithPriority(0);
}
void Node::scheduleUpdateWithPriority(int priority)
{
_scheduler->scheduleUpdate(this, priority, !_running);
}
void Node::scheduleUpdateWithPriorityLua(int nHandler, int priority)
2013-01-17 11:20:25 +08:00
{
unscheduleUpdate();
#if CC_ENABLE_SCRIPT_BINDING
_updateScriptHandler = nHandler;
#endif
_scheduler->scheduleUpdate(this, priority, !_running);
2013-01-17 11:20:25 +08:00
}
void Node::unscheduleUpdate()
{
_scheduler->unscheduleUpdate(this);
#if CC_ENABLE_SCRIPT_BINDING
if (_updateScriptHandler)
{
ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler);
_updateScriptHandler = 0;
}
#endif
}
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->schedule(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 null handling
if (selector == nullptr)
return;
_scheduler->unschedule(selector, this);
}
void Node::unscheduleAllSelectors()
{
_scheduler->unscheduleAllForTarget(this);
}
void Node::resume()
{
_scheduler->resumeTarget(this);
_actionManager->resumeTarget(this);
_eventDispatcher->resumeEventListenersForTarget(this);
}
void Node::pause()
{
_scheduler->pauseTarget(this);
_actionManager->pauseTarget(this);
_eventDispatcher->pauseEventListenersForTarget(this);
}
void Node::resumeSchedulerAndActions()
{
resume();
}
void Node::pauseSchedulerAndActions()
{
pause();
}
2012-11-14 18:05:15 +08:00
// override me
void Node::update(float fDelta)
2012-11-14 18:05:15 +08:00
{
#if CC_ENABLE_SCRIPT_BINDING
if (0 != _updateScriptHandler)
{
//only lua use
SchedulerScriptData data(_updateScriptHandler,fDelta);
ScriptEvent event(kScheduleEvent,&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
}
#endif
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
}
AffineTransform Node::getNodeToParentAffineTransform() const
{
AffineTransform ret;
2014-04-10 23:12:29 +08:00
GLToCGAffine(getNodeToParentTransform().m, &ret);
2012-04-04 21:58:04 +08:00
return ret;
}
const Matrix& Node::getNodeToParentTransform() const
{
if (_transformDirty)
2012-06-08 14:17:39 +08:00
{
// Translate values
float x = _position.x;
float y = _position.y;
2014-02-23 11:16:42 +08:00
float z = _positionZ;
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;
2014-02-23 11:16:42 +08:00
if (_rotationZ_X || _rotationZ_Y)
2012-06-08 14:17:39 +08:00
{
2014-02-23 11:16:42 +08:00
float radiansX = -CC_DEGREES_TO_RADIANS(_rotationZ_X);
float radiansY = -CC_DEGREES_TO_RADIANS(_rotationZ_Y);
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(Vector2::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
2014-04-08 23:05:33 +08:00
float mat[] = {
2014-02-23 11:16:42 +08:00
cy * _scaleX, sy * _scaleX, 0, 0,
-sx * _scaleY, cx * _scaleY, 0, 0,
0, 0, _scaleZ, 0,
x, y, z, 1 };
2014-04-08 23:05:33 +08:00
_transform.set(mat);
2012-04-04 21:58:04 +08:00
2014-02-23 11:16:42 +08:00
// XXX
// FIX ME: Expensive operation.
// FIX ME: It should be done together with the rotationZ
if(_rotationY) {
2014-04-10 23:12:29 +08:00
Matrix rotY;
Matrix::createRotationY(CC_DEGREES_TO_RADIANS(_rotationY), &rotY);
2014-04-08 23:05:33 +08:00
_transform = _transform * rotY;
2014-02-23 11:16:42 +08:00
}
if(_rotationX) {
2014-04-10 23:12:29 +08:00
Matrix rotX;
Matrix::createRotationY(CC_DEGREES_TO_RADIANS(_rotationX), &rotX);
2014-04-08 23:05:33 +08:00
_transform = _transform * rotX;
2014-02-23 11:16:42 +08:00
}
// XXX: Try to inline skew
// If skew is needed, apply skew and then anchor point
if (needsSkewMatrix)
2012-06-08 14:17:39 +08:00
{
2014-04-10 23:12:29 +08:00
Matrix skewMatrix(1, (float)tanf(CC_DEGREES_TO_RADIANS(_skewY)), 0, 0,
(float)tanf(CC_DEGREES_TO_RADIANS(_skewX)), 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
2014-04-08 23:05:33 +08:00
_transform = _transform * skewMatrix;
2012-04-04 21:58:04 +08:00
// adjust anchor point
if (!_anchorPointInPoints.equals(Vector2::ZERO))
2012-06-08 14:17:39 +08:00
{
// XXX: Argh, Matrix needs a "translate" method.
// XXX: Although this is faster than multiplying a vec4 * mat4
2014-04-08 23:05:33 +08:00
_transform.m[12] += _transform.m[0] * -_anchorPointInPoints.x + _transform.m[4] * -_anchorPointInPoints.y;
_transform.m[13] += _transform.m[1] * -_anchorPointInPoints.x + _transform.m[5] * -_anchorPointInPoints.y;
2012-06-08 14:17:39 +08:00
}
}
if (_useAdditionalTransform)
{
2014-04-08 23:05:33 +08:00
_transform = _transform * _additionalTransform;
}
_transformDirty = false;
}
return _transform;
}
2014-04-08 23:05:33 +08:00
void Node::setNodeToParentTransform(const Matrix& transform)
2013-12-21 08:33:31 +08:00
{
_transform = transform;
_transformDirty = false;
_transformUpdated = true;
2013-12-21 08:33:31 +08:00
}
void Node::setAdditionalTransform(const AffineTransform& additionalTransform)
{
2014-04-08 23:05:33 +08:00
Matrix tmp;
CGAffineToGL(additionalTransform, tmp.m);
setAdditionalTransform(&tmp);
}
2014-04-08 23:05:33 +08:00
void Node::setAdditionalTransform(Matrix* additionalTransform)
{
if(additionalTransform == nullptr) {
_useAdditionalTransform = false;
} else {
_additionalTransform = *additionalTransform;
_useAdditionalTransform = true;
}
_transformUpdated = _transformDirty = _inverseDirty = true;
}
AffineTransform Node::getParentToNodeAffineTransform() const
{
AffineTransform ret;
2014-04-08 22:13:59 +08:00
Matrix ret4 = getParentToNodeTransform();
2014-04-08 22:13:59 +08:00
GLToCGAffine(ret4.m,&ret);
return ret;
}
2014-04-08 22:13:59 +08:00
const Matrix& Node::getParentToNodeTransform() const
{
if ( _inverseDirty ) {
2014-04-08 23:05:33 +08:00
_transform.invert(&_inverse);
_inverseDirty = false;
}
return _inverse;
}
AffineTransform Node::getNodeToWorldAffineTransform() const
{
AffineTransform t = this->getNodeToParentAffineTransform();
for (Node *p = _parent; p != nullptr; p = p->getParent())
t = AffineTransformConcat(t, p->getNodeToParentAffineTransform());
return t;
}
2010-07-21 17:40:10 +08:00
Matrix Node::getNodeToWorldTransform() const
{
Matrix t = this->getNodeToParentTransform();
for (Node *p = _parent; p != nullptr; p = p->getParent())
{
t = p->getNodeToParentTransform() * t;
}
return t;
}
2010-07-21 17:40:10 +08:00
AffineTransform Node::getWorldToNodeAffineTransform() const
{
return AffineTransformInvert(this->getNodeToWorldAffineTransform());
}
2014-04-08 23:05:33 +08:00
Matrix Node::getWorldToNodeTransform() const
{
2014-04-08 23:05:33 +08:00
Matrix result = getNodeToWorldTransform();
result.invert();
return result;
}
Vector2 Node::convertToNodeSpace(const Vector2& worldPoint) const
{
2014-04-10 23:12:29 +08:00
Matrix tmp = getWorldToNodeTransform();
Vector3 vec3(worldPoint.x, worldPoint.y, 0);
Vector3 ret;
tmp.transformPoint(vec3,&ret);
return Vector2(ret.x, ret.y);
}
Vector2 Node::convertToWorldSpace(const Vector2& nodePoint) const
{
2014-04-10 23:12:29 +08:00
Matrix tmp = getNodeToWorldTransform();
Vector3 vec3(nodePoint.x, nodePoint.y, 0);
Vector3 ret;
tmp.transformPoint(vec3,&ret);
return Vector2(ret.x, ret.y);
}
2014-04-15 14:29:12 +08:00
Vector2 Node::convertToNodeSpaceAR(const Vector2& worldPoint) const
{
2014-04-15 14:29:12 +08:00
Vector2 nodePoint = convertToNodeSpace(worldPoint);
return nodePoint - _anchorPointInPoints;
}
2014-04-15 14:29:12 +08:00
Vector2 Node::convertToWorldSpaceAR(const Vector2& nodePoint) const
{
2014-04-15 14:29:12 +08:00
Vector2 pt = nodePoint + _anchorPointInPoints;
return convertToWorldSpace(pt);
}
2014-04-15 14:41:09 +08:00
Vector2 Node::convertToWindowSpace(const Vector2& nodePoint) const
{
2014-04-15 14:41:09 +08:00
Vector2 worldPoint = this->convertToWorldSpace(nodePoint);
return Director::getInstance()->convertToUI(worldPoint);
}
// convenience methods which take a Touch instead of Vector2
2014-04-15 14:29:12 +08:00
Vector2 Node::convertTouchToNodeSpace(Touch *touch) const
{
Vector2 point = touch->getLocation();
return this->convertToNodeSpace(point);
2010-07-30 16:35:07 +08:00
}
2014-04-15 14:29:12 +08:00
Vector2 Node::convertTouchToNodeSpaceAR(Touch *touch) const
{
Vector2 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
for( const auto &child: _children)
child->updateTransform();
}
Component* Node::getComponent(const std::string& 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 std::string& 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
}
#if CC_USE_PHYSICS
void Node::updatePhysicsBodyPosition(Layer* layer)
{
if (_physicsBody != nullptr)
{
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
{
Point pos = getParent() == layer ? getPosition() : layer->convertToNodeSpace(_parent->convertToWorldSpace(getPosition()));
_physicsBody->setPosition(pos);
}
else
{
_physicsBody->setPosition(getPosition());
}
}
}
void Node::updatePhysicsBodyRotation(Layer* layer)
{
if (_physicsBody != nullptr)
{
if (layer != nullptr && layer->getPhysicsWorld() != nullptr)
{
float rotation = _rotationZ_X;
for (Node* parent = _parent; parent != layer; parent = parent->getParent())
{
rotation += parent->getRotation();
}
_physicsBody->setRotation(rotation);
}
else
{
_physicsBody->setRotation(_rotationZ_X);
}
}
}
void Node::setPhysicsBody(PhysicsBody* body)
{
if (body != nullptr)
{
body->_node = this;
body->retain();
// physics rotation based on body position, but node rotation based on node anthor point
// it cann't support both of them, so I clear the anthor point to default.
if (!getAnchorPoint().equals(Vector2::ANCHOR_MIDDLE))
{
CCLOG("Node warning: setPhysicsBody sets anchor point to Vector2::ANCHOR_MIDDLE.");
setAnchorPoint(Vector2::ANCHOR_MIDDLE);
}
}
if (_physicsBody != nullptr)
{
PhysicsWorld* world = _physicsBody->getWorld();
_physicsBody->removeFromWorld();
2013-11-01 16:26:03 +08:00
_physicsBody->_node = nullptr;
_physicsBody->release();
if (world != nullptr && body != nullptr)
{
world->addBody(body);
}
}
_physicsBody = body;
if (body != nullptr)
{
Node* node;
Layer* layer = nullptr;
for (node = this->getParent(); node != nullptr; node = node->getParent())
{
Layer* tmpLayer = dynamic_cast<Layer*>(node);
if (tmpLayer != nullptr && tmpLayer->getPhysicsWorld() != nullptr)
{
layer = tmpLayer;
break;
}
}
updatePhysicsBodyPosition(layer);
updatePhysicsBodyRotation(layer);
}
}
PhysicsBody* Node::getPhysicsBody() const
{
return _physicsBody;
}
#endif //CC_USE_PHYSICS
GLubyte Node::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 Node::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 Node::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
updateCascadeOpacity();
2013-02-27 09:38:30 +08:00
}
void Node::updateDisplayedOpacity(GLubyte parentOpacity)
2013-02-27 09:38:30 +08:00
{
2013-02-27 14:48:19 +08:00
_displayedOpacity = _realOpacity * parentOpacity/255.0;
updateColor();
2013-02-27 18:21:35 +08:00
if (_cascadeOpacityEnabled)
2013-02-27 14:48:19 +08:00
{
for(auto child : _children){
child->updateDisplayedOpacity(_displayedOpacity);
}
2013-02-27 09:38:30 +08:00
}
}
bool Node::isCascadeOpacityEnabled(void) const
2013-02-27 15:30:49 +08:00
{
return _cascadeOpacityEnabled;
}
void Node::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
2013-02-27 15:30:49 +08:00
{
if (_cascadeOpacityEnabled == cascadeOpacityEnabled)
{
return;
}
2013-02-27 15:30:49 +08:00
_cascadeOpacityEnabled = cascadeOpacityEnabled;
if (cascadeOpacityEnabled)
{
updateCascadeOpacity();
}
else
{
disableCascadeOpacity();
}
2013-02-27 15:30:49 +08:00
}
void Node::updateCascadeOpacity()
{
GLubyte parentOpacity = 255;
if (_parent != nullptr && _parent->isCascadeOpacityEnabled())
{
parentOpacity = _parent->getDisplayedOpacity();
}
updateDisplayedOpacity(parentOpacity);
}
void Node::disableCascadeOpacity()
{
_displayedOpacity = _realOpacity;
for(auto child : _children){
child->updateDisplayedOpacity(255);
}
}
const Color3B& Node::getColor(void) const
2013-02-27 09:38:30 +08:00
{
return _realColor;
}
const Color3B& Node::getDisplayedColor() const
2013-02-27 09:38:30 +08:00
{
return _displayedColor;
}
void Node::setColor(const Color3B& color)
2013-02-27 09:38:30 +08:00
{
_displayedColor = _realColor = color;
updateCascadeColor();
2013-02-27 09:38:30 +08:00
}
void Node::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;
updateColor();
2013-02-27 09:38:30 +08:00
2013-02-27 14:48:19 +08:00
if (_cascadeColorEnabled)
{
for(const auto &child : _children){
child->updateDisplayedColor(_displayedColor);
}
2013-02-27 09:38:30 +08:00
}
}
2013-01-17 11:20:25 +08:00
bool Node::isCascadeColorEnabled(void) const
2013-02-27 15:30:49 +08:00
{
return _cascadeColorEnabled;
}
void Node::setCascadeColorEnabled(bool cascadeColorEnabled)
2013-02-27 15:30:49 +08:00
{
if (_cascadeColorEnabled == cascadeColorEnabled)
{
return;
}
2013-02-27 15:30:49 +08:00
_cascadeColorEnabled = cascadeColorEnabled;
if (_cascadeColorEnabled)
{
updateCascadeColor();
}
else
{
disableCascadeColor();
}
}
void Node::updateCascadeColor()
{
Color3B parentColor = Color3B::WHITE;
if (_parent && _parent->isCascadeColorEnabled())
{
parentColor = _parent->getDisplayedColor();
}
updateDisplayedColor(parentColor);
}
void Node::disableCascadeColor()
{
for(auto child : _children){
child->updateDisplayedColor(Color3B::WHITE);
}
2013-02-27 15:30:49 +08:00
}
2013-12-16 03:42:54 +08:00
__NodeRGBA::__NodeRGBA()
{
CCLOG("NodeRGBA deprecated.");
}
2012-04-18 18:43:45 +08:00
NS_CC_END