2013-12-25 17:40:54 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2013-12-25 17:40:54 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ObjectFactory.h"
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
namespace cocostudio {
|
|
|
|
|
|
|
|
ObjectFactory::TInfo::TInfo(void)
|
|
|
|
:_class("")
|
2013-12-26 01:17:04 +08:00
|
|
|
,_fun(nullptr)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::TInfo::TInfo(const std::string& type, Instance ins)
|
|
|
|
:_class(type)
|
|
|
|
,_fun(ins)
|
|
|
|
{
|
|
|
|
ObjectFactory::getInstance()->registerType(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::TInfo::TInfo(const TInfo &t)
|
|
|
|
{
|
|
|
|
_class = t._class;
|
|
|
|
_fun = t._fun;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::TInfo::~TInfo(void)
|
|
|
|
{
|
|
|
|
_class = "";
|
2013-12-26 01:17:04 +08:00
|
|
|
_fun = nullptr;
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::TInfo& ObjectFactory::TInfo::operator= (const TInfo &t)
|
|
|
|
{
|
|
|
|
_class = t._class;
|
|
|
|
_fun = t._fun;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-26 01:17:04 +08:00
|
|
|
ObjectFactory* ObjectFactory::_sharedFactory = nullptr;
|
2013-12-25 17:40:54 +08:00
|
|
|
|
|
|
|
ObjectFactory::ObjectFactory(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory::~ObjectFactory(void)
|
|
|
|
{
|
|
|
|
_typeMap.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjectFactory* ObjectFactory::getInstance()
|
|
|
|
{
|
2013-12-26 01:17:04 +08:00
|
|
|
if ( nullptr == _sharedFactory)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
|
|
|
_sharedFactory = new ObjectFactory();
|
|
|
|
}
|
|
|
|
return _sharedFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjectFactory::destroyInstance()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_sharedFactory);
|
|
|
|
}
|
|
|
|
|
2014-01-05 02:22:32 +08:00
|
|
|
Object* ObjectFactory::createObject(const std::string &name)
|
2013-12-25 17:40:54 +08:00
|
|
|
{
|
2013-12-26 01:17:04 +08:00
|
|
|
Object *o = nullptr;
|
2013-12-25 17:40:54 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
const TInfo t = _typeMap[name];
|
2013-12-26 01:17:04 +08:00
|
|
|
CC_BREAK_IF(t._fun == nullptr);
|
2013-12-25 17:40:54 +08:00
|
|
|
o = t._fun();
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-01-05 12:42:39 +08:00
|
|
|
Component* ObjectFactory::createComponent(const std::string &name)
|
2014-01-05 02:22:32 +08:00
|
|
|
{
|
2014-01-05 12:42:39 +08:00
|
|
|
std::string comName;
|
2014-01-05 02:22:32 +08:00
|
|
|
if (name == "CCSprite" || name == "CCTMXTiledMap" || name == "CCParticleSystemQuad" || name == "CCArmature" || name == "GUIComponent")
|
|
|
|
{
|
2014-01-05 12:42:39 +08:00
|
|
|
comName = "ComRender";
|
2014-01-05 02:22:32 +08:00
|
|
|
}
|
|
|
|
else if (name == "CCComAudio" || name == "CCBackgroundAudio")
|
|
|
|
{
|
2014-01-05 12:42:39 +08:00
|
|
|
comName = "ComAudio";
|
2014-01-05 02:22:32 +08:00
|
|
|
}
|
|
|
|
else if (name == "CCComController")
|
|
|
|
{
|
2014-01-05 12:42:39 +08:00
|
|
|
comName = "ComController";
|
2014-01-05 02:22:32 +08:00
|
|
|
}
|
|
|
|
else if (name == "CCComAttribute")
|
|
|
|
{
|
2014-01-05 12:42:39 +08:00
|
|
|
comName = "ComAttribute";
|
2014-01-05 02:22:32 +08:00
|
|
|
}
|
|
|
|
else if (name == "CCScene")
|
|
|
|
{
|
2014-01-05 13:16:23 +08:00
|
|
|
comName = "Scene";
|
2014-01-05 02:22:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCASSERT(false, "Unregistered Component!");
|
|
|
|
}
|
|
|
|
Object *o = NULL;
|
|
|
|
do
|
|
|
|
{
|
2014-01-05 12:42:39 +08:00
|
|
|
const TInfo t = _typeMap[comName];
|
2014-01-05 02:22:32 +08:00
|
|
|
CC_BREAK_IF(t._fun == NULL);
|
|
|
|
o = t._fun();
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return (Component*)o;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-25 17:40:54 +08:00
|
|
|
void ObjectFactory::registerType(const TInfo &t)
|
|
|
|
{
|
2013-12-27 20:54:05 +08:00
|
|
|
_typeMap.insert(std::make_pair(t._class, t));
|
2013-12-25 17:40:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|