标签:cocos2dx cocostudio objectfactory 自动化注册
在cocos2dx的ui编辑器cocostudio有一套相关的api:
1、表示各个UI控件的类,如LoadingBar,ListView等等,其都继承自Widget
2、有相关各个UI控件解析的类,由于cocos提供UI编辑器,方便了开发者快速制作UI,动画等,载入游戏需要相关解析支持。
从cocos2dx的实现来看,通过ObjectFactory实现了,自动注册,将类型与创建UI接口对应,在UI解析方面,将类型与解析类别对应,都是
通过ObjectFactory来实现的,下面我看看相关分析:
1、首先我们要将类型与相应的UI创建方法建立对应,那么需要一个数据结构,在ObjectFactory里有一个叫TInfo,
typedef cocos2d::Ref* (*Instance)(void); struct TInfo { TInfo(void); TInfo(const std::string& type, Instance ins = NULL); TInfo(const TInfo &t); ~TInfo(void); TInfo& operator= (const TInfo &t); std::string _class; Instance _fun; };
typedef cocos2d::Ref* (*Instance)(void);
在TInfo的构造函数里,有如下实现:
ObjectFactory::TInfo::TInfo(const std::string& type, Instance ins) :_class(type) ,_fun(ins) { ObjectFactory::getInstance()->registerType(*this); }
知道,要想达到自动的目的,其中一个办法就是全局对象或者静态对象的构造函数里
在GUIDefine.h下有3个宏
#define DECLARE_CLASS_GUI_INFO public: static cocos2d::ObjectFactory::TInfo __Type; static cocos2d::Ref* createInstance(void); #define IMPLEMENT_CLASS_GUI_INFO(className) cocos2d::Ref* className::createInstance(void) { return className::create(); } cocos2d::ObjectFactory::TInfo className::__Type(#className, &className::createInstance); #define CREATE_CLASS_GUI_INFO(className) cocos2d::ObjectFactory::TInfo(#className, &className::createInstance) \
知道静态成员都是属于类的。
在该文件的下面,还有专门给UIReader的宏,差别就是createInstance里的实现,不一样,UI是通过create,而UIReader是通过getInstance
#define DECLARE_CLASS_WIDGET_READER_INFO public: static cocos2d::ObjectFactory::TInfo __Type; static cocos2d::Ref* createInstance(void); #define IMPLEMENT_CLASS_WIDGET_READER_INFO(className) cocos2d::Ref* className::createInstance(void) { return className::getInstance(); } cocos2d::ObjectFactory::TInfo className::__Type(#className, &className::createInstance); #define CREATE_CLASS_WIDGET_READER_INFO(className) cocos2d::ObjectFactory::TInfo(#className, &className::createInstance) \
在所有ui扩展里的ui类实现,都会有
class UIClass : public Widget { // 注意这里 DECLARE_CLASS_GUI_INFO ...
IMPLEMENT_CLASS_GUI_INFO(CheckBox)
同上,UIReader也用了类似的方式:
class LayoutReader : public WidgetReader { public: DECLARE_CLASS_WIDGET_READER_INFO
关于最后一个CREATE宏,发现其调用的地方,是GUIReader初始化 的时候,但是从上面分析,这些注册过程都是自动化的,这里主动调用是为何?
GUIReader::GUIReader(): m_strFilePath("") { ObjectFactory* factoryCreate = ObjectFactory::getInstance(); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ButtonReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(CheckBoxReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(SliderReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ImageViewReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(LoadingBarReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextAtlasReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextBMFontReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(TextFieldReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(LayoutReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(PageViewReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ScrollViewReader)); factoryCreate->registerType(CREATE_CLASS_WIDGET_READER_INFO(ListViewReader)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Button)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(CheckBox)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ImageView)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Text)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextAtlas)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextBMFont)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(LoadingBar)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Slider)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(TextField)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(Layout)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ListView)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(PageView)); factoryCreate->registerType(CREATE_CLASS_GUI_INFO(ScrollView)); }
标签:cocos2dx cocostudio objectfactory 自动化注册
原文地址:http://blog.csdn.net/djvc/article/details/44518493