PuzzleSDK
UIFocusTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25//
26// UIFocusTest.cpp
27// cocos2d_tests
28//
29// Created by guanghui on 5/4/14.
30//
31//
32
33#include "UIFocusTest.h"
34
36using namespace cocos2d::ui;
37
38UIFocusTests::UIFocusTests()
39{
45}
46
48{
49
50}
51
53{
54 _eventDispatcher->removeEventListener(_eventListener);
55}
56
58{
59 if (UIScene::init()) {
60 Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
61
62 Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
63 background->removeFromParentAndCleanup(true);
64
65 _dpadMenu = Menu::create();
66
67 auto winSize = Director::getInstance()->getVisibleSize();
68 auto leftItem = MenuItemFont::create("Left", CC_CALLBACK_0(UIFocusTestBase::onLeftKeyPressed, this));
69 leftItem->setPosition(Vec2(winSize.width - 100, winSize.height/2));
70 _dpadMenu->addChild(leftItem);
71
72
73 auto rightItem = MenuItemFont::create("Right", CC_CALLBACK_0(UIFocusTestBase::onRightKeyPressed, this));
74 rightItem->setPosition(Vec2(winSize.width - 30, winSize.height/2));
75 _dpadMenu->addChild(rightItem);
76
77 auto upItem = MenuItemFont::create("Up", CC_CALLBACK_0(UIFocusTestBase::onUpKeyPressed, this));
78 upItem->setPosition(Vec2(winSize.width - 60, winSize.height/2 + 50));
79 _dpadMenu->addChild(upItem);
80
81 auto downItem = MenuItemFont::create("Down", CC_CALLBACK_0(UIFocusTestBase::onDownKeyPressed, this));
82 downItem->setPosition(Vec2(winSize.width - 60, winSize.height/2 - 50));
83 _dpadMenu->addChild(downItem);
84
85 _dpadMenu->setPosition(Vec2::ZERO);
86 _uiLayer->addChild(_dpadMenu);
87
88 //call this method to enable Dpad focus navigation
89 Widget::enableDpadNavigation(true);
90
91 _eventListener = EventListenerFocus::create();
92 _eventListener->onFocusChanged = CC_CALLBACK_2(UIFocusTestBase::onFocusChanged, this);
93
94 _eventDispatcher->addEventListenerWithFixedPriority(_eventListener, 1);
95
96 _toggleButton = Button::create("cocosui/switch-mask.png");
97 _toggleButton->setTitleText("Toggle Loop");
98 _toggleButton->setPosition(Vec2(60.0f, winSize.height - 50));
99 _toggleButton->setTitleColor(Color3B::RED);
100 _toggleButton->setFocusEnabled(false);
101 this->addChild(_toggleButton);
102
103 return true;
104 }
105
106 return false;
107}
108
109void UIFocusTestBase::onImageViewClicked(cocos2d::Ref *ref, Widget::TouchEventType touchType)
110{
111 if (touchType == Widget::TouchEventType::ENDED) {
112 Widget *w = (Widget*)ref;
113 if (w->isFocusEnabled()) {
114 w->setFocusEnabled(false);
115 w->setColor(Color3B::YELLOW);
116 }else{
117 w->setFocusEnabled(true);
118 w->setColor(Color3B::WHITE);
119 }
120 }
121}
122
124{
125 cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_LEFT;
126 cocos2d::EventKeyboard event(cocos2dKey, false);
127 cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
128}
129
131{
132 cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_RIGHT;
133 cocos2d::EventKeyboard event(cocos2dKey, false);
134 cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
135}
136
138{
139 cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_UP;
140 cocos2d::EventKeyboard event(cocos2dKey, false);
141 cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
142
143}
144
146{
147 cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_DOWN;
148 cocos2d::EventKeyboard event(cocos2dKey, false);
149 cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
150
151}
152
153void UIFocusTestBase::onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus)
154{
155 if (widgetGetFocus && widgetGetFocus->isFocusEnabled()) {
156 widgetGetFocus->setColor(Color3B::RED);
157 }
158
159 if (widgetLostFocus && widgetLostFocus->isFocusEnabled()) {
160 widgetLostFocus->setColor(Color3B::WHITE);
161 }
162
163 if (widgetLostFocus && widgetGetFocus) {
164 CCLOG("on focus change, %d widget get focus, %d widget lose focus", widgetGetFocus->getTag(), widgetLostFocus->getTag());
165 }
166}
167
168
169//UIFocusTestHorizontal
171{
172
173}
174
176{
177
178}
179
181{
182 if (UIFocusTestBase::init()) {
183
184 Size winSize = Director::getInstance()->getVisibleSize();
185
186 _horizontalLayout = HBox::create();
187 _horizontalLayout->setPosition(Vec2(20.0f, winSize.height/2 + 40));
188 _uiLayer->addChild(_horizontalLayout);
189
190 _horizontalLayout->setFocused(true);
191 _horizontalLayout->setLoopFocus(true);
192 _horizontalLayout->setTag(100);
194
195 int count = 3;
196 for (int i=0; i<count; ++i) {
197 ImageView *w = ImageView::create("cocosui/scrollviewbg.png");
198 w->setTouchEnabled(true);
199 w->setTag(i);
200 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestHorizontal::onImageViewClicked, this));
201 _horizontalLayout->addChild(w);
202 }
203
204 _loopText = Text::create("loop enabled", "Arial", 20);
205 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
206 _loopText->setColor(Color3B::GREEN);
207 this->addChild(_loopText);
208
209 _toggleButton->addTouchEventListener(CC_CALLBACK_2(UIFocusTestHorizontal::toggleFocusLoop,this));
210
211 return true;
212 }
213 return false;
214}
215
216
217
218
219void UIFocusTestHorizontal::toggleFocusLoop(cocos2d::Ref * pObjc, Widget::TouchEventType type)
220{
221 if (type == Widget::TouchEventType::ENDED) {
222 _horizontalLayout->setLoopFocus(!_horizontalLayout->isLoopFocus());
223 if (_horizontalLayout->isLoopFocus()) {
224 _loopText->setString("loop enabled");
225 }else{
226 _loopText->setString("loop disabled");
227 }
228 }
229}
230
231
232//UIFocusTestVertical
234{
235
236}
237
239{
240
241}
242
244{
245 if (UIFocusTestBase::init()) {
246
247 Size winSize = Director::getInstance()->getVisibleSize();
248
249 _verticalLayout = VBox::create();
250 _verticalLayout->setPosition(Vec2(winSize.width/2 - 100, winSize.height - 70));
251 _uiLayer->addChild(_verticalLayout);
252 _verticalLayout->setTag(100);
253 _verticalLayout->setScale(0.5);
254
255 _verticalLayout->setFocused(true);
256 _verticalLayout->setLoopFocus(true);
258
259 int count = 3;
260 for (int i=0; i<count; ++i) {
261 ImageView *w = ImageView::create("cocosui/scrollviewbg.png");
262 w->setTouchEnabled(true);
263 w->setTag(i);
264 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestVertical::onImageViewClicked, this));
265 _verticalLayout->addChild(w);
266 }
267
268 _loopText = Text::create("loop enabled", "Arial", 20);
269 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
270 _loopText->setColor(Color3B::GREEN);
271 this->addChild(_loopText);
272
273 _toggleButton->addTouchEventListener(CC_CALLBACK_2(UIFocusTestVertical::toggleFocusLoop, this));
274
275 return true;
276 }
277 return false;
278}
279
280
281void UIFocusTestVertical::toggleFocusLoop(cocos2d::Ref * pObjc, Widget::TouchEventType type)
282{
283 if (type == Widget::TouchEventType::ENDED) {
284 _verticalLayout->setLoopFocus(!_verticalLayout->isLoopFocus());
285 if (_verticalLayout->isLoopFocus()) {
286 _loopText->setString("loop enabled");
287 }else{
288 _loopText->setString("loop disabled");
289 }
290 }
291}
292
293//UIFocusTestNestedLayout1
295{
296
297}
298
300{
301
302}
303
305{
306 if (UIFocusTestBase::init()) {
307
308 Size winSize = Director::getInstance()->getVisibleSize();
309
310 _verticalLayout = VBox::create();
311 _verticalLayout->setPosition(Vec2(winSize.width/2 - 80, winSize.height - 70));
312 _uiLayer->addChild(_verticalLayout);
313 _verticalLayout->setScale(0.5);
314
315 _verticalLayout->setFocused(true);
316 _verticalLayout->setLoopFocus(true);
317 _verticalLayout->setTag(100);
319
320 int count1 = 1;
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);
325 w->setScaleX(2.5);
326 w->setTag(i+count1);
327 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestNestedLayout1::onImageViewClicked, this));
328 _verticalLayout->addChild(w);
329 }
330
331 //add HBox into VBox
332 HBox *hbox = HBox::create();
333 hbox->setScale(0.8f);
334 hbox->setTag(101);
335 _verticalLayout->addChild(hbox);
336
337 int count2 = 2;
338 for (int i=0; i < count2; ++i) {
339 ImageView *w = ImageView::create("cocosui/scrollviewbg.png");
340 w->setAnchorPoint(Vec2(0,1));
341 w->setScaleY(2.0);
342 w->setTouchEnabled(true);
343 w->setTag(i+count1+count2);
344 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestNestedLayout1::onImageViewClicked, this));
345 hbox->addChild(w);
346 }
347
348 VBox *innerVBox = VBox::create();
349 hbox->addChild(innerVBox);
350 innerVBox->setTag(102);
351// innerVBox->setPassFocusToChild(false);
352// innerVBox->setFocusEnabled(false);
353
354
355 int count3 = 2;
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);
360 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestNestedLayout1::onImageViewClicked, this));
361 innerVBox->addChild(w);
362 }
363
364 _loopText = Text::create("loop enabled", "Arial", 20);
365 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
366 _loopText->setColor(Color3B::GREEN);
367 this->addChild(_loopText);
368
369 _toggleButton->addTouchEventListener(CC_CALLBACK_2(UIFocusTestNestedLayout1::toggleFocusLoop, this));
370
371 return true;
372 }
373 return false;
374}
375
376
377void UIFocusTestNestedLayout1::toggleFocusLoop(cocos2d::Ref * pObjc, Widget::TouchEventType type)
378{
379 if (type == Widget::TouchEventType::ENDED) {
380 _verticalLayout->setLoopFocus(!_verticalLayout->isLoopFocus());
381 if (_verticalLayout->isLoopFocus()) {
382 _loopText->setString("loop enabled");
383 }else{
384 _loopText->setString("loop disabled");
385 }
386 }
387}
388
389//UIFocusTestNestedLayout2
391{
392
393}
394
396{
397
398}
399
401{
402 if (UIFocusTestBase::init()) {
403
404 Size winSize = Director::getInstance()->getVisibleSize();
405
406 _horizontalLayout = HBox::create();
407 _horizontalLayout->setPosition(Vec2(winSize.width/2 - 200, winSize.height - 70));
408 _uiLayer->addChild(_horizontalLayout);
409 _horizontalLayout->setScale(0.6f);
410
411 _horizontalLayout->setFocused(true);
412 _horizontalLayout->setLoopFocus(true);
413 _horizontalLayout->setTag(100);
415
416 int count1 = 2;
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);
421 w->setTag(i+count1);
422 w->setScaleY(2.4f);
423 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestNestedLayout2::onImageViewClicked, this));
424 _horizontalLayout->addChild(w);
425 }
426
427 //add HBox into VBox
428 VBox *vbox = VBox::create();
429 vbox->setScale(0.8f);
430 vbox->setTag(101);
431 _horizontalLayout->addChild(vbox);
432
433 int count2 = 2;
434 for (int i=0; i < count2; ++i) {
435 ImageView *w = ImageView::create("cocosui/scrollviewbg.png");
436 w->setAnchorPoint(Vec2(0,1));
437 w->setScaleX(2.0);
438 w->setTouchEnabled(true);
439 w->setTag(i+count1+count2);
440 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestNestedLayout2::onImageViewClicked, this));
441 vbox->addChild(w);
442 }
443
444 HBox *innerHBox = HBox::create();
445 vbox->addChild(innerHBox);
446 innerHBox->setTag(102);
447 // innerVBox->setPassFocusToChild(false);
448 // innerVBox->setFocusEnabled(false);
449
450
451 int count3 = 2;
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);
456 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestNestedLayout2::onImageViewClicked, this));
457 innerHBox->addChild(w);
458 }
459
460 _loopText = Text::create("loop enabled", "Arial", 20);
461 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
462 _loopText->setColor(Color3B::GREEN);
463 this->addChild(_loopText);
464
465 _toggleButton->addTouchEventListener(CC_CALLBACK_2(UIFocusTestNestedLayout2::toggleFocusLoop, this));
466
467 return true;
468 }
469 return false;
470}
471
472
473void UIFocusTestNestedLayout2::toggleFocusLoop(cocos2d::Ref * pObjc, Widget::TouchEventType type)
474{
475 if (type == Widget::TouchEventType::ENDED) {
476 _horizontalLayout->setLoopFocus(!_horizontalLayout->isLoopFocus());
477 if (_horizontalLayout->isLoopFocus()) {
478 _loopText->setString("loop enabled");
479 }else{
480 _loopText->setString("loop disabled");
481 }
482 }
483}
484
485//UIFocusTestNestedLayout3
487{
488
489}
490
492{
493
494}
495
497{
498 if (UIFocusTestBase::init()) {
499
500 Size winSize = Director::getInstance()->getVisibleSize();
501
502 _verticalLayout = VBox::create();
503 _verticalLayout->setPosition(Vec2(40.0f, winSize.height - 70));
504 _uiLayer->addChild(_verticalLayout);
505 _verticalLayout->setScale(0.8f);
506
507 _verticalLayout->setFocused(true);
508 _verticalLayout->setLoopFocus(true);
509 _verticalLayout->setTag(-1000);
511
512
513 HBox *upperHBox = HBox::create();
514 upperHBox->setTag(-200);
515 _verticalLayout->addChild(upperHBox);
516
517 LinearLayoutParameter *params = LinearLayoutParameter::create();
518 params->setMargin(Margin(0.0f,0.0f,50.0f,0.0f));
519
520 LinearLayoutParameter *vparams = LinearLayoutParameter::create();
521 vparams->setMargin(Margin(10.0f, 0.0f, 0.0f, 140.0f));
522 upperHBox->setLayoutParameter(vparams);
523
524 int count = 3;
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);
530
531 int count1 = 3;
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);
536 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestBase::onImageViewClicked, this));
537 firstVbox->addChild(w);
538 }
539
540 upperHBox->addChild(firstVbox);
541
542 }
543
544 HBox *bottomHBox = HBox::create();
545 bottomHBox->setScale(0.5);
546 bottomHBox->setTag(600);
547
548 bottomHBox->setLayoutParameter(vparams);
549 count = 3;
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);
556 w->setTag(i+601);
557 w->addTouchEventListener(CC_CALLBACK_2(UIFocusTestBase::onImageViewClicked, this));
558 bottomHBox->addChild(w);
559 }
560 _verticalLayout->addChild(bottomHBox);
561
562
563
564 _loopText = Text::create("loop enabled", "Arial", 20);
565 _loopText->setPosition(Vec2(winSize.width/2, winSize.height - 50));
566 _loopText->setColor(Color3B::GREEN);
567 this->addChild(_loopText);
568
569 _toggleButton->addTouchEventListener(CC_CALLBACK_2(UIFocusTestNestedLayout3::toggleFocusLoop, this));
570
571 return true;
572 }
573 return false;
574}
575
576
577void UIFocusTestNestedLayout3::toggleFocusLoop(cocos2d::Ref * pObjc, Widget::TouchEventType type)
578{
579 if (type == Widget::TouchEventType::ENDED) {
580 _verticalLayout->setLoopFocus(!_verticalLayout->isLoopFocus());
581 if (_verticalLayout->isLoopFocus()) {
582 _loopText->setString("loop enabled");
583 }else{
584 _loopText->setString("loop disabled");
585 }
586 }
587}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC
Definition: UIFocusTest.cpp:35
cocos2d::Menu * _dpadMenu
Definition: UIFocusTest.h:57
cocos2d::ui::Widget * _firstFocusedWidget
Definition: UIFocusTest.h:58
cocos2d::EventListenerFocus * _eventListener
Definition: UIFocusTest.h:59
virtual void onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus)
virtual void onUpKeyPressed()
virtual void onLeftKeyPressed()
virtual void onDownKeyPressed()
virtual bool init() override
Definition: UIFocusTest.cpp:57
void onImageViewClicked(cocos2d::Ref *ref, cocos2d::ui::Widget::TouchEventType touchType)
virtual void onRightKeyPressed()
virtual ~UIFocusTestBase()
Definition: UIFocusTest.cpp:52
cocos2d::ui::Button * _toggleButton
Definition: UIFocusTest.h:60
virtual bool init() override
virtual ~UIFocusTestHorizontal()
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
cocos2d::ui::Layout * _horizontalLayout
Definition: UIFocusTest.h:76
cocos2d::ui::Text * _loopText
Definition: UIFocusTest.h:77
cocos2d::ui::Layout * _verticalLayout
Definition: UIFocusTest.h:108
virtual bool init() override
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
virtual ~UIFocusTestNestedLayout1()
cocos2d::ui::Text * _loopText
Definition: UIFocusTest.h:109
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
cocos2d::ui::Layout * _horizontalLayout
Definition: UIFocusTest.h:124
cocos2d::ui::Text * _loopText
Definition: UIFocusTest.h:125
virtual bool init() override
virtual ~UIFocusTestNestedLayout2()
virtual bool init() override
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
virtual ~UIFocusTestNestedLayout3()
cocos2d::ui::Layout * _verticalLayout
Definition: UIFocusTest.h:140
cocos2d::ui::Text * _loopText
Definition: UIFocusTest.h:141
virtual ~UIFocusTestVertical()
cocos2d::ui::Text * _loopText
Definition: UIFocusTest.h:93
virtual bool init() override
cocos2d::ui::Layout * _verticalLayout
Definition: UIFocusTest.h:92
void toggleFocusLoop(cocos2d::Ref *, cocos2d::ui::Widget::TouchEventType)
cocos2d::Layer * _uiLayer
Definition: UIScene.h:44
virtual bool init() override
Definition: UIScene.cpp:46