2010-12-18 16:12:59 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2009 Valentin Milea
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-01 15:48:05 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
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
|
|
|
|
2010-12-18 16:12:59 +08:00
|
|
|
#include "CCNode.h"
|
2013-08-26 01:50:29 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCString.h"
|
|
|
|
#include "ccCArray.h"
|
|
|
|
#include "TransformUtils.h"
|
|
|
|
#include "CCGrid.h"
|
2010-12-18 16:12:59 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "CCScheduler.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCTouch.h"
|
|
|
|
#include "CCActionManager.h"
|
|
|
|
#include "CCScriptSupport.h"
|
|
|
|
#include "CCGLProgram.h"
|
|
|
|
#include "CCEventDispatcher.h"
|
|
|
|
#include "CCEvent.h"
|
|
|
|
#include "CCEventTouch.h"
|
2013-10-28 16:17:19 +08:00
|
|
|
#include "CCScene.h"
|
2013-08-27 09:04:51 +08:00
|
|
|
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCPhysicsBody.h"
|
2013-09-30 13:02:17 +08:00
|
|
|
#endif
|
|
|
|
|
2012-04-04 21:58:04 +08:00
|
|
|
// externals
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "kazmath/GL/matrix.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCComponent.h"
|
|
|
|
#include "CCComponentContainer.h"
|
2012-04-14 19:11:57 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
|
2012-03-23 10:26:36 +08:00
|
|
|
#if CC_NODE_RENDER_SUBPIXEL
|
2010-07-20 14:29:18 +08:00
|
|
|
#define RENDER_IN_SUBPIXEL
|
|
|
|
#else
|
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
|
|
|
|
2014-01-22 01:26:58 +08:00
|
|
|
bool nodeComparisonLess(Node* n1, Node* n2)
|
2013-09-01 06:18:28 +08:00
|
|
|
{
|
2014-01-22 01:26:58 +08:00
|
|
|
return( n1->getLocalZOrder() < n2->getLocalZOrder() ||
|
|
|
|
( n1->getLocalZOrder() == n2->getLocalZOrder() && n1->getOrderOfArrival() < n2->getOrderOfArrival() )
|
2013-09-01 06:18:28 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-04 21:58:04 +08:00
|
|
|
// XXX: Yes, nodes might have a sort problem once every 15 days if the game runs at 60 FPS and each frame sprites are reordered.
|
2012-03-14 14:55:17 +08:00
|
|
|
static int s_globalOrderOfArrival = 1;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::Node(void)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _rotationX(0.0f)
|
|
|
|
, _rotationY(0.0f)
|
|
|
|
, _scaleX(1.0f)
|
|
|
|
, _scaleY(1.0f)
|
|
|
|
, _vertexZ(0.0f)
|
2013-07-12 14:47:36 +08:00
|
|
|
, _position(Point::ZERO)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _skewX(0.0f)
|
|
|
|
, _skewY(0.0f)
|
2013-07-12 14:47:36 +08:00
|
|
|
, _anchorPointInPoints(Point::ZERO)
|
|
|
|
, _anchorPoint(Point::ZERO)
|
|
|
|
, _contentSize(Size::ZERO)
|
2014-01-14 04:52:07 +08:00
|
|
|
, _useAdditionalTransform(false)
|
2013-07-29 14:07:57 +08:00
|
|
|
, _transformDirty(true)
|
|
|
|
, _inverseDirty(true)
|
2011-06-10 17:51:37 +08:00
|
|
|
// children (lazy allocs)
|
|
|
|
// lazy alloc
|
2014-01-19 03:35:27 +08:00
|
|
|
, _localZOrder(0)
|
|
|
|
, _globalZOrder(0)
|
2013-12-18 17:47:20 +08:00
|
|
|
, _parent(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
// "whole screen" objects. like Scenes and Layers, should set _ignoreAnchorPointForPosition to true
|
2013-08-29 03:42:25 +08:00
|
|
|
, _tag(Node::INVALID_TAG)
|
2011-06-10 17:51:37 +08:00
|
|
|
// userData is always inited as nil
|
2013-12-18 17:47:20 +08:00
|
|
|
, _userData(nullptr)
|
|
|
|
, _userObject(nullptr)
|
|
|
|
, _shaderProgram(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _orderOfArrival(0)
|
|
|
|
, _running(false)
|
|
|
|
, _visible(true)
|
|
|
|
, _ignoreAnchorPointForPosition(false)
|
|
|
|
, _reorderChildDirty(false)
|
|
|
|
, _isTransitionFinished(false)
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-06-15 14:03:30 +08:00
|
|
|
, _updateScriptHandler(0)
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2013-12-18 17:47:20 +08:00
|
|
|
, _componentContainer(nullptr)
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-09-30 13:02:17 +08:00
|
|
|
, _physicsBody(nullptr)
|
|
|
|
#endif
|
2013-12-06 18:07:16 +08:00
|
|
|
, _displayedOpacity(255)
|
|
|
|
, _realOpacity(255)
|
|
|
|
, _displayedColor(Color3B::WHITE)
|
|
|
|
, _realColor(Color3B::WHITE)
|
|
|
|
, _cascadeColorEnabled(false)
|
|
|
|
, _cascadeOpacityEnabled(false)
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// set default scheduler and actionManager
|
2013-07-12 06:24:23 +08:00
|
|
|
Director *director = Director::getInstance();
|
2013-06-15 14:03:30 +08:00
|
|
|
_actionManager = director->getActionManager();
|
|
|
|
_actionManager->retain();
|
|
|
|
_scheduler = director->getScheduler();
|
|
|
|
_scheduler->retain();
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher = director->getEventDispatcher();
|
|
|
|
_eventDispatcher->retain();
|
|
|
|
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-12-18 17:47:20 +08:00
|
|
|
ScriptEngineProtocol* engine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
_scriptType = engine != nullptr ? engine->getScriptType() : kScriptTypeNone;
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
kmMat4Identity(&_transform);
|
|
|
|
kmMat4Identity(&_inverse);
|
|
|
|
kmMat4Identity(&_additionalTransform);
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
2012-03-14 18:11:25 +08:00
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
Node::~Node()
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2013-08-22 11:05:06 +08:00
|
|
|
CCLOGINFO( "deallocing Node: %p - tag: %i", this, _tag );
|
2012-09-21 00:21:24 +08:00
|
|
|
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_updateScriptHandler)
|
2012-12-10 13:48:27 +08:00
|
|
|
{
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler);
|
2012-12-10 13:48:27 +08:00
|
|
|
}
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_actionManager);
|
|
|
|
CC_SAFE_RELEASE(_scheduler);
|
2013-10-26 15:04:01 +08:00
|
|
|
|
|
|
|
_eventDispatcher->cleanTarget(this);
|
|
|
|
CC_SAFE_RELEASE(_eventDispatcher);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// attributes
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_shaderProgram);
|
|
|
|
CC_SAFE_RELEASE(_userObject);
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
for (auto& child : _children)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-19 09:45:21 +08:00
|
|
|
child->_parent = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
removeAllComponents();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_componentContainer);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-09-30 13:02:17 +08:00
|
|
|
CC_SAFE_RELEASE(_physicsBody);
|
|
|
|
#endif
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Node::init()
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
float Node::getSkewX() const
|
2011-07-01 15:48:05 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _skewX;
|
2011-07-01 15:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setSkewX(float newSkewX)
|
2011-07-01 15:48:05 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_skewX = newSkewX;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
2011-07-01 15:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
float Node::getSkewY() const
|
2011-07-01 15:48:05 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _skewY;
|
2011-07-01 15:48:05 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setSkewY(float newSkewY)
|
2011-07-01 15:48:05 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_skewY = newSkewY;
|
2011-07-01 15:48:05 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformDirty = _inverseDirty = true;
|
2011-07-01 15:48:05 +08:00
|
|
|
}
|
|
|
|
|
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
|
2014-01-19 03:35:27 +08:00
|
|
|
void Node::_setLocalZOrder(int z)
|
2010-07-14 10:36:26 +08:00
|
|
|
{
|
2014-01-19 03:35:27 +08:00
|
|
|
_localZOrder = z;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
void Node::setLocalZOrder(int z)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2014-01-19 03:35:27 +08:00
|
|
|
_localZOrder = z;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_parent)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_parent->reorderChild(this, z);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2014-01-19 03:35:27 +08:00
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->setDirtyForNode(this);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2014-01-22 02:10:50 +08:00
|
|
|
void Node::setGlobalZOrder(float globalZOrder)
|
2014-01-21 10:23:05 +08:00
|
|
|
{
|
2014-01-22 02:10:50 +08:00
|
|
|
if (_globalZOrder != globalZOrder)
|
2014-01-21 10:23:05 +08:00
|
|
|
{
|
2014-01-22 02:10:50 +08:00
|
|
|
_globalZOrder = globalZOrder;
|
2014-01-21 10:23:05 +08:00
|
|
|
_eventDispatcher->setDirtyForNode(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/// vertexZ getter
|
2013-07-04 08:22:15 +08:00
|
|
|
float Node::getVertexZ() const
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _vertexZ;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// vertexZ setter
|
2014-01-19 03:35:27 +08:00
|
|
|
void Node::setVertexZ(float zOrder)
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2014-01-19 03:35:27 +08:00
|
|
|
_vertexZ = zOrder;
|
|
|
|
setGlobalZOrder(zOrder);
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// rotation getter
|
2013-07-04 08:22:15 +08:00
|
|
|
float Node::getRotation() const
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(_rotationX == _rotationY, "CCNode#rotation. RotationX != RotationY. Don't know which one to return");
|
2013-06-15 14:03:30 +08:00
|
|
|
return _rotationX;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// rotation setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setRotation(float newRotation)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_rotationX = _rotationY = newRotation;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
2013-09-30 13:02:17 +08:00
|
|
|
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-10-18 15:34:13 +08:00
|
|
|
if (_physicsBody)
|
2013-09-30 13:02:17 +08:00
|
|
|
{
|
|
|
|
_physicsBody->setRotation(newRotation);
|
|
|
|
}
|
|
|
|
#endif
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
float Node::getRotationX() const
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _rotationX;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setRotationX(float fRotationX)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_rotationX = fRotationX;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
float Node::getRotationY() const
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _rotationY;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
void Node::setRotationY(float rotationY)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_rotationY = rotationY;
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformDirty = _inverseDirty = true;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// scale getter
|
2013-07-04 08:22:15 +08:00
|
|
|
float Node::getScale(void) const
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( _scaleX == _scaleY, "CCNode#scale. ScaleX != ScaleY. Don't know which one to return");
|
2013-06-15 14:03:30 +08:00
|
|
|
return _scaleX;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// scale setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setScale(float scale)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scaleX = _scaleY = scale;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
2010-12-18 16:12:59 +08:00
|
|
|
|
|
|
|
/// scaleX getter
|
2013-07-04 08:22:15 +08:00
|
|
|
float Node::getScaleX() const
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _scaleX;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2013-10-08 15:57:34 +08:00
|
|
|
/// scale setter
|
|
|
|
void Node::setScale(float scaleX,float scaleY)
|
|
|
|
{
|
|
|
|
_scaleX = scaleX;
|
|
|
|
_scaleY = scaleY;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
|
|
|
}
|
|
|
|
|
2010-12-18 16:12:59 +08:00
|
|
|
/// scaleX setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setScaleX(float newScaleX)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scaleX = newScaleX;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
2010-12-18 16:12:59 +08:00
|
|
|
|
|
|
|
/// scaleY getter
|
2013-07-04 08:22:15 +08:00
|
|
|
float Node::getScaleY() const
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _scaleY;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
|
|
|
/// scaleY setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setScaleY(float newScaleY)
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scaleY = newScaleY;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// position getter
|
2013-07-04 08:22:15 +08:00
|
|
|
const Point& Node::getPosition() const
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _position;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// position setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setPosition(const Point& newPosition)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_position = newPosition;
|
|
|
|
_transformDirty = _inverseDirty = true;
|
2013-09-30 13:02:17 +08:00
|
|
|
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-10-18 15:34:13 +08:00
|
|
|
if (_physicsBody)
|
2013-09-30 13:02:17 +08:00
|
|
|
{
|
|
|
|
_physicsBody->setPosition(newPosition);
|
|
|
|
}
|
|
|
|
#endif
|
2010-12-31 14:56:24 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
void Node::getPosition(float* x, float* y) const
|
2012-02-02 10:39:19 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
*x = _position.x;
|
|
|
|
*y = _position.y;
|
2012-02-02 10:39:19 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setPosition(float x, float y)
|
2012-02-02 10:39:19 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
setPosition(Point(x, y));
|
2012-02-02 10:39:19 +08:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
float Node::getPositionX() const
|
2013-01-21 16:26:02 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _position.x;
|
2013-01-21 16:26:02 +08:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
float Node::getPositionY() const
|
2013-01-21 16:26:02 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _position.y;
|
2013-01-21 16:26:02 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setPositionX(float x)
|
2013-01-21 16:26:02 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
setPosition(Point(x, _position.y));
|
2013-01-21 16:26:02 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setPositionY(float y)
|
2013-01-21 16:26:02 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
setPosition(Point(_position.x, y));
|
2013-01-21 16:26:02 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t Node::getChildrenCount() const
|
2012-02-02 10:39:19 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
return _children.size();
|
2012-02-02 10:39:19 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isVisible getter
|
2013-07-04 08:22:15 +08:00
|
|
|
bool Node::isVisible() const
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _visible;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isVisible setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setVisible(bool var)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_visible = var;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 08:22:15 +08:00
|
|
|
const Point& Node::getAnchorPointInPoints() const
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _anchorPointInPoints;
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
|
|
|
|
/// anchorPoint getter
|
2013-07-04 08:22:15 +08:00
|
|
|
const Point& Node::getAnchorPoint() const
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _anchorPoint;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setAnchorPoint(const Point& point)
|
2010-07-08 10:26:58 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( ! point.equals(_anchorPoint))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_anchorPoint = point;
|
2013-07-12 14:11:55 +08:00
|
|
|
_anchorPointInPoints = Point(_contentSize.width * _anchorPoint.x, _contentSize.height * _anchorPoint.y );
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformDirty = _inverseDirty = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// contentSize getter
|
2013-06-20 14:13:12 +08:00
|
|
|
const Size& Node::getContentSize() const
|
2010-07-12 17:56:06 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _contentSize;
|
2010-07-12 17:56:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setContentSize(const Size & size)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( ! size.equals(_contentSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_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 );
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformDirty = _inverseDirty = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2011-01-31 06:59:03 +08:00
|
|
|
// isRunning getter
|
2013-07-04 08:22:15 +08:00
|
|
|
bool Node::isRunning() const
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _running;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// parent setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setParent(Node * var)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_parent = var;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isRelativeAnchorPoint getter
|
2013-07-04 08:22:15 +08:00
|
|
|
bool Node::isIgnoreAnchorPointForPosition() const
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _ignoreAnchorPointForPosition;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
2010-07-12 17:56:06 +08:00
|
|
|
/// isRelativeAnchorPoint setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::ignoreAnchorPointForPosition(bool newValue)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (newValue != _ignoreAnchorPointForPosition)
|
2012-06-08 14:17:39 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +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
|
2013-06-20 14:13:12 +08:00
|
|
|
int Node::getTag() const
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _tag;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// tag setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setTag(int var)
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_tag = var;
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2010-07-12 17:56:06 +08:00
|
|
|
/// userData setter
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setUserData(void *var)
|
2010-07-07 15:24:44 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_userData = var;
|
2010-07-07 15:24:44 +08:00
|
|
|
}
|
2010-12-18 16:12:59 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
int Node::getOrderOfArrival() const
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _orderOfArrival;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void Node::setOrderOfArrival(int orderOfArrival)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(orderOfArrival >=0, "Invalid orderOfArrival");
|
|
|
|
_orderOfArrival = orderOfArrival;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void Node::setUserObject(Ref *pUserObject)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2012-11-26 22:11:04 +08:00
|
|
|
CC_SAFE_RETAIN(pUserObject);
|
2013-06-20 22:51:54 +08:00
|
|
|
CC_SAFE_RELEASE(_userObject);
|
2013-06-15 14:03:30 +08:00
|
|
|
_userObject = pUserObject;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setShaderProgram(GLProgram *pShaderProgram)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-20 22:51:54 +08:00
|
|
|
CC_SAFE_RETAIN(pShaderProgram);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_shaderProgram);
|
|
|
|
_shaderProgram = pShaderProgram;
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
2010-12-18 16:12:59 +08:00
|
|
|
|
2013-12-22 04:28:49 +08:00
|
|
|
Scene* Node::getScene()
|
|
|
|
{
|
|
|
|
if(!_parent)
|
|
|
|
return nullptr;
|
|
|
|
return _parent->getScene();
|
|
|
|
}
|
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
Rect Node::getBoundingBox() const
|
2010-07-20 14:29:18 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
Rect rect = Rect(0, 0, _contentSize.width, _contentSize.height);
|
2013-12-10 09:32:51 +08:00
|
|
|
return RectApplyAffineTransform(rect, getNodeToParentAffineTransform());
|
2010-07-20 14:29:18 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node * Node::create(void)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Node * ret = new Node();
|
|
|
|
if (ret && ret->init())
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::cleanup()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// actions
|
|
|
|
this->stopAllActions();
|
2012-10-23 01:48:22 +08:00
|
|
|
this->unscheduleAllSelectors();
|
|
|
|
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-07-22 17:12:53 +08:00
|
|
|
if ( _scriptType != kScriptTypeNone)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
|
|
|
int action = kNodeOnCleanup;
|
2013-07-22 17:12:53 +08:00
|
|
|
BasicScriptData data(this,(void*)&action);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif // #if CC_ENABLE_SCRIPT_BINDING
|
2012-10-23 01:48:22 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// timers
|
2013-12-19 08:56:59 +08:00
|
|
|
for( const auto &child: _children)
|
2013-11-28 16:02:03 +08:00
|
|
|
child->cleanup();
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2010-07-15 11:39:05 +08:00
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
std::string Node::getDescription() const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-12 09:47:35 +08:00
|
|
|
return StringUtils::format("<Node | Tag = %d", _tag);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-11-28 17:57:13 +08:00
|
|
|
// lazy allocs
|
|
|
|
void Node::childrenAlloc(void)
|
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
_children.reserve(4);
|
2013-11-28 17:57:13 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
Node* Node::getChildByTag(int tag)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-11-28 16:02:03 +08:00
|
|
|
for (auto& child : _children)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if(child && child->_tag == tag)
|
2013-11-28 16:02:03 +08:00
|
|
|
return child;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-11-28 16:02:03 +08:00
|
|
|
return nullptr;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* "add" logic MUST only be on this method
|
2012-09-17 15:02:24 +08:00
|
|
|
* If a class want's to extend the 'addChild' behavior it only needs
|
2010-07-09 18:24:08 +08:00
|
|
|
* to override this method
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::addChild(Node *child, int zOrder, int tag)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT( child != nullptr, "Argument must be non-nil");
|
|
|
|
CCASSERT( child->_parent == nullptr, "child already added. It can't be added again");
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-11-28 18:23:06 +08:00
|
|
|
if (_children.empty())
|
2013-11-28 17:57:13 +08:00
|
|
|
{
|
|
|
|
this->childrenAlloc();
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->insertChild(child, zOrder);
|
2013-10-28 16:17:19 +08:00
|
|
|
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-10-28 16:17:19 +08:00
|
|
|
for (Node* node = this->getParent(); node != nullptr; node = node->getParent())
|
|
|
|
{
|
|
|
|
if (dynamic_cast<Scene*>(node) != nullptr)
|
|
|
|
{
|
|
|
|
(dynamic_cast<Scene*>(node))->addChildToPhysicsWorld(child);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
child->_tag = tag;
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
child->setParent(this);
|
|
|
|
child->setOrderOfArrival(s_globalOrderOfArrival++);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _running )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
child->onEnter();
|
2013-05-27 20:46:15 +08:00
|
|
|
// prevent onEnterTransitionDidFinish to be called twice when a node is added in onEnter
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_isTransitionFinished) {
|
2013-05-27 20:46:15 +08:00
|
|
|
child->onEnterTransitionDidFinish();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-06 18:07:16 +08:00
|
|
|
|
|
|
|
if (_cascadeColorEnabled)
|
|
|
|
{
|
|
|
|
updateCascadeColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_cascadeOpacityEnabled)
|
|
|
|
{
|
|
|
|
updateCascadeOpacity();
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::addChild(Node *child, int zOrder)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT( child != nullptr, "Argument must be non-nil");
|
2013-06-15 14:03:30 +08:00
|
|
|
this->addChild(child, zOrder, child->_tag);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::addChild(Node *child)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT( child != nullptr, "Argument must be non-nil");
|
2014-01-19 03:35:27 +08:00
|
|
|
this->addChild(child, child->_localZOrder, child->_tag);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::removeFromParent()
|
2012-11-09 12:08:18 +08:00
|
|
|
{
|
|
|
|
this->removeFromParentAndCleanup(true);
|
2012-11-05 18:41:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::removeFromParentAndCleanup(bool cleanup)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if (_parent != nullptr)
|
2012-05-02 12:04:27 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_parent->removeChild(this,cleanup);
|
2012-05-02 12:04:27 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* "remove" logic MUST only be on this method
|
|
|
|
* If a class want's to extend the 'removeChild' behavior it only needs
|
|
|
|
* to override this method
|
|
|
|
*/
|
2013-07-19 14:57:54 +08:00
|
|
|
void Node::removeChild(Node* child, bool cleanup /* = true */)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// explicit nil handling
|
2013-11-28 18:23:06 +08:00
|
|
|
if (_children.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t index = _children.getIndex(child);
|
2013-08-27 09:04:51 +08:00
|
|
|
if( index != CC_INVALID_INDEX )
|
|
|
|
this->detachChild( child, index, cleanup );
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 14:57:54 +08:00
|
|
|
void Node::removeChildByTag(int tag, bool cleanup/* = true */)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-08-29 03:42:25 +08:00
|
|
|
CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node *child = this->getChildByTag(tag);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (child == nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-04-28 03:15:00 +08:00
|
|
|
CCLOG("cocos2d: removeChildByTag(tag = %d): child not found!", tag);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->removeChild(child, cleanup);
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::removeAllChildren()
|
2012-11-05 18:41:52 +08:00
|
|
|
{
|
|
|
|
this->removeAllChildrenWithCleanup(true);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::removeAllChildrenWithCleanup(bool cleanup)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// not using detachChild improves speed here
|
2013-12-19 08:56:59 +08:00
|
|
|
for (auto& child : _children)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-19 08:56:59 +08:00
|
|
|
// IMPORTANT:
|
|
|
|
// -1st do onExit
|
|
|
|
// -2nd cleanup
|
|
|
|
if(_running)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-19 08:56:59 +08:00
|
|
|
child->onExitTransitionDidStart();
|
|
|
|
child->onExit();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-19 08:56:59 +08:00
|
|
|
|
|
|
|
if (cleanup)
|
|
|
|
{
|
|
|
|
child->cleanup();
|
|
|
|
}
|
|
|
|
// set parent nil at the end
|
|
|
|
child->setParent(nullptr);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 08:56:59 +08:00
|
|
|
_children.clear();
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
void Node::detachChild(Node *child, ssize_t childIndex, bool doCleanup)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// IMPORTANT:
|
|
|
|
// -1st do onExit
|
|
|
|
// -2nd cleanup
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_running)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
child->onExitTransitionDidStart();
|
|
|
|
child->onExit();
|
|
|
|
}
|
2013-10-28 11:08:41 +08:00
|
|
|
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-10-28 11:08:41 +08:00
|
|
|
if (child->_physicsBody != nullptr)
|
|
|
|
{
|
|
|
|
child->_physicsBody->removeFromWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// If you don't do cleanup, the child's actions will not get removed and the
|
|
|
|
// its scheduledSelectors_ dict will not get released!
|
|
|
|
if (doCleanup)
|
|
|
|
{
|
|
|
|
child->cleanup();
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// set parent nil at the end
|
2013-11-28 16:02:03 +08:00
|
|
|
child->setParent(nullptr);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-12-11 17:53:45 +08:00
|
|
|
_children.erase(childIndex);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// helper used by reorderChild & add
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::insertChild(Node* child, int z)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_reorderChildDirty = true;
|
2013-12-05 10:35:10 +08:00
|
|
|
_children.pushBack(child);
|
2014-01-19 03:35:27 +08:00
|
|
|
child->_setLocalZOrder(z);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::reorderChild(Node *child, int zOrder)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT( child != nullptr, "Child must be non-nil");
|
2013-06-15 14:03:30 +08:00
|
|
|
_reorderChildDirty = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
child->setOrderOfArrival(s_globalOrderOfArrival++);
|
2014-01-19 03:35:27 +08:00
|
|
|
child->_setLocalZOrder(zOrder);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::sortAllChildren()
|
2012-04-04 21:58:04 +08:00
|
|
|
{
|
2013-08-25 01:56:07 +08:00
|
|
|
if( _reorderChildDirty ) {
|
2013-11-28 16:02:03 +08:00
|
|
|
std::sort( std::begin(_children), std::end(_children), nodeComparisonLess );
|
2013-08-25 01:56:07 +08:00
|
|
|
_reorderChildDirty = false;
|
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::draw()
|
2010-12-18 16:12:59 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
//CCASSERT(0);
|
2012-04-19 14:35:52 +08:00
|
|
|
// override me
|
2012-09-15 06:26:38 +08:00
|
|
|
// Only use- this function to draw your stuff.
|
2012-04-19 14:35:52 +08:00
|
|
|
// DON'T draw your stuff outside this method
|
2010-12-18 16:12:59 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::visit()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// quick return if not visible. children won't be drawn.
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_visible)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-09-30 13:02:17 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLPushMatrix();
|
2010-08-17 14:25:41 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->transform();
|
2013-12-05 17:19:01 +08:00
|
|
|
int i = 0;
|
2010-08-04 10:36:37 +08:00
|
|
|
|
2013-11-28 18:23:06 +08:00
|
|
|
if(!_children.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
sortAllChildren();
|
|
|
|
// draw children zOrder < 0
|
2013-12-05 10:35:10 +08:00
|
|
|
for( ; i < _children.size(); i++ )
|
2011-04-21 14:46:15 +08:00
|
|
|
{
|
2013-12-05 10:35:10 +08:00
|
|
|
auto node = _children.at(i);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2014-01-19 03:35:27 +08:00
|
|
|
if ( node && node->_localZOrder < 0 )
|
2013-08-20 08:27:31 +08:00
|
|
|
node->visit();
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// self draw
|
|
|
|
this->draw();
|
|
|
|
|
2013-12-19 10:33:04 +08:00
|
|
|
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
|
2013-12-19 08:56:59 +08:00
|
|
|
(*it)->visit();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset for next frame
|
2013-06-15 14:03:30 +08:00
|
|
|
_orderOfArrival = 0;
|
2010-08-17 14:25:41 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLPopMatrix();
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::transformAncestors()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if( _parent != nullptr )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_parent->transformAncestors();
|
|
|
|
_parent->transform();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::transform()
|
2013-10-18 15:34:13 +08:00
|
|
|
{
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-10-18 15:34:13 +08:00
|
|
|
updatePhysicsTransform();
|
|
|
|
#endif
|
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
kmMat4 transfrom4x4 = this->getNodeToParentTransform();
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLMultMatrix( &transfrom4x4 );
|
2014-01-04 09:54:07 +08:00
|
|
|
|
2013-12-07 09:42:16 +08:00
|
|
|
// saves the MV matrix
|
|
|
|
kmGLGetMatrix(KM_GL_MODELVIEW, &_modelViewTransform);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::onEnter()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isTransitionFinished = false;
|
2013-05-27 20:46:15 +08:00
|
|
|
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-07-22 17:12:53 +08:00
|
|
|
if (_scriptType != kScriptTypeNone)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
|
|
|
int action = kNodeOnEnter;
|
2013-07-22 17:12:53 +08:00
|
|
|
BasicScriptData data(this,(void*)&action);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2013-12-30 11:10:02 +08:00
|
|
|
|
|
|
|
for( const auto &child: _children)
|
|
|
|
child->onEnter();
|
|
|
|
|
|
|
|
this->resume();
|
|
|
|
|
|
|
|
_running = true;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::onEnterTransitionDidFinish()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isTransitionFinished = true;
|
2013-05-27 20:46:15 +08:00
|
|
|
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-07-22 17:12:53 +08:00
|
|
|
if (_scriptType != kScriptTypeNone)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
|
|
|
int action = kNodeOnEnterTransitionDidFinish;
|
2013-07-22 17:12:53 +08:00
|
|
|
BasicScriptData data(this,(void*)&action);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2013-12-30 11:10:02 +08:00
|
|
|
|
|
|
|
for( const auto &child: _children)
|
|
|
|
child->onEnterTransitionDidFinish();
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::onExitTransitionDidStart()
|
2012-04-04 21:58:04 +08:00
|
|
|
{
|
2013-12-19 08:56:59 +08:00
|
|
|
for( const auto &child: _children)
|
2013-11-28 16:02:03 +08:00
|
|
|
child->onExitTransitionDidStart();
|
2014-02-20 16:40:46 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-07-22 17:12:53 +08:00
|
|
|
if (_scriptType != kScriptTypeNone)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
|
|
|
int action = kNodeOnExitTransitionDidStart;
|
2013-07-22 17:12:53 +08:00
|
|
|
BasicScriptData data(this,(void*)&action);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
|
2012-08-31 17:55:45 +08:00
|
|
|
}
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::onExit()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
this->pause();
|
2010-07-14 10:36:26 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_running = false;
|
2013-12-30 11:10:02 +08:00
|
|
|
|
|
|
|
for( const auto &child: _children)
|
|
|
|
child->onExit();
|
|
|
|
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-07-22 17:12:53 +08:00
|
|
|
if (_scriptType != kScriptTypeNone)
|
2013-07-02 15:23:51 +08:00
|
|
|
{
|
|
|
|
int action = kNodeOnExit;
|
2013-07-22 17:12:53 +08:00
|
|
|
BasicScriptData data(this,(void*)&action);
|
2013-07-04 15:44:42 +08:00
|
|
|
ScriptEvent scriptEvent(kNodeEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
void Node::setEventDispatcher(EventDispatcher* dispatcher)
|
|
|
|
{
|
|
|
|
if (dispatcher != _eventDispatcher)
|
|
|
|
{
|
|
|
|
_eventDispatcher->cleanTarget(this);
|
|
|
|
CC_SAFE_RETAIN(dispatcher);
|
|
|
|
CC_SAFE_RELEASE(_eventDispatcher);
|
|
|
|
_eventDispatcher = dispatcher;
|
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
2012-02-02 15:58:10 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setActionManager(ActionManager* actionManager)
|
2012-04-04 21:58:04 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( actionManager != _actionManager ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
this->stopAllActions();
|
|
|
|
CC_SAFE_RETAIN(actionManager);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_actionManager);
|
|
|
|
_actionManager = actionManager;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Action * Node::runAction(Action* action)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
CCASSERT( action != nullptr, "Argument must be non-nil");
|
2013-06-15 14:03:30 +08:00
|
|
|
_actionManager->addAction(action, this, !_running);
|
2012-04-19 14:35:52 +08:00
|
|
|
return action;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::stopAllActions()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_actionManager->removeAllActionsFromTarget(this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::stopAction(Action* action)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_actionManager->removeAction(action);
|
2010-08-06 09:46:56 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::stopActionByTag(int tag)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-08-29 03:42:25 +08:00
|
|
|
CCASSERT( tag != Action::INVALID_TAG, "Invalid tag");
|
2013-06-15 14:03:30 +08:00
|
|
|
_actionManager->removeActionByTag(tag, this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
Action * Node::getActionByTag(int tag)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-08-29 03:42:25 +08:00
|
|
|
CCASSERT( tag != Action::INVALID_TAG, "Invalid tag");
|
2013-06-15 14:03:30 +08:00
|
|
|
return _actionManager->getActionByTag(tag, this);
|
2010-08-06 09:46:56 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t Node::getNumberOfRunningActions() const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-07-18 07:56:19 +08:00
|
|
|
return _actionManager->getNumberOfRunningActionsInTarget(this);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// Node - Callbacks
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setScheduler(Scheduler* scheduler)
|
2012-04-04 21:58:04 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( scheduler != _scheduler ) {
|
2012-04-19 14:35:52 +08:00
|
|
|
this->unscheduleAllSelectors();
|
|
|
|
CC_SAFE_RETAIN(scheduler);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_scheduler);
|
|
|
|
_scheduler = scheduler;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-27 12:05:47 +08:00
|
|
|
bool Node::isScheduled(SEL_SCHEDULE selector)
|
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
return _scheduler->isScheduled(this, schedule_selector_to_key(selector));
|
2013-06-27 12:05:47 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::scheduleUpdate()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
scheduleUpdateWithPriority(0);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::scheduleUpdateWithPriority(int priority)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
_scheduler->scheduleUpdate([this](float dt){
|
|
|
|
this->update(dt);
|
|
|
|
}, this, priority, !_running);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::scheduleUpdateWithPriorityLua(int nHandler, int priority)
|
2013-01-17 11:20:25 +08:00
|
|
|
{
|
|
|
|
unscheduleUpdate();
|
2014-02-20 16:40:46 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-06-15 14:03:30 +08:00
|
|
|
_updateScriptHandler = nHandler;
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2014-02-20 10:53:49 +08:00
|
|
|
_scheduler->scheduleUpdate([this](float dt){
|
|
|
|
this->update(dt);
|
|
|
|
}, this, priority, !_running);
|
2013-01-17 11:20:25 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::unscheduleUpdate()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
_scheduler->unscheduleUpdate(this);
|
2014-02-20 16:40:46 +08:00
|
|
|
|
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_updateScriptHandler)
|
2012-12-10 13:48:27 +08:00
|
|
|
{
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_updateScriptHandler);
|
2013-06-15 14:03:30 +08:00
|
|
|
_updateScriptHandler = 0;
|
2012-12-10 13:48:27 +08:00
|
|
|
}
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::schedule(SEL_SCHEDULE selector)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
this->schedule(selector, 0.0f, kRepeatForever, 0.0f);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::schedule(SEL_SCHEDULE selector, float interval)
|
2012-04-04 21:58:04 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
this->schedule(selector, interval, kRepeatForever, 0.0f);
|
2010-07-14 11:52:27 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( selector, "Argument must be non-nil");
|
|
|
|
CCASSERT( interval >=0, "Argument must be positive");
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
_scheduler->schedule([=](float dt){
|
|
|
|
(this->*selector)(dt);
|
|
|
|
}, this, schedule_selector_to_key(selector), interval , repeat, delay, !_running);
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::scheduleOnce(SEL_SCHEDULE selector, float delay)
|
2012-04-04 21:58:04 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->schedule(selector, 0.0f, 0, delay);
|
2010-07-14 11:52:27 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::unschedule(SEL_SCHEDULE selector)
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
// explicit null handling
|
|
|
|
if (selector == nullptr)
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
2014-02-20 10:53:49 +08:00
|
|
|
|
|
|
|
_scheduler->unschedule(this, schedule_selector_to_key(selector));
|
2010-07-14 11:52:27 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::unscheduleAllSelectors()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scheduler->unscheduleAllForTarget(this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
void Node::resume()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scheduler->resumeTarget(this);
|
|
|
|
_actionManager->resumeTarget(this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->resumeTarget(this);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
void Node::pause()
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_scheduler->pauseTarget(this);
|
|
|
|
_actionManager->pauseTarget(this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->pauseTarget(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node::resumeSchedulerAndActions()
|
|
|
|
{
|
|
|
|
resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node::pauseSchedulerAndActions()
|
|
|
|
{
|
|
|
|
pause();
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
// override me
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::update(float fDelta)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-07-02 15:23:51 +08:00
|
|
|
if (0 != _updateScriptHandler)
|
2012-12-10 13:48:27 +08:00
|
|
|
{
|
2013-07-02 15:23:51 +08:00
|
|
|
//only lua use
|
2013-07-04 15:44:42 +08:00
|
|
|
SchedulerScriptData data(_updateScriptHandler,fDelta);
|
2013-07-02 15:23:51 +08:00
|
|
|
ScriptEvent event(kScheduleEvent,&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2012-12-10 13:48:27 +08:00
|
|
|
}
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_componentContainer && !_componentContainer->isEmpty())
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_componentContainer->visit(fDelta);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
AffineTransform Node::getNodeToParentAffineTransform() const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-10 09:32:51 +08:00
|
|
|
AffineTransform ret;
|
|
|
|
kmMat4 ret4 = getNodeToParentTransform();
|
|
|
|
GLToCGAffine(ret4.mat, &ret);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const kmMat4& Node::getNodeToParentTransform() const
|
|
|
|
{
|
|
|
|
if (_transformDirty)
|
2012-06-08 14:17:39 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// Translate values
|
2013-06-15 14:03:30 +08:00
|
|
|
float x = _position.x;
|
|
|
|
float y = _position.y;
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
if (_ignoreAnchorPointForPosition)
|
2012-06-08 14:17:39 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
x += _anchorPointInPoints.x;
|
|
|
|
y += _anchorPointInPoints.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +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;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_rotationX || _rotationY)
|
2012-06-08 14:17:39 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +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-19 14:35:52 +08:00
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
bool needsSkewMatrix = ( _skewX || _skewY );
|
2012-04-04 21:58:04 +08:00
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// optimization:
|
|
|
|
// inline anchor point calculation if skew is not needed
|
2012-11-14 18:05:15 +08:00
|
|
|
// Adjusted transform calculation for rotational skew
|
2013-07-12 14:47:36 +08:00
|
|
|
if (! needsSkewMatrix && !_anchorPointInPoints.equals(Point::ZERO))
|
2012-06-08 14:17:39 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
x += cy * -_anchorPointInPoints.x * _scaleX + -sx * -_anchorPointInPoints.y * _scaleY;
|
|
|
|
y += sy * -_anchorPointInPoints.x * _scaleX + cx * -_anchorPointInPoints.y * _scaleY;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-04 21:58:04 +08:00
|
|
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Build Transform Matrix
|
2012-11-14 18:05:15 +08:00
|
|
|
// Adjusted transform calculation for rotational skew
|
2013-12-19 20:32:22 +08:00
|
|
|
kmScalar mat[] = { cy * _scaleX, sy * _scaleX, 0, 0,
|
2013-12-19 08:56:59 +08:00
|
|
|
-sx * _scaleY, cx * _scaleY, 0, 0,
|
2013-12-10 09:32:51 +08:00
|
|
|
0, 0, 1, 0,
|
|
|
|
x, y, 0, 1 };
|
2013-12-19 20:32:22 +08:00
|
|
|
|
|
|
|
kmMat4Fill(&_transform, mat);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// XXX: Try to inline skew
|
|
|
|
// If skew is needed, apply skew and then anchor point
|
2013-12-10 09:32:51 +08:00
|
|
|
if (needsSkewMatrix)
|
2012-06-08 14:17:39 +08:00
|
|
|
{
|
2013-12-27 15:06:16 +08:00
|
|
|
kmMat4 skewMatrix = { 1, (float)tanf(CC_DEGREES_TO_RADIANS(_skewY)), 0, 0,
|
|
|
|
(float)tanf(CC_DEGREES_TO_RADIANS(_skewX)), 1, 0, 0,
|
2013-12-19 08:56:59 +08:00
|
|
|
0, 0, 1, 0,
|
|
|
|
0, 0, 0, 1};
|
2013-12-10 09:32:51 +08:00
|
|
|
|
2013-12-19 13:11:06 +08:00
|
|
|
kmMat4Multiply(&_transform, &_transform, &skewMatrix);
|
2012-04-04 21:58:04 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// adjust anchor point
|
2013-07-12 14:47:36 +08:00
|
|
|
if (!_anchorPointInPoints.equals(Point::ZERO))
|
2012-06-08 14:17:39 +08:00
|
|
|
{
|
2013-12-19 10:00:55 +08:00
|
|
|
// XXX: Argh, kmMat needs a "translate" method.
|
|
|
|
// XXX: Although this is faster than multiplying a vec4 * mat4
|
|
|
|
_transform.mat[12] += _transform.mat[0] * -_anchorPointInPoints.x + _transform.mat[4] * -_anchorPointInPoints.y;
|
|
|
|
_transform.mat[13] += _transform.mat[1] * -_anchorPointInPoints.x + _transform.mat[5] * -_anchorPointInPoints.y;
|
2012-06-08 14:17:39 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-10 09:32:51 +08:00
|
|
|
|
2014-01-04 09:54:07 +08:00
|
|
|
// vertex Z
|
|
|
|
_transform.mat[14] = _vertexZ;
|
|
|
|
|
2014-01-14 04:52:07 +08:00
|
|
|
if (_useAdditionalTransform)
|
2013-02-22 16:01:49 +08:00
|
|
|
{
|
2013-12-10 09:32:51 +08:00
|
|
|
kmMat4Multiply(&_transform, &_transform, &_additionalTransform);
|
2013-02-22 16:01:49 +08:00
|
|
|
}
|
2014-01-04 09:54:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformDirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2014-01-04 09:54:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
return _transform;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
2013-02-22 16:01:49 +08:00
|
|
|
|
2013-12-21 08:33:31 +08:00
|
|
|
void Node::setNodeToParentTransform(const kmMat4& transform)
|
|
|
|
{
|
|
|
|
_transform = transform;
|
|
|
|
_transformDirty = false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::setAdditionalTransform(const AffineTransform& additionalTransform)
|
2013-12-10 09:32:51 +08:00
|
|
|
{
|
|
|
|
CGAffineToGL(additionalTransform, _additionalTransform.mat);
|
|
|
|
_transformDirty = true;
|
2014-01-14 04:52:07 +08:00
|
|
|
_useAdditionalTransform = true;
|
2013-12-10 09:32:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Node::setAdditionalTransform(const kmMat4& additionalTransform)
|
2013-02-20 22:33:28 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_additionalTransform = additionalTransform;
|
|
|
|
_transformDirty = true;
|
2014-01-14 04:52:07 +08:00
|
|
|
_useAdditionalTransform = true;
|
2013-02-20 22:33:28 +08:00
|
|
|
}
|
2013-02-22 16:01:49 +08:00
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
|
|
|
|
AffineTransform Node::getParentToNodeAffineTransform() const
|
|
|
|
{
|
|
|
|
AffineTransform ret;
|
|
|
|
kmMat4 ret4 = getParentToNodeTransform();
|
|
|
|
|
|
|
|
GLToCGAffine(ret4.mat,&ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const kmMat4& Node::getParentToNodeTransform() const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if ( _inverseDirty ) {
|
2013-12-10 09:32:51 +08:00
|
|
|
kmMat4Inverse(&_inverse, &_transform);
|
2013-06-15 14:03:30 +08:00
|
|
|
_inverseDirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
return _inverse;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
|
|
|
|
AffineTransform Node::getNodeToWorldAffineTransform() const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-10 09:32:51 +08:00
|
|
|
AffineTransform t = this->getNodeToParentAffineTransform();
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
for (Node *p = _parent; p != nullptr; p = p->getParent())
|
2013-12-10 09:32:51 +08:00
|
|
|
t = AffineTransformConcat(t, p->getNodeToParentAffineTransform());
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return t;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
2010-07-21 17:40:10 +08:00
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
kmMat4 Node::getNodeToWorldTransform() const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-10 09:32:51 +08:00
|
|
|
kmMat4 t = this->getNodeToParentTransform();
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
for (Node *p = _parent; p != nullptr; p = p->getParent())
|
2014-01-04 06:21:33 +08:00
|
|
|
kmMat4Multiply(&t, &p->getNodeToParentTransform(), &t);
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return t;
|
2010-07-19 16:45:53 +08:00
|
|
|
}
|
2010-07-21 17:40:10 +08:00
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
AffineTransform Node::getWorldToNodeAffineTransform() const
|
|
|
|
{
|
|
|
|
return AffineTransformInvert(this->getNodeToWorldAffineTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
kmMat4 Node::getWorldToNodeTransform() const
|
2013-07-17 09:16:04 +08:00
|
|
|
{
|
2013-12-10 09:32:51 +08:00
|
|
|
kmMat4 tmp, tmp2;
|
|
|
|
|
|
|
|
tmp2 = this->getNodeToWorldTransform();
|
|
|
|
kmMat4Inverse(&tmp, &tmp2);
|
|
|
|
return tmp;
|
2013-07-17 09:16:04 +08:00
|
|
|
}
|
|
|
|
|
2013-12-10 09:32:51 +08:00
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
Point Node::convertToNodeSpace(const Point& worldPoint) const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-10 09:32:51 +08:00
|
|
|
kmMat4 tmp = getWorldToNodeTransform();
|
|
|
|
kmVec3 vec3 = {worldPoint.x, worldPoint.y, 0};
|
|
|
|
kmVec3 ret;
|
|
|
|
kmVec3Transform(&ret, &vec3, &tmp);
|
2013-12-18 14:36:49 +08:00
|
|
|
return Point(ret.x, ret.y);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
Point Node::convertToWorldSpace(const Point& nodePoint) const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-12-10 09:32:51 +08:00
|
|
|
kmMat4 tmp = getNodeToWorldTransform();
|
|
|
|
kmVec3 vec3 = {nodePoint.x, nodePoint.y, 0};
|
|
|
|
kmVec3 ret;
|
|
|
|
kmVec3Transform(&ret, &vec3, &tmp);
|
2013-12-18 14:36:49 +08:00
|
|
|
return Point(ret.x, ret.y);
|
2013-12-10 09:32:51 +08:00
|
|
|
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
Point Node::convertToNodeSpaceAR(const Point& worldPoint) const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Point nodePoint = convertToNodeSpace(worldPoint);
|
2013-07-11 16:38:58 +08:00
|
|
|
return nodePoint - _anchorPointInPoints;
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
Point Node::convertToWorldSpaceAR(const Point& nodePoint) const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-07-11 16:38:58 +08:00
|
|
|
Point pt = nodePoint + _anchorPointInPoints;
|
2012-04-19 14:35:52 +08:00
|
|
|
return convertToWorldSpace(pt);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2013-07-17 09:16:04 +08:00
|
|
|
Point Node::convertToWindowSpace(const Point& nodePoint) const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Point worldPoint = this->convertToWorldSpace(nodePoint);
|
2013-07-12 06:24:23 +08:00
|
|
|
return Director::getInstance()->convertToUI(worldPoint);
|
2010-07-20 13:49:13 +08:00
|
|
|
}
|
2010-07-09 18:24:08 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// convenience methods which take a Touch instead of Point
|
2013-07-17 09:16:04 +08:00
|
|
|
Point Node::convertTouchToNodeSpace(Touch *touch) const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Point point = touch->getLocation();
|
2012-04-19 14:35:52 +08:00
|
|
|
return this->convertToNodeSpace(point);
|
2010-07-30 16:35:07 +08:00
|
|
|
}
|
2013-07-17 09:16:04 +08:00
|
|
|
Point Node::convertTouchToNodeSpaceAR(Touch *touch) const
|
2010-07-09 18:24:08 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Point point = touch->getLocation();
|
2012-04-19 14:35:52 +08:00
|
|
|
return this->convertToNodeSpaceAR(point);
|
2010-07-09 18:24:08 +08:00
|
|
|
}
|
2010-07-30 16:35:07 +08:00
|
|
|
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-12-03 16:28:18 +08:00
|
|
|
bool Node::updatePhysicsTransform()
|
2013-10-18 15:34:13 +08:00
|
|
|
{
|
2013-12-03 16:28:18 +08:00
|
|
|
if (_physicsBody != nullptr && _physicsBody->getWorld() != nullptr && !_physicsBody->isResting())
|
2013-10-10 18:31:43 +08:00
|
|
|
{
|
2013-10-18 15:34:13 +08:00
|
|
|
_position = _physicsBody->getPosition();
|
|
|
|
_rotationX = _rotationY = _physicsBody->getRotation();
|
|
|
|
_transformDirty = _inverseDirty = true;
|
2013-12-03 16:28:18 +08:00
|
|
|
return true;
|
2013-10-10 18:31:43 +08:00
|
|
|
}
|
2013-12-03 16:28:18 +08:00
|
|
|
|
|
|
|
return false;
|
2013-10-18 15:34:13 +08:00
|
|
|
}
|
2013-10-10 18:31:43 +08:00
|
|
|
#endif
|
2013-10-18 15:34:13 +08:00
|
|
|
|
|
|
|
void Node::updateTransform()
|
|
|
|
{
|
2012-11-09 12:08:18 +08:00
|
|
|
// Recursively iterate over children
|
2013-12-19 08:56:59 +08:00
|
|
|
for( const auto &child: _children)
|
2013-11-28 16:02:03 +08:00
|
|
|
child->updateTransform();
|
2012-10-30 10:24:28 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
Component* Node::getComponent(const std::string& pName)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-09-07 07:00:24 +08:00
|
|
|
if( _componentContainer )
|
|
|
|
return _componentContainer->get(pName);
|
|
|
|
return nullptr;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Node::addComponent(Component *pComponent)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-09-07 07:00:24 +08:00
|
|
|
// lazy alloc
|
|
|
|
if( !_componentContainer )
|
|
|
|
_componentContainer = new ComponentContainer(this);
|
2013-06-15 14:03:30 +08:00
|
|
|
return _componentContainer->add(pComponent);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
bool Node::removeComponent(const std::string& pName)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-09-07 07:00:24 +08:00
|
|
|
if( _componentContainer )
|
|
|
|
return _componentContainer->remove(pName);
|
|
|
|
return false;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Node::removeAllComponents()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-09-07 07:00:24 +08:00
|
|
|
if( _componentContainer )
|
|
|
|
_componentContainer->removeAll();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-12-26 23:55:05 +08:00
|
|
|
#if CC_USE_PHYSICS
|
2013-09-30 13:02:17 +08:00
|
|
|
void Node::setPhysicsBody(PhysicsBody* body)
|
|
|
|
{
|
|
|
|
if (_physicsBody != nullptr)
|
|
|
|
{
|
2013-11-01 16:26:03 +08:00
|
|
|
_physicsBody->_node = nullptr;
|
2013-09-30 13:02:17 +08:00
|
|
|
_physicsBody->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
_physicsBody = body;
|
2013-11-01 16:26:03 +08:00
|
|
|
_physicsBody->_node = this;
|
2013-09-30 13:02:17 +08:00
|
|
|
_physicsBody->retain();
|
|
|
|
_physicsBody->setPosition(getPosition());
|
|
|
|
_physicsBody->setRotation(getRotation());
|
|
|
|
}
|
|
|
|
|
|
|
|
PhysicsBody* Node::getPhysicsBody() const
|
|
|
|
{
|
|
|
|
return _physicsBody;
|
|
|
|
}
|
|
|
|
#endif //CC_USE_PHYSICS
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +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
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +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
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
updateCascadeOpacity();
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +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;
|
2013-12-11 15:57:20 +08:00
|
|
|
updateColor();
|
|
|
|
|
2013-02-27 18:21:35 +08:00
|
|
|
if (_cascadeOpacityEnabled)
|
2013-02-27 14:48:19 +08:00
|
|
|
{
|
2013-12-19 17:05:59 +08:00
|
|
|
for(auto child : _children){
|
2013-12-06 18:07:16 +08:00
|
|
|
child->updateDisplayedOpacity(_displayedOpacity);
|
2013-12-19 10:33:04 +08:00
|
|
|
}
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
bool Node::isCascadeOpacityEnabled(void) const
|
2013-02-27 15:30:49 +08:00
|
|
|
{
|
|
|
|
return _cascadeOpacityEnabled;
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
void Node::setCascadeOpacityEnabled(bool cascadeOpacityEnabled)
|
2013-02-27 15:30:49 +08:00
|
|
|
{
|
2013-12-11 15:57:20 +08:00
|
|
|
if (_cascadeOpacityEnabled == cascadeOpacityEnabled)
|
2013-12-06 18:07:16 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-27 15:30:49 +08:00
|
|
|
_cascadeOpacityEnabled = cascadeOpacityEnabled;
|
2013-12-06 18:07:16 +08:00
|
|
|
|
|
|
|
if (cascadeOpacityEnabled)
|
|
|
|
{
|
|
|
|
updateCascadeOpacity();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
disableCascadeOpacity();
|
|
|
|
}
|
2013-02-27 15:30:49 +08:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
void Node::updateCascadeOpacity()
|
|
|
|
{
|
|
|
|
GLubyte parentOpacity = 255;
|
|
|
|
|
|
|
|
if (_parent != nullptr && _parent->isCascadeOpacityEnabled())
|
|
|
|
{
|
|
|
|
parentOpacity = _parent->getDisplayedOpacity();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDisplayedOpacity(parentOpacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node::disableCascadeOpacity()
|
|
|
|
{
|
|
|
|
_displayedOpacity = _realOpacity;
|
|
|
|
|
2013-12-19 17:05:59 +08:00
|
|
|
for(auto child : _children){
|
2013-12-11 15:57:20 +08:00
|
|
|
child->updateDisplayedOpacity(255);
|
2013-12-19 17:05:59 +08:00
|
|
|
}
|
2013-12-06 18:07:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const Color3B& Node::getColor(void) const
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
return _realColor;
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
const Color3B& Node::getDisplayedColor() const
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
return _displayedColor;
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
void Node::setColor(const Color3B& color)
|
2013-02-27 09:38:30 +08:00
|
|
|
{
|
|
|
|
_displayedColor = _realColor = color;
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
updateCascadeColor();
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +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;
|
2013-12-11 15:57:20 +08:00
|
|
|
updateColor();
|
2013-02-27 09:38:30 +08:00
|
|
|
|
2013-02-27 14:48:19 +08:00
|
|
|
if (_cascadeColorEnabled)
|
|
|
|
{
|
2013-12-19 17:05:59 +08:00
|
|
|
for(const auto &child : _children){
|
2013-12-06 18:07:16 +08:00
|
|
|
child->updateDisplayedColor(_displayedColor);
|
2013-12-19 10:33:04 +08:00
|
|
|
}
|
2013-02-27 09:38:30 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-17 11:20:25 +08:00
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
bool Node::isCascadeColorEnabled(void) const
|
2013-02-27 15:30:49 +08:00
|
|
|
{
|
|
|
|
return _cascadeColorEnabled;
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:07:16 +08:00
|
|
|
void Node::setCascadeColorEnabled(bool cascadeColorEnabled)
|
2013-02-27 15:30:49 +08:00
|
|
|
{
|
2013-12-06 18:07:16 +08:00
|
|
|
if (_cascadeColorEnabled == cascadeColorEnabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-27 15:30:49 +08:00
|
|
|
_cascadeColorEnabled = cascadeColorEnabled;
|
2013-12-06 18:07:16 +08:00
|
|
|
|
|
|
|
if (_cascadeColorEnabled)
|
|
|
|
{
|
|
|
|
updateCascadeColor();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
disableCascadeColor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node::updateCascadeColor()
|
|
|
|
{
|
|
|
|
Color3B parentColor = Color3B::WHITE;
|
|
|
|
if (_parent && _parent->isCascadeColorEnabled())
|
|
|
|
{
|
|
|
|
parentColor = _parent->getDisplayedColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDisplayedColor(parentColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Node::disableCascadeColor()
|
|
|
|
{
|
2013-12-19 17:05:59 +08:00
|
|
|
for(auto child : _children){
|
2013-12-11 15:57:20 +08:00
|
|
|
child->updateDisplayedColor(Color3B::WHITE);
|
2013-12-19 17:05:59 +08:00
|
|
|
}
|
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
|