2013-06-04 17:38:43 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-06-04 17:38:43 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCComponentContainer.h"
|
|
|
|
#include "2d/CCComponent.h"
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "2d/CCNode.h"
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-09-09 11:53:55 +08:00
|
|
|
ComponentContainer::ComponentContainer(Node* node)
|
|
|
|
: _owner(node)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-09 11:53:55 +08:00
|
|
|
ComponentContainer::~ComponentContainer()
|
|
|
|
{
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
Component* ComponentContainer::get(const std::string& name) const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-18 18:51:36 +08:00
|
|
|
Component* ret = nullptr;
|
2015-09-09 11:53:55 +08:00
|
|
|
|
|
|
|
auto it = _componentMap.find(name);
|
|
|
|
if (it != _componentMap.end())
|
|
|
|
{
|
|
|
|
ret = it->second;
|
|
|
|
}
|
|
|
|
|
2013-12-18 18:51:36 +08:00
|
|
|
return ret;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2013-12-18 18:51:36 +08:00
|
|
|
bool ComponentContainer::add(Component *com)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-18 18:51:36 +08:00
|
|
|
bool ret = false;
|
2015-09-09 11:53:55 +08:00
|
|
|
CCASSERT(com != nullptr, "Component must be non-nil");
|
2013-12-18 18:51:36 +08:00
|
|
|
CCASSERT(com->getOwner() == nullptr, "Component already added. It can't be added again");
|
2013-06-04 17:38:43 +08:00
|
|
|
do
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
auto componentName = com->getName();
|
|
|
|
|
|
|
|
if (_componentMap.find(componentName) != _componentMap.end())
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2015-11-27 21:33:20 +08:00
|
|
|
CCASSERT(false, "ComponentContainer already have this kind of component");
|
2015-09-09 11:53:55 +08:00
|
|
|
break;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
2015-09-18 11:48:43 +08:00
|
|
|
_componentMap[componentName] = com;
|
|
|
|
com->retain();
|
2013-12-18 18:51:36 +08:00
|
|
|
com->setOwner(_owner);
|
2015-06-04 14:06:31 +08:00
|
|
|
com->onAdd();
|
2015-09-09 11:53:55 +08:00
|
|
|
|
2013-12-18 18:51:36 +08:00
|
|
|
ret = true;
|
2013-06-04 17:38:43 +08:00
|
|
|
} while(0);
|
2013-12-18 18:51:36 +08:00
|
|
|
return ret;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 11:48:43 +08:00
|
|
|
bool ComponentContainer::remove(const std::string& componentName)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-18 18:51:36 +08:00
|
|
|
bool ret = false;
|
2013-06-04 17:38:43 +08:00
|
|
|
do
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
auto iter = _componentMap.find(componentName);
|
|
|
|
CC_BREAK_IF(iter == _componentMap.end());
|
|
|
|
|
|
|
|
auto component = iter->second;
|
|
|
|
_componentMap.erase(componentName);
|
|
|
|
|
|
|
|
component->onRemove();
|
|
|
|
component->setOwner(nullptr);
|
|
|
|
component->release();
|
2015-09-09 11:53:55 +08:00
|
|
|
|
2013-12-18 18:51:36 +08:00
|
|
|
ret = true;
|
2013-06-04 17:38:43 +08:00
|
|
|
} while(0);
|
2015-09-09 11:53:55 +08:00
|
|
|
|
2013-12-18 18:51:36 +08:00
|
|
|
return ret;
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
2014-07-21 17:45:56 +08:00
|
|
|
bool ComponentContainer::remove(Component *com)
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
return remove(com->getName());
|
2014-07-21 17:45:56 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ComponentContainer::removeAll()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2015-09-09 11:53:55 +08:00
|
|
|
if (!_componentMap.empty())
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
for (auto iter = _componentMap.begin(); iter != _componentMap.end(); ++iter)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
auto component = iter->second;
|
|
|
|
component->onRemove();
|
|
|
|
component->setOwner(nullptr);
|
|
|
|
component->release();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
2013-12-11 17:15:27 +08:00
|
|
|
|
2015-09-09 11:53:55 +08:00
|
|
|
_componentMap.clear();
|
2013-06-15 14:03:30 +08:00
|
|
|
_owner->unscheduleUpdate();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 18:51:36 +08:00
|
|
|
void ComponentContainer::visit(float delta)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
if (!_componentMap.empty())
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2014-03-06 09:23:27 +08:00
|
|
|
CC_SAFE_RETAIN(_owner);
|
2015-09-18 11:48:43 +08:00
|
|
|
auto iterEnd = _componentMap.end();
|
|
|
|
for (auto iter = _componentMap.begin(); iter != iterEnd; ++iter)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-18 18:51:36 +08:00
|
|
|
iter->second->update(delta);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
2014-03-06 09:23:27 +08:00
|
|
|
CC_SAFE_RELEASE(_owner);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
void ComponentContainer::onEnter()
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
for (auto iter = _componentMap.begin(); iter != _componentMap.end(); ++iter)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
iter->second->onEnter();
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 11:53:55 +08:00
|
|
|
|
2015-05-08 15:49:33 +08:00
|
|
|
void ComponentContainer::onExit()
|
|
|
|
{
|
2015-09-18 11:48:43 +08:00
|
|
|
for (auto iter = _componentMap.begin(); iter != _componentMap.end(); ++iter)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
iter->second->onExit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-04 17:38:43 +08:00
|
|
|
NS_CC_END
|