axmol/tests/cpp-tests/Classes/UITest/CocoStudioGUITest/CocosGUIScene.cpp

404 lines
14 KiB
C++
Raw Normal View History

2013-09-16 20:54:13 +08:00
#include "CocosGUIScene.h"
2014-03-04 16:51:35 +08:00
#include "CocoStudioGUITest.h"
2013-09-16 20:54:13 +08:00
#include "UISceneManager.h"
#include "cocostudio/CocoStudio.h"
2013-09-16 20:54:13 +08:00
2013-12-23 15:35:35 +08:00
enum
{
LINE_SPACE = 40,
kItemTagBasic = 1000,
};
static struct
{
const char *name;
std::function<void(Ref* sender)> callback;
2013-12-23 15:35:35 +08:00
}
g_guisTests[] =
{
2014-05-08 11:26:29 +08:00
{
"gui focus test",
[](Ref* sender)
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(KUIFocusTest_HBox);
pManager->setMinUISceneId(KUIFocusTest_HBox);
pManager->setMaxUISceneId(KUIFocusTest_NestedLayout3);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
2013-12-23 15:35:35 +08:00
{
2014-05-08 11:26:29 +08:00
2013-12-23 15:35:35 +08:00
"gui ButtonTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIButtonTest);
pManager->setMinUISceneId(kUIButtonTest);
pManager->setMaxUISceneId(kUIButtonTest_Title);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui CheckBoxTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUICheckBoxTest);
pManager->setMinUISceneId(kUICheckBoxTest);
pManager->setMaxUISceneId(kUICheckBoxTest);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui SliderTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUISliderTest);
pManager->setMinUISceneId(kUISliderTest);
pManager->setMaxUISceneId(kUISliderTest_Scale9);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
/*
{
"gui PotentiometerTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIPotentiometerTest);
pManager->setMinUISceneId(kUIPotentiometerTest);
pManager->setMaxUISceneId(kUIPotentiometerTest);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
2014-03-04 16:51:35 +08:00
*/
/*
2013-12-23 15:35:35 +08:00
{
"gui SwitchTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUISwitchTest_Horizontal);
pManager->setMinUISceneId(kUISwitchTest_Horizontal);
pManager->setMaxUISceneId(kUISwitchTest_VerticalAndTitleVertical);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
*/
{
"gui ImageViewTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIImageViewTest);
pManager->setMinUISceneId(kUIImageViewTest);
pManager->setMaxUISceneId(kUIImageViewTest_Scale9);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui LoadingBarTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUILoadingBarTest_Left);
pManager->setMinUISceneId(kUILoadingBarTest_Left);
pManager->setMaxUISceneId(kUILoadingBarTest_Right_Scale9);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
/*
{
"gui ProgressTimerTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIProgressTimerTest_Radial);
pManager->setMinUISceneId(kUIProgressTimerTest_Radial);
pManager->setMaxUISceneId(kUIProgressTimerTest_WithSpriteFrame);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
*/
{
2014-03-04 16:51:35 +08:00
"gui TextAtalsTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
2014-03-04 16:51:35 +08:00
pManager->setCurrentUISceneId(kUITextAtlasTest);
pManager->setMinUISceneId(kUITextAtlasTest);
pManager->setMaxUISceneId(kUITextAtlasTest);
2013-12-23 15:35:35 +08:00
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
2014-03-04 16:51:35 +08:00
"gui TextTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
2014-03-04 16:51:35 +08:00
pManager->setCurrentUISceneId(kUITextTest);
pManager->setMinUISceneId(kUITextTest);
pManager->setMaxUISceneId(kUITextTest_TTF);
2013-12-23 15:35:35 +08:00
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
2014-03-04 16:51:35 +08:00
"gui TextBMFontTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
2014-03-04 16:51:35 +08:00
pManager->setCurrentUISceneId(kUITextBMFontTest);
pManager->setMinUISceneId(kUITextBMFontTest);
pManager->setMaxUISceneId(kUITextBMFontTest);
2013-12-23 15:35:35 +08:00
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui TextFieldTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUITextFieldTest);
pManager->setMinUISceneId(kUITextFieldTest);
2014-03-04 16:51:35 +08:00
pManager->setMaxUISceneId(kUITextFieldTest_LineWrap);
2013-12-23 15:35:35 +08:00
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui LayoutTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUILayoutTest);
pManager->setMinUISceneId(kUILayoutTest);
pManager->setMaxUISceneId(kUILayoutTest_Layout_Relative_Location);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui ScrollViewTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIScrollViewTest_Vertical);
pManager->setMinUISceneId(kUIScrollViewTest_Vertical);
pManager->setMaxUISceneId(kUIScrollViewTest_ScrollToPercentBothDirection_Bounce);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui PageViewTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIPageViewTest);
pManager->setMinUISceneId(kUIPageViewTest);
pManager->setMaxUISceneId(kUIPageViewTest);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui ListViewTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIListViewTest_Vertical);
pManager->setMinUISceneId(kUIListViewTest_Vertical);
pManager->setMaxUISceneId(kUIListViewTest_Horizontal);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
/*
{
"gui GridViewTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIGridViewTest_Mode_Column);
pManager->setMinUISceneId(kUIGridViewTest_Mode_Column);
pManager->setMaxUISceneId(kUIGridViewTest_Mode_Row);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
2014-03-04 16:51:35 +08:00
*/
/*
2013-12-23 15:35:35 +08:00
{
"gui PickerViewTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIPickerViewTest_Vertical);
pManager->setMinUISceneId(kUIPickerViewTest_Vertical);
pManager->setMaxUISceneId(kUIPickerViewTest_Horizontal);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
},
*/
{
"gui WidgetAddNodeTest",
[](Ref* sender)
2013-12-23 15:35:35 +08:00
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIWidgetAddNodeTest);
pManager->setMinUISceneId(kUIWidgetAddNodeTest);
pManager->setMaxUISceneId(kUIWidgetAddNodeTest);
Scene* pScene = pManager->currentUIScene();
2014-03-06 20:52:29 +08:00
Director::getInstance()->replaceScene(pScene);
}
},
{
"gui RichTextTest",
[](Ref* sender)
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
pManager->setCurrentUISceneId(kUIRichTextTest);
pManager->setMinUISceneId(kUIRichTextTest);
pManager->setMaxUISceneId(kUIRichTextTest);
Scene* pScene = pManager->currentUIScene();
2013-12-23 15:35:35 +08:00
Director::getInstance()->replaceScene(pScene);
}
},
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
{
2014-05-09 11:30:07 +08:00
"gui VideoPlayerTest",
[](Ref* sender)
{
UISceneManager* pManager = UISceneManager::sharedUISceneManager();
2014-05-09 11:30:07 +08:00
pManager->setCurrentUISceneId(kUIVideoPlayerTest);
pManager->setMinUISceneId(kUIVideoPlayerTest);
pManager->setMaxUISceneId(kUIVideoPlayerTest);
Scene* pScene = pManager->currentUIScene();
Director::getInstance()->replaceScene(pScene);
}
}
#endif
2013-12-23 15:35:35 +08:00
};
static const int g_maxTests = sizeof(g_guisTests) / sizeof(g_guisTests[0]);
static Vector2 s_tCurPos = Vector2::ZERO;
2013-12-23 15:35:35 +08:00
////////////////////////////////////////////////////////
//
// CocosGUITestMainLayer
//
////////////////////////////////////////////////////////
void CocosGUITestMainLayer::onEnter()
{
Layer::onEnter();
auto s = Director::getInstance()->getWinSize();
_itemMenu = Menu::create();
_itemMenu->setPosition( s_tCurPos );
MenuItemFont::setFontName("fonts/arial.ttf");
2013-12-23 15:35:35 +08:00
MenuItemFont::setFontSize(24);
for (int i = 0; i < g_maxTests; ++i)
{
auto pItem = MenuItemFont::create(g_guisTests[i].name, g_guisTests[i].callback);
pItem->setPosition(Vector2(s.width / 2, s.height - (i + 1) * LINE_SPACE));
2013-12-23 15:35:35 +08:00
_itemMenu->addChild(pItem, kItemTagBasic + i);
}
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(CocosGUITestMainLayer::onTouchesBegan, this);
listener->onTouchesMoved = CC_CALLBACK_2(CocosGUITestMainLayer::onTouchesMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
addChild(_itemMenu);
}
void CocosGUITestMainLayer::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
{
auto touch = static_cast<Touch*>(touches[0]);
_beginPos = touch->getLocation();
}
void CocosGUITestMainLayer::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
{
auto touch = static_cast<Touch*>(touches[0]);
auto touchLocation = touch->getLocation();
float nMoveY = touchLocation.y - _beginPos.y;
auto curPos = _itemMenu->getPosition();
auto nextPos = Vector2(curPos.x, curPos.y + nMoveY);
2013-12-23 15:35:35 +08:00
if (nextPos.y < 0.0f)
{
_itemMenu->setPosition(Vector2::ZERO);
2013-12-23 15:35:35 +08:00
return;
}
if (nextPos.y > ((g_maxTests + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))
{
_itemMenu->setPosition(Vector2(0, ((g_maxTests + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)));
2013-12-23 15:35:35 +08:00
return;
}
_itemMenu->setPosition(nextPos);
_beginPos = touchLocation;
s_tCurPos = nextPos;
}
////////////////////////////////////////////////////////
//
// CocosGUITestScene
//
////////////////////////////////////////////////////////
2014-03-04 16:51:35 +08:00
void CocosGUITestScene::onEnter()
{
Scene::onEnter();
auto label = Label::createWithTTF("Back", "fonts/arial.ttf", 20);
2014-03-04 16:51:35 +08:00
//#endif
auto pMenuItem = MenuItemLabel::create(label, CC_CALLBACK_1(CocosGUITestScene::BackCallback, this));
Menu* pMenu =Menu::create(pMenuItem, NULL);
pMenu->setPosition( Vector2::ZERO );
pMenuItem->setPosition( Vector2( VisibleRect::right().x - 50, VisibleRect::bottom().y + 25) );
2014-03-04 16:51:35 +08:00
addChild(pMenu, 1);
}
2013-12-23 15:35:35 +08:00
void CocosGUITestScene::runThisTest()
{
auto layer = new CocosGUITestMainLayer();
addChild(layer);
layer->release();
Director::getInstance()->replaceScene(this);
2014-03-04 16:51:35 +08:00
}
void CocosGUITestScene::BackCallback(Ref* pSender)
{
CocoStudioGUITestScene* pScene = new CocoStudioGUITestScene();
pScene->runThisTest();
pScene->release();
}