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"));
2013-12-23 15:35:35 +08:00
back_label->addTouchEventListener(this, toucheventselector(UIScene::toCocosGUITestScene));
Button* left_button = dynamic_cast<Button*>(root->getChildByName("left_Button"));
2013-09-16 20:54:13 +08:00
left_button->addTouchEventListener(this, toucheventselector(UIScene::previousCallback));
2013-12-23 15:35:35 +08:00
Button* middle_button = dynamic_cast<Button*>(root->getChildByName("middle_Button"));
2013-09-16 20:54:13 +08:00
middle_button->addTouchEventListener(this, toucheventselector(UIScene::restartCallback));
2013-12-23 15:35:35 +08:00
Button* right_button = dynamic_cast<Button*>(root->getChildByName("right_Button"));
2013-09-16 20:54:13 +08:00
right_button->addTouchEventListener(this, toucheventselector(UIScene::nextCallback));
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;
}
void UIScene::menuCloseCallback(Ref* pSender, TouchEventType type)
2013-09-16 20:54:13 +08:00
{
if (type == TOUCH_EVENT_ENDED)
{
2013-12-23 15:35:35 +08:00
_uiLayer->removeFromParent();
2013-09-16 20:54:13 +08:00
}
}
void UIScene::toCocosGUITestScene(Ref* sender, TouchEventType type)
2013-12-23 15:35:35 +08:00
{
if (type == TOUCH_EVENT_ENDED)
{
UISceneManager::purgeUISceneManager();
CocosGUITestScene* pScene = new CocosGUITestScene();
pScene->runThisTest();
pScene->release();
}
}
void UIScene::previousCallback(Ref* sender, TouchEventType type)
2013-09-16 20:54:13 +08:00
{
if (type == TOUCH_EVENT_ENDED)
{
CCDirector::getInstance()->replaceScene(UISceneManager::sharedUISceneManager()->previousUIScene());
}
}
void UIScene::restartCallback(Ref* sender, TouchEventType type)
2013-09-16 20:54:13 +08:00
{
if (type == TOUCH_EVENT_ENDED)
{
CCDirector::getInstance()->replaceScene(UISceneManager::sharedUISceneManager()->currentUIScene());
}
}
void UIScene::nextCallback(Ref* sender, TouchEventType type)
2013-09-16 20:54:13 +08:00
{
if (type == TOUCH_EVENT_ENDED)
{
CCDirector::getInstance()->replaceScene(UISceneManager::sharedUISceneManager()->nextUIScene());
}
}