cocos2dx v3 with cocosbuilder

# 概要
- cocos2dxの3.3でcocosBuilderの対応関係のメモ
- cocos2dxの2.xでしかcocosBuilder使ったことなかったのでハマった

# 環境
- Mac OSX 10.10
- cocos2dx v3.3
- cocosbuilder v3.0α （[Download](https://code.google.com/p/cocosbuilder/downloads/detail?name=CocosBuilder-3.0-alpha5.zip&can=2&q=, cocosbuilder)）

# 基本確認
- とりあえずCCがとれたらしい...
- CCNode → Node
- CCLayer → Layer
- 以下略。

# cocosBuilder側の操作
- rootNode(Layer？)にcustomClassを設定。今回はOpeningLayerにした
- buttonとか設置
- buttonのcallbackセレクタとか設定。document Rootにアタッチ。
- Sprite（画像）とか設置
- code connectionでdoc root varとnameを設定。

# Loaderクラスの作成

- OpeningLayerLoader.h
- CocosBuilder.hのincludeがよく分かってない... とりあえず階層全部のせたらうまくいった...

```
#include "cocos2d.h"
#include "cocos-ext.h"
#include "OpeningLayer.h"
#include "editor-support/cocosbuilder/CocosBuilder.h"

class OpeningLayerLoader: public cocosbuilder::LayerLoader {
public:
    CCB_STATIC_NEW_AUTORELEASE_OBJECT_METHOD(OpeningLayerLoader, loader);
    
    CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(OpeningLayer);
};
```

# OpeningLayerクラスの作成
- OpeningLayer.h

```
#include "cocos2d.h"
#include "cocos-ext.h"
#include "cocosbuilder/CocosBuilder.h"
USING_NS_CC;
USING_NS_CC_EXT;

class OpeningLayer:
public cocos2d::Layer,
public cocosbuilder::CCBMemberVariableAssigner,
public cocosbuilder::CCBSelectorResolver
{
public:
    static Scene* createScene();
    OpeningLayer();
    ~OpeningLayer();
    CREATE_FUNC(OpeningLayer);
    
    bool init(){return true;};
    
    bool onAssignCCBMemberVariable(cocos2d::Ref* pTarget, const char* pMemberVariableName, cocos2d::Node* pNode);
    cocos2d::SEL_MenuHandler onResolveCCBCCMenuItemSelector(cocos2d::Ref * pTarget, const char* pSelectorName)
    { return nullptr;};
    Control::Handler onResolveCCBCCControlSelector(cocos2d::Ref * pTarget, const char* pSelectorName);
    cocos2d::SEL_CallFuncN onResolveCCBCCCallFuncSelector(cocos2d::Ref * pTarget, const char* pSelectorName)
    { return NULL; };
    
    void onPushButton(Ref* pSender, cocos2d::extension::Control::EventType event);
    void onPushReplaceScene(Ref* pSender, cocos2d::extension::Control::EventType event);
private:
    Sprite *_image;
};

```

- OpeningLayer.cpp
- 長くなるのでcocosbuilder関係のとこだけ。
 
```
bool OpeningLayer::onAssignCCBMemberVariable(cocos2d::Ref *pTarget, const char *pMemberVariableName, Node *pNode)
{
    CCB_MEMBERVARIABLEASSIGNER_GLUE_WEAK(this, "image", Sprite*, _image);
    return false;
}

cocos2d::extension::Control::Handler OpeningLayer::onResolveCCBCCControlSelector(cocos2d::Ref * pTarget, const char* pSelectorName)
{
    CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "pushButton", OpeningLayer::onPushButton);
    CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "replaceScene", OpeningLayer::onPushReplaceScene);
    return nullptr;
}
```

とりあえずこれで動いた。

# 読み込み

```
cocosbuilder::NodeLoaderLibrary* pNodeLoaderLibrary = cocosbuilder::NodeLoaderLibrary::getInstance();
pNodeLoaderLibrary->registerNodeLoader("OpeningLayer", OpeningLayerLoader::loader());
cocosbuilder::CCBReader* pReader = new cocosbuilder::CCBReader(cocosbuilder::NodeLoaderLibrary::getInstance());
Node *n = pReader->readNodeGraphFromFile("OpeningLayer.ccbi");
```

- 必要に応じてcastしてもよい。

# 参考文献
- v2に関してはこの記事とか [cocos2dxとCocosbuilderの連携方法](http://ichiokumanyen.hatenablog.jp/entry/2013/08/23/021823, title)