axmol/cocos/2d/CCProtectedNode.cpp

471 lines
12 KiB
C++
Raw Normal View History

2014-03-24 15:25:44 +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.
2016-08-05 09:42:15 +08:00
Copyright (c) 2013-2016 Chukong Technologies Inc.
2014-03-24 15:25:44 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "2d/CCProtectedNode.h"
2014-03-24 15:25:44 +08:00
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
2014-05-02 07:42:35 +08:00
#include "2d/CCScene.h"
2014-03-24 15:25:44 +08:00
NS_CC_BEGIN
ProtectedNode::ProtectedNode() : _reorderProtectedChildDirty(false)
{
}
ProtectedNode::~ProtectedNode()
{
CCLOGINFO( "deallocing ProtectedNode: %p - tag: %i", this, _tag );
2015-12-17 21:55:47 +08:00
removeAllProtectedChildren();
2014-03-24 15:25:44 +08:00
}
ProtectedNode * ProtectedNode::create(void)
{
ProtectedNode * ret = new (std::nothrow) ProtectedNode();
2014-03-24 15:25:44 +08:00
if (ret && ret->init())
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
void ProtectedNode::cleanup()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnCleanup))
return;
}
#endif // #if CC_ENABLE_SCRIPT_BINDING
2014-03-24 15:25:44 +08:00
Node::cleanup();
// timers
for( const auto &child: _protectedChildren)
child->cleanup();
}
void ProtectedNode::addProtectedChild(cocos2d::Node *child)
{
addProtectedChild(child, child->getLocalZOrder(), child->getTag());
}
void ProtectedNode::addProtectedChild(cocos2d::Node *child, int localZOrder)
{
addProtectedChild(child, localZOrder, child->getTag());
}
/* "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 ProtectedNode::addProtectedChild(Node *child, int zOrder, int tag)
{
CCASSERT( child != nullptr, "Argument must be non-nil");
CCASSERT( child->getParent() == nullptr, "child already added. It can't be added again");
if (_protectedChildren.empty())
{
_protectedChildren.reserve(4);
}
this->insertProtectedChild(child, zOrder);
child->setTag(tag);
child->setParent(this);
child->updateOrderOfArrival();
2014-03-24 15:25:44 +08:00
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();
}
}
Node* ProtectedNode::getProtectedChildByTag(int tag)
{
CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
for (auto& child : _protectedChildren)
{
if(child && child->getTag() == tag)
return child;
}
return nullptr;
}
/* "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 ProtectedNode::removeProtectedChild(cocos2d::Node *child, bool cleanup)
{
// explicit nil handling
if (_protectedChildren.empty())
{
return;
}
ssize_t index = _protectedChildren.getIndex(child);
if( index != CC_INVALID_INDEX )
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
if (_running)
{
child->onExitTransitionDidStart();
child->onExit();
}
// If you don't do cleanup, the child's actions will not get removed and the
// its scheduledSelectors_ dict will not get released!
if (cleanup)
{
child->cleanup();
}
// set parent nil at the end
child->setParent(nullptr);
2015-12-17 21:55:47 +08:00
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
sEngine->releaseScriptObject(this, child);
}
2015-12-17 21:55:47 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
2014-03-24 15:25:44 +08:00
_protectedChildren.erase(index);
}
}
void ProtectedNode::removeAllProtectedChildren()
{
removeAllProtectedChildrenWithCleanup(true);
}
void ProtectedNode::removeAllProtectedChildrenWithCleanup(bool cleanup)
{
// not using detachChild improves speed here
for (auto& child : _protectedChildren)
{
// IMPORTANT:
// -1st do onExit
// -2nd cleanup
if(_running)
{
child->onExitTransitionDidStart();
child->onExit();
}
if (cleanup)
{
child->cleanup();
}
2015-12-17 21:55:47 +08:00
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
sEngine->releaseScriptObject(this, child);
}
2015-12-17 21:55:47 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
2014-03-24 15:25:44 +08:00
// set parent nil at the end
child->setParent(nullptr);
}
_protectedChildren.clear();
}
void ProtectedNode::removeProtectedChildByTag(int tag, bool cleanup)
{
CCASSERT( tag != Node::INVALID_TAG, "Invalid tag");
Node *child = this->getProtectedChildByTag(tag);
if (child == nullptr)
{
CCLOG("cocos2d: removeChildByTag(tag = %d): child not found!", tag);
}
else
{
this->removeProtectedChild(child, cleanup);
}
}
// helper used by reorderChild & add
void ProtectedNode::insertProtectedChild(cocos2d::Node *child, int z)
{
2015-12-17 21:55:47 +08:00
#if CC_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
sEngine->retainScriptObject(this, child);
}
2015-12-17 21:55:47 +08:00
#endif // CC_ENABLE_GC_FOR_NATIVE_OBJECTS
2014-03-24 15:25:44 +08:00
_reorderProtectedChildDirty = true;
_protectedChildren.pushBack(child);
2014-09-03 17:26:50 +08:00
child->setLocalZOrder(z);
2014-03-24 15:25:44 +08:00
}
void ProtectedNode::sortAllProtectedChildren()
{
if( _reorderProtectedChildDirty ) {
sortNodes(_protectedChildren);
2014-03-24 15:25:44 +08:00
_reorderProtectedChildDirty = false;
}
}
void ProtectedNode::reorderProtectedChild(cocos2d::Node *child, int localZOrder)
{
CCASSERT( child != nullptr, "Child must be non-nil");
_reorderProtectedChildDirty = true;
child->updateOrderOfArrival();
2014-09-03 17:26:50 +08:00
child->setLocalZOrder(localZOrder);
2014-03-24 15:25:44 +08:00
}
void ProtectedNode::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags)
2014-03-24 15:25:44 +08:00
{
// quick return if not visible. children won't be drawn.
if (!_visible)
2014-03-24 15:25:44 +08:00
{
return;
}
uint32_t flags = processParentFlags(parentTransform, parentFlags);
2014-03-24 15:25:44 +08:00
// IMPORTANT:
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
// To ease the migration to v3.0, we still support the Mat4 stack,
2014-03-24 15:25:44 +08:00
// but it is deprecated and your code should not rely on it
Director* director = Director::getInstance();
CCASSERT(nullptr != director, "Director is null when setting matrix stack");
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
2014-03-24 15:25:44 +08:00
int i = 0; // used by _children
int j = 0; // used by _protectedChildren
sortAllChildren();
sortAllProtectedChildren();
//
// draw children and protectedChildren zOrder < 0
//
for( ; i < _children.size(); i++ )
{
auto node = _children.at(i);
if ( node && node->getLocalZOrder() < 0 )
node->visit(renderer, _modelViewTransform, flags);
2014-03-24 15:25:44 +08:00
else
break;
}
for( ; j < _protectedChildren.size(); j++ )
{
auto node = _protectedChildren.at(j);
if ( node && node->getLocalZOrder() < 0 )
node->visit(renderer, _modelViewTransform, flags);
2014-03-24 15:25:44 +08:00
else
break;
}
//
// draw self
//
if (isVisitableByVisitingCamera())
2014-08-13 12:06:51 +08:00
this->draw(renderer, _modelViewTransform, flags);
2014-03-24 15:25:44 +08:00
//
// draw children and protectedChildren zOrder >= 0
//
for(auto it=_protectedChildren.cbegin()+j; it != _protectedChildren.cend(); ++it)
(*it)->visit(renderer, _modelViewTransform, flags);
2014-03-24 15:25:44 +08:00
for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
(*it)->visit(renderer, _modelViewTransform, flags);
2014-03-24 15:25:44 +08:00
2014-09-11 15:44:31 +08:00
// FIX ME: Why need to set _orderOfArrival to 0??
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
// setOrderOfArrival(0);
2014-03-24 15:25:44 +08:00
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
2014-03-24 15:25:44 +08:00
}
void ProtectedNode::onEnter()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
return;
}
#endif
2014-03-24 15:25:44 +08:00
Node::onEnter();
for( const auto &child: _protectedChildren)
child->onEnter();
}
void ProtectedNode::onEnterTransitionDidFinish()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnterTransitionDidFinish))
return;
}
#endif
2014-03-24 15:25:44 +08:00
Node::onEnterTransitionDidFinish();
for( const auto &child: _protectedChildren)
child->onEnterTransitionDidFinish();
}
void ProtectedNode::onExitTransitionDidStart()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExitTransitionDidStart))
return;
}
#endif
2014-03-24 15:25:44 +08:00
Node::onExitTransitionDidStart();
for( const auto &child: _protectedChildren)
child->onExitTransitionDidStart();
}
void ProtectedNode::onExit()
{
#if CC_ENABLE_SCRIPT_BINDING
if (_scriptType == kScriptTypeJavascript)
{
if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit))
return;
}
#endif
2014-03-24 15:25:44 +08:00
Node::onExit();
for( const auto &child: _protectedChildren)
child->onExit();
}
void ProtectedNode::updateDisplayedOpacity(GLubyte parentOpacity)
{
_displayedOpacity = _realOpacity * parentOpacity/255.0;
updateColor();
if (_cascadeOpacityEnabled)
{
for(auto child : _children){
child->updateDisplayedOpacity(_displayedOpacity);
}
}
for(auto child : _protectedChildren){
child->updateDisplayedOpacity(_displayedOpacity);
2014-03-24 15:25:44 +08:00
}
}
void ProtectedNode::updateDisplayedColor(const Color3B& parentColor)
{
_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();
if (_cascadeColorEnabled)
{
for(const auto &child : _children){
child->updateDisplayedColor(_displayedColor);
}
}
for(const auto &child : _protectedChildren){
child->updateDisplayedColor(_displayedColor);
2014-03-24 15:25:44 +08:00
}
}
void ProtectedNode::disableCascadeColor()
{
for(auto child : _children){
child->updateDisplayedColor(Color3B::WHITE);
}
for(auto child : _protectedChildren){
child->updateDisplayedColor(Color3B::WHITE);
2014-03-24 15:25:44 +08:00
}
}
void ProtectedNode::disableCascadeOpacity()
{
_displayedOpacity = _realOpacity;
for(auto child : _children){
child->updateDisplayedOpacity(255);
}
for(auto child : _protectedChildren){
child->updateDisplayedOpacity(255);
}
}
2015-02-12 09:24:04 +08:00
void ProtectedNode::setCameraMask(unsigned short mask, bool applyChildren)
{
Node::setCameraMask(mask, applyChildren);
if (applyChildren)
{
for (auto& iter: _protectedChildren)
{
iter->setCameraMask(mask);
}
}
}
NS_CC_END