issue #4861. add a horizontal focus test

This commit is contained in:
andyque 2014-05-04 16:31:24 +08:00
parent 9383900123
commit a7fbd590dc
8 changed files with 222 additions and 3 deletions

View File

@ -828,6 +828,8 @@
1AF152DA18FD252A00A52F3D /* PerformanceCallbackTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1AF152D718FD252A00A52F3D /* PerformanceCallbackTest.cpp */; };
1F33634F18E37E840074764D /* RefPtrTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1F33634D18E37E840074764D /* RefPtrTest.cpp */; };
1F33635018E37E840074764D /* RefPtrTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1F33634D18E37E840074764D /* RefPtrTest.cpp */; };
296CAD2E19161CCC00C64FBF /* UIFocusTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 296CAD2C19161CCC00C64FBF /* UIFocusTest.cpp */; };
296CAD2F19161CCC00C64FBF /* UIFocusTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 296CAD2C19161CCC00C64FBF /* UIFocusTest.cpp */; };
298B28F818CFF9EC00C1920F /* cocosui in Resources */ = {isa = PBXBuildFile; fileRef = 298B28F718CFF9EC00C1920F /* cocosui */; };
298B28F918CFF9EC00C1920F /* cocosui in Resources */ = {isa = PBXBuildFile; fileRef = 298B28F718CFF9EC00C1920F /* cocosui */; };
298B28FA18CFF9EC00C1920F /* cocosui in Resources */ = {isa = PBXBuildFile; fileRef = 298B28F718CFF9EC00C1920F /* cocosui */; };
@ -1865,6 +1867,8 @@
1D6058910D05DD3D006BFB54 /* cpp-tests Mac.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "cpp-tests Mac.app"; sourceTree = BUILT_PRODUCTS_DIR; };
1F33634D18E37E840074764D /* RefPtrTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RefPtrTest.cpp; sourceTree = "<group>"; };
1F33634E18E37E840074764D /* RefPtrTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RefPtrTest.h; sourceTree = "<group>"; };
296CAD2C19161CCC00C64FBF /* UIFocusTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UIFocusTest.cpp; path = UIFocusTest/UIFocusTest.cpp; sourceTree = "<group>"; };
296CAD2D19161CCC00C64FBF /* UIFocusTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = UIFocusTest.h; path = UIFocusTest/UIFocusTest.h; sourceTree = "<group>"; };
298B28F718CFF9EC00C1920F /* cocosui */ = {isa = PBXFileReference; lastKnownFileType = folder; name = cocosui; path = "../tests/cpp-tests/Resources/cocosui"; sourceTree = "<group>"; };
46A15F9C1807A4F8005B8026 /* cocos2d_libs.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; path = cocos2d_libs.xcodeproj; sourceTree = "<group>"; };
A035A71117822E9E00987F6C /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = usr/lib/libsqlite3.dylib; sourceTree = SDKROOT; };
@ -2725,6 +2729,7 @@
1AC359EC18CECF0B00F37B72 /* CocoStudioGUITest */ = {
isa = PBXGroup;
children = (
296CAD3219161CD500C64FBF /* UIFocusTest */,
1AC359ED18CECF0B00F37B72 /* CocosGUIScene.cpp */,
1AC359EE18CECF0B00F37B72 /* CocosGUIScene.h */,
1AC359EF18CECF0B00F37B72 /* CocoStudioGUITest.cpp */,
@ -3724,6 +3729,15 @@
name = tests;
sourceTree = "<group>";
};
296CAD3219161CD500C64FBF /* UIFocusTest */ = {
isa = PBXGroup;
children = (
296CAD2C19161CCC00C64FBF /* UIFocusTest.cpp */,
296CAD2D19161CCC00C64FBF /* UIFocusTest.h */,
);
name = UIFocusTest;
sourceTree = "<group>";
};
29B97314FDCFA39411CA2CEA /* CustomTemplate */ = {
isa = PBXGroup;
children = (
@ -4697,6 +4711,7 @@
1AC35B6518CECF0C00F37B72 /* EffectsTest.cpp in Sources */,
1AC35B7118CECF0C00F37B72 /* TestHeaderLayer.cpp in Sources */,
1AC35B2118CECF0C00F37B72 /* ActionManagerTest.cpp in Sources */,
296CAD2E19161CCC00C64FBF /* UIFocusTest.cpp in Sources */,
1AC35BA318CECF0C00F37B72 /* UIImageViewTest_Editor.cpp in Sources */,
1AC35BCF18CECF0C00F37B72 /* UITextFieldTest.cpp in Sources */,
1AC35B9B18CECF0C00F37B72 /* UIButtonTest_Editor.cpp in Sources */,
@ -4868,6 +4883,7 @@
1AC35BDE18CECF0C00F37B72 /* acts.cpp in Sources */,
1AC35B7618CECF0C00F37B72 /* ArmatureScene.cpp in Sources */,
1AC35B8E18CECF0C00F37B72 /* CustomImageView.cpp in Sources */,
296CAD2F19161CCC00C64FBF /* UIFocusTest.cpp in Sources */,
1AC35B8218CECF0C00F37B72 /* SceneController.cpp in Sources */,
1AC35B3418CECF0C00F37B72 /* Test.cpp in Sources */,
1AC35C2418CECF0C00F37B72 /* ParticleTest.cpp in Sources */,

View File

@ -69,6 +69,8 @@ THE SOFTWARE.
#include "base/CCEventListenerAcceleration.h"
#include "base/CCEventCustom.h"
#include "base/CCEventListenerCustom.h"
#include "base/CCEventFocus.h"
#include "base/CCEventListenerFocus.h"
// math
#include "math/CCAffineTransform.h"

View File

@ -1015,9 +1015,9 @@ void Widget::dispatchFocusEvent(cocos2d::ui::Widget *widgetLoseFocus, cocos2d::u
widgetLoseFocus->onFocusChanged(widgetLoseFocus, widgetGetFocus);
}
// EventFocus event(widgetLoseFocus, widgetGetFocus);
// auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher();
// dispatcher->dispatchEvent(&event);
EventFocus event(widgetLoseFocus, widgetGetFocus);
auto dispatcher = cocos2d::Director::getInstance()->getEventDispatcher();
dispatcher->dispatchEvent(&event);
}

View File

@ -267,6 +267,18 @@ g_guisTests[] =
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui focus test",
[](Ref* sender)
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(KUIFocusTest);
pManager->setMinUISceneId(KUIFocusTest);
pManager->setMaxUISceneId(KUIFocusTest);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
}
};
static const int g_maxTests = sizeof(g_guisTests) / sizeof(g_guisTests[0]);

View File

@ -0,0 +1,137 @@
//
// UIFocusTest.cpp
// cocos2d_tests
//
// Created by guanghui on 5/4/14.
//
//
#include "UIFocusTest.h"
UIFocusTestBase::UIFocusTestBase()
{
}
UIFocusTestBase::~UIFocusTestBase()
{
_eventDispatcher->removeEventListener(_eventListener);
}
bool UIFocusTestBase::init()
{
if (UIScene::init()) {
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
Layout* background = dynamic_cast<Layout*>(root->getChildByName("background_Panel"));
background->removeFromParentAndCleanup(true);
_dpadMenu = Menu::create();
auto winSize = Director::getInstance()->getVisibleSize();
auto leftItem = MenuItemFont::create("Left", CC_CALLBACK_0(UIFocusTestBase::onLeftKeyPressed, this));
leftItem->setPosition(Vector2(winSize.width - 100, winSize.height/2));
_dpadMenu->addChild(leftItem);
auto rightItem = MenuItemFont::create("Right", CC_CALLBACK_0(UIFocusTestBase::onRightKeyPressed, this));
rightItem->setPosition(Vector2(winSize.width - 30, winSize.height/2));
_dpadMenu->addChild(rightItem);
auto upItem = MenuItemFont::create("Up", CC_CALLBACK_0(UIFocusTestBase::onUpKeyPressed, this));
upItem->setPosition(Vector2(winSize.width - 60, winSize.height/2 + 50));
_dpadMenu->addChild(upItem);
auto downItem = MenuItemFont::create("Down", CC_CALLBACK_0(UIFocusTestBase::onDownKeyPressed, this));
downItem->setPosition(Vector2(winSize.width - 60, winSize.height/2 - 50));
_dpadMenu->addChild(downItem);
_dpadMenu->setPosition(Vector2::ZERO);
_uiLayer->addChild(_dpadMenu);
_eventListener = EventListenerFocus::create();
_eventListener->onFocusChanged = CC_CALLBACK_2(UIFocusTestBase::onFocusChanged, this);
_eventDispatcher->addEventListenerWithFixedPriority(_eventListener, 1);
return true;
}
return false;
}
//UIFocusTestHorizontal
UIFocusTestHorizontal::UIFocusTestHorizontal()
{
}
UIFocusTestHorizontal::~UIFocusTestHorizontal()
{
}
bool UIFocusTestHorizontal::init()
{
if (UIFocusTestBase::init()) {
Size winSize = Director::getInstance()->getVisibleSize();
HBox *layout = HBox::create();
layout->setPosition(Vector2(20, winSize.height/2 + 40));
_uiLayer->addChild(layout);
layout->setFocused(true);
_firstFocusedWidget = layout;
int count = 3;
for (int i=0; i<count; ++i) {
ImageView *w = ImageView::create("cocosui/scrollviewbg.png");
layout->addChild(w);
}
return true;
}
return false;
}
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->setScale(1.2);
widgetGetFocus->setColor(Color3B::RED);
}
Layout *loseLayout = dynamic_cast<Layout*>(widgetLostFocus);
if (!loseLayout && widgetLostFocus) {
widgetLostFocus->setScale(1.0);
widgetLostFocus->setColor(Color3B::WHITE);
}
}
void UIFocusTestHorizontal::onLeftKeyPressed()
{
_firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Left, _firstFocusedWidget);
}
void UIFocusTestHorizontal::onRightKeyPressed()
{
_firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Right, _firstFocusedWidget);
}
void UIFocusTestHorizontal::onUpKeyPressed()
{
_firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Up, _firstFocusedWidget);
}
void UIFocusTestHorizontal::onDownKeyPressed()
{
_firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Down, _firstFocusedWidget);
}

View File

@ -0,0 +1,47 @@
//
// UIFocusTest.h
// cocos2d_tests
//
// Created by guanghui on 5/4/14.
//
//
#ifndef __cocos2d_tests__UIFocusTest__
#define __cocos2d_tests__UIFocusTest__
#include "../UIScene.h"
class UIFocusTestBase : public UIScene
{
public:
UIFocusTestBase();
~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;
protected:
Menu *_dpadMenu;
Widget *_firstFocusedWidget;
EventListenerFocus *_eventListener;
};
class UIFocusTestHorizontal : public UIFocusTestBase
{
public:
UIFocusTestHorizontal();
~UIFocusTestHorizontal();
bool init();
virtual void onLeftKeyPressed() ;
virtual void onRightKeyPressed();
virtual void onUpKeyPressed() ;
virtual void onDownKeyPressed() ;
virtual void onFocusChanged(Widget* widgetLostFocus, Widget* widgetGetFocus);
protected:
UI_SCENE_CREATE_FUNC(UIFocusTestHorizontal);
};
#endif /* defined(__cocos2d_tests__UIFocusTest__) */

View File

@ -17,6 +17,7 @@
#include "UIListViewTest/UIListViewTest.h"
#include "UIWidgetAddNodeTest/UIWidgetAddNodeTest.h"
#include "UIRichTextTest/UIRichTextTest.h"
#include "UIFocusTest/UIFocusTest.h"
/*
#include "UISwitchTest/UISwitchTest.h"
*/
@ -109,6 +110,7 @@ static const char* s_testArray[] =
*/
"UIWidgetAddNodeTest",
"UIRichTextTest",
"UIFocusTest",
};
static UISceneManager *sharedInstance = NULL;
@ -359,6 +361,8 @@ Scene *UISceneManager::currentUIScene()
case kUIRichTextTest:
return UIRichTextTest::sceneWithTitle(s_testArray[_currentUISceneId]);
case KUIFocusTest:
return UIFocusTestHorizontal::sceneWithTitle(s_testArray[_currentUISceneId]);
}
return NULL;
}

View File

@ -101,6 +101,7 @@ enum
*/
kUIWidgetAddNodeTest,
kUIRichTextTest,
KUIFocusTest,
kUITestMax
};