From 9cef55d62cdda737f11a4ffe5b5d7848c27d8dcc Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 27 May 2014 09:49:34 +0800 Subject: [PATCH] issue #5176, refactor FocusNavigationController --- cocos/ui/UIWidget.cpp | 2 - .../UIFocusTest/UIFocusTest.cpp | 39 +++++++------------ 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/cocos/ui/UIWidget.cpp b/cocos/ui/UIWidget.cpp index e327bd7732..b1258e77a7 100644 --- a/cocos/ui/UIWidget.cpp +++ b/cocos/ui/UIWidget.cpp @@ -109,8 +109,6 @@ void Widget::FocusNavigationController::addKeyboardEventListener() { if (nullptr == _keyboardListener) { - CCASSERT(_firstFocusedWidget != nullptr, "Please set the first focused widget first!"); - _keyboardListener = EventListenerKeyboard::create(); _keyboardListener->onKeyReleased = CC_CALLBACK_2(Widget::FocusNavigationController::onKeypadKeyPressed, this); EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher(); diff --git a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp index c4696296e8..718e5dc673 100644 --- a/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp +++ b/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp @@ -49,6 +49,9 @@ bool UIFocusTestBase::init() _dpadMenu->setPosition(Vec2::ZERO); _uiLayer->addChild(_dpadMenu); + //call this method to enable Dpad focus navigation + Widget::enableDpadNavigation(true); + _eventListener = EventListenerFocus::create(); _eventListener->onFocusChanged = CC_CALLBACK_2(UIFocusTestBase::onFocusChanged, this); @@ -76,43 +79,31 @@ void UIFocusTestBase::onImageViewClicked(cocos2d::Ref *ref, Widget::TouchEventTy void UIFocusTestBase::onLeftKeyPressed() { - if (_firstFocusedWidget) { - if (!_firstFocusedWidget->isFocused()) { - _firstFocusedWidget = _firstFocusedWidget->getCurrentFocusedWidget(); - } - _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::LEFT, _firstFocusedWidget); - } + cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_LEFT; + cocos2d::EventKeyboard event(cocos2dKey, false); + cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } void UIFocusTestBase::onRightKeyPressed() { - if (_firstFocusedWidget) { - if (!_firstFocusedWidget->isFocused()) { - _firstFocusedWidget = _firstFocusedWidget->getCurrentFocusedWidget(); - } - _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::RIGHT, _firstFocusedWidget); - } + cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_RIGHT; + cocos2d::EventKeyboard event(cocos2dKey, false); + cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } void UIFocusTestBase::onUpKeyPressed() { - if (_firstFocusedWidget) { - if (!_firstFocusedWidget->isFocused()) { - _firstFocusedWidget = _firstFocusedWidget->getCurrentFocusedWidget(); - } - _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::UP, _firstFocusedWidget); - } + cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_UP; + cocos2d::EventKeyboard event(cocos2dKey, false); + cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); } void UIFocusTestBase::onDownKeyPressed() { - if (_firstFocusedWidget) { - if (!_firstFocusedWidget->isFocused()) { - _firstFocusedWidget = _firstFocusedWidget->getCurrentFocusedWidget(); - } - _firstFocusedWidget = _firstFocusedWidget->findNextFocusedWidget(Widget::FocusDirection::DOWN, _firstFocusedWidget); - } + cocos2d::EventKeyboard::KeyCode cocos2dKey =EventKeyboard::KeyCode::KEY_DPAD_DOWN; + cocos2d::EventKeyboard event(cocos2dKey, false); + cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); }