2014-05-04 16:31:24 +08:00
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:14:22 +08:00
|
|
|
void UIFocusTestBase::onImageViewClicked(cocos2d::Ref *ref, TouchEventType touchType)
|
|
|
|
{
|
|
|
|
if (touchType == TouchEventType::TOUCH_EVENT_ENDED) {
|
|
|
|
Widget *w = (Widget*)ref;
|
|
|
|
if (w->isFocusEnabled()) {
|
|
|
|
w->setFocusEnabled(false);
|
|
|
|
w->setColor(Color3B::WHITE);
|
|
|
|
w->setScale(0.8);
|
|
|
|
}else{
|
|
|
|
w->setFocusEnabled(true);
|
|
|
|
w->setScale(1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
|
|
|
|
//UIFocusTestHorizontal
|
|
|
|
UIFocusTestHorizontal::UIFocusTestHorizontal()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UIFocusTestHorizontal::~UIFocusTestHorizontal()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UIFocusTestHorizontal::init()
|
|
|
|
{
|
|
|
|
if (UIFocusTestBase::init()) {
|
|
|
|
|
|
|
|
Size winSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
2014-05-04 18:11:41 +08:00
|
|
|
_horizontalLayout = HBox::create();
|
|
|
|
_horizontalLayout->setPosition(Vector2(20, winSize.height/2 + 40));
|
|
|
|
_uiLayer->addChild(_horizontalLayout);
|
2014-05-05 11:25:30 +08:00
|
|
|
|
2014-05-04 18:11:41 +08:00
|
|
|
_horizontalLayout->setFocused(true);
|
|
|
|
_horizontalLayout->setLoopFocus(true);
|
|
|
|
_firstFocusedWidget = _horizontalLayout;
|
2014-05-04 16:31:24 +08:00
|
|
|
|
|
|
|
int count = 3;
|
|
|
|
for (int i=0; i<count; ++i) {
|
|
|
|
ImageView *w = ImageView::create("cocosui/scrollviewbg.png");
|
2014-05-06 14:14:22 +08:00
|
|
|
w->setTouchEnabled(true);
|
|
|
|
w->addTouchEventListener(this, toucheventselector(UIFocusTestHorizontal::onImageViewClicked));
|
2014-05-04 18:11:41 +08:00
|
|
|
_horizontalLayout->addChild(w);
|
2014-05-04 16:31:24 +08:00
|
|
|
}
|
|
|
|
|
2014-05-04 18:11:41 +08:00
|
|
|
_loopText = Text::create("loop enabled", "Airal", 20);
|
|
|
|
_loopText->setPosition(Vector2(winSize.width/2, winSize.height - 50));
|
|
|
|
_loopText->setColor(Color3B::GREEN);
|
|
|
|
this->addChild(_loopText);
|
|
|
|
|
|
|
|
auto btn = Button::create("cocosui/switch-mask.png");
|
|
|
|
btn->setTitleText("Toggle Loop");
|
|
|
|
btn->setPosition(Vector2(60, winSize.height - 50));
|
|
|
|
btn->setTitleColor(Color3B::RED);
|
|
|
|
btn->addTouchEventListener(this, toucheventselector(UIFocusTestHorizontal::toggleFocusLoop));
|
|
|
|
this->addChild(btn);
|
2014-05-04 16:31:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:14:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-04 18:11:41 +08:00
|
|
|
void UIFocusTestHorizontal::toggleFocusLoop(cocos2d::Ref * pObjc, TouchEventType type)
|
|
|
|
{
|
|
|
|
if (type == TouchEventType::TOUCH_EVENT_ENDED) {
|
|
|
|
_horizontalLayout->setLoopFocus(!_horizontalLayout->getLoopFocus());
|
|
|
|
if (_horizontalLayout->getLoopFocus()) {
|
|
|
|
_loopText->setText("loop enabled");
|
|
|
|
}else{
|
|
|
|
_loopText->setText("loop disabled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
void UIFocusTestHorizontal::onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus)
|
|
|
|
{
|
|
|
|
//only change the widgets' state
|
|
|
|
Layout *getLayout = dynamic_cast<Layout*>(widgetGetFocus);
|
2014-05-06 14:14:22 +08:00
|
|
|
if (!getLayout && widgetGetFocus && widgetGetFocus->isFocusEnabled()) {
|
2014-05-04 16:31:24 +08:00
|
|
|
widgetGetFocus->setScale(1.2);
|
|
|
|
widgetGetFocus->setColor(Color3B::RED);
|
|
|
|
}
|
|
|
|
|
|
|
|
Layout *loseLayout = dynamic_cast<Layout*>(widgetLostFocus);
|
2014-05-06 14:14:22 +08:00
|
|
|
if (!loseLayout && widgetLostFocus && widgetLostFocus->isFocusEnabled()) {
|
2014-05-04 16:31:24 +08:00
|
|
|
widgetLostFocus->setScale(1.0);
|
|
|
|
widgetLostFocus->setColor(Color3B::WHITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIFocusTestHorizontal::onLeftKeyPressed()
|
|
|
|
{
|
2014-05-05 11:25:30 +08:00
|
|
|
if (_firstFocusedWidget) {
|
|
|
|
_firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Left, _firstFocusedWidget);
|
|
|
|
}
|
2014-05-04 16:31:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIFocusTestHorizontal::onRightKeyPressed()
|
|
|
|
{
|
2014-05-05 11:25:30 +08:00
|
|
|
if (_firstFocusedWidget) {
|
|
|
|
_firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Right, _firstFocusedWidget);
|
|
|
|
}
|
2014-05-04 16:31:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIFocusTestHorizontal::onUpKeyPressed()
|
|
|
|
{
|
2014-05-05 11:25:30 +08:00
|
|
|
if (_firstFocusedWidget) {
|
|
|
|
_firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Up, _firstFocusedWidget);
|
|
|
|
}
|
2014-05-04 16:31:24 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIFocusTestHorizontal::onDownKeyPressed()
|
|
|
|
{
|
2014-05-05 11:25:30 +08:00
|
|
|
if (_firstFocusedWidget) {
|
|
|
|
_firstFocusedWidget = _firstFocusedWidget->nextFocusedWidget(FocusDirection::FocusDirection_Down, _firstFocusedWidget);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//UIFocusTestVertical
|
|
|
|
UIFocusTestVertical::UIFocusTestVertical()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UIFocusTestVertical::~UIFocusTestVertical()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2014-05-04 16:31:24 +08:00
|
|
|
|
2014-05-05 11:25:30 +08:00
|
|
|
bool UIFocusTestVertical::init()
|
|
|
|
{
|
|
|
|
if (UIFocusTestBase::init()) {
|
|
|
|
|
|
|
|
Size winSize = Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
_verticalLayout = VBox::create();
|
|
|
|
_verticalLayout->setPosition(Vector2(winSize.width/2 - 100, winSize.height - 70));
|
|
|
|
_uiLayer->addChild(_verticalLayout);
|
|
|
|
_verticalLayout->setScale(0.8);
|
|
|
|
|
|
|
|
_verticalLayout->setFocused(true);
|
|
|
|
_verticalLayout->setLoopFocus(true);
|
|
|
|
_firstFocusedWidget = _verticalLayout;
|
|
|
|
|
|
|
|
int count = 3;
|
|
|
|
for (int i=0; i<count; ++i) {
|
|
|
|
ImageView *w = ImageView::create("cocosui/scrollviewbg.png");
|
2014-05-06 14:14:22 +08:00
|
|
|
w->setTouchEnabled(true);
|
|
|
|
w->addTouchEventListener(this, toucheventselector(UIFocusTestVertical::onImageViewClicked));
|
2014-05-05 11:25:30 +08:00
|
|
|
_verticalLayout->addChild(w);
|
|
|
|
}
|
|
|
|
|
|
|
|
_loopText = Text::create("loop enabled", "Airal", 20);
|
|
|
|
_loopText->setPosition(Vector2(winSize.width/2, winSize.height - 50));
|
|
|
|
_loopText->setColor(Color3B::GREEN);
|
|
|
|
this->addChild(_loopText);
|
|
|
|
|
|
|
|
auto btn = Button::create("cocosui/switch-mask.png");
|
|
|
|
btn->setTitleText("Toggle Loop");
|
|
|
|
btn->setPosition(Vector2(60, winSize.height - 50));
|
|
|
|
btn->setTitleColor(Color3B::RED);
|
|
|
|
btn->addTouchEventListener(this, toucheventselector(UIFocusTestHorizontal::toggleFocusLoop));
|
|
|
|
this->addChild(btn);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-06 14:14:22 +08:00
|
|
|
|
2014-05-05 11:25:30 +08:00
|
|
|
void UIFocusTestVertical::toggleFocusLoop(cocos2d::Ref * pObjc, TouchEventType type)
|
|
|
|
{
|
|
|
|
if (type == TouchEventType::TOUCH_EVENT_ENDED) {
|
|
|
|
_verticalLayout->setLoopFocus(!_verticalLayout->getLoopFocus());
|
|
|
|
if (_verticalLayout->getLoopFocus()) {
|
|
|
|
_loopText->setText("loop enabled");
|
|
|
|
}else{
|
|
|
|
_loopText->setText("loop disabled");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIFocusTestVertical::onFocusChanged(cocos2d::ui::Widget *widgetLostFocus, cocos2d::ui::Widget *widgetGetFocus)
|
|
|
|
{
|
|
|
|
//only change the widgets' state
|
|
|
|
Layout *getLayout = dynamic_cast<Layout*>(widgetGetFocus);
|
2014-05-06 14:14:22 +08:00
|
|
|
if (!getLayout && widgetGetFocus && widgetGetFocus->isFocusEnabled()) {
|
2014-05-05 11:25:30 +08:00
|
|
|
widgetGetFocus->setScale(1.2);
|
|
|
|
widgetGetFocus->setColor(Color3B::RED);
|
|
|
|
}
|
|
|
|
|
|
|
|
Layout *loseLayout = dynamic_cast<Layout*>(widgetLostFocus);
|
2014-05-06 14:14:22 +08:00
|
|
|
if (!loseLayout && widgetLostFocus && widgetLostFocus->isFocusEnabled()) {
|
2014-05-05 11:25:30 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-04 16:31:24 +08:00
|
|
|
}
|