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

114 lines
3.4 KiB
C++
Raw Normal View History

2013-09-16 20:54:13 +08:00
#include "extensions/cocos-ext.h"
2013-09-16 20:54:13 +08:00
#include "UIScene.h"
#include "UISceneManager.h"
2013-11-06 16:04:06 +08:00
#include "editor-support/cocostudio/CCSGUIReader.h"
2013-12-23 15:35:35 +08:00
#include "CocosGUIScene.h"
2013-09-16 20:54:13 +08:00
using namespace ui;
2013-09-16 20:54:13 +08:00
UIScene::UIScene()
2013-12-23 15:35:35 +08:00
: _sceneTitle(nullptr)
, _uiLayer(nullptr)
2013-09-16 20:54:13 +08:00
{
}
UIScene::~UIScene()
{
}
bool UIScene::init()
{
if (CCLayer::init())
{
2013-12-23 15:35:35 +08:00
_uiLayer = Layer::create();
addChild(_uiLayer);
2013-09-16 20:54:13 +08:00
2014-03-11 17:13:54 +08:00
_widget = dynamic_cast<Layout*>(cocostudio::GUIReader::getInstance()->widgetFromJsonFile("cocosui/UITest/UITest.json"));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(_widget);
2013-09-16 20:54:13 +08:00
2014-03-04 16:51:35 +08:00
Size screenSize = CCDirector::getInstance()->getWinSize();
2014-01-03 12:14:21 +08:00
Size rootSize = _widget->getSize();
_uiLayer->setPosition(Vector2((screenSize.width - rootSize.width) / 2,
2014-03-04 16:51:35 +08:00
(screenSize.height - rootSize.height) / 2));
2014-01-03 12:14:21 +08:00
2013-12-23 15:35:35 +08:00
Layout* root = static_cast<Layout*>(_uiLayer->getChildByTag(81));
2013-09-16 20:54:13 +08:00
2014-03-04 16:51:35 +08:00
_sceneTitle = dynamic_cast<Text*>(root->getChildByName("UItest"));
2013-09-16 20:54:13 +08:00
2014-03-04 16:51:35 +08:00
Text* back_label = dynamic_cast<Text*>(root->getChildByName("back"));
2014-05-09 11:52:51 +08:00
back_label->addTouchEventListener(CC_CALLBACK_2(UIScene::toCocosGUITestScene, this));
2013-12-23 15:35:35 +08:00
Button* left_button = dynamic_cast<Button*>(root->getChildByName("left_Button"));
2014-05-09 11:52:51 +08:00
left_button->addTouchEventListener(CC_CALLBACK_2(UIScene::previousCallback, this));
2013-09-16 20:54:13 +08:00
2013-12-23 15:35:35 +08:00
Button* middle_button = dynamic_cast<Button*>(root->getChildByName("middle_Button"));
2014-05-09 11:52:51 +08:00
middle_button->addTouchEventListener(CC_CALLBACK_2(UIScene::restartCallback, this));
2013-09-16 20:54:13 +08:00
2013-12-23 15:35:35 +08:00
Button* right_button = dynamic_cast<Button*>(root->getChildByName("right_Button"));
2014-05-09 11:52:51 +08:00
right_button->addTouchEventListener(CC_CALLBACK_2(UIScene::nextCallback, this));
2013-09-16 20:54:13 +08:00
2014-03-04 16:51:35 +08:00
/*
Text* mainMenuLabel = Text::create();
mainMenuLabel->setText("MainMenu");
mainMenuLabel->setFontSize(20);
mainMenuLabel->setTouchScaleChangeEnabled(true);
mainMenuLabel->setPosition(Vector2(430,30));
2014-03-04 16:51:35 +08:00
mainMenuLabel->setTouchEnabled(true);
mainMenuLabel->addTouchEventListener(this, toucheventselector(UIScene::menuCloseCallback));
_uiLayer->addWidget(mainMenuLabel);
*/
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
2014-05-09 11:52:51 +08:00
void UIScene::menuCloseCallback(Ref* sender, Widget::TouchEventType type)
2013-09-16 20:54:13 +08:00
{
2014-05-09 11:52:51 +08:00
if (type == Widget::TouchEventType::ENDED)
2013-09-16 20:54:13 +08:00
{
2013-12-23 15:35:35 +08:00
_uiLayer->removeFromParent();
2013-09-16 20:54:13 +08:00
}
}
2014-05-09 11:52:51 +08:00
void UIScene::toCocosGUITestScene(Ref* sender, Widget::TouchEventType type)
2013-12-23 15:35:35 +08:00
{
2014-05-09 11:52:51 +08:00
if (type == Widget::TouchEventType::ENDED)
2013-12-23 15:35:35 +08:00
{
UISceneManager::purgeUISceneManager();
CocosGUITestScene* pScene = new CocosGUITestScene();
pScene->runThisTest();
pScene->release();
}
}
2014-05-09 11:52:51 +08:00
void UIScene::previousCallback(Ref* sender, Widget::TouchEventType type)
2013-09-16 20:54:13 +08:00
{
2014-05-09 11:52:51 +08:00
if (type == Widget::TouchEventType::ENDED)
2013-09-16 20:54:13 +08:00
{
CCDirector::getInstance()->replaceScene(UISceneManager::sharedUISceneManager()->previousUIScene());
}
}
2014-05-09 11:52:51 +08:00
void UIScene::restartCallback(Ref* sender, Widget::TouchEventType type)
2013-09-16 20:54:13 +08:00
{
2014-05-09 11:52:51 +08:00
if (type == Widget::TouchEventType::ENDED)
2013-09-16 20:54:13 +08:00
{
CCDirector::getInstance()->replaceScene(UISceneManager::sharedUISceneManager()->currentUIScene());
}
}
2014-05-09 11:52:51 +08:00
void UIScene::nextCallback(Ref* sender, Widget::TouchEventType type)
2013-09-16 20:54:13 +08:00
{
2014-05-09 11:52:51 +08:00
if (type == Widget::TouchEventType::ENDED)
2013-09-16 20:54:13 +08:00
{
CCDirector::getInstance()->replaceScene(UISceneManager::sharedUISceneManager()->nextUIScene());
}
}