mirror of https://github.com/axmolengine/axmol.git
issue #5176, refactor UIFocusManager and UIScrollView
This commit is contained in:
parent
9118d7a98a
commit
cc8b70b87d
|
@ -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
|
|
@ -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<void(EventKeyboard::KeyCode, Event*)> 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 ;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Layout*>(widgetGetFocus) || dynamic_cast<Layout*>(widgetLostFocus)) {
|
||||
return;
|
||||
}
|
||||
if (this->getLayoutType() == Layout::Type::VERTICAL) {
|
||||
float loseFocusWidgetBoundary = widgetLostFocus->getTopInParent();
|
||||
float getFocusWidgetBoundary = widgetGetFocus->getTopInParent();
|
||||
|
||||
if (loseFocusWidgetBoundary >= getFocusWidgetBoundary) {
|
||||
CCLOG("down");
|
||||
}
|
||||
else{
|
||||
CCLOG("up");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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)))
|
||||
|
|
Loading…
Reference in New Issue