diff --git a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp index f0dc62249745..4049f3490bef 100644 --- a/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp +++ b/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp @@ -29426,63 +29426,7 @@ int lua_cocos2dx_EventDispatcher_setPriority(lua_State* tolua_S) return 0; } -int lua_cocos2dx_EventDispatcher_addCustomEventListener(lua_State* tolua_S) -{ - int argc = 0; - cocos2d::EventDispatcher* cobj = nullptr; - bool ok = true; - -#if COCOS2D_DEBUG >= 1 - tolua_Error tolua_err; -#endif - - -#if COCOS2D_DEBUG >= 1 - if (!tolua_isusertype(tolua_S,1,"cc.EventDispatcher",0,&tolua_err)) goto tolua_lerror; -#endif - - cobj = (cocos2d::EventDispatcher*)tolua_tousertype(tolua_S,1,0); - -#if COCOS2D_DEBUG >= 1 - if (!cobj) - { - tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_EventDispatcher_addCustomEventListener'", nullptr); - return 0; - } -#endif - argc = lua_gettop(tolua_S)-1; - if (argc == 2) - { - std::string arg0; - std::function arg1; - - ok &= luaval_to_std_string(tolua_S, 2,&arg0, "cc.EventDispatcher:addCustomEventListener"); - - do { - // Lambda binding for lua is not supported. - assert(false); - } while(0) - ; - if(!ok) - { - tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_EventDispatcher_addCustomEventListener'", nullptr); - return 0; - } - cocos2d::EventListenerCustom* ret = cobj->addCustomEventListener(arg0, arg1); - object_to_luaval(tolua_S, "cc.EventListenerCustom",(cocos2d::EventListenerCustom*)ret); - return 1; - } - luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.EventDispatcher:addCustomEventListener",argc, 2); - return 0; - -#if COCOS2D_DEBUG >= 1 - tolua_lerror: - tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_EventDispatcher_addCustomEventListener'.",&tolua_err); -#endif - - return 0; -} int lua_cocos2dx_EventDispatcher_dispatchEvent(lua_State* tolua_S) { int argc = 0; @@ -29785,7 +29729,6 @@ int lua_register_cocos2dx_EventDispatcher(lua_State* tolua_S) tolua_function(tolua_S,"resumeEventListenersForTarget",lua_cocos2dx_EventDispatcher_resumeEventListenersForTarget); tolua_function(tolua_S,"removeEventListenersForTarget",lua_cocos2dx_EventDispatcher_removeEventListenersForTarget); tolua_function(tolua_S,"setPriority",lua_cocos2dx_EventDispatcher_setPriority); - tolua_function(tolua_S,"addCustomEventListener",lua_cocos2dx_EventDispatcher_addCustomEventListener); tolua_function(tolua_S,"dispatchEvent",lua_cocos2dx_EventDispatcher_dispatchEvent); tolua_function(tolua_S,"removeAllEventListeners",lua_cocos2dx_EventDispatcher_removeAllEventListeners); tolua_function(tolua_S,"removeCustomEventListeners",lua_cocos2dx_EventDispatcher_removeCustomEventListeners); diff --git a/cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.cpp b/cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.cpp index 96ade969f961..320ee5a025fa 100644 --- a/cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.cpp +++ b/cocos/scripting/lua-bindings/manual/cocos2d/lua_cocos2dx_manual.cpp @@ -32,6 +32,127 @@ #include #endif + + +struct LuaCustomEventListener { + LuaCustomEventListener(lua_State* state, int index): L(state), ref(LUA_NOREF) + { + luaL_checktype(L, index, LUA_TFUNCTION); + lua_pushvalue(L, index); + ref = luaL_ref(L, LUA_REGISTRYINDEX); + } + ~LuaCustomEventListener() + { + unref(); + } + + void operator()(cocos2d::EventCustom* e) + { + lua_rawgeti(L, LUA_REGISTRYINDEX, ref); + + object_to_luaval(L, "cc.EventCustom", e); + lua_call(L, 1, 0); + } + + LuaCustomEventListener(const LuaCustomEventListener& other): L(nullptr), ref(LUA_NOREF) + { + *this = other; + } + LuaCustomEventListener& operator=(const LuaCustomEventListener& rhs) + { + if (this != &rhs) + { + unref(); + L = rhs.L; + lua_rawgeti(L, LUA_REGISTRYINDEX, rhs.ref); + ref = luaL_ref(L, LUA_REGISTRYINDEX); + } + return *this; + } + + LuaCustomEventListener(LuaCustomEventListener&& other): L(nullptr), ref(LUA_NOREF) + { + *this = std::move(other); + } + + LuaCustomEventListener& operator=(LuaCustomEventListener&& rhs) + { + if (this != &rhs) + { + unref(); + + L = rhs.L; + ref = rhs.ref; + + rhs.L = nullptr; + rhs.ref = LUA_NOREF; + } + return *this; + } +private: + inline void unref() { + if (L && ref != LUA_NOREF && ref != LUA_REFNIL) + luaL_unref(L, LUA_REGISTRYINDEX, ref); + } + + lua_State* L; + int ref; +}; + +int lua_cocos2dx_EventDispatcher_addCustomEventListener(lua_State* tolua_S) +{ + int argc = 0; + cocos2d::EventDispatcher* cobj = nullptr; + bool ok = true; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; +#endif + + +#if COCOS2D_DEBUG >= 1 + if (!tolua_isusertype(tolua_S,1,"cc.EventDispatcher",0,&tolua_err)) goto tolua_lerror; +#endif + + cobj = (cocos2d::EventDispatcher*)tolua_tousertype(tolua_S,1,0); + +#if COCOS2D_DEBUG >= 1 + if (!cobj) + { + tolua_error(tolua_S,"invalid 'cobj' in function 'lua_cocos2dx_EventDispatcher_addCustomEventListener'", nullptr); + return 0; + } +#endif + + argc = lua_gettop(tolua_S)-1; + if (argc == 2) + { + std::string arg0; + + ok &= luaval_to_std_string(tolua_S, 2,&arg0, "cc.EventDispatcher:addCustomEventListener"); + auto callback = LuaCustomEventListener(tolua_S, 3); + + if(!ok) + { + tolua_error(tolua_S,"invalid arguments in function 'lua_cocos2dx_EventDispatcher_addCustomEventListener'", nullptr); + return 0; + } + cocos2d::EventListenerCustom* ret = cobj->addCustomEventListener(arg0, std::function(std::move(callback))); + object_to_luaval(tolua_S, "cc.EventListenerCustom",(cocos2d::EventListenerCustom*)ret); + return 1; + } + luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "cc.EventDispatcher:addCustomEventListener",argc, 2); + return 0; + +#if COCOS2D_DEBUG >= 1 + tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_EventDispatcher_addCustomEventListener'.",&tolua_err); +#endif + + return 0; +} + + static int tolua_cocos2d_MenuItemImage_create(lua_State* tolua_S) { if (nullptr == tolua_S) @@ -8327,6 +8448,10 @@ int register_all_cocos2dx_module_manual(lua_State* tolua_S) tolua_function(tolua_S, "captureScreen", tolua_cocos2d_utils_captureScreen); tolua_function(tolua_S, "findChildren", tolua_cocos2d_utils_findChildren); tolua_endmodule(tolua_S); + tolua_module(tolua_S, "EventDispatcher", 0); + tolua_beginmodule(tolua_S,"EventDispatcher"); + tolua_function(tolua_S,"addCustomEventListener",lua_cocos2dx_EventDispatcher_addCustomEventListener); + tolua_endmodule(tolua_S); tolua_endmodule(tolua_S); return 0; diff --git a/tools/tolua/cocos2dx.ini b/tools/tolua/cocos2dx.ini index 9ff7d738263d..ceb3e4f9880c 100644 --- a/tools/tolua/cocos2dx.ini +++ b/tools/tolua/cocos2dx.ini @@ -111,7 +111,7 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS DisplayLinkDirector::[mainLoop setAnimationInterval startAnimation stopAnimation], RenderTexture::[listenToBackground listenToForeground], TMXTiledMap::[getPropertiesForGID], - EventDispatcher::[dispatchCustomEvent], + EventDispatcher::[addCustomEventListener dispatchCustomEvent], EventCustom::[getUserData setUserData], Component::[serialize], Console::[addCommand],