36using namespace cocos2d::ui;
38UIFocusTests::UIFocusTests()
60 Layout* root =
static_cast<Layout*
>(
_uiLayer->getChildByTag(81));
62 Layout* background =
dynamic_cast<Layout*
>(root->getChildByName(
"background_Panel"));
63 background->removeFromParentAndCleanup(
true);
67 auto winSize = Director::getInstance()->getVisibleSize();
69 leftItem->setPosition(Vec2(winSize.width - 100, winSize.height/2));
74 rightItem->setPosition(Vec2(winSize.width - 30, winSize.height/2));
78 upItem->setPosition(Vec2(winSize.width - 60, winSize.height/2 + 50));
82 downItem->setPosition(Vec2(winSize.width - 60, winSize.height/2 - 50));
89 Widget::enableDpadNavigation(
true);
94 _eventDispatcher->addEventListenerWithFixedPriority(
_eventListener, 1);
111 if (touchType == Widget::TouchEventType::ENDED) {
112 Widget *w = (Widget*)ref;
113 if (w->isFocusEnabled()) {
114 w->setFocusEnabled(
false);
115 w->setColor(Color3B::YELLOW);
117 w->setFocusEnabled(
true);
118 w->setColor(Color3B::WHITE);
125 cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_LEFT;
126 cocos2d::EventKeyboard event(cocos2dKey,
false);
127 cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
132 cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_RIGHT;
133 cocos2d::EventKeyboard event(cocos2dKey,
false);
134 cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
139 cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_UP;
140 cocos2d::EventKeyboard event(cocos2dKey,
false);
141 cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
147 cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_DOWN;
148 cocos2d::EventKeyboard event(cocos2dKey,
false);
149 cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
155 if (widgetGetFocus && widgetGetFocus->isFocusEnabled()) {
156 widgetGetFocus->setColor(Color3B::RED);
159 if (widgetLostFocus && widgetLostFocus->isFocusEnabled()) {
160 widgetLostFocus->setColor(Color3B::WHITE);
163 if (widgetLostFocus && widgetGetFocus) {
164 CCLOG(
"on focus change, %d widget get focus, %d widget lose focus", widgetGetFocus->getTag(), widgetLostFocus->getTag());
184 Size winSize = Director::getInstance()->getVisibleSize();
196 for (
int i=0; i<count; ++i) {
197 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
198 w->setTouchEnabled(
true);
204 _loopText = Text::create(
"loop enabled",
"Arial", 20);
205 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
221 if (type == Widget::TouchEventType::ENDED) {
247 Size winSize = Director::getInstance()->getVisibleSize();
250 _verticalLayout->setPosition(Vec2(winSize.width/2 - 100, winSize.height - 70));
260 for (
int i=0; i<count; ++i) {
261 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
262 w->setTouchEnabled(
true);
268 _loopText = Text::create(
"loop enabled",
"Arial", 20);
269 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
283 if (type == Widget::TouchEventType::ENDED) {
308 Size winSize = Director::getInstance()->getVisibleSize();
311 _verticalLayout->setPosition(Vec2(winSize.width/2 - 80, winSize.height - 70));
321 for (
int i=0; i<count1; ++i) {
322 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
323 w->setAnchorPoint(Vec2::ZERO);
324 w->setTouchEnabled(
true);
332 HBox *hbox = HBox::create();
333 hbox->setScale(0.8f);
338 for (
int i=0; i < count2; ++i) {
339 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
340 w->setAnchorPoint(Vec2(0,1));
342 w->setTouchEnabled(
true);
343 w->setTag(i+count1+count2);
348 VBox *innerVBox = VBox::create();
349 hbox->addChild(innerVBox);
350 innerVBox->setTag(102);
356 for (
int i=0; i<count3; ++i) {
357 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
358 w->setTouchEnabled(
true);
359 w->setTag(i+count1+count2+count3);
361 innerVBox->addChild(w);
364 _loopText = Text::create(
"loop enabled",
"Arial", 20);
365 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
379 if (type == Widget::TouchEventType::ENDED) {
404 Size winSize = Director::getInstance()->getVisibleSize();
407 _horizontalLayout->setPosition(Vec2(winSize.width/2 - 200, winSize.height - 70));
417 for (
int i=0; i<count1; ++i) {
418 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
419 w->setAnchorPoint(Vec2(0,1));
420 w->setTouchEnabled(
true);
428 VBox *vbox = VBox::create();
429 vbox->setScale(0.8f);
434 for (
int i=0; i < count2; ++i) {
435 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
436 w->setAnchorPoint(Vec2(0,1));
438 w->setTouchEnabled(
true);
439 w->setTag(i+count1+count2);
444 HBox *innerHBox = HBox::create();
445 vbox->addChild(innerHBox);
446 innerHBox->setTag(102);
452 for (
int i=0; i<count3; ++i) {
453 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
454 w->setTouchEnabled(
true);
455 w->setTag(i+count1+count2+count3);
457 innerHBox->addChild(w);
460 _loopText = Text::create(
"loop enabled",
"Arial", 20);
461 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
475 if (type == Widget::TouchEventType::ENDED) {
500 Size winSize = Director::getInstance()->getVisibleSize();
513 HBox *upperHBox = HBox::create();
514 upperHBox->setTag(-200);
517 LinearLayoutParameter *params = LinearLayoutParameter::create();
518 params->setMargin(Margin(0.0f,0.0f,50.0f,0.0f));
520 LinearLayoutParameter *vparams = LinearLayoutParameter::create();
521 vparams->setMargin(Margin(10.0f, 0.0f, 0.0f, 140.0f));
522 upperHBox->setLayoutParameter(vparams);
525 for (
int i=0; i<count; ++i) {
526 VBox *firstVbox = VBox::create();
527 firstVbox->setScale(0.5);
528 firstVbox->setLayoutParameter(params);
529 firstVbox->setTag((i+1) * 100);
532 for (
int j=0; j<count1; ++j) {
533 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
534 w->setTouchEnabled(
true);
535 w->setTag(j+firstVbox->getTag()+1);
537 firstVbox->addChild(w);
540 upperHBox->addChild(firstVbox);
544 HBox *bottomHBox = HBox::create();
545 bottomHBox->setScale(0.5);
546 bottomHBox->setTag(600);
548 bottomHBox->setLayoutParameter(vparams);
550 LinearLayoutParameter *bottomParams = LinearLayoutParameter::create();
551 bottomParams->setMargin(Margin(0.0f, 0.0f, 8.0f, 0.0f));
552 for (
int i=0; i < count; ++i) {
553 ImageView *w = ImageView::create(
"cocosui/scrollviewbg.png");
554 w->setLayoutParameter(bottomParams);
555 w->setTouchEnabled(
true);
558 bottomHBox->addChild(w);
564 _loopText = Text::create(
"loop enabled",
"Arial", 20);
565 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
579 if (type == Widget::TouchEventType::ENDED) {
#define ADD_TEST_CASE(__className__)
cocos2d::Menu * _dpadMenu
cocos2d::ui::Widget * _firstFocusedWidget
cocos2d::EventListenerFocus * _eventListener
virtual void onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus)
virtual void onUpKeyPressed()
virtual void onLeftKeyPressed()
virtual void onDownKeyPressed()
virtual bool init() override
void onImageViewClicked(cocos2d::Ref *ref, cocos2d::ui::Widget::TouchEventType touchType)
virtual void onRightKeyPressed()
virtual ~UIFocusTestBase()
cocos2d::ui::Button * _toggleButton
virtual bool init() override
virtual ~UIFocusTestHorizontal()
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
cocos2d::ui::Layout * _horizontalLayout
cocos2d::ui::Text * _loopText
cocos2d::ui::Layout * _verticalLayout
UIFocusTestNestedLayout1()
virtual bool init() override
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
virtual ~UIFocusTestNestedLayout1()
cocos2d::ui::Text * _loopText
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
cocos2d::ui::Layout * _horizontalLayout
cocos2d::ui::Text * _loopText
UIFocusTestNestedLayout2()
virtual bool init() override
virtual ~UIFocusTestNestedLayout2()
virtual bool init() override
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
virtual ~UIFocusTestNestedLayout3()
cocos2d::ui::Layout * _verticalLayout
cocos2d::ui::Text * _loopText
UIFocusTestNestedLayout3()
virtual ~UIFocusTestVertical()
cocos2d::ui::Text * _loopText
virtual bool init() override
cocos2d::ui::Layout * _verticalLayout
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
cocos2d::Layer * _uiLayer
virtual bool init() override