diff --git a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp index ff19c31618..e9c0598539 100644 --- a/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp +++ b/cocos/scripting/lua-bindings/manual/CCLuaStack.cpp @@ -160,6 +160,7 @@ bool LuaStack::init(void) register_all_cocos2dx_ui(_state); register_all_cocos2dx_studio(_state); register_all_cocos2dx_manual(_state); + register_all_cocos2dx_module_manual(_state); register_all_cocos2dx_extension_manual(_state); register_all_cocos2dx_coco_studio_manual(_state); register_all_cocos2dx_ui_manual(_state); diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp index edee61b00e..518658a019 100644 --- a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp @@ -6429,3 +6429,90 @@ int register_all_cocos2dx_manual(lua_State* tolua_S) return 0; } + +static int tolua_cocos2d_utils_captureScreen(lua_State* tolua_S) +{ +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_istable(tolua_S,1,0, &tolua_err) || + !toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err) || + !tolua_isstring(tolua_S, 3, 0, &tolua_err) + ) + goto tolua_lerror; + else +#endif + { + LUA_FUNCTION handler = toluafix_ref_function(tolua_S,2,0); + std::string fileName = tolua_tocppstring(tolua_S, 3, ""); + cocos2d::utils::captureScreen([=](bool succeed, const std::string& name ){ + + tolua_pushboolean(tolua_S, succeed); + tolua_pushstring(tolua_S, name.c_str()); + LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 2); + LuaEngine::getInstance()->removeScriptHandler(handler); + }, fileName); + + return 0; + } +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'captureScreen'.",&tolua_err); + return 0; +#endif +} + +static int tolua_cocos2d_utils_findChildren(lua_State* tolua_S) +{ +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_istable(tolua_S,1,0, &tolua_err) || + !tolua_isusertype(tolua_S, 2, "cc.Node", 0, &tolua_err) || + !tolua_isstring(tolua_S, 3, 0, &tolua_err) + ) + goto tolua_lerror; + else +#endif + { + cocos2d::Node* node = static_cast(tolua_tousertype(tolua_S, 2, nullptr)); + std::string name = tolua_tocppstring(tolua_S, 3, ""); + std::vector children = cocos2d::utils::findChildren(*node, name); + lua_newtable(tolua_S); + int index = 1; + for (const auto& obj : children) + { + if (nullptr == obj) + continue; + + lua_pushnumber(tolua_S, (lua_Number)index); + int ID = (obj) ? (int)obj->_ID : -1; + int* luaID = (obj) ? &obj->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, ID, luaID, (void*)obj, "cc.Node"); + lua_rawset(tolua_S, -3); + ++index; + } + return 1; + } +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'findChildren'.",&tolua_err); + return 0; +#endif +} + +int register_all_cocos2dx_module_manual(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + tolua_open(tolua_S); + tolua_module(tolua_S, "cc", 0); + tolua_beginmodule(tolua_S, "cc"); + tolua_module(tolua_S, "utils", 0); + tolua_beginmodule(tolua_S,"utils"); + tolua_function(tolua_S, "captureScreen", tolua_cocos2d_utils_captureScreen); + tolua_function(tolua_S, "findChildren", tolua_cocos2d_utils_findChildren); + tolua_endmodule(tolua_S); + tolua_endmodule(tolua_S); + + return 0; +} diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.hpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.hpp index d8dae90200..814b25578c 100644 --- a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.hpp +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.hpp @@ -55,6 +55,8 @@ TOLUA_API int register_all_cocos2dx_manual(lua_State* tolua_S); TOLUA_API int register_cocos2dx_event_releated(lua_State* tolua_S); +TOLUA_API int register_all_cocos2dx_module_manual(lua_State* tolua_S); + struct LuaEventAccelerationData { void* acc; diff --git a/tests/lua-tests/src/CaptureScreenTest/CaptureScreenTest.lua b/tests/lua-tests/src/CaptureScreenTest/CaptureScreenTest.lua new file mode 100644 index 0000000000..37d39ddc3a --- /dev/null +++ b/tests/lua-tests/src/CaptureScreenTest/CaptureScreenTest.lua @@ -0,0 +1,75 @@ +local winSize = cc.Director:getInstance():getWinSize() +local kTagSprite = 1 +local childTag = 119 + +local function createLayer() + local layer = cc.Layer:create() + local filename = "" + + local title = cc.Label:createWithTTF("New Renderer", "fonts/arial.ttf", 36) + title:setColor(cc.c3b(255,255,0)) + layer:addChild(title, 1, 10000) + title:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 30)) + + local subTitle = cc.Label:createWithTTF("Capture screen test, press the menu items to capture the screen", "fonts/arial.ttf", 12) + subTitle:setColor(cc.c3b(255,255,0)) + layer:addChild(subTitle, 1, 10001) + subTitle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 60) ) + + local left = cc.p(winSize.width / 4, winSize.height / 2) + local right = cc.p(winSize.width / 4 * 3, winSize.height / 2) + + local sp1 = cc.Sprite:create("Images/grossini.png") + sp1:setPosition(left) + local move1 = cc.MoveBy:create(1, cc.p(winSize.width/2, 0)) + local seq1 = cc.RepeatForever:create(cc.Sequence:create(move1, move1:reverse())) + layer:addChild(sp1) + sp1:runAction(seq1) + local sp2 = cc.Sprite:create("Images/grossinis_sister1.png") + sp2:setPosition(right) + local move2 = cc.MoveBy:create(1, cc.p(-winSize.width/2, 0)) + local seq2 = cc.RepeatForever:create(cc.Sequence:create(move2, move2:reverse())) + layer:addChild(sp2) + sp2:runAction(seq2) + + local function afterCaptured(succeed, outputFile) + if succeed then + local sp = cc.Sprite:create(outputFile) + layer:addChild(sp, 0, childTag) + sp:setPosition(winSize.width / 2, winSize.height / 2) + sp:setScale(0.25) + fileName = outputFile + else + cclog("Capture screen failed.") + end + end + + local function onCaptured(tag, sender) + cc.Director:getInstance():getTextureCache():removeTextureForKey(fileName) + layer:removeChildByTag(childTag) + fileName = "CaptureScreenTest.png" + cc.utils:captureScreen(afterCaptured, fileName) + end + + local ttfConfig = {} + ttfConfig.fontFilePath = "fonts/arial.ttf" + ttfConfig.fontSize = 24 + local label1 = cc.Label:createWithTTF(ttfConfig, "capture all", cc.TEXT_ALIGNMENT_CENTER, winSize.width) + local mi1 = cc.MenuItemLabel:create(label1) + mi1:registerScriptTapHandler(onCaptured) + local menu = cc.Menu:create(mi1) + layer:addChild(menu) + menu:setPosition(winSize.width / 2, winSize.height / 4) + + return layer +end + +-------------------------------- +-- CaptureScreen +-------------------------------- +function CaptureScreenTestMain() + local scene = cc.Scene:create() + scene:addChild(createLayer()) + scene:addChild(CreateBackMenuItem()) + return scene +end diff --git a/tests/lua-tests/src/mainMenu.lua b/tests/lua-tests/src/mainMenu.lua index ce3f86c60c..436b473cca 100644 --- a/tests/lua-tests/src/mainMenu.lua +++ b/tests/lua-tests/src/mainMenu.lua @@ -53,6 +53,7 @@ require "src/ZwoptexTest/ZwoptexTest" require "src/LuaBridgeTest/LuaBridgeTest" require "src/XMLHttpRequestTest/XMLHttpRequestTest" require "src/PhysicsTest/PhysicsTest" +require "src/CaptureScreenTest/CaptureScreenTest" local LINE_SPACE = 40 @@ -72,6 +73,7 @@ local _allTests = { { isSupported = false, name = "Box2dTestBed" , create_func= Box2dTestBedMain }, { isSupported = true, name = "BugsTest" , create_func= BugsTestMain }, { isSupported = true, name = "ByteCodeEncryptTest" , create_func= ByteCodeEncryptTestMain }, + { isSupported = true, name = "CaptureScreenTest" , create_func = CaptureScreenTestMain }, { isSupported = false, name = "ChipmunkAccelTouchTest" , create_func= ChipmunkAccelTouchTestMain }, { isSupported = true, name = "ClickAndMoveTest" , create_func = ClickAndMoveTest }, { isSupported = true, name = "CocosDenshionTest" , create_func = CocosDenshionTestMain },