From cc8b70b87d5b75293d23f768599183d62a12e401 Mon Sep 17 00:00:00 2001 From: andyque Date: Mon, 26 May 2014 17:34:56 +0800 Subject: [PATCH] issue #5176, refactor UIFocusManager and UIScrollView --- cocos/ui/UIFocusManager.cpp | 65 +++++++++++++++++-------------------- cocos/ui/UIFocusManager.h | 15 +++++---- cocos/ui/UIScrollView.cpp | 40 ++++++----------------- cocos/ui/UIScrollView.h | 5 --- 4 files changed, 49 insertions(+), 76 deletions(-) diff --git a/cocos/ui/UIFocusManager.cpp b/cocos/ui/UIFocusManager.cpp index 285195bdad..bef980541d 100644 --- a/cocos/ui/UIFocusManager.cpp +++ b/cocos/ui/UIFocusManager.cpp @@ -50,23 +50,12 @@ namespace ui { FocusManager::~FocusManager() { - if (nullptr != _keyboardListener) - { - EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher(); - dispatcher->removeEventListener(_keyboardListener); - _keyboardListener = nullptr; - } + this->removeKeyboardEventListener(); } void FocusManager::onKeypadKeyPressed(EventKeyboard::KeyCode keyCode, Event *event) { - - if (onKeypadReleased != nullptr) - { - onKeypadReleased(keyCode, event); - } - - if (_enableAndroidDpad) + if (_enableFocusNavigation && _firstFocusedWidget) { if (keyCode == EventKeyboard::KeyCode::KEY_DPAD_DOWN) { @@ -87,40 +76,46 @@ namespace ui { } } - void FocusManager::enableAndroidDpad(bool flag) + void FocusManager::enableFocusNavigation(bool flag) { - if (_enableAndroidDpad == flag) - { + if (_enableFocusNavigation == flag) return; - } - _enableAndroidDpad = flag; + _enableFocusNavigation = flag; if (flag) - { - if (nullptr == _keyboardListener) - { - _keyboardListener = EventListenerKeyboard::create(); - _keyboardListener->onKeyReleased = CC_CALLBACK_2(FocusManager::onKeypadKeyPressed, this); - EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher(); - dispatcher->addEventListenerWithFixedPriority(_keyboardListener, keyboardEventPriority); - } - } + this->addKeyboardEventListener(); else - { - if (nullptr != _keyboardListener) - { - EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher(); - dispatcher->removeEventListener(_keyboardListener); - _keyboardListener = nullptr; - } - } + this->removeKeyboardEventListener(); } void FocusManager::setFirstFocsuedWidget(Widget* widget) { _firstFocusedWidget = widget; } + + void FocusManager::addKeyboardEventListener() + { + if (nullptr == _keyboardListener) + { + CCASSERT(_firstFocusedWidget != nullptr, "Please set the first focused widget first!"); + + _keyboardListener = EventListenerKeyboard::create(); + _keyboardListener->onKeyReleased = CC_CALLBACK_2(FocusManager::onKeypadKeyPressed, this); + EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->addEventListenerWithFixedPriority(_keyboardListener, keyboardEventPriority); + } + } + + void FocusManager::removeKeyboardEventListener() + { + if (nullptr != _keyboardListener) + { + EventDispatcher* dispatcher = Director::getInstance()->getEventDispatcher(); + dispatcher->removeEventListener(_keyboardListener); + _keyboardListener = nullptr; + } + } } NS_CC_END \ No newline at end of file diff --git a/cocos/ui/UIFocusManager.h b/cocos/ui/UIFocusManager.h index 6953e69e0e..6847cf5b50 100644 --- a/cocos/ui/UIFocusManager.h +++ b/cocos/ui/UIFocusManager.h @@ -43,19 +43,22 @@ namespace ui { static FocusManager* getInstance(); static void destroyInstance(); - void enableAndroidDpad(bool flag); - void setFirstFocsuedWidget(Widget* widget); + void enableFocusNavigation(bool flag); - std::function onKeypadReleased; protected: + void setFirstFocsuedWidget(Widget* widget); + void onKeypadKeyPressed(EventKeyboard::KeyCode, Event*); + void addKeyboardEventListener(); + void removeKeyboardEventListener(); + + friend class Widget; private: FocusManager():_keyboardListener(nullptr), _firstFocusedWidget(nullptr), - _enableAndroidDpad(false), - onKeypadReleased(nullptr) + _enableFocusNavigation(false) {} ~FocusManager(); @@ -63,7 +66,7 @@ namespace ui { EventListenerKeyboard* _keyboardListener ; Widget* _firstFocusedWidget ; - bool _enableAndroidDpad ; + bool _enableFocusNavigation ; }; } diff --git a/cocos/ui/UIScrollView.cpp b/cocos/ui/UIScrollView.cpp index e24cc2575b..23b8ebb34c 100644 --- a/cocos/ui/UIScrollView.cpp +++ b/cocos/ui/UIScrollView.cpp @@ -136,10 +136,7 @@ void ScrollView::onEnter() { Layout::onEnter(); scheduleUpdate(); - _eventListener = EventListenerFocus::create(); - _eventListener->onFocusChanged = CC_CALLBACK_2(ScrollView::onFocusChanged, this); - - _eventDispatcher->addEventListenerWithFixedPriority(_eventListener, 1); + } bool ScrollView::init() @@ -880,14 +877,14 @@ bool ScrollView::scrollChildrenVertical(float touchOffsetX, float touchOffsetY) bool scrollEnabled = false; if (_bounceEnabled) { - float icBottomPos = _innerContainer->getBottomInParent(); + float icBottomPos = _innerContainer->getBottomBoundary(); if (icBottomPos + touchOffsetY >= _bounceBottomBoundary) { realOffset = _bounceBottomBoundary - icBottomPos; scrollToBottomEvent(); scrollEnabled = false; } - float icTopPos = _innerContainer->getTopInParent(); + float icTopPos = _innerContainer->getTopBoundary(); if (icTopPos + touchOffsetY <= _bounceTopBoundary) { realOffset = _bounceTopBoundary - icTopPos; @@ -898,14 +895,14 @@ bool ScrollView::scrollChildrenVertical(float touchOffsetX, float touchOffsetY) } else { - float icBottomPos = _innerContainer->getBottomInParent(); + float icBottomPos = _innerContainer->getBottomBoundary(); if (icBottomPos + touchOffsetY >= _bottomBoundary) { realOffset = _bottomBoundary - icBottomPos; scrollToBottomEvent(); scrollEnabled = false; } - float icTopPos = _innerContainer->getTopInParent(); + float icTopPos = _innerContainer->getTopBoundary(); if (icTopPos + touchOffsetY <= _topBoundary) { realOffset = _topBoundary - icTopPos; @@ -923,14 +920,14 @@ bool ScrollView::scrollChildrenHorizontal(float touchOffsetX, float touchOffestY float realOffset = touchOffsetX; if (_bounceEnabled) { - float icRightPos = _innerContainer->getRightInParent(); + float icRightPos = _innerContainer->getRightBoundary(); if (icRightPos + touchOffsetX <= _bounceRightBoundary) { realOffset = _bounceRightBoundary - icRightPos; scrollToRightEvent(); scrollenabled = false; } - float icLeftPos = _innerContainer->getLeftInParent(); + float icLeftPos = _innerContainer->getLeftBoundary(); if (icLeftPos + touchOffsetX >= _bounceLeftBoundary) { realOffset = _bounceLeftBoundary - icLeftPos; @@ -940,14 +937,14 @@ bool ScrollView::scrollChildrenHorizontal(float touchOffsetX, float touchOffestY } else { - float icRightPos = _innerContainer->getRightInParent(); + float icRightPos = _innerContainer->getRightBoundary(); if (icRightPos + touchOffsetX <= _rightBoundary) { realOffset = _rightBoundary - icRightPos; scrollToRightEvent(); scrollenabled = false; } - float icLeftPos = _innerContainer->getLeftInParent(); + float icLeftPos = _innerContainer->getLeftBoundary(); if (icLeftPos + touchOffsetX >= _leftBoundary) { realOffset = _leftBoundary - icLeftPos; @@ -968,7 +965,7 @@ bool ScrollView::scrollChildrenBoth(float touchOffsetX, float touchOffsetY) { if (touchOffsetX > 0.0f && touchOffsetY > 0.0f) // up right { - float icLeftPos = _innerContainer->getLeftInParent(); + float icLeftPos = _innerContainer->getLeftBoundary(); if (icLeftPos + touchOffsetX >= _bounceLeftBoundary) { @@ -1804,23 +1801,6 @@ Widget* ScrollView::findNextFocusedWidget(cocos2d::ui::Widget::FocusDirection di } } -void ScrollView::onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus) -{ - if (dynamic_cast(widgetGetFocus) || dynamic_cast(widgetLostFocus)) { - return; - } - if (this->getLayoutType() == Layout::Type::VERTICAL) { - float loseFocusWidgetBoundary = widgetLostFocus->getTopInParent(); - float getFocusWidgetBoundary = widgetGetFocus->getTopInParent(); - - if (loseFocusWidgetBoundary >= getFocusWidgetBoundary) { - CCLOG("down"); - } - else{ - CCLOG("up"); - } - } -} } diff --git a/cocos/ui/UIScrollView.h b/cocos/ui/UIScrollView.h index 8bfae7f546..c2daeb3de1 100644 --- a/cocos/ui/UIScrollView.h +++ b/cocos/ui/UIScrollView.h @@ -383,9 +383,6 @@ protected: void bounceLeftEvent(); void bounceRightEvent(); - - virtual void onFocusChanged(Widget* widgetLostFocus, Widget* widgetGetFocus); - protected: Layout* _innerContainer; @@ -432,8 +429,6 @@ protected: Vec2 _bounceDir; float _bounceOriginalSpeed; bool _inertiaScrollEnabled; - - EventListenerFocus *_eventListener; Ref* _scrollViewEventListener; #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))