diff --git a/cocos/scripting/lua-bindings/manual/ui/lua_cocos2dx_ui_manual.cpp b/cocos/scripting/lua-bindings/manual/ui/lua_cocos2dx_ui_manual.cpp index 24aac48fdc..0e0afe8623 100644 --- a/cocos/scripting/lua-bindings/manual/ui/lua_cocos2dx_ui_manual.cpp +++ b/cocos/scripting/lua-bindings/manual/ui/lua_cocos2dx_ui_manual.cpp @@ -103,6 +103,62 @@ tolua_lerror: #endif } +static int lua_cocos2dx_Widget_addClickEventListener(lua_State* L) +{ + if (nullptr == L) + return 0; + + int argc = 0; + Widget* self = nullptr; + +#if COCOS2D_DEBUG >= 1 + tolua_Error tolua_err; + if (!tolua_isusertype(L,1,"ccui.Widget",0,&tolua_err)) goto tolua_lerror; +#endif + + self = static_cast(tolua_tousertype(L,1,0)); + +#if COCOS2D_DEBUG >= 1 + if (nullptr == self) { + tolua_error(L,"invalid 'self' in function 'lua_cocos2dx_Widget_addClickEventListener'\n", NULL); + return 0; + } +#endif + + argc = lua_gettop(L) - 1; + + if (1 == argc) + { +#if COCOS2D_DEBUG >= 1 + if (!toluafix_isfunction(L,2,"LUA_FUNCTION",0,&tolua_err)) + { + goto tolua_lerror; + } +#endif + + LUA_FUNCTION handler = ( toluafix_ref_function(L,2,0)); + + self->addClickEventListener([=](cocos2d::Ref* sender){ + LuaStack* stack = LuaEngine::getInstance()->getLuaStack(); + stack->pushObject(sender, "cc.Ref"); + stack->executeFunctionByHandler(handler, 1); + stack->clean(); + }); + + ScriptHandlerMgr::getInstance()->addCustomHandler((void*)self, handler); + return 0; + } + + luaL_error(L, "'addClickEventListener' function of Widget has wrong number of arguments: %d, was expecting %d\n", argc, 1); + return 0; + +#if COCOS2D_DEBUG >= 1 +tolua_lerror: + tolua_error(L,"#ferror in function 'addClickEventListener'.",&tolua_err); + return 0; +#endif +} + static void extendWidget(lua_State* L) { lua_pushstring(L, "ccui.Widget"); @@ -110,6 +166,7 @@ static void extendWidget(lua_State* L) if (lua_istable(L,-1)) { tolua_function(L, "addTouchEventListener", lua_cocos2dx_Widget_addTouchEventListener); + tolua_function(L, "addClickEventListener", lua_cocos2dx_Widget_addClickEventListener); } lua_pop(L, 1); } diff --git a/tests/lua-tests/src/CocoStudioTest/CocoStudioUIEditorTest/CocoStudioUIEditorTest.lua b/tests/lua-tests/src/CocoStudioTest/CocoStudioUIEditorTest/CocoStudioUIEditorTest.lua index a7e6f0d9c1..80b5abe93a 100644 --- a/tests/lua-tests/src/CocoStudioTest/CocoStudioUIEditorTest/CocoStudioUIEditorTest.lua +++ b/tests/lua-tests/src/CocoStudioTest/CocoStudioUIEditorTest/CocoStudioUIEditorTest.lua @@ -663,38 +663,232 @@ function LayoutComponentTest.extend(target) end function LayoutComponentTest:configureGUIScene() + local screenSize = cc.Director:getInstance():getWinSize() + local rootSize = self._layout:getContentSize() + self._uiLayer:setPosition(cc.p((screenSize.width - rootSize.width) / 2, (screenSize.height - rootSize.height) / 2)) - self._displayValueLabel = ccui.Text:create("UILayoutComponentTest_Editor", "", 20) - self._displayValueLabel:setPosition(cc.p(screenSize.width / 2, screenSize.height - self._displayValueLabel:getContentSize().height / 2)) - self._uiLayer:addChild(self._displayValueLabel) - local back_label = ccui.Text:create("Back", "", 20) - back_label:setTouchEnabled(true) - local labelLayout = ccui.LayoutComponent:bindLayoutComponent(back_label) - labelLayout:setHorizontalEdge(ccui.LayoutComponent.HorizontalEdge.Right) - labelLayout:setVerticalEdge(ccui.LayoutComponent.VerticalEdge.Bottom) + local scheduler = cc.Director:getInstance():getScheduler() + local schedulerEntry = 0 + + local root = self._layout:getChildByName("root_Panel") + + local back_label = ccui.Helper:seekWidgetByName(root, "back") back_label:addTouchEventListener(function(sender, eventType) - if eventType == ccui.TouchEventType.ended then - self:unscheduleUpdate() - runCocoStudioUIEditorTestScene() + self:unscheduleUpdate() + runCocoStudioUIEditorTestScene() + end) + + local sceneTitle = ccui.Helper:seekWidgetByName(root, "UItest") + + + local hUnchecked = ccui.Helper:seekWidgetByName(root, "Button_h_unchecked") + local vUnchecked = ccui.Helper:seekWidgetByName(root, "Button_v_unchecked") + local hChecked = ccui.Helper:seekWidgetByName(root, "Button_h_checked") + local vChecked = ccui.Helper:seekWidgetByName(root, "Button_v_checked") + local lPinUnchecked = ccui.Helper:seekWidgetByName(root, "Button_Pin_Left") + local rPinUnchecked = ccui.Helper:seekWidgetByName(root, "Button_Pin_Right") + local tPinUnchecked = ccui.Helper:seekWidgetByName(root, "Button_Pin_Top") + local bPinUnchecked = ccui.Helper:seekWidgetByName(root, "Button_Pin_Bottom") + local lPinChecked = ccui.Helper:seekWidgetByName(root, "Button_Pin_Left_Checked") + local rPinChecked = ccui.Helper:seekWidgetByName(root, "Button_Pin_Right_Checked") + local tPinChecked = ccui.Helper:seekWidgetByName(root, "Button_Pin_Top_Checked") + local bPinChecked = ccui.Helper:seekWidgetByName(root, "Button_Pin_Bottom_Checked") + + local textPin = ccui.Helper:seekWidgetByName(root, "Text_Pin") + local textStretch = ccui.Helper:seekWidgetByName(root, "Text_Stretch") + local widget = ccui.Helper:seekWidgetByName(root, "Image_Widget") + local container = ccui.Helper:seekWidgetByName(root, "background_Panel") + + local strenchStartIndex = 0 + local function onChangeLayoutComponent(sender) + local statusStretch = textStretch:getString() + --if statusStretch == nil + local statusPin = textPin:getString() + local hPinStatus = " Left" + local vPinStatus = " Bottom" + + if sender == hUnchecked then + hUnchecked:setVisible(false) + hChecked:setVisible(true) + statusStretch = statusStretch .. " Horizontal" + textStretch:setString(statusStretch) + elseif sender == hChecked then + hChecked:setVisible(false) + hUnchecked:setVisible(true) + strenchStartIndex = string.find(statusStretch, " Horizontal") + if strenchStartIndex ~= nil then + statusStretch = string.gsub(statusStretch, " Horizontal", "") + end + textStretch:setString(statusStretch) + elseif sender == vUnchecked then + vUnchecked:setVisible(false) + vChecked:setVisible(true) + statusStretch = statusStretch .. " Vertical" + textStretch:setString(statusStretch) + elseif sender == vChecked then + vChecked:setVisible(false) + vUnchecked:setVisible(true) + strenchStartIndex = string.find(statusStretch, " Vertical") + if strenchStartIndex ~= nil then + statusStretch = string.gsub(statusStretch, " Vertical", "") + end + textStretch:setString(statusStretch) + elseif sender == lPinUnchecked then + lPinUnchecked:setVisible(false) + lPinChecked:setVisible(true) + elseif sender == lPinChecked then + lPinChecked:setVisible(false) + lPinUnchecked:setVisible(true) + elseif sender == rPinUnchecked then + rPinUnchecked:setVisible(false) + rPinChecked:setVisible(true) + elseif sender == rPinChecked then + rPinChecked:setVisible(false) + rPinUnchecked:setVisible(true) + elseif sender == tPinUnchecked then + tPinUnchecked:setVisible(false) + tPinChecked:setVisible(true) + elseif sender == tPinChecked then + tPinChecked:setVisible(false) + tPinUnchecked:setVisible(true) + elseif sender == bPinUnchecked then + bPinUnchecked:setVisible(false) + bPinChecked:setVisible(true) + elseif (sender == bPinChecked) then + bPinChecked:setVisible(false) + bPinUnchecked:setVisible(true) + end + + if rPinChecked:isVisible() then + if lPinChecked:isVisible() then + hPinStatus = " Left Right" + else + hPinStatus = " Right" + end + end + + if tPinChecked:isVisible() then + if bPinChecked:isVisible() then + vPinStatus = " Top Bottom" + else + vPinStatus = " Top" + end + end + + statusPin = string.format("Pin:%s%s", hPinStatus, vPinStatus) + textPin:setString(statusPin) + + local layoutComponent = ccui.LayoutComponent:bindLayoutComponent(widget) + local widthEnableFlag = false + if string.find(statusStretch, "Horizontal") ~= nil then + widthEnableFlag = true + end + layoutComponent:setStretchWidthEnabled(widthEnableFlag) + + local heightEnableFlag = false + if string.find(statusStretch, "Vertical") ~= nil then + heightEnableFlag = true + end + layoutComponent:setStretchHeightEnabled(heightEnableFlag) + + local horizontalEdgeType = ccui.LayoutComponent.HorizontalEdge.None + if string.find(statusPin, "Left") ~= nil and string.find(statusPin, "Right") == nil then + horizontalEdgeType = ccui.LayoutComponent.HorizontalEdge.Left + elseif string.find(statusPin, "Left") == nil and string.find(statusPin, "Right") ~= nil then + horizontalEdgeType = ccui.LayoutComponent.HorizontalEdge.Right + elseif string.find(statusPin, "Left") ~= nil and string.find(statusPin, "Right") ~= nil then + horizontalEdgeType = ccui.LayoutComponent.HorizontalEdge.Center + end + layoutComponent:setHorizontalEdge(horizontalEdgeType) + + local verticalEdgeType = ccui.LayoutComponent.VerticalEdge.None + if string.find(statusPin, "Top") ~= nil and string.find(statusPin, "Bottom") == nil then + verticalEdgeType = ccui.LayoutComponent.VerticalEdge.Top + elseif string.find(statusPin, "Top") == nil and string.find(statusPin, "Bottom") ~= nil then + verticalEdgeType = ccui.LayoutComponent.VerticalEdge.Bottom + elseif string.find(statusPin, "Top") ~= nil and string.find(statusPin, "Bottom") ~= nil then + verticalEdgeType = ccui.LayoutComponent.VerticalEdge.Center + end + layoutComponent:setVerticalEdge(verticalEdgeType) + end + + hUnchecked:addClickEventListener(onChangeLayoutComponent) + vUnchecked:addClickEventListener(onChangeLayoutComponent) + hChecked:addClickEventListener(onChangeLayoutComponent) + vChecked:addClickEventListener(onChangeLayoutComponent) + lPinUnchecked:addClickEventListener(onChangeLayoutComponent) + rPinUnchecked:addClickEventListener(onChangeLayoutComponent) + tPinUnchecked:addClickEventListener(onChangeLayoutComponent) + bPinUnchecked:addClickEventListener(onChangeLayoutComponent) + lPinChecked:addClickEventListener(onChangeLayoutComponent) + rPinChecked:addClickEventListener(onChangeLayoutComponent) + tPinChecked:addClickEventListener(onChangeLayoutComponent) + bPinChecked:addClickEventListener(onChangeLayoutComponent) + + local btnSwitch = ccui.Helper:seekWidgetByName(root, "Button_Switch") + local scheduleTimes = 0 + btnSwitch:addClickEventListener(function(sender) + local layoutController = ccui.Helper:seekWidgetByName(root, "Panel_Controller") + local size = container:getContentSize() + local switchButton = ccui.Helper:seekWidgetByName(root, "Button_Switch") + switchButton:setEnabled(false) + switchButton:setBright(false) + + if size.width < 200 then + layoutController:setVisible(false) + + scheduler:unscheduleScriptEntry(schedulerEntry) + + schedulerEntry = scheduler:scheduleScriptFunc(function(dt) + + local increaseSize = container:getContentSize() + increaseSize.width = increaseSize.width + 303.0 / 40.0 + increaseSize.height = increaseSize.height + 70.0 / 40.0 + container:setContentSize(increaseSize) + ccui.Helper:doLayout(container) + + if increaseSize.width > 390 then + switchButton:setEnabled(true) + switchButton:setBright(true) + scheduler:unscheduleScriptEntry(schedulerEntry) + schedulerEntry = 0 + end + end, 0.025, false) + else + layoutController:setVisible(true) + + scheduler:unscheduleScriptEntry(schedulerEntry) + schedulerEntry = scheduler:scheduleScriptFunc(function(dt) + + local decreaseSize = container:getContentSize() + decreaseSize.width = decreaseSize.width - 303.0 / 40.0 + decreaseSize.height = decreaseSize.height - 70.0 / 40.0 + container:setContentSize(decreaseSize) + ccui.Helper:doLayout(container) + + if decreaseSize.width < 110 then + switchButton:setEnabled(true) + switchButton:setBright(true) + scheduler:unscheduleScriptEntry(schedulerEntry) + schedulerEntry = 0 + end + end, 0.025, false) end end) - self._layout:addChild(back_label) end function LayoutComponentTest:initExtend() self:init() - local screenSize = cc.Director:getInstance():getWinSize() - self._layout = ccui.Layout:create() - self._uiLayer:addChild(self._layout) - self._layout:setContentSize(screenSize) - local node = cc.CSLoader:createNode("cocosui/UIEditorTest/UILayout/LayoutComponent/Scene.csb") - node:setContentSize(screenSize) - self._layout:addChild(node) + local node = cc.CSLoader:createNode("cocosui/UIEditorTest/UILayout/LayoutComponent/UILayoutComponent.csb") + local child = node:getChildByTag(5) + child:removeFromParent() + self._layout = child + self._uiLayer:addChild(self._layout) + self:configureGUIScene() - ccui.Helper:doLayout(self._layout) end function LayoutComponentTest.create() diff --git a/tools/tolua/cocos2dx_ui.ini b/tools/tolua/cocos2dx_ui.ini index cbebcc7fb6..e56f7f4852 100644 --- a/tools/tolua/cocos2dx_ui.ini +++ b/tools/tolua/cocos2dx_ui.ini @@ -40,7 +40,7 @@ classes = Helper Widget Layer Layout RootWidget Button CheckBox ImageView Text T # functions from all classes. skip = *::[^visit$ copyWith.* onEnter.* onExit.* ^description$ getObjectType .*HSV onTouch.* onAcc.* onKey.* onRegisterTouchListener ccTouch.* (g|s)etDelegate], - Widget::[(s|g)etUserObject], + Widget::[(s|g)etUserObject addTouchEventListener addClickEventListener addCCSEventListener], Layer::[getInputManager], LayoutParameter::[(s|g)etMargin], Helper::[init],