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
|
|
|
|
|
|
|
|
CCComponentContainer::CCComponentContainer(CCNode *pNode)
|
2013-06-15 14:03:30 +08:00
|
|
|
: _components(NULL)
|
|
|
|
, _owner(pNode)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCComponentContainer::~CCComponentContainer(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_components);
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCComponent* CCComponentContainer::get(const char *pName) const
|
|
|
|
{
|
|
|
|
CCComponent* pRet = NULL;
|
|
|
|
CCAssert(pName != NULL, "Argument must be non-nil");
|
|
|
|
do {
|
|
|
|
CC_BREAK_IF(NULL == pName);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(NULL == _components);
|
|
|
|
pRet = dynamic_cast<CCComponent*>(_components->objectForKey(pName));
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
} while (0);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCComponentContainer::add(CCComponent *pCom)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
CCAssert(pCom != NULL, "Argument must be non-nil");
|
|
|
|
CCAssert(pCom->getOwner() == NULL, "Component already added. It can't be added again");
|
|
|
|
do
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_components == NULL)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_components = CCDictionary::create();
|
|
|
|
_components->retain();
|
|
|
|
_owner->scheduleUpdate();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
CCComponent *pComponent = dynamic_cast<CCComponent*>(_components->objectForKey(pCom->getName()));
|
2013-06-04 17:38:43 +08:00
|
|
|
|
|
|
|
CCAssert(pComponent == NULL, "Component already added. It can't be added again");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCComponentContainer::remove(const char *pName)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
CCAssert(pName != NULL, "Argument must be non-nil");
|
|
|
|
do
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_BREAK_IF(!_components);
|
2013-06-04 17:38:43 +08:00
|
|
|
CCObject* pRetObject = NULL;
|
|
|
|
CCDictElement *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();
|
|
|
|
}
|
|
|
|
CCComponent *com = dynamic_cast<CCComponent*>(pRetObject);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCComponentContainer::removeAll()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_components != NULL)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CCDictElement *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-04 17:38:43 +08:00
|
|
|
((CCComponent*)pElement->getObject())->onExit();
|
|
|
|
((CCComponent*)pElement->getObject())->setOwner(NULL);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCComponentContainer::alloc(void)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_components = CCDictionary::create();
|
|
|
|
_components->retain();
|
2013-06-04 17:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCComponentContainer::visit(float fDelta)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_components != NULL)
|
2013-06-04 17:38:43 +08:00
|
|
|
{
|
|
|
|
CCDictElement *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
|
|
|
{
|
|
|
|
((CCComponent*)pElement->getObject())->update(fDelta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-05 15:05:49 +08:00
|
|
|
bool CCComponentContainer::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
|