add CustomGUIReader for lua

This commit is contained in:
lite3 2014-08-29 18:50:21 +08:00
parent 4a374a1a2a
commit 3872ee9b59
5 changed files with 192 additions and 0 deletions

View File

@ -0,0 +1,87 @@
#include "CustomGUIReader.h"
#include "CCLuaEngine.h"
#include "base/ObjectFactory.h"
#include "json/document.h"
#include "json/writer.h"
#include "json/stringbuffer.h"
USING_NS_CC;
namespace cocostudio
{
CustomGUIReader* CustomGUIReader::create(std::string &className, int createFunc, int setPropsFunc)
{
auto reader = new CustomGUIReader();
reader->init(className, createFunc, setPropsFunc);
return reader;
}
Ref* CustomGUIReader::createInstance()
{
Ref* result = nullptr;
LuaStack* stack = LuaEngine::getInstance()->getLuaStack();
stack->executeFunction(_createFunc, 0, 1, [&result](lua_State* L,int numReturn){
result = static_cast<Ref*>(tolua_tousertype(L, -1, nullptr));
lua_pop(L, 1);
});
return result;
}
CustomGUIReader::CustomGUIReader()
:_className()
,_createFunc(0)
,_setPropsFunc(0)
{
}
CustomGUIReader::~CustomGUIReader()
{
if (_createFunc)
{
LuaEngine::getInstance()->removeScriptHandler(_createFunc);
_createFunc = 0;
}
if (_setPropsFunc)
{
LuaEngine::getInstance()->removeScriptHandler(_setPropsFunc);
_setPropsFunc = 0;
}
}
void CustomGUIReader::init(std::string &className, int createFunc, int setPropsFunc)
{
_className = className;
_createFunc = createFunc;
_setPropsFunc = setPropsFunc;
ObjectFactory* factoryCreate = ObjectFactory::getInstance();
ObjectFactory::TInfo t;
t._class = className;
t._func = CC_CALLBACK_0(CustomGUIReader::createInstance, this);
factoryCreate->registerType(t);
auto guiReader = GUIReader::getInstance();
auto objMap = guiReader->getParseObjectMap();
(*objMap)[className] = this;
auto callbackMap = guiReader->getParseCallBackMap();
(*callbackMap)[className] = parseselector(CustomGUIReader::setCustomProps);
}
void CustomGUIReader::setCustomProps(const std::string &classType, cocos2d::Ref *widget, const rapidjson::Value &customOptions)
{
if (_setPropsFunc != 0)
{
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
customOptions.Accept(writer);
auto stack = LuaEngine::getInstance()->getLuaStack();
stack->pushString(classType.c_str(), classType.size());
stack->pushObject(widget, "cc.Ref");
stack->pushString(buffer.GetString(), buffer.Size());
stack->executeFunctionByHandler(_setPropsFunc, 3);
}
}
}

View File

@ -0,0 +1,34 @@
#ifndef COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_CUSTOM_GUI_READER_H
#define COCOS_SCRIPTING_LUA_BINDINGS_LUA_COCOS2DX_CUSTOM_GUI_READER_H
#include "cocostudio/WidgetReader/WidgetReader.h"
USING_NS_CC;
namespace cocostudio
{
class CustomGUIReader : public Ref
{
public:
CustomGUIReader();
virtual ~CustomGUIReader();
static CustomGUIReader* create(std::string &className, int createFunc, int setPropsFunc);
void init(std::string &className, int createFunc, int setPropsFunc);
Ref* createInstance();
void setCustomProps(const std::string &classType, cocos2d::Ref *widget, const rapidjson::Value &customOptions);
private:
std::string _className;
int _createFunc;
int _setPropsFunc;
};
}
#endif

View File

@ -30,6 +30,7 @@
#include "CCLuaValue.h"
#include "CocoStudio.h"
#include "CCLuaEngine.h"
#include "CustomGUIReader.h"
using namespace cocostudio;
@ -554,6 +555,53 @@ static void extendActionTimeline(lua_State* L)
lua_pop(L, 1);
}
int lua_cocos2dx_CustomGUIReader_create(lua_State* tolua_S)
{
int argc = 0;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
#if COCOS2D_DEBUG >= 1
if (!tolua_isusertable(tolua_S,1,"ccs.CustomGUIReader",0,&tolua_err)) goto tolua_lerror;
#endif
argc = lua_gettop(tolua_S)-1;
do
{
if (argc == 3)
{
std::string arg0;
ok &= luaval_to_std_string(tolua_S, 2,&arg0, "ccs.CustomGUIReader:create");
if (!ok) { break; }
#if COCOS2D_DEBUG >= 1
if (!toluafix_isfunction(tolua_S,3,"LUA_FUNCTION",0,&tolua_err)) {
goto tolua_lerror;
}
#endif
LUA_FUNCTION arg1 = toluafix_ref_function(tolua_S,3,0);
#if COCOS2D_DEBUG >= 1
if (!toluafix_isfunction(tolua_S,4,"LUA_FUNCTION",0,&tolua_err)) {
goto tolua_lerror;
}
#endif
LUA_FUNCTION arg2 = toluafix_ref_function(tolua_S,4,0);
cocostudio::CustomGUIReader* ret = cocostudio::CustomGUIReader::create(arg0, arg1, arg2);
object_to_luaval<cocostudio::CustomGUIReader>(tolua_S, "ccs.CustomGUIReader",(cocostudio::CustomGUIReader*)ret);
return 1;
}
} while (0);
CCLOG("%s has wrong number of arguments: %d, was expecting %d", "ccs.CustomGUIReader:create",argc, 1);
return 0;
#if COCOS2D_DEBUG >= 1
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_CustomGUIReader_create'.",&tolua_err);
#endif
return 0;
}
int register_all_cocos2dx_coco_studio_manual(lua_State* L)
{
@ -569,6 +617,20 @@ int register_all_cocos2dx_coco_studio_manual(lua_State* L)
return 0;
}
int lua_register_cocos2dx_coco_studio_CustomGUIReader(lua_State* tolua_S)
{
tolua_usertype(tolua_S,"ccs.CustomGUIReader");
tolua_cclass(tolua_S,"CustomGUIReader","ccs.CustomGUIReader","cc.Ref",nullptr);
tolua_beginmodule(tolua_S,"CustomGUIReader");
tolua_function(tolua_S,"create",lua_cocos2dx_CustomGUIReader_create);
tolua_endmodule(tolua_S);
std::string typeName = typeid(cocostudio::CustomGUIReader).name();
g_luaType[typeName] = "ccs.CustomGUIReader";
g_typeCast["CustomGUIReader"] = "ccs.CustomGUIReader";
return 1;
}
int register_cocostudio_module(lua_State* L)
{
lua_getglobal(L, "_G");
@ -576,6 +638,7 @@ int register_cocostudio_module(lua_State* L)
{
register_all_cocos2dx_studio(L);
register_all_cocos2dx_coco_studio_manual(L);
lua_register_cocos2dx_coco_studio_CustomGUIReader(L);
}
lua_pop(L, 1);

View File

@ -56,6 +56,7 @@
<ClCompile Include="..\manual\cocosbuilder\CCBProxy.cpp" />
<ClCompile Include="..\manual\cocosbuilder\lua_cocos2dx_cocosbuilder_manual.cpp" />
<ClCompile Include="..\manual\cocosdenshion\lua_cocos2dx_cocosdenshion_manual.cpp" />
<ClCompile Include="..\manual\cocostudio\CustomGUIReader.cpp" />
<ClCompile Include="..\manual\cocostudio\lua_cocos2dx_coco_studio_manual.cpp" />
<ClCompile Include="..\manual\extension\lua_cocos2dx_extension_manual.cpp" />
<ClCompile Include="..\manual\LuaBasicConversions.cpp" />
@ -117,6 +118,7 @@
<ClInclude Include="..\manual\cocosbuilder\CCBProxy.h" />
<ClInclude Include="..\manual\cocosbuilder\lua_cocos2dx_cocosbuilder_manual.h" />
<ClInclude Include="..\manual\cocosdenshion\lua_cocos2dx_cocosdenshion_manual.h" />
<ClInclude Include="..\manual\cocostudio\CustomGUIReader.h" />
<ClInclude Include="..\manual\cocostudio\lua_cocos2dx_coco_studio_manual.hpp" />
<ClInclude Include="..\manual\extension\lua_cocos2dx_extension_manual.h" />
<ClInclude Include="..\manual\LuaBasicConversions.h" />

View File

@ -243,6 +243,9 @@
<ClCompile Include="..\..\..\..\external\lua\luasocket\wsocket.c">
<Filter>luasocket</Filter>
</ClCompile>
<ClCompile Include="..\manual\cocostudio\CustomGUIReader.cpp">
<Filter>manual\cocostudio</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\auto\lua_cocos2dx_auto.hpp">
@ -422,6 +425,9 @@
<ClInclude Include="..\..\..\..\external\lua\luasocket\wsocket.h">
<Filter>luasocket</Filter>
</ClInclude>
<ClInclude Include="..\manual\cocostudio\CustomGUIReader.h">
<Filter>manual\cocostudio</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\script\CCBReaderLoad.lua">