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

114 lines
3.4 KiB
C++

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