issue #4861, refactor UIFocusTest, simplify code

This commit is contained in:
andyque 2014-05-06 14:20:01 +08:00
parent ad4e1f1e2c
commit e52370e1c2
2 changed files with 70 additions and 105 deletions

View File

@ -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<Layout*>(widgetGetFocus);
if (!getLayout && widgetGetFocus && widgetGetFocus->isFocusEnabled()) {
widgetGetFocus->setScale(1.2);
widgetGetFocus->setColor(Color3B::RED);
}
Layout *loseLayout = dynamic_cast<Layout*>(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<Layout*>(widgetGetFocus);
if (!getLayout && widgetGetFocus && widgetGetFocus->isFocusEnabled()) {
widgetGetFocus->setScale(1.2);
widgetGetFocus->setColor(Color3B::RED);
}
Layout *loseLayout = dynamic_cast<Layout*>(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<Layout*>(widgetGetFocus);
if (!getLayout && widgetGetFocus && widgetGetFocus->isFocusEnabled()) {
widgetGetFocus->setScale(1.2);
widgetGetFocus->setColor(Color3B::RED);
}
Layout *loseLayout = dynamic_cast<Layout*>(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);
}
}
//UIFocusTestNestedLayout1

View File

@ -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__) */