2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
//
|
|
|
|
// UIFocusTest.cpp
|
|
|
|
// cocos2d_tests
|
|
|
|
//
|
|
|
|
// Created by guanghui on 5/4/14.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "UIFocusTest.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
using namespace axis::ui;
|
2015-04-09 12:23:47 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
UIFocusTests::UIFocusTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(UIFocusTestHorizontal);
|
|
|
|
ADD_TEST_CASE(UIFocusTestVertical);
|
|
|
|
ADD_TEST_CASE(UIFocusTestNestedLayout1);
|
|
|
|
ADD_TEST_CASE(UIFocusTestNestedLayout2);
|
|
|
|
ADD_TEST_CASE(UIFocusTestNestedLayout3);
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIFocusTestBase::UIFocusTestBase() {}
|
2014-05-04 16:31:24 +08:00
|
|
|
|
|
|
|
UIFocusTestBase::~UIFocusTestBase()
|
|
|
|
{
|
|
|
|
_eventDispatcher->removeEventListener(_eventListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIFocusTestBase::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2014-05-04 16:31:24 +08:00
|
|
|
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
|
|
|
|
background->removeFromParentAndCleanup(true);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
_dpadMenu = Menu::create();
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto winSize = Director::getInstance()->getVisibleSize();
|
2022-07-15 19:17:01 +08:00
|
|
|
auto leftItem = MenuItemFont::create("Left", AX_CALLBACK_0(UIFocusTestBase::onLeftKeyPressed, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
leftItem->setPosition(Vec2(winSize.width - 100, winSize.height / 2));
|
2014-05-04 16:31:24 +08:00
|
|
|
_dpadMenu->addChild(leftItem);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
auto rightItem = MenuItemFont::create("Right", AX_CALLBACK_0(UIFocusTestBase::onRightKeyPressed, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
rightItem->setPosition(Vec2(winSize.width - 30, winSize.height / 2));
|
2014-05-04 16:31:24 +08:00
|
|
|
_dpadMenu->addChild(rightItem);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
auto upItem = MenuItemFont::create("Up", AX_CALLBACK_0(UIFocusTestBase::onUpKeyPressed, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
upItem->setPosition(Vec2(winSize.width - 60, winSize.height / 2 + 50));
|
2014-05-04 16:31:24 +08:00
|
|
|
_dpadMenu->addChild(upItem);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
auto downItem = MenuItemFont::create("Down", AX_CALLBACK_0(UIFocusTestBase::onDownKeyPressed, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
downItem->setPosition(Vec2(winSize.width - 60, winSize.height / 2 - 50));
|
2014-05-04 16:31:24 +08:00
|
|
|
_dpadMenu->addChild(downItem);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
_dpadMenu->setPosition(Vec2::ZERO);
|
2014-05-04 16:31:24 +08:00
|
|
|
_uiLayer->addChild(_dpadMenu);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// call this method to enable Dpad focus navigation
|
2014-05-27 09:49:34 +08:00
|
|
|
Widget::enableDpadNavigation(true);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
_eventListener = EventListenerFocus::create();
|
2022-07-15 19:17:01 +08:00
|
|
|
_eventListener->onFocusChanged = AX_CALLBACK_2(UIFocusTestBase::onFocusChanged, this);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(_eventListener, 1);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2016-12-12 09:41:53 +08:00
|
|
|
_toggleButton = Button::create("cocosui/switch-mask.png");
|
|
|
|
_toggleButton->setTitleText("Toggle Loop");
|
2019-09-24 11:31:35 +08:00
|
|
|
_toggleButton->setPosition(Vec2(60.0f, winSize.height - 50));
|
2016-12-12 09:41:53 +08:00
|
|
|
_toggleButton->setTitleColor(Color3B::RED);
|
|
|
|
_toggleButton->setFocusEnabled(false);
|
|
|
|
this->addChild(_toggleButton);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void UIFocusTestBase::onImageViewClicked(axis::Ref* ref, Widget::TouchEventType touchType)
|
2014-05-06 14:14:22 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (touchType == Widget::TouchEventType::ENDED)
|
|
|
|
{
|
|
|
|
Widget* w = (Widget*)ref;
|
|
|
|
if (w->isFocusEnabled())
|
|
|
|
{
|
2014-05-06 14:14:22 +08:00
|
|
|
w->setFocusEnabled(false);
|
2014-05-06 15:11:55 +08:00
|
|
|
w->setColor(Color3B::YELLOW);
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-06 14:14:22 +08:00
|
|
|
w->setFocusEnabled(true);
|
2014-05-06 15:11:55 +08:00
|
|
|
w->setColor(Color3B::WHITE);
|
2014-05-06 14:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:20:01 +08:00
|
|
|
void UIFocusTestBase::onLeftKeyPressed()
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::EventKeyboard::KeyCode cocos2dKey = EventKeyboard::KeyCode::KEY_DPAD_LEFT;
|
|
|
|
axis::EventKeyboard event(cocos2dKey, false);
|
|
|
|
axis::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
2014-05-06 14:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIFocusTestBase::onRightKeyPressed()
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::EventKeyboard::KeyCode cocos2dKey = EventKeyboard::KeyCode::KEY_DPAD_RIGHT;
|
|
|
|
axis::EventKeyboard event(cocos2dKey, false);
|
|
|
|
axis::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
2014-05-06 14:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIFocusTestBase::onUpKeyPressed()
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::EventKeyboard::KeyCode cocos2dKey = EventKeyboard::KeyCode::KEY_DPAD_UP;
|
|
|
|
axis::EventKeyboard event(cocos2dKey, false);
|
|
|
|
axis::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
2014-05-06 14:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIFocusTestBase::onDownKeyPressed()
|
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::EventKeyboard::KeyCode cocos2dKey = EventKeyboard::KeyCode::KEY_DPAD_DOWN;
|
|
|
|
axis::EventKeyboard event(cocos2dKey, false);
|
|
|
|
axis::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
|
2014-05-06 14:20:01 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void UIFocusTestBase::onFocusChanged(axis::ui::Widget* widgetLostFocus, axis::ui::Widget* widgetGetFocus)
|
2014-05-06 14:20:01 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (widgetGetFocus && widgetGetFocus->isFocusEnabled())
|
|
|
|
{
|
2014-05-06 14:20:01 +08:00
|
|
|
widgetGetFocus->setColor(Color3B::RED);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
if (widgetLostFocus && widgetLostFocus->isFocusEnabled())
|
|
|
|
{
|
2014-05-06 14:20:01 +08:00
|
|
|
widgetLostFocus->setColor(Color3B::WHITE);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
if (widgetLostFocus && widgetGetFocus)
|
|
|
|
{
|
|
|
|
CCLOG("on focus change, %d widget get focus, %d widget lose focus", widgetGetFocus->getTag(),
|
|
|
|
widgetLostFocus->getTag());
|
2014-05-07 10:23:57 +08:00
|
|
|
}
|
2014-05-06 14:20:01 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// UIFocusTestHorizontal
|
|
|
|
UIFocusTestHorizontal::UIFocusTestHorizontal() {}
|
2014-05-04 16:31:24 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIFocusTestHorizontal::~UIFocusTestHorizontal() {}
|
2014-05-04 16:31:24 +08:00
|
|
|
|
|
|
|
bool UIFocusTestHorizontal::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (UIFocusTestBase::init())
|
|
|
|
{
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
Size winSize = Director::getInstance()->getVisibleSize();
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 18:11:41 +08:00
|
|
|
_horizontalLayout = HBox::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
_horizontalLayout->setPosition(Vec2(20.0f, winSize.height / 2 + 40));
|
2014-05-04 18:11:41 +08:00
|
|
|
_uiLayer->addChild(_horizontalLayout);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 18:11:41 +08:00
|
|
|
_horizontalLayout->setFocused(true);
|
|
|
|
_horizontalLayout->setLoopFocus(true);
|
2014-05-07 10:05:20 +08:00
|
|
|
_horizontalLayout->setTag(100);
|
2014-05-04 18:11:41 +08:00
|
|
|
_firstFocusedWidget = _horizontalLayout;
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
int count = 3;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
2014-05-06 14:14:22 +08:00
|
|
|
w->setTouchEnabled(true);
|
2014-05-06 17:28:12 +08:00
|
|
|
w->setTag(i);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestHorizontal::onImageViewClicked, this));
|
2014-05-04 18:11:41 +08:00
|
|
|
_horizontalLayout->addChild(w);
|
2014-05-04 16:31:24 +08:00
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2015-01-15 10:10:53 +08:00
|
|
|
_loopText = Text::create("loop enabled", "Arial", 20);
|
2021-12-28 16:06:23 +08:00
|
|
|
_loopText->setPosition(Vec2(winSize.width / 2, winSize.height - 50));
|
2014-05-04 18:11:41 +08:00
|
|
|
_loopText->setColor(Color3B::GREEN);
|
|
|
|
this->addChild(_loopText);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_toggleButton->addTouchEventListener(AX_CALLBACK_2(UIFocusTestHorizontal::toggleFocusLoop, this));
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void UIFocusTestHorizontal::toggleFocusLoop(axis::Ref* pObjc, Widget::TouchEventType type)
|
2014-05-04 18:11:41 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (type == Widget::TouchEventType::ENDED)
|
|
|
|
{
|
2014-05-07 14:06:44 +08:00
|
|
|
_horizontalLayout->setLoopFocus(!_horizontalLayout->isLoopFocus());
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_horizontalLayout->isLoopFocus())
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop enabled");
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop disabled");
|
2014-05-04 18:11:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// UIFocusTestVertical
|
|
|
|
UIFocusTestVertical::UIFocusTestVertical() {}
|
2014-05-05 11:25:30 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIFocusTestVertical::~UIFocusTestVertical() {}
|
2014-05-04 16:31:24 +08:00
|
|
|
|
2014-05-05 11:25:30 +08:00
|
|
|
bool UIFocusTestVertical::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (UIFocusTestBase::init())
|
|
|
|
{
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-05 11:25:30 +08:00
|
|
|
Size winSize = Director::getInstance()->getVisibleSize();
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-05 11:25:30 +08:00
|
|
|
_verticalLayout = VBox::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
_verticalLayout->setPosition(Vec2(winSize.width / 2 - 100, winSize.height - 70));
|
2014-05-05 11:25:30 +08:00
|
|
|
_uiLayer->addChild(_verticalLayout);
|
2014-05-07 10:05:20 +08:00
|
|
|
_verticalLayout->setTag(100);
|
2014-05-08 14:35:45 +08:00
|
|
|
_verticalLayout->setScale(0.5);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-05 11:25:30 +08:00
|
|
|
_verticalLayout->setFocused(true);
|
|
|
|
_verticalLayout->setLoopFocus(true);
|
|
|
|
_firstFocusedWidget = _verticalLayout;
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-05 11:25:30 +08:00
|
|
|
int count = 3;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
2014-05-06 14:14:22 +08:00
|
|
|
w->setTouchEnabled(true);
|
2014-05-07 10:05:20 +08:00
|
|
|
w->setTag(i);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestVertical::onImageViewClicked, this));
|
2014-05-05 11:25:30 +08:00
|
|
|
_verticalLayout->addChild(w);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2015-01-15 10:10:53 +08:00
|
|
|
_loopText = Text::create("loop enabled", "Arial", 20);
|
2021-12-28 16:06:23 +08:00
|
|
|
_loopText->setPosition(Vec2(winSize.width / 2, winSize.height - 50));
|
2014-05-05 11:25:30 +08:00
|
|
|
_loopText->setColor(Color3B::GREEN);
|
|
|
|
this->addChild(_loopText);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_toggleButton->addTouchEventListener(AX_CALLBACK_2(UIFocusTestVertical::toggleFocusLoop, this));
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-05 11:25:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void UIFocusTestVertical::toggleFocusLoop(axis::Ref* pObjc, Widget::TouchEventType type)
|
2014-05-05 11:25:30 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (type == Widget::TouchEventType::ENDED)
|
|
|
|
{
|
2014-05-07 14:06:44 +08:00
|
|
|
_verticalLayout->setLoopFocus(!_verticalLayout->isLoopFocus());
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_verticalLayout->isLoopFocus())
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop enabled");
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop disabled");
|
2014-05-05 11:25:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// UIFocusTestNestedLayout1
|
|
|
|
UIFocusTestNestedLayout1::UIFocusTestNestedLayout1() {}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIFocusTestNestedLayout1::~UIFocusTestNestedLayout1() {}
|
2014-05-06 14:38:45 +08:00
|
|
|
|
|
|
|
bool UIFocusTestNestedLayout1::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (UIFocusTestBase::init())
|
|
|
|
{
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-06 14:38:45 +08:00
|
|
|
Size winSize = Director::getInstance()->getVisibleSize();
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-06 14:38:45 +08:00
|
|
|
_verticalLayout = VBox::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
_verticalLayout->setPosition(Vec2(winSize.width / 2 - 80, winSize.height - 70));
|
2014-05-06 14:38:45 +08:00
|
|
|
_uiLayer->addChild(_verticalLayout);
|
2014-05-08 14:35:45 +08:00
|
|
|
_verticalLayout->setScale(0.5);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-06 14:38:45 +08:00
|
|
|
_verticalLayout->setFocused(true);
|
|
|
|
_verticalLayout->setLoopFocus(true);
|
2014-05-07 10:05:20 +08:00
|
|
|
_verticalLayout->setTag(100);
|
2014-05-06 14:38:45 +08:00
|
|
|
_firstFocusedWidget = _verticalLayout;
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 10:05:20 +08:00
|
|
|
int count1 = 1;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count1; ++i)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
w->setAnchorPoint(Vec2::ZERO);
|
2014-05-06 14:38:45 +08:00
|
|
|
w->setTouchEnabled(true);
|
2014-05-06 15:11:55 +08:00
|
|
|
w->setScaleX(2.5);
|
2021-12-28 16:06:23 +08:00
|
|
|
w->setTag(i + count1);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestNestedLayout1::onImageViewClicked, this));
|
2014-05-06 14:38:45 +08:00
|
|
|
_verticalLayout->addChild(w);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// add HBox into VBox
|
|
|
|
HBox* hbox = HBox::create();
|
2014-07-10 00:45:27 +08:00
|
|
|
hbox->setScale(0.8f);
|
2014-05-07 10:05:20 +08:00
|
|
|
hbox->setTag(101);
|
2014-05-06 14:38:45 +08:00
|
|
|
_verticalLayout->addChild(hbox);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 10:05:20 +08:00
|
|
|
int count2 = 2;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count2; ++i)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
|
|
|
w->setAnchorPoint(Vec2(0, 1));
|
2014-05-06 15:11:55 +08:00
|
|
|
w->setScaleY(2.0);
|
2014-05-06 14:38:45 +08:00
|
|
|
w->setTouchEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
w->setTag(i + count1 + count2);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestNestedLayout1::onImageViewClicked, this));
|
2014-05-06 14:38:45 +08:00
|
|
|
hbox->addChild(w);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
VBox* innerVBox = VBox::create();
|
2014-05-06 14:38:45 +08:00
|
|
|
hbox->addChild(innerVBox);
|
2014-05-07 10:05:20 +08:00
|
|
|
innerVBox->setTag(102);
|
2021-12-28 16:06:23 +08:00
|
|
|
// innerVBox->setPassFocusToChild(false);
|
|
|
|
// innerVBox->setFocusEnabled(false);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 10:05:20 +08:00
|
|
|
int count3 = 2;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count3; ++i)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
2014-05-06 14:38:45 +08:00
|
|
|
w->setTouchEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
w->setTag(i + count1 + count2 + count3);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestNestedLayout1::onImageViewClicked, this));
|
2014-05-06 14:38:45 +08:00
|
|
|
innerVBox->addChild(w);
|
|
|
|
}
|
|
|
|
|
2015-01-15 10:10:53 +08:00
|
|
|
_loopText = Text::create("loop enabled", "Arial", 20);
|
2021-12-28 16:06:23 +08:00
|
|
|
_loopText->setPosition(Vec2(winSize.width / 2, winSize.height - 50));
|
2014-05-06 14:38:45 +08:00
|
|
|
_loopText->setColor(Color3B::GREEN);
|
|
|
|
this->addChild(_loopText);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_toggleButton->addTouchEventListener(AX_CALLBACK_2(UIFocusTestNestedLayout1::toggleFocusLoop, this));
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-06 14:38:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void UIFocusTestNestedLayout1::toggleFocusLoop(axis::Ref* pObjc, Widget::TouchEventType type)
|
2014-05-06 14:38:45 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (type == Widget::TouchEventType::ENDED)
|
|
|
|
{
|
2014-05-07 14:06:44 +08:00
|
|
|
_verticalLayout->setLoopFocus(!_verticalLayout->isLoopFocus());
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_verticalLayout->isLoopFocus())
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop enabled");
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop disabled");
|
2014-05-06 14:38:45 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-07 11:40:01 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// UIFocusTestNestedLayout2
|
|
|
|
UIFocusTestNestedLayout2::UIFocusTestNestedLayout2() {}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIFocusTestNestedLayout2::~UIFocusTestNestedLayout2() {}
|
2014-05-07 11:40:01 +08:00
|
|
|
|
|
|
|
bool UIFocusTestNestedLayout2::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (UIFocusTestBase::init())
|
|
|
|
{
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 11:40:01 +08:00
|
|
|
Size winSize = Director::getInstance()->getVisibleSize();
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 11:40:01 +08:00
|
|
|
_horizontalLayout = HBox::create();
|
2021-12-28 16:06:23 +08:00
|
|
|
_horizontalLayout->setPosition(Vec2(winSize.width / 2 - 200, winSize.height - 70));
|
2014-05-07 11:40:01 +08:00
|
|
|
_uiLayer->addChild(_horizontalLayout);
|
2014-07-10 00:45:27 +08:00
|
|
|
_horizontalLayout->setScale(0.6f);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 11:40:01 +08:00
|
|
|
_horizontalLayout->setFocused(true);
|
|
|
|
_horizontalLayout->setLoopFocus(true);
|
|
|
|
_horizontalLayout->setTag(100);
|
|
|
|
_firstFocusedWidget = _horizontalLayout;
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 11:40:01 +08:00
|
|
|
int count1 = 2;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count1; ++i)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
|
|
|
w->setAnchorPoint(Vec2(0, 1));
|
2014-05-07 11:40:01 +08:00
|
|
|
w->setTouchEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
w->setTag(i + count1);
|
2014-07-10 00:45:27 +08:00
|
|
|
w->setScaleY(2.4f);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestNestedLayout2::onImageViewClicked, this));
|
2014-05-07 11:40:01 +08:00
|
|
|
_horizontalLayout->addChild(w);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// add HBox into VBox
|
|
|
|
VBox* vbox = VBox::create();
|
2014-07-10 00:45:27 +08:00
|
|
|
vbox->setScale(0.8f);
|
2014-05-07 11:40:01 +08:00
|
|
|
vbox->setTag(101);
|
|
|
|
_horizontalLayout->addChild(vbox);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 11:40:01 +08:00
|
|
|
int count2 = 2;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count2; ++i)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
|
|
|
w->setAnchorPoint(Vec2(0, 1));
|
2014-05-07 11:40:01 +08:00
|
|
|
w->setScaleX(2.0);
|
|
|
|
w->setTouchEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
w->setTag(i + count1 + count2);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestNestedLayout2::onImageViewClicked, this));
|
2014-05-07 11:40:01 +08:00
|
|
|
vbox->addChild(w);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
HBox* innerHBox = HBox::create();
|
2014-05-07 11:40:01 +08:00
|
|
|
vbox->addChild(innerHBox);
|
|
|
|
innerHBox->setTag(102);
|
|
|
|
// innerVBox->setPassFocusToChild(false);
|
|
|
|
// innerVBox->setFocusEnabled(false);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 11:40:01 +08:00
|
|
|
int count3 = 2;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count3; ++i)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
2014-05-07 11:40:01 +08:00
|
|
|
w->setTouchEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
w->setTag(i + count1 + count2 + count3);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestNestedLayout2::onImageViewClicked, this));
|
2014-05-07 11:40:01 +08:00
|
|
|
innerHBox->addChild(w);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2015-01-15 10:10:53 +08:00
|
|
|
_loopText = Text::create("loop enabled", "Arial", 20);
|
2021-12-28 16:06:23 +08:00
|
|
|
_loopText->setPosition(Vec2(winSize.width / 2, winSize.height - 50));
|
2014-05-07 11:40:01 +08:00
|
|
|
_loopText->setColor(Color3B::GREEN);
|
|
|
|
this->addChild(_loopText);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_toggleButton->addTouchEventListener(AX_CALLBACK_2(UIFocusTestNestedLayout2::toggleFocusLoop, this));
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 11:40:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void UIFocusTestNestedLayout2::toggleFocusLoop(axis::Ref* pObjc, Widget::TouchEventType type)
|
2014-05-07 11:40:01 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (type == Widget::TouchEventType::ENDED)
|
|
|
|
{
|
2014-05-07 14:06:44 +08:00
|
|
|
_horizontalLayout->setLoopFocus(!_horizontalLayout->isLoopFocus());
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_horizontalLayout->isLoopFocus())
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop enabled");
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop disabled");
|
2014-05-07 11:40:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-07 18:27:05 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// UIFocusTestNestedLayout3
|
|
|
|
UIFocusTestNestedLayout3::UIFocusTestNestedLayout3() {}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
UIFocusTestNestedLayout3::~UIFocusTestNestedLayout3() {}
|
2014-05-07 18:27:05 +08:00
|
|
|
|
|
|
|
bool UIFocusTestNestedLayout3::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (UIFocusTestBase::init())
|
|
|
|
{
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
Size winSize = Director::getInstance()->getVisibleSize();
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
_verticalLayout = VBox::create();
|
2019-09-24 11:31:35 +08:00
|
|
|
_verticalLayout->setPosition(Vec2(40.0f, winSize.height - 70));
|
2014-05-07 18:27:05 +08:00
|
|
|
_uiLayer->addChild(_verticalLayout);
|
2014-07-10 00:45:27 +08:00
|
|
|
_verticalLayout->setScale(0.8f);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
_verticalLayout->setFocused(true);
|
|
|
|
_verticalLayout->setLoopFocus(true);
|
|
|
|
_verticalLayout->setTag(-1000);
|
|
|
|
_firstFocusedWidget = _verticalLayout;
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
HBox* upperHBox = HBox::create();
|
2014-05-07 18:27:05 +08:00
|
|
|
upperHBox->setTag(-200);
|
|
|
|
_verticalLayout->addChild(upperHBox);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
LinearLayoutParameter* params = LinearLayoutParameter::create();
|
|
|
|
params->setMargin(Margin(0.0f, 0.0f, 50.0f, 0.0f));
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
LinearLayoutParameter* vparams = LinearLayoutParameter::create();
|
2019-09-24 11:31:35 +08:00
|
|
|
vparams->setMargin(Margin(10.0f, 0.0f, 0.0f, 140.0f));
|
2014-05-07 18:27:05 +08:00
|
|
|
upperHBox->setLayoutParameter(vparams);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
int count = 3;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
VBox* firstVbox = VBox::create();
|
2014-05-07 18:27:05 +08:00
|
|
|
firstVbox->setScale(0.5);
|
|
|
|
firstVbox->setLayoutParameter(params);
|
2021-12-28 16:06:23 +08:00
|
|
|
firstVbox->setTag((i + 1) * 100);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
int count1 = 3;
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int j = 0; j < count1; ++j)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
2014-05-07 18:27:05 +08:00
|
|
|
w->setTouchEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
w->setTag(j + firstVbox->getTag() + 1);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestBase::onImageViewClicked, this));
|
2014-05-07 18:27:05 +08:00
|
|
|
firstVbox->addChild(w);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
upperHBox->addChild(firstVbox);
|
|
|
|
}
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
HBox* bottomHBox = HBox::create();
|
2014-05-07 18:27:05 +08:00
|
|
|
bottomHBox->setScale(0.5);
|
|
|
|
bottomHBox->setTag(600);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
bottomHBox->setLayoutParameter(vparams);
|
2021-12-28 16:06:23 +08:00
|
|
|
count = 3;
|
|
|
|
LinearLayoutParameter* bottomParams = LinearLayoutParameter::create();
|
2019-09-24 11:31:35 +08:00
|
|
|
bottomParams->setMargin(Margin(0.0f, 0.0f, 8.0f, 0.0f));
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
ImageView* w = ImageView::create("cocosui/scrollviewbg.png");
|
2014-05-07 18:27:05 +08:00
|
|
|
w->setLayoutParameter(bottomParams);
|
|
|
|
w->setTouchEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
w->setTag(i + 601);
|
2022-07-15 19:17:01 +08:00
|
|
|
w->addTouchEventListener(AX_CALLBACK_2(UIFocusTestBase::onImageViewClicked, this));
|
2014-05-07 18:27:05 +08:00
|
|
|
bottomHBox->addChild(w);
|
|
|
|
}
|
|
|
|
_verticalLayout->addChild(bottomHBox);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2015-01-15 10:10:53 +08:00
|
|
|
_loopText = Text::create("loop enabled", "Arial", 20);
|
2021-12-28 16:06:23 +08:00
|
|
|
_loopText->setPosition(Vec2(winSize.width / 2, winSize.height - 50));
|
2014-05-07 18:27:05 +08:00
|
|
|
_loopText->setColor(Color3B::GREEN);
|
|
|
|
this->addChild(_loopText);
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_toggleButton->addTouchEventListener(AX_CALLBACK_2(UIFocusTestNestedLayout3::toggleFocusLoop, this));
|
2015-04-14 17:48:43 +08:00
|
|
|
|
2014-05-07 18:27:05 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void UIFocusTestNestedLayout3::toggleFocusLoop(axis::Ref* pObjc, Widget::TouchEventType type)
|
2014-05-07 18:27:05 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (type == Widget::TouchEventType::ENDED)
|
|
|
|
{
|
2014-05-07 18:27:05 +08:00
|
|
|
_verticalLayout->setLoopFocus(!_verticalLayout->isLoopFocus());
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_verticalLayout->isLoopFocus())
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop enabled");
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-14 15:26:14 +08:00
|
|
|
_loopText->setString("loop disabled");
|
2014-05-07 18:27:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|