From 5c765f6883023eca149feb3f094d2bdd2d116855 Mon Sep 17 00:00:00 2001 From: samuele3hu Date: Wed, 25 Jun 2014 15:08:59 +0800 Subject: [PATCH] Add coco studio ui focus test cases for Lua --- .../lua-bindings/manual/LuaScriptHandlerMgr.h | 2 + .../manual/lua_cocos2dx_manual.cpp | 178 ++++ .../CocoStudioGUITest/CocoStudioGUITest.lua | 888 ++++++++++++++++++ tools/tolua/cocos2dx.ini | 2 +- tools/tolua/cocos2dx_ui.ini | 2 +- 5 files changed, 1070 insertions(+), 2 deletions(-) diff --git a/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.h b/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.h index df49bf50e0..afe3017b63 100644 --- a/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.h +++ b/cocos/scripting/lua-bindings/manual/LuaScriptHandlerMgr.h @@ -161,6 +161,8 @@ public: EVENT_PHYSICS_CONTACT_POSTSOLVE, EVENT_PHYSICS_CONTACT_SEPERATE, + EVENT_FOCUS, + EVENT_CUSTOM_BEGAN = 10000, EVENT_CUSTOM_ENDED = 11000, }; diff --git a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp index 6c4076f154..115d67d617 100644 --- a/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp +++ b/cocos/scripting/lua-bindings/manual/lua_cocos2dx_manual.cpp @@ -31,6 +31,7 @@ #else #include #endif +#include "ui/UIWidget.h" static int tolua_cocos2d_MenuItemImage_create(lua_State* tolua_S) { @@ -6134,6 +6135,182 @@ static void extendTMXLayer(lua_State* tolua_S) lua_pop(tolua_S, 1); } +static int tolua_cocos2dx_EventListenerFocus_create(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + int argc = 0; +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertable(tolua_S, 1, "cc.EventListenerFocus", 0, &tolua_err)) goto tolua_lerror; +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 0) + { + cocos2d::EventListenerFocus* tolua_ret = cocos2d::EventListenerFocus::create(); + if(nullptr == tolua_ret) + return 0; + + int ID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* luaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, ID, luaID, (void*)tolua_ret,"cc.EventListenerFocus"); + + return 1; + } + + CCLOG("'create' has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'create'.",&tolua_err); + return 0; +#endif +} + +static void cloneFocusHandler(const EventListenerFocus* src,EventListenerFocus* dst ) +{ + if (nullptr == src || nullptr == dst) + return; + + LUA_FUNCTION handler = ScriptHandlerMgr::getInstance()->getObjectHandler((void*)src, ScriptHandlerMgr::HandlerType::EVENT_FOCUS); + if (0 != handler) + { + int newscriptHandler = cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->reallocateScriptHandler(handler); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)dst, newscriptHandler, ScriptHandlerMgr::HandlerType::EVENT_FOCUS); + dst->onFocusChanged = [=](ui::Widget* widgetLostFocus, ui::Widget* widgetGetFocus){ + lua_State* tolua_S = LuaEngine::getInstance()->getLuaStack()->getLuaState(); + int id = (widgetLostFocus) ? (int)widgetLostFocus->_ID : -1; + int* luaID = (widgetLostFocus) ? &widgetLostFocus->_luaID : nullptr; + toluafix_pushusertype_ccobject(tolua_S, id, luaID, (void*)widgetLostFocus,"ccui.Widget"); + id = (widgetGetFocus) ? (int)widgetGetFocus->_ID : -1; + luaID = (widgetGetFocus) ? &widgetGetFocus->_luaID : nullptr; + toluafix_pushusertype_ccobject(tolua_S, id, luaID, (void*)widgetGetFocus,"ccui.Widget"); + LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 2); + }; + } +} + +static int tolua_cocos2dx_EventListenerFocus_clone(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + int argc = 0; + EventListenerFocus* self = nullptr; +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S, 1, "cc.EventListenerFocus", 0, &tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2dx_EventListenerFocus_clone'\n", nullptr); + return 0; + } +#endif + + argc = lua_gettop(tolua_S) - 1; + + if (argc == 0) + { + cocos2d::EventListenerFocus* tolua_ret = cocos2d::EventListenerFocus::create(); + if(nullptr == tolua_ret) + return 0; + + cloneFocusHandler(self, tolua_ret); + + int ID = (tolua_ret) ? (int)tolua_ret->_ID : -1; + int* luaID = (tolua_ret) ? &tolua_ret->_luaID : NULL; + toluafix_pushusertype_ccobject(tolua_S, ID, luaID, (void*)tolua_ret,"cc.EventListenerFocus"); + + return 1; + } + + CCLOG("'clone' has wrong number of arguments: %d, was expecting %d\n", argc, 0); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'clone'.",&tolua_err); + return 0; +#endif +} + + +static int tolua_cocos2dx_EventListenerFocus_registerScriptHandler(lua_State* tolua_S) +{ + if (nullptr == tolua_S) + return 0; + + int argc = 0; + EventListenerFocus* self = nullptr; +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(tolua_S, 1, "cc.EventListenerFocus", 0, &tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(tolua_S,1,0)); +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(tolua_S,"invalid 'self' in function 'tolua_cocos2dx_EventListenerFocus_registerScriptHandler'\n", nullptr); + return 0; + } +#endif + argc = lua_gettop(tolua_S) - 1; + + if (argc == 1) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(tolua_S,2,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + LUA_FUNCTION handler = toluafix_ref_function(tolua_S,2,0); + + ScriptHandlerMgr::getInstance()->addObjectHandler((void*)self, handler, ScriptHandlerMgr::HandlerType::EVENT_FOCUS); + + self->onFocusChanged = [=](ui::Widget* widgetLostFocus, ui::Widget* widgetGetFocus){ + int id = (widgetLostFocus) ? (int)widgetLostFocus->_ID : -1; + int* luaID = (widgetLostFocus) ? &widgetLostFocus->_luaID : nullptr; + toluafix_pushusertype_ccobject(tolua_S, id, luaID, (void*)widgetLostFocus,"ccui.Widget"); + id = (widgetGetFocus) ? (int)widgetGetFocus->_ID : -1; + luaID = (widgetGetFocus) ? &widgetGetFocus->_luaID : nullptr; + toluafix_pushusertype_ccobject(tolua_S, id, luaID, (void*)widgetGetFocus,"ccui.Widget"); + LuaEngine::getInstance()->getLuaStack()->executeFunctionByHandler(handler, 2); + }; + return 0; + } + + CCLOG("'registerScriptHandler' has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(tolua_S,"#ferror in function 'registerScriptHandler'.",&tolua_err); + return 0; +#endif +} + +static void extendEventListenerFocus(lua_State* tolua_S) +{ + lua_pushstring(tolua_S, "cc.EventListenerFocus"); + lua_rawget(tolua_S, LUA_REGISTRYINDEX); + if (lua_istable(tolua_S,-1)) + { + tolua_function(tolua_S, "create", tolua_cocos2dx_EventListenerFocus_create); + tolua_function(tolua_S, "registerScriptHandler", tolua_cocos2dx_EventListenerFocus_registerScriptHandler); + tolua_function(tolua_S, "clone", tolua_cocos2dx_EventListenerFocus_clone); + } + lua_pop(tolua_S, 1); +} + int register_all_cocos2dx_manual(lua_State* tolua_S) { @@ -6186,6 +6363,7 @@ int register_all_cocos2dx_manual(lua_State* tolua_S) extendGLProgramState(tolua_S); extendOrbitCamera(tolua_S); extendTMXLayer(tolua_S); + extendEventListenerFocus(tolua_S); return 0; } diff --git a/tests/lua-tests/src/CocoStudioTest/CocoStudioGUITest/CocoStudioGUITest.lua b/tests/lua-tests/src/CocoStudioTest/CocoStudioGUITest/CocoStudioGUITest.lua index bd26c09888..b6b8ea1157 100644 --- a/tests/lua-tests/src/CocoStudioTest/CocoStudioGUITest/CocoStudioGUITest.lua +++ b/tests/lua-tests/src/CocoStudioTest/CocoStudioGUITest/CocoStudioGUITest.lua @@ -2960,8 +2960,896 @@ function UIRichTextTest.create() return scene end +local UIFocusTestHorizontal = class("UIFocusTestHorizontal",UIScene) +function UIFocusTestHorizontal.extend( target ) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, UIFocusTestHorizontal) + return target +end + +function UIFocusTestHorizontal:initExtend() + self:init() + + local root = self._uiLayer:getChildByTag(81) + local background = root:getChildByName("background_Panel") + background:removeFromParent(true) + + local function onLeftKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_LEFT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onRightKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_RIGHT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onUpKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_UP, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onDownKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_DOWN, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + self._dpadMenu = cc.Menu:create() + local winSize = cc.Director:getInstance():getVisibleSize() + + local leftItem = cc.MenuItemFont:create("Left") + leftItem:setPosition(cc.p(winSize.width - 100, winSize.height/2)) + leftItem:registerScriptTapHandler(onLeftKeyPressed) + self._dpadMenu:addChild(leftItem) + + local rightItem = cc.MenuItemFont:create("Right") + rightItem:setPosition(cc.p(winSize.width - 30, winSize.height/2)) + rightItem:registerScriptTapHandler(onRightKeyPressed) + self._dpadMenu:addChild(rightItem) + + local upItem = cc.MenuItemFont:create("Up") + upItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 + 50)) + upItem:registerScriptTapHandler(onUpKeyPressed) + self._dpadMenu:addChild(upItem) + + local downItem = cc.MenuItemFont:create("Down") + downItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 - 50)) + downItem:registerScriptTapHandler(onDownKeyPressed) + self._dpadMenu:addChild(downItem) + self._dpadMenu:setPosition(cc.p(0, 0)) + self._uiLayer:addChild(self._dpadMenu) + + ccui.Widget:enableDpadNavigation(true) + + local function onFocusChanged(widgetLostFocus,widgetGetFocus) + if nil ~= widgetGetFocus and widgetGetFocus:isFocusEnabled() then + widgetGetFocus:setColor(cc.c3b(255, 0, 0)) + end + + if nil ~= widgetLostFocus and widgetLostFocus:isFocusEnabled() then + widgetLostFocus:setColor(cc.c3b(255, 255, 255)) + end + + if nil ~= widgetLostFocus and nil ~= widgetGetFocus then + print(string.format("on focus change, %d widget get focus, %d widget lose focus", widgetGetFocus:getTag(), widgetLostFocus:getTag())) + end + end + + local eventListener = cc.EventListenerFocus:create() + eventListener:registerScriptHandler(onFocusChanged) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithFixedPriority(eventListener, 1) + + local winSize = cc.Director:getInstance():getVisibleSize() + self._horizontalLayout = ccui.HBox:create() + self._horizontalLayout:setPosition(cc.p(20, winSize.height / 2 + 40)) + self._uiLayer:addChild(self._horizontalLayout) + + self._horizontalLayout:setFocused(true) + self._horizontalLayout:setLoopFocus(true) + self._horizontalLayout:setTag(100) + self._firstFocusedWidget = self._horizontalLayout + + local function onImageViewClicked(sender, eventType) + if eventType == ccui.TouchEventType.ended then + local w = sender + if w:isFocusEnabled() then + w:setFocusEnabled(false) + w:setColor(cc.c3b(255, 255, 0)) + else + w:setFocusEnabled(true) + w:setColor(cc.c3b(255, 255, 255)) + end + end + end + + local count = 3 + for i = 1, count do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setTouchEnabled(true) + imageView:setTag(i) + imageView:addTouchEventListener(onImageViewClicked) + self._horizontalLayout:addChild(imageView) + end + + self._loopText = ccui.Text:create("loop enabled", "Airal", 20) + self._loopText:setPosition(cc.p(winSize.width/2, winSize.height - 50)) + self._loopText:setColor(cc.c3b(255, 0 ,0)) + self:addChild(self._loopText) + + local function toggleFocusLoop(sender, eventType) + if eventType == ccui.TouchEventType.ended then + self._horizontalLayout:setLoopFocus(not self._horizontalLayout:isLoopFocus()) + if self._horizontalLayout:isLoopFocus() then + self._loopText:setString("loop enabled") + else + self._loopText:setString("loop disabled") + end + end + end + + local btn = ccui.Button:create("cocosui/switch-mask.png") + btn:setTitleText("Toggle Loop") + btn:setPosition(cc.p(60, winSize.height - 50)) + btn:setTitleColor(cc.c3b(0, 255, 0)) + btn:addTouchEventListener(toggleFocusLoop) + self:addChild(btn) +end + +function UIFocusTestHorizontal.create() + local scene = cc.Scene:create() + local layer = UIFocusTestHorizontal.extend(cc.Layer:create()) + layer:initExtend() + scene:addChild(layer) + return scene +end + +local UIFocusTestVertical = class("UIFocusTestVertical",UIScene) + +function UIFocusTestVertical.extend( target ) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, UIFocusTestVertical) + return target +end + +function UIFocusTestVertical:initExtend() + self:init() + + local root = self._uiLayer:getChildByTag(81) + local background = root:getChildByName("background_Panel") + background:removeFromParent(true) + + local function onLeftKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_LEFT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onRightKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_RIGHT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onUpKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_UP, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onDownKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_DOWN, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + self._dpadMenu = cc.Menu:create() + local winSize = cc.Director:getInstance():getVisibleSize() + + local leftItem = cc.MenuItemFont:create("Left") + leftItem:setPosition(cc.p(winSize.width - 100, winSize.height/2)) + leftItem:registerScriptTapHandler(onLeftKeyPressed) + self._dpadMenu:addChild(leftItem) + + local rightItem = cc.MenuItemFont:create("Right") + rightItem:setPosition(cc.p(winSize.width - 30, winSize.height/2)) + rightItem:registerScriptTapHandler(onRightKeyPressed) + self._dpadMenu:addChild(rightItem) + + local upItem = cc.MenuItemFont:create("Up") + upItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 + 50)) + upItem:registerScriptTapHandler(onUpKeyPressed) + self._dpadMenu:addChild(upItem) + + local downItem = cc.MenuItemFont:create("Down") + downItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 - 50)) + downItem:registerScriptTapHandler(onDownKeyPressed) + self._dpadMenu:addChild(downItem) + self._dpadMenu:setPosition(cc.p(0, 0)) + self._uiLayer:addChild(self._dpadMenu) + + ccui.Widget:enableDpadNavigation(true) + + local function onFocusChanged(widgetLostFocus,widgetGetFocus) + if nil ~= widgetGetFocus and widgetGetFocus:isFocusEnabled() then + widgetGetFocus:setColor(cc.c3b(255, 0, 0)) + end + + if nil ~= widgetLostFocus and widgetLostFocus:isFocusEnabled() then + widgetLostFocus:setColor(cc.c3b(255, 255, 255)) + end + + if nil ~= widgetLostFocus and nil ~= widgetGetFocus then + print(string.format("on focus change, %d widget get focus, %d widget lose focus", widgetGetFocus:getTag(), widgetLostFocus:getTag())) + end + end + + local eventListener = cc.EventListenerFocus:create() + eventListener:registerScriptHandler(onFocusChanged) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithFixedPriority(eventListener, 1) + + local winSize = cc.Director:getInstance():getVisibleSize() + self._verticalLayout = ccui.VBox:create() + self._verticalLayout:setPosition(cc.p(winSize.width/2 - 100, winSize.height - 70)) + self._uiLayer:addChild(self._verticalLayout) + + self._verticalLayout:setFocused(true) + self._verticalLayout:setLoopFocus(true) + self._verticalLayout:setTag(100) + self._verticalLayout:setScale(0.5) + self._firstFocusedWidget = self._verticalLayout + + local function onImageViewClicked(sender, eventType) + if eventType == ccui.TouchEventType.ended then + local w = sender + if w:isFocusEnabled() then + w:setFocusEnabled(false) + w:setColor(cc.c3b(255, 255, 0)) + else + w:setFocusEnabled(true) + w:setColor(cc.c3b(255, 255, 255)) + end + end + end + + local count = 3 + for i = 1, count do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setTouchEnabled(true) + imageView:setTag(i) + imageView:addTouchEventListener(onImageViewClicked) + self._verticalLayout:addChild(imageView) + end + + self._loopText = ccui.Text:create("loop enabled", "Airal", 20) + self._loopText:setPosition(cc.p(winSize.width/2, winSize.height - 50)) + self._loopText:setColor(cc.c3b(255, 0 ,0)) + self:addChild(self._loopText) + + local function toggleFocusLoop(sender, eventType) + if eventType == ccui.TouchEventType.ended then + self._verticalLayout:setLoopFocus(not self._verticalLayout:isLoopFocus()) + if self._verticalLayout:isLoopFocus() then + self._loopText:setString("loop enabled") + else + self._loopText:setString("loop disabled") + end + end + end + + local btn = ccui.Button:create("cocosui/switch-mask.png") + btn:setTitleText("Toggle Loop") + btn:setPosition(cc.p(60, winSize.height - 50)) + btn:setTitleColor(cc.c3b(0, 255, 0)) + btn:addTouchEventListener(toggleFocusLoop) + self:addChild(btn) +end + +function UIFocusTestVertical.create() + local scene = cc.Scene:create() + local layer = UIFocusTestVertical.extend(cc.Layer:create()) + layer:initExtend() + scene:addChild(layer) + return scene +end + +local UIFocusTestNestedLayout1 = class("UIFocusTestNestedLayout1",UIScene) + +function UIFocusTestNestedLayout1.extend( target ) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, UIFocusTestNestedLayout1) + return target +end + +function UIFocusTestNestedLayout1:initExtend() + self:init() + + local root = self._uiLayer:getChildByTag(81) + local background = root:getChildByName("background_Panel") + background:removeFromParent(true) + + local function onLeftKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_LEFT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onRightKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_RIGHT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onUpKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_UP, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onDownKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_DOWN, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + self._dpadMenu = cc.Menu:create() + local winSize = cc.Director:getInstance():getVisibleSize() + + local leftItem = cc.MenuItemFont:create("Left") + leftItem:setPosition(cc.p(winSize.width - 100, winSize.height/2)) + leftItem:registerScriptTapHandler(onLeftKeyPressed) + self._dpadMenu:addChild(leftItem) + + local rightItem = cc.MenuItemFont:create("Right") + rightItem:setPosition(cc.p(winSize.width - 30, winSize.height/2)) + rightItem:registerScriptTapHandler(onRightKeyPressed) + self._dpadMenu:addChild(rightItem) + + local upItem = cc.MenuItemFont:create("Up") + upItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 + 50)) + upItem:registerScriptTapHandler(onUpKeyPressed) + self._dpadMenu:addChild(upItem) + + local downItem = cc.MenuItemFont:create("Down") + downItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 - 50)) + downItem:registerScriptTapHandler(onDownKeyPressed) + self._dpadMenu:addChild(downItem) + self._dpadMenu:setPosition(cc.p(0, 0)) + self._uiLayer:addChild(self._dpadMenu) + + ccui.Widget:enableDpadNavigation(true) + + local function onFocusChanged(widgetLostFocus,widgetGetFocus) + if nil ~= widgetGetFocus and widgetGetFocus:isFocusEnabled() then + widgetGetFocus:setColor(cc.c3b(255, 0, 0)) + end + + if nil ~= widgetLostFocus and widgetLostFocus:isFocusEnabled() then + widgetLostFocus:setColor(cc.c3b(255, 255, 255)) + end + + if nil ~= widgetLostFocus and nil ~= widgetGetFocus then + print(string.format("on focus change, %d widget get focus, %d widget lose focus", widgetGetFocus:getTag(), widgetLostFocus:getTag())) + end + end + + local eventListener = cc.EventListenerFocus:create() + eventListener:registerScriptHandler(onFocusChanged) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithFixedPriority(eventListener, 1) + + local winSize = cc.Director:getInstance():getVisibleSize() + + self._verticalLayout = ccui.VBox:create() + self._verticalLayout:setPosition(cc.p(winSize.width/2 - 80, winSize.height - 70)) + self._uiLayer:addChild(self._verticalLayout) + self._verticalLayout:setScale(0.5) + + self._verticalLayout:setFocused(true) + self._verticalLayout:setLoopFocus(true) + self._verticalLayout:setTag(100) + self._firstFocusedWidget = self._verticalLayout + + local function onImageViewClicked(sender, eventType) + if eventType == ccui.TouchEventType.ended then + local w = sender + if w:isFocusEnabled() then + w:setFocusEnabled(false) + w:setColor(cc.c3b(255, 255, 0)) + else + w:setFocusEnabled(true) + w:setColor(cc.c3b(255, 255, 255)) + end + end + end + + local count1 = 1 + for i = 1, count1 do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setAnchorPoint(cc.p(0, 0)) + imageView:setTouchEnabled(true) + imageView:setScaleX(2.5) + imageView:setTag(i - 1 + count1) + imageView:addTouchEventListener(onImageViewClicked) + self._verticalLayout:addChild(imageView) + end + + local hbox = ccui.HBox:create() + hbox:setScale(0.8) + hbox:setTag(101) + self._verticalLayout:addChild(hbox) + + local count2 = 2 + for i = 1, count2 do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setAnchorPoint(cc.p(0, 1)) + imageView:setScaleY(2.0) + imageView:setTouchEnabled(true) + imageView:setTag(i - 1 + count1 + count2) + imageView:addTouchEventListener(onImageViewClicked) + hbox:addChild(imageView) + end + + local innerVBox = ccui.VBox:create() + hbox:addChild(innerVBox) + innerVBox:setTag(102) + + local count3 = 2 + for i = 1, count3 do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setTouchEnabled(true) + imageView:setTag(i - 1 + count1 + count2 + count3) + imageView:addTouchEventListener(onImageViewClicked) + innerVBox:addChild(imageView) + end + + self._loopText = ccui.Text:create("loop enabled", "Airal", 20) + self._loopText:setPosition(cc.p(winSize.width/2, winSize.height - 50)) + self._loopText:setColor(cc.c3b(255, 0 ,0)) + self:addChild(self._loopText) + + local function toggleFocusLoop(sender, eventType) + if eventType == ccui.TouchEventType.ended then + self._verticalLayout:setLoopFocus(not self._verticalLayout:isLoopFocus()) + if self._verticalLayout:isLoopFocus() then + self._loopText:setString("loop enabled") + else + self._loopText:setString("loop disabled") + end + end + end + + local btn = ccui.Button:create("cocosui/switch-mask.png") + btn:setTitleText("Toggle Loop") + btn:setPosition(cc.p(60, winSize.height - 50)) + btn:setTitleColor(cc.c3b(0, 255, 0)) + btn:addTouchEventListener(toggleFocusLoop) + self:addChild(btn) +end + +function UIFocusTestNestedLayout1.create() + local scene = cc.Scene:create() + local layer = UIFocusTestNestedLayout1.extend(cc.Layer:create()) + layer:initExtend() + scene:addChild(layer) + return scene +end + +local UIFocusTestNestedLayout2 = class("UIFocusTestNestedLayout2",UIScene) + +function UIFocusTestNestedLayout2.extend( target ) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, UIFocusTestNestedLayout2) + return target +end + +function UIFocusTestNestedLayout2:initExtend() + self:init() + + local root = self._uiLayer:getChildByTag(81) + local background = root:getChildByName("background_Panel") + background:removeFromParent(true) + + local function onLeftKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_LEFT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onRightKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_RIGHT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onUpKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_UP, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onDownKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_DOWN, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + self._dpadMenu = cc.Menu:create() + local winSize = cc.Director:getInstance():getVisibleSize() + + local leftItem = cc.MenuItemFont:create("Left") + leftItem:setPosition(cc.p(winSize.width - 100, winSize.height/2)) + leftItem:registerScriptTapHandler(onLeftKeyPressed) + self._dpadMenu:addChild(leftItem) + + local rightItem = cc.MenuItemFont:create("Right") + rightItem:setPosition(cc.p(winSize.width - 30, winSize.height/2)) + rightItem:registerScriptTapHandler(onRightKeyPressed) + self._dpadMenu:addChild(rightItem) + + local upItem = cc.MenuItemFont:create("Up") + upItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 + 50)) + upItem:registerScriptTapHandler(onUpKeyPressed) + self._dpadMenu:addChild(upItem) + + local downItem = cc.MenuItemFont:create("Down") + downItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 - 50)) + downItem:registerScriptTapHandler(onDownKeyPressed) + self._dpadMenu:addChild(downItem) + self._dpadMenu:setPosition(cc.p(0, 0)) + self._uiLayer:addChild(self._dpadMenu) + + ccui.Widget:enableDpadNavigation(true) + + local function onFocusChanged(widgetLostFocus,widgetGetFocus) + if nil ~= widgetGetFocus and widgetGetFocus:isFocusEnabled() then + widgetGetFocus:setColor(cc.c3b(255, 0, 0)) + end + + if nil ~= widgetLostFocus and widgetLostFocus:isFocusEnabled() then + widgetLostFocus:setColor(cc.c3b(255, 255, 255)) + end + + if nil ~= widgetLostFocus and nil ~= widgetGetFocus then + print(string.format("on focus change, %d widget get focus, %d widget lose focus", widgetGetFocus:getTag(), widgetLostFocus:getTag())) + end + end + + local eventListener = cc.EventListenerFocus:create() + eventListener:registerScriptHandler(onFocusChanged) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithFixedPriority(eventListener, 1) + + local winSize = cc.Director:getInstance():getVisibleSize() + + self._horizontalLayout = ccui.HBox:create() + self._horizontalLayout:setPosition(cc.p(winSize.width/2 - 200, winSize.height - 70)) + self._uiLayer:addChild(self._horizontalLayout) + self._horizontalLayout:setScale(0.6) + + self._horizontalLayout:setFocused(true) + self._horizontalLayout:setLoopFocus(true) + self._horizontalLayout:setTag(100) + self._firstFocusedWidget = self._horizontalLayout + + local function onImageViewClicked(sender, eventType) + if eventType == ccui.TouchEventType.ended then + local w = sender + if w:isFocusEnabled() then + w:setFocusEnabled(false) + w:setColor(cc.c3b(255, 255, 0)) + else + w:setFocusEnabled(true) + w:setColor(cc.c3b(255, 255, 255)) + end + end + end + + local count1 = 2 + for i = 1, count1 do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setAnchorPoint(cc.p(0, 1)) + imageView:setTouchEnabled(true) + imageView:setScaleY(2.4) + imageView:setTag(i - 1 + count1) + imageView:addTouchEventListener(onImageViewClicked) + self._horizontalLayout:addChild(imageView) + end + + local vbox = ccui.VBox:create() + vbox:setScale(0.8) + vbox:setTag(101) + self._horizontalLayout:addChild(vbox) + + local count2 = 2 + for i = 1, count2 do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setAnchorPoint(cc.p(0, 1)) + imageView:setScaleX(2.0) + imageView:setTouchEnabled(true) + imageView:setTag(i - 1 + count1 + count2) + imageView:addTouchEventListener(onImageViewClicked) + vbox:addChild(imageView) + end + + local innerVBox = ccui.HBox:create() + vbox:addChild(innerVBox) + innerVBox:setTag(102) + + local count3 = 2 + for i = 1, count3 do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setTouchEnabled(true) + imageView:setTag(i - 1 + count1 + count2 + count3) + imageView:addTouchEventListener(onImageViewClicked) + innerVBox:addChild(imageView) + end + + self._loopText = ccui.Text:create("loop enabled", "Airal", 20) + self._loopText:setPosition(cc.p(winSize.width/2, winSize.height - 50)) + self._loopText:setColor(cc.c3b(255, 0 ,0)) + self:addChild(self._loopText) + + local function toggleFocusLoop(sender, eventType) + if eventType == ccui.TouchEventType.ended then + self._horizontalLayout:setLoopFocus(not self._horizontalLayout:isLoopFocus()) + if self._horizontalLayout:isLoopFocus() then + self._loopText:setString("loop enabled") + else + self._loopText:setString("loop disabled") + end + end + end + + local btn = ccui.Button:create("cocosui/switch-mask.png") + btn:setTitleText("Toggle Loop") + btn:setPosition(cc.p(60, winSize.height - 50)) + btn:setTitleColor(cc.c3b(0, 255, 0)) + btn:addTouchEventListener(toggleFocusLoop) + self:addChild(btn) +end + +function UIFocusTestNestedLayout2.create() + local scene = cc.Scene:create() + local layer = UIFocusTestNestedLayout2.extend(cc.Layer:create()) + layer:initExtend() + scene:addChild(layer) + return scene +end + +local UIFocusTestNestedLayout3 = class("UIFocusTestNestedLayout3",UIScene) + +function UIFocusTestNestedLayout3.extend( target ) + local t = tolua.getpeer(target) + if not t then + t = {} + tolua.setpeer(target, t) + end + setmetatable(t, UIFocusTestNestedLayout3) + return target +end + +function UIFocusTestNestedLayout3:initExtend() + self:init() + + local root = self._uiLayer:getChildByTag(81) + local background = root:getChildByName("background_Panel") + background:removeFromParent(true) + + local function onLeftKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_LEFT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onRightKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_RIGHT, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onUpKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_UP, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + local function onDownKeyPressed() + local event = cc.EventKeyboard:new(cc.KeyCode.KEY_DPAD_DOWN, false) + cc.Director:getInstance():getEventDispatcher():dispatchEvent(event) + end + + self._dpadMenu = cc.Menu:create() + local winSize = cc.Director:getInstance():getVisibleSize() + + local leftItem = cc.MenuItemFont:create("Left") + leftItem:setPosition(cc.p(winSize.width - 100, winSize.height/2)) + leftItem:registerScriptTapHandler(onLeftKeyPressed) + self._dpadMenu:addChild(leftItem) + + local rightItem = cc.MenuItemFont:create("Right") + rightItem:setPosition(cc.p(winSize.width - 30, winSize.height/2)) + rightItem:registerScriptTapHandler(onRightKeyPressed) + self._dpadMenu:addChild(rightItem) + + local upItem = cc.MenuItemFont:create("Up") + upItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 + 50)) + upItem:registerScriptTapHandler(onUpKeyPressed) + self._dpadMenu:addChild(upItem) + + local downItem = cc.MenuItemFont:create("Down") + downItem:setPosition(cc.p(winSize.width - 60, winSize.height/2 - 50)) + downItem:registerScriptTapHandler(onDownKeyPressed) + self._dpadMenu:addChild(downItem) + self._dpadMenu:setPosition(cc.p(0, 0)) + self._uiLayer:addChild(self._dpadMenu) + + ccui.Widget:enableDpadNavigation(true) + + local function onFocusChanged(widgetLostFocus,widgetGetFocus) + if nil ~= widgetGetFocus and widgetGetFocus:isFocusEnabled() then + widgetGetFocus:setColor(cc.c3b(255, 0, 0)) + end + + if nil ~= widgetLostFocus and widgetLostFocus:isFocusEnabled() then + widgetLostFocus:setColor(cc.c3b(255, 255, 255)) + end + + if nil ~= widgetLostFocus and nil ~= widgetGetFocus then + print(string.format("on focus change, %d widget get focus, %d widget lose focus", widgetGetFocus:getTag(), widgetLostFocus:getTag())) + end + end + + local eventListener = cc.EventListenerFocus:create() + eventListener:registerScriptHandler(onFocusChanged) + local eventDispatcher = self:getEventDispatcher() + eventDispatcher:addEventListenerWithFixedPriority(eventListener, 1) + + local winSize = cc.Director:getInstance():getVisibleSize() + + self._verticalLayout = ccui.VBox:create() + self._verticalLayout:setPosition(cc.p(40, winSize.height - 70)) + self._uiLayer:addChild(self._verticalLayout) + self._verticalLayout:setScale(0.8) + + self._verticalLayout:setFocused(true) + self._verticalLayout:setLoopFocus(true) + self._verticalLayout:setTag(-1000) + self._firstFocusedWidget = self._verticalLayout + + local upperHBox = ccui.HBox:create() + upperHBox:setTag(-200) + self._verticalLayout:addChild(upperHBox) + + local params = ccui.LinearLayoutParameter:create() + params:setMargin({left = 0, top = 0, right = 50, bottom = 0}) + + local vparams = ccui.LinearLayoutParameter:create() + vparams:setMargin({left = 10, top = 0, right = 0, bottom = 140}) + upperHBox:setLayoutParameter(vparams) + + local function onImageViewClicked(sender, eventType) + if eventType == ccui.TouchEventType.ended then + local w = sender + if w:isFocusEnabled() then + w:setFocusEnabled(false) + w:setColor(cc.c3b(255, 255, 0)) + else + w:setFocusEnabled(true) + w:setColor(cc.c3b(255, 255, 255)) + end + end + end + + local count = 3 + for i = 1, count do + local firstVbox = ccui.VBox:create() + firstVbox:setScale(0.5) + firstVbox:setLayoutParameter(params) + firstVbox:setTag(i * 100) + + local count1 = 3 + for j = 1, count1 do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setTouchEnabled(true) + imageView:setTag(j + firstVbox:getTag()) + imageView:addTouchEventListener(onImageViewClicked) + firstVbox:addChild(imageView) + end + + upperHBox:addChild(firstVbox) + end + + local bottomHBox = ccui.HBox:create() + bottomHBox:setScale(0.5) + bottomHBox:setTag(600) + bottomHBox:setLayoutParameter(vparams) + + count = 3 + local bottomParams = ccui.LinearLayoutParameter:create() + bottomParams:setMargin({left = 0, top = 0, right = 8, bottom = 0}) + for i = 1, count do + local imageView = ccui.ImageView:create("cocosui/scrollviewbg.png") + imageView:setLayoutParameter(bottomParams) + imageView:setTouchEnabled(true) + imageView:setTag(i + 600) + imageView:addTouchEventListener(onImageViewClicked) + bottomHBox:addChild(imageView) + end + + self._verticalLayout:addChild(bottomHBox) + + self._loopText = ccui.Text:create("loop enabled", "Airal", 20) + self._loopText:setPosition(cc.p(winSize.width/2, winSize.height - 50)) + self._loopText:setColor(cc.c3b(255, 0 ,0)) + self:addChild(self._loopText) + + local function toggleFocusLoop(sender, eventType) + if eventType == ccui.TouchEventType.ended then + self._verticalLayout:setLoopFocus(not self._verticalLayout:isLoopFocus()) + if self._verticalLayout:isLoopFocus() then + self._loopText:setString("loop enabled") + else + self._loopText:setString("loop disabled") + end + end + end + + local btn = ccui.Button:create("cocosui/switch-mask.png") + btn:setTitleText("Toggle Loop") + btn:setPosition(cc.p(60, winSize.height - 50)) + btn:setTitleColor(cc.c3b(0, 255, 0)) + btn:addTouchEventListener(toggleFocusLoop) + self:addChild(btn) +end + +function UIFocusTestNestedLayout3.create() + local scene = cc.Scene:create() + local layer = UIFocusTestNestedLayout3.extend(cc.Layer:create()) + layer:initExtend() + scene:addChild(layer) + return scene +end + local cocoStudioGuiArray = { + { + title = "UIFocusTestHorizontal", + func = function () + return UIFocusTestHorizontal.create() + end, + }, + + { + title = "UIFocusTestVertical", + func = function () + return UIFocusTestVertical.create() + end, + }, + + { + title = "UIFocusTestNestedLayout1", + func = function () + return UIFocusTestNestedLayout1.create() + end, + }, + + { + title = "UIFocusTestNestedLayout2", + func = function () + return UIFocusTestNestedLayout2.create() + end, + }, + + { + title = "UIFocusTestNestedLayout3", + func = function () + return UIFocusTestNestedLayout3.create() + end, + }, + { title = "UIButtonTest", func = function () diff --git a/tools/tolua/cocos2dx.ini b/tools/tolua/cocos2dx.ini index 6a2a9cd6e6..20ef1d54cc 100644 --- a/tools/tolua/cocos2dx.ini +++ b/tools/tolua/cocos2dx.ini @@ -44,7 +44,7 @@ skip = Node::[setGLServerState description getUserObject .*UserData getGLServerS LayerColor::[getBlendFunc setBlendFunc], ParticleSystem::[(g|s)etBlendFunc updateQuadWithParticle initParticle], DrawNode::[getBlendFunc setBlendFunc drawPolygon listenBackToForeground], - Director::[getAccelerometer (g|s)et.*Dispatcher getProjection getFrustum getRenderer], + Director::[getAccelerometer getProjection getFrustum getRenderer], Layer.*::[didAccelerate (g|s)etBlendFunc keyPressed keyReleased], Menu.*::[.*Target getSubItems create initWithItems alignItemsInRows alignItemsInColumns], MenuItem.*::[create setCallback initWithCallback], diff --git a/tools/tolua/cocos2dx_ui.ini b/tools/tolua/cocos2dx_ui.ini index 1befd7db27..1bbc7331f4 100644 --- a/tools/tolua/cocos2dx_ui.ini +++ b/tools/tolua/cocos2dx_ui.ini @@ -30,7 +30,7 @@ headers = %(cocosdir)s/cocos/ui/CocosGUI.h # what classes to produce code for. You can use regular expressions here. When testing the regular # expression, it will be enclosed in "^$", like this: "^Menu*$". -classes = Helper Widget Layer Layout RootWidget Button CheckBox ImageView Text TextAtlas TextBMFont LoadingBar Slider Switch TextField ScrollView ListView PageView LayoutParameter LinearLayoutParameter RelativeLayoutParameter Rich.* +classes = Helper Widget Layer Layout RootWidget Button CheckBox ImageView Text TextAtlas TextBMFont LoadingBar Slider Switch TextField ScrollView ListView PageView LayoutParameter LinearLayoutParameter RelativeLayoutParameter Rich.* HBox VBox RelativeBox # what should we skip? in the format ClassName::[function function] # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also