axmol/samples/Cpp/TestCpp/Classes/controller.cpp

192 lines
6.8 KiB
C++
Raw Normal View History

// C++ includes
#include <map>
#include <functional>
#include <string>
// test inclues
#include "controller.h"
#include "testResource.h"
#include "tests.h"
struct {
const char *test_name;
std::function<TestScene*()> callback;
} g_aTestNames[] = {
{ "Accelerometer", []() { return new AccelerometerTestScene(); } },
{ "ActionManagerTest", [](){return new ActionManagerTestScene(); } },
{ "ActionsEaseTest", [](){return new ActionsEaseTestScene();} },
{ "ActionsProgressTest", [](){return new ProgressActionsTestScene(); } },
{ "ActionsTest", [](){ return new ActionsTestScene(); } },
{ "Box2dTest", []() { return new Box2DTestScene(); } },
{ "Box2dTestBed", []() { return new Box2dTestBedScene(); } },
{ "BugsTest", []() { return new BugsTestScene(); } },
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
{ "ChipmunkTest", []() { return new ChipmunkAccelTouchTestScene(); } },
#endif
{ "ClickAndMoveTest", [](){return new ClickAndMoveTestScene(); } },
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
{ "ClippingNodeTest", []() { return new ClippingNodeTestScene(); } },
#endif
{ "CocosDenshionTest", []() { return new CocosDenshionTestScene(); } },
{ "ConfigurationTest", []() { return new ConfigurationTestScene(); } },
#if (CC_TARGET_PLATFORM != CC_PLATFORM_EMSCRIPTEN)
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
{ "CurlTest", []() { return new CurlTestScene(); } },
#endif
#endif
#endif
{ "CurrentLanguageTest", []() { return new CurrentLanguageTestScene(); } },
{ "DrawPrimitivesTest", [](){return new DrawPrimitivesTestScene();} },
{ "EffectAdvancedTest", []() { return new EffectAdvanceScene(); } },
{ "EffectsTest", [](){return new EffectTestScene();} },
{ "ExtensionsTest", []() { return new ExtensionsTestScene(); } },
{ "FileUtilsTest", []() { return new FileUtilsTestScene(); } },
{ "FontTest", []() { return new FontTestScene(); } },
{ "IntervalTest", [](){return new IntervalTestScene(); } },
#ifdef KEYBOARD_SUPPORT
{ "KeyboardTest", []() { return new KeyboardTestScene(); } },
#endif
2013-06-04 13:47:28 +08:00
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
{ "KeypadTest", []() { return new KeypadTestScene(); } },
#endif
{ "LabelTest", [](){return new AtlasTestScene(); } },
{ "LayerTest", [](){return new LayerTestScene();} },
{ "MenuTest", [](){return new MenuTestScene();} },
{ "MotionStreakTest", [](){return new MotionStreakTestScene();} },
2013-06-04 13:47:28 +08:00
{ "MutiTouchTest", []() { return new MutiTouchTestScene(); } },
{ "NodeTest", [](){return new CocosNodeTestScene();} },
{ "ParallaxTest", [](){return new ParallaxTestScene(); } },
{ "ParticleTest", [](){return new ParticleTestScene(); } },
{ "PerformanceTest", []() { return new PerformanceTestScene(); } },
{ "RenderTextureTest", [](){return new RenderTextureScene(); } },
{ "RotateWorldTest", [](){return new RotateWorldTestScene(); } },
{ "SceneTest", [](){return new SceneTestScene();} },
{ "SchedulerTest", [](){return new SchedulerTestScene(); } },
{ "ShaderTest", []() { return new ShaderTestScene(); } },
{ "SpineTest", []() { return new SpineTestScene(); } },
{ "SpriteTest", [](){return new SpriteTestScene(); } },
{ "TextInputTest", [](){return new TextInputTestScene(); } },
{ "Texture2DTest", [](){return new TextureTestScene(); } },
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
{ "TextureCacheTest", []() { return new TextureCacheTestScene(); } },
#endif
2013-06-04 13:47:28 +08:00
{ "TexturePackerEncryption", []() { return new TextureAtlasEncryptionTestScene(); } },
{ "TileMapTest", [](){return new TileMapTestScene(); } },
{ "TouchesTest", [](){return new PongScene();} },
{ "TransitionsTest", [](){return new TransitionsTestScene();} },
{ "UserDefaultTest", []() { return new UserDefaultTestScene(); } },
{ "ZwoptexTest", []() { return new ZwoptexTestScene(); } },
};
static int g_testCount = sizeof(g_aTestNames) / sizeof(g_aTestNames[0]);
#define LINE_SPACE 40
static CCPoint s_tCurPos = CCPointZero;
TestController::TestController()
: _beginPos(CCPointZero)
{
// add close menu
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(s_pPathClose, s_pPathClose, CC_CALLBACK_1(TestController::closeCallback, this) );
CCMenu* pMenu =CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
pCloseItem->setPosition(ccp( VisibleRect::right().x - 30, VisibleRect::top().y - 30));
// add menu items for tests
_itemMenu = CCMenu::create();
for (int i = 0; i < g_testCount; ++i)
{
// #if (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
// CCLabelBMFont* label = CCLabelBMFont::create(g_aTestNames[i].c_str(), "fonts/arial16.fnt");
// #else
CCLabelTTF* label = CCLabelTTF::create( g_aTestNames[i].test_name, "Arial", 24);
// #endif
CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, CC_CALLBACK_1(TestController::menuCallback, this));
_itemMenu->addChild(pMenuItem, i + 10000);
pMenuItem->setPosition( ccp( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) ));
}
_itemMenu->setContentSize(CCSizeMake(VisibleRect::getVisibleRect().size.width, (g_testCount + 1) * (LINE_SPACE)));
_itemMenu->setPosition(s_tCurPos);
addChild(_itemMenu);
setTouchEnabled(true);
addChild(pMenu, 1);
}
TestController::~TestController()
{
}
void TestController::menuCallback(CCObject * pSender)
{
2013-06-04 13:47:28 +08:00
CCDirector::sharedDirector()->purgeCachedData();
// get the userdata, it's the index of the menu item clicked
CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
2013-06-04 13:47:28 +08:00
int idx = pMenuItem->getZOrder() - 10000;
// create the test scene and run it
TestScene* pScene = g_aTestNames[idx].callback();
2013-06-04 13:47:28 +08:00
if (pScene)
{
pScene->runThisTest();
pScene->release();
}
}
void TestController::closeCallback(CCObject * pSender)
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
void TestController::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator it = pTouches->begin();
CCTouch* touch = (CCTouch*)(*it);
_beginPos = touch->getLocation();
}
void TestController::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
CCSetIterator it = pTouches->begin();
CCTouch* touch = (CCTouch*)(*it);
CCPoint touchLocation = touch->getLocation();
float nMoveY = touchLocation.y - _beginPos.y;
CCPoint curPos = _itemMenu->getPosition();
CCPoint nextPos = ccp(curPos.x, curPos.y + nMoveY);
if (nextPos.y < 0.0f)
{
_itemMenu->setPosition(CCPointZero);
return;
}
if (nextPos.y > ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))
{
_itemMenu->setPosition(ccp(0, ((g_testCount + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)));
return;
}
_itemMenu->setPosition(nextPos);
_beginPos = touchLocation;
s_tCurPos = nextPos;
}