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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#include "support/component/CCComponentContainer.h"
|
|
|
|
#include "support/component/CCComponent.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ComponentContainer::ComponentContainer(Node *pNode)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _components(NULL)
|
|
|
|
, _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-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_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-06-20 14:13:12 +08:00
|
|
|
pRet = dynamic_cast<Component*>(_components->objectForKey(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-06-15 14:03:30 +08:00
|
|
|
if (_components == NULL)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_components = Dictionary::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
_components->retain();
|
|
|
|
_owner->scheduleUpdate();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
Component *pComponent = dynamic_cast<Component*>(_components->objectForKey(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);
|
|
|
|
_components->setObject(pCom, pCom->getName());
|
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-06-20 14:13:12 +08:00
|
|
|
Object* pRetObject = NULL;
|
|
|
|
DictElement *pElement = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_FIND_PTR(_components->_elements, pName, pElement);
|
2013-06-04 17:38:43 +08:00
|
|
|
if (pElement != NULL)
|
|
|
|
{
|
|
|
|
pRetObject = pElement->getObject();
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
Component *com = dynamic_cast<Component*>(pRetObject);
|
2013-06-04 17:38:43 +08:00
|
|
|
CC_BREAK_IF(!com);
|
|
|
|
com->onExit();
|
|
|
|
com->setOwner(NULL);
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_DEL(_components->_elements, pElement);
|
2013-06-04 17:38:43 +08:00
|
|
|
pElement->getObject()->release();
|
|
|
|
CC_SAFE_DELETE(pElement);
|
|
|
|
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-06-15 14:03:30 +08:00
|
|
|
if (_components != NULL)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *pElement, *tmp;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_ITER(hh, _components->_elements, pElement, tmp)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_DEL(_components->_elements, pElement);
|
2013-06-20 14:13:12 +08:00
|
|
|
((Component*)pElement->getObject())->onExit();
|
|
|
|
((Component*)pElement->getObject())->setOwner(NULL);
|
2013-06-04 17:38:43 +08:00
|
|
|
pElement->getObject()->release();
|
|
|
|
CC_SAFE_DELETE(pElement);
|
|
|
|
}
|
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-06-20 14:13:12 +08:00
|
|
|
_components = Dictionary::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
_components->retain();
|
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-06-15 14:03:30 +08:00
|
|
|
if (_components != NULL)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
DictElement *pElement, *tmp;
|
2013-06-15 14:03:30 +08:00
|
|
|
HASH_ITER(hh, _components->_elements, pElement, tmp)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
((Component*)pElement->getObject())->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-06-15 14:03:30 +08:00
|
|
|
return (bool)(!(_components && _components->count()));
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|