From e52370e1c2776fdc0c955d679a5cf80f578a0f01 Mon Sep 17 00:00:00 2001 From: andyque Date: Tue, 6 May 2014 14:20:01 +0800 Subject: [PATCH] issue #4861, refactor UIFocusTest, simplify code --- .../UIFocusTest/UIFocusTest.cpp | 137 ++++++------------ .../UIFocusTest/UIFocusTest.h | 38 +++-- 2 files changed, 70 insertions(+), 105 deletions(-) diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp index 5d40587a41..3abd184f64 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UIFocusTest/UIFocusTest.cpp @@ -75,6 +75,52 @@ void UIFocusTestBase::onImageViewClicked(cocos2d::Ref *ref, TouchEventType touch } } +void UIFocusTestBase::onLeftKeyPressed() +{ + if (_firstFocusedWidget) { + _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Left, _firstFocusedWidget); + } +} + +void UIFocusTestBase::onRightKeyPressed() +{ + if (_firstFocusedWidget) { + _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Right, _firstFocusedWidget); + } +} + +void UIFocusTestBase::onUpKeyPressed() +{ + if (_firstFocusedWidget) { + _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Up, _firstFocusedWidget); + } + +} + +void UIFocusTestBase::onDownKeyPressed() +{ + if (_firstFocusedWidget) { + _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Down, _firstFocusedWidget); + } + +} + +void UIFocusTestBase::onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus) +{ + //only change the widgets' state + Layout *getLayout = dynamic_cast(widgetGetFocus); + if (!getLayout && widgetGetFocus && widgetGetFocus->isFocusEnabled()) { + widgetGetFocus->setScale(1.2); + widgetGetFocus->setColor(Color3B::RED); + } + + Layout *loseLayout = dynamic_cast(widgetLostFocus); + if (!loseLayout && widgetLostFocus && widgetLostFocus->isFocusEnabled()) { + widgetLostFocus->setScale(1.0); + widgetLostFocus->setColor(Color3B::WHITE); + } +} + //UIFocusTestHorizontal UIFocusTestHorizontal::UIFocusTestHorizontal() @@ -142,51 +188,6 @@ void UIFocusTestHorizontal::toggleFocusLoop(cocos2d::Ref * pObjc, TouchEventType } } -void UIFocusTestHorizontal::onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus) -{ - //only change the widgets' state - Layout *getLayout = dynamic_cast(widgetGetFocus); - if (!getLayout && widgetGetFocus && widgetGetFocus->isFocusEnabled()) { - widgetGetFocus->setScale(1.2); - widgetGetFocus->setColor(Color3B::RED); - } - - Layout *loseLayout = dynamic_cast(widgetLostFocus); - if (!loseLayout && widgetLostFocus && widgetLostFocus->isFocusEnabled()) { - widgetLostFocus->setScale(1.0); - widgetLostFocus->setColor(Color3B::WHITE); - } -} - -void UIFocusTestHorizontal::onLeftKeyPressed() -{ - if (_firstFocusedWidget) { - _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Left, _firstFocusedWidget); - } -} - -void UIFocusTestHorizontal::onRightKeyPressed() -{ - if (_firstFocusedWidget) { - _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Right, _firstFocusedWidget); - } -} - -void UIFocusTestHorizontal::onUpKeyPressed() -{ - if (_firstFocusedWidget) { - _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Up, _firstFocusedWidget); - } - -} - -void UIFocusTestHorizontal::onDownKeyPressed() -{ - if (_firstFocusedWidget) { - _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Down, _firstFocusedWidget); - } - -} //UIFocusTestVertical UIFocusTestVertical::UIFocusTestVertical() @@ -253,48 +254,4 @@ void UIFocusTestVertical::toggleFocusLoop(cocos2d::Ref * pObjc, TouchEventType t } } -void UIFocusTestVertical::onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus) -{ - //only change the widgets' state - Layout *getLayout = dynamic_cast(widgetGetFocus); - if (!getLayout && widgetGetFocus && widgetGetFocus->isFocusEnabled()) { - widgetGetFocus->setScale(1.2); - widgetGetFocus->setColor(Color3B::RED); - } - - Layout *loseLayout = dynamic_cast(widgetLostFocus); - if (!loseLayout && widgetLostFocus && widgetLostFocus->isFocusEnabled()) { - widgetLostFocus->setScale(1.0); - widgetLostFocus->setColor(Color3B::WHITE); - } -} - -void UIFocusTestVertical::onLeftKeyPressed() -{ - if (_firstFocusedWidget) { - _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Left, _firstFocusedWidget); - } -} - -void UIFocusTestVertical::onRightKeyPressed() -{ - if (_firstFocusedWidget) { - _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Right, _firstFocusedWidget); - } -} - -void UIFocusTestVertical::onUpKeyPressed() -{ - if (_firstFocusedWidget) { - _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Up, _firstFocusedWidget); - } - -} - -void UIFocusTestVertical::onDownKeyPressed() -{ - if (_firstFocusedWidget) { - _firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Down, _firstFocusedWidget); - } - -} \ No newline at end of file +//UIFocusTestNestedLayout1 diff --git a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UIFocusTest/UIFocusTest.h b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UIFocusTest/UIFocusTest.h index 33448a79eb..c41f36c1e3 100644 --- a/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UIFocusTest/UIFocusTest.h +++ b/tests/cpp-tests/Classes/ExtensionsTest/CocoStudioGUITest/UIFocusTest/UIFocusTest.h @@ -20,11 +20,11 @@ public: virtual ~UIFocusTestBase(); bool init(); - virtual void onLeftKeyPressed() = 0; - virtual void onRightKeyPressed() = 0; - virtual void onUpKeyPressed() = 0; - virtual void onDownKeyPressed() = 0; - virtual void onFocusChanged(Widget* widgetLostFocus, Widget* widgetGetFocus) = 0; + virtual void onLeftKeyPressed(); + virtual void onRightKeyPressed(); + virtual void onUpKeyPressed(); + virtual void onDownKeyPressed(); + virtual void onFocusChanged(Widget* widgetLostFocus, Widget* widgetGetFocus); void onImageViewClicked(Ref* ref, TouchEventType touchType); @@ -40,11 +40,7 @@ public: UIFocusTestHorizontal(); virtual ~UIFocusTestHorizontal(); bool init(); - virtual void onLeftKeyPressed() ; - virtual void onRightKeyPressed(); - virtual void onUpKeyPressed() ; - virtual void onDownKeyPressed() ; - virtual void onFocusChanged(Widget* widgetLostFocus, Widget* widgetGetFocus); + void toggleFocusLoop(Ref*,TouchEventType); @@ -60,11 +56,7 @@ public: UIFocusTestVertical(); virtual ~UIFocusTestVertical(); bool init(); - virtual void onLeftKeyPressed() ; - virtual void onRightKeyPressed(); - virtual void onUpKeyPressed() ; - virtual void onDownKeyPressed() ; - virtual void onFocusChanged(Widget* widgetLostFocus, Widget* widgetGetFocus); + void toggleFocusLoop(Ref*,TouchEventType); protected: @@ -73,5 +65,21 @@ protected: Text *_loopText; }; +class UIFocusTestNestedLayout1 : public UIFocusTestBase +{ +public: + UIFocusTestNestedLayout1(); + virtual ~UIFocusTestNestedLayout1(); + bool init(); + + + void toggleFocusLoop(Ref*,TouchEventType); +protected: + UI_SCENE_CREATE_FUNC(UIFocusTestNestedLayout1); + Layout *_verticalLayout; + Text *_loopText; +}; + + #endif /* defined(__cocos2d_tests__UIFocusTest__) */