2013-06-04 17:38:43 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 cocos2d-x.org
|
|
|
|
|
|
|
|
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-06-20 14:13:12 +08:00
|
|
|
ComponentContainer::ComponentContainer(Node *pNode)
|
2013-12-11 17:15:27 +08:00
|
|
|
: _components(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _owner(pNode)
|
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-06-20 14:13:12 +08:00
|
|
|
Component* ComponentContainer::get(const char *pName) const
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Component* pRet = NULL;
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pName != NULL, "Argument must be non-nil");
|
2013-06-04 17:38:43 +08:00
|
|
|
do {
|
|
|
|
CC_BREAK_IF(NULL == pName);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(NULL == _components);
|
2013-12-11 17:15:27 +08:00
|
|
|
pRet = _components->at(pName);
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
} while (0);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ComponentContainer::add(Component *pCom)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pCom != NULL, "Argument must be non-nil");
|
|
|
|
CCASSERT(pCom->getOwner() == NULL, "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-11 17:15:27 +08:00
|
|
|
Component *pComponent = _components->at(pCom->getName());
|
2013-06-04 17:38:43 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pComponent == NULL, "Component already added. It can't be added again");
|
2013-06-04 17:38:43 +08:00
|
|
|
CC_BREAK_IF(pComponent);
|
2013-06-15 14:03:30 +08:00
|
|
|
pCom->setOwner(_owner);
|
2013-12-11 17:15:27 +08:00
|
|
|
_components->insert(pCom->getName(), pCom);
|
2013-06-04 17:38:43 +08:00
|
|
|
pCom->onEnter();
|
|
|
|
bRet = true;
|
|
|
|
} while(0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ComponentContainer::remove(const char *pName)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pName != NULL, "Argument must be non-nil");
|
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
|
|
|
|
|
|
|
auto iter = _components->find(pName);
|
|
|
|
CC_BREAK_IF(iter == _components->end());
|
|
|
|
|
|
|
|
auto com = iter->second;
|
2013-06-04 17:38:43 +08:00
|
|
|
com->onExit();
|
|
|
|
com->setOwner(NULL);
|
2013-12-11 17:15:27 +08:00
|
|
|
|
|
|
|
_components->erase(iter);
|
|
|
|
|
2013-06-04 17:38:43 +08:00
|
|
|
bRet = true;
|
|
|
|
} while(0);
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
iter->second->setOwner(NULL);
|
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-06-20 14:13:12 +08:00
|
|
|
void ComponentContainer::visit(float fDelta)
|
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->update(fDelta);
|
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
|