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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCComponentContainer.h"
|
|
|
|
#include "CCComponent.h"
|
2013-06-04 17:38:43 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-12-18 18:51:36 +08:00
|
|
|
ComponentContainer::ComponentContainer(Node *node)
|
2013-12-11 17:15:27 +08:00
|
|
|
: _components(nullptr)
|
2013-12-18 18:51:36 +08:00
|
|
|
, _owner(node)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ComponentContainer::~ComponentContainer(void)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
CC_SAFE_DELETE(_components);
|
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;
|
2013-06-04 17:38:43 +08:00
|
|
|
do {
|
2013-12-18 17:47:20 +08:00
|
|
|
CC_BREAK_IF(nullptr == _components);
|
2013-12-18 18:51:36 +08:00
|
|
|
ret = _components->at(name);
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
CCASSERT(com != nullptr, "Argument must be non-nil");
|
|
|
|
CCASSERT(com->getOwner() == nullptr, "Component already added. It can't be added again");
|
2013-06-04 17:38:43 +08:00
|
|
|
do
|
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
if (_components == nullptr)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
_components = new Map<std::string, Component*>();
|
2013-06-15 14:03:30 +08:00
|
|
|
_owner->scheduleUpdate();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
2013-12-18 18:51:36 +08:00
|
|
|
Component *component = _components->at(com->getName());
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-12-18 18:51:36 +08:00
|
|
|
CCASSERT(component == nullptr, "Component already added. It can't be added again");
|
|
|
|
CC_BREAK_IF(component);
|
|
|
|
com->setOwner(_owner);
|
|
|
|
_components->insert(com->getName(), com);
|
|
|
|
com->onEnter();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-24 10:51:47 +08:00
|
|
|
bool ComponentContainer::remove(const std::string& name)
|
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
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(!_components);
|
2013-12-11 17:15:27 +08:00
|
|
|
|
2013-12-18 18:51:36 +08:00
|
|
|
auto iter = _components->find(name);
|
2013-12-11 17:15:27 +08:00
|
|
|
CC_BREAK_IF(iter == _components->end());
|
|
|
|
|
|
|
|
auto com = iter->second;
|
2013-06-04 17:38:43 +08:00
|
|
|
com->onExit();
|
2013-12-18 17:47:20 +08:00
|
|
|
com->setOwner(nullptr);
|
2013-12-11 17:15:27 +08:00
|
|
|
|
|
|
|
_components->erase(iter);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ComponentContainer::removeAll()
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
if (_components != nullptr)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
for (auto iter = _components->begin(); iter != _components->end(); ++iter)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
iter->second->onExit();
|
2013-12-18 17:47:20 +08:00
|
|
|
iter->second->setOwner(nullptr);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
2013-12-11 17:15:27 +08:00
|
|
|
|
|
|
|
_components->clear();
|
|
|
|
CC_SAFE_DELETE(_components);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_owner->unscheduleUpdate();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ComponentContainer::alloc(void)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
_components = new Map<std::string, Component*>();
|
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
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
if (_components != nullptr)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
for (auto iter = _components->begin(); iter != _components->end(); ++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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ComponentContainer::isEmpty() const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-12-11 17:15:27 +08:00
|
|
|
return (_components == nullptr || _components->empty());
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|