axmol/test_uphone/tests/controller.cpp

219 lines
6.8 KiB
C++
Raw Normal View History

#include "tests.h"
#include "controller.h"
2010-08-27 11:53:35 +08:00
#include "testResource.h"
2010-09-18 15:21:13 +08:00
#define LINE_SPACE 40
2010-08-19 15:55:55 +08:00
static int s_nPageIndex = 0;
static TestScene* CreateTestScene(int nIdx)
{
2010-08-19 15:55:55 +08:00
TestScene* pScene = NULL;
switch (nIdx)
{
case TEST_ACTIONS:
pScene = new ActionsTestScene(); break;
2010-08-20 14:35:37 +08:00
case TEST_TRANSITIONS:
pScene = new TransitionsTestScene(); break;
2010-08-21 10:26:26 +08:00
case TEST_PROGRESS_ACTIONS:
pScene = new ProgressActionsTestScene(); break;
2010-08-23 10:17:29 +08:00
case TEST_EFFECTS:
pScene = new EffectTestScene(); break;
2010-08-23 11:53:37 +08:00
case TEST_CLICK_AND_MOVE:
pScene = new ClickAndMoveTestScene(); break;
2010-08-23 13:58:57 +08:00
case TEST_ROTATE_WORLD:
pScene = new RotateWorldTestScene(); break;
2010-08-23 16:38:10 +08:00
case TEST_PARTICLE:
pScene = new ParticleTestScene(); break;
2010-08-25 17:02:58 +08:00
case TEST_EASE_ACTIONS:
pScene = new EaseActionsTestScene(); break;
case TEST_MOTION_STREAK:
pScene = new MotionStreakTestScene(); break;
2010-08-26 10:53:49 +08:00
case TEST_DRAW_PRIMITIVES:
pScene = new DrawPrimitivesTestScene(); break;
2010-08-26 15:09:02 +08:00
case TEST_COCOSNODE:
pScene = new CocosNodeTestScene(); break;
2010-08-27 11:53:35 +08:00
case TEST_TOUCHES:
2010-10-15 16:02:50 +08:00
CCDirector::getSharedDirector()->setDeviceOrientation(CCDeviceOrientationPortrait);
2010-08-27 11:53:35 +08:00
pScene = new PongScene(); break;
2010-08-28 15:47:51 +08:00
case TEST_MENU:
pScene = new MenuTestScene(); break;
2010-08-30 15:04:46 +08:00
case TEST_ACTION_MANAGER:
pScene = new ActionManagerTestScene(); break;
2010-08-31 17:03:58 +08:00
case TEST_LAYER:
pScene = new LayerTestScene(); break;
2010-09-01 11:34:30 +08:00
case TEST_SCENE:
pScene = new SceneTestScene(); break;
2010-09-01 14:57:55 +08:00
case TEST_PARALLAX:
pScene = new ParallaxTestScene(); break;
2010-09-01 17:30:51 +08:00
case TEST_TILE_MAP:
pScene = new TileMapTestScene(); break;
2010-09-06 17:14:41 +08:00
case TEST_INTERVAL:
pScene = new IntervalTestScene(); break;
case TEST_CHIPMUNK:
2010-10-15 16:02:50 +08:00
CCDirector::getSharedDirector()->setDeviceOrientation(CCDeviceOrientationPortrait);
pScene = new ChipmunkTestScene(); break;
2010-09-07 11:47:54 +08:00
case TEST_ATLAS:
pScene = new AtlasTestScene(); break;
2010-09-09 10:17:38 +08:00
case TEST_SPRITE:
pScene = new SpriteTestScene(); break;
2010-09-09 16:15:21 +08:00
case TEST_SCHEDULER:
pScene = new SchedulerTestScene(); break;
2010-09-10 10:29:39 +08:00
case TEST_RENDERTEXTURE:
pScene = new RenderTextureScene(); break;
case TEST_BOX2D:
pScene = new Box2DTestScene(); break;
2010-09-14 16:23:50 +08:00
case TEST_BOX2DBED:
pScene = new Box2dTestBedScene(); break;
case TEST_EFFECT_ADVANCE:
pScene = new EffectAdvanceScene(); break;
2010-09-24 12:24:38 +08:00
case TEST_HIRES:
pScene = new HiResTestScene(); break;
2010-08-19 15:55:55 +08:00
default:
break;
}
2010-08-19 15:55:55 +08:00
return pScene;
}
2010-08-17 17:38:26 +08:00
TestController::TestController()
2010-08-19 15:55:55 +08:00
{
2010-10-15 16:02:50 +08:00
CCDirector::getSharedDirector()->setDeviceOrientation(CCDeviceOrientationLandscapeLeft);
2010-08-17 17:38:26 +08:00
// add close menu
2010-08-27 11:53:35 +08:00
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(s_pPathClose, s_pPathClose, this, menu_selector(TestController::closeCallback) );
2010-08-19 15:55:55 +08:00
CCMenu* pMenu =CCMenu::menuWithItems(pCloseItem, NULL);
2010-08-17 17:38:26 +08:00
CGSize s = CCDirector::getSharedDirector()->getWinSize();
pMenu->setPosition( CGPointZero );
2010-08-17 17:38:26 +08:00
pCloseItem->setPosition(CGPointMake( s.width - 30, s.height - 30));
2010-08-19 15:55:55 +08:00
// add menu items for tests
for (int i = 0; i < ITEM_EVERYPAGE; ++i)
{
2010-09-18 15:21:13 +08:00
CCLabel* label = CCLabel::labelWithString("For Test", "Arial", 32);
2010-08-19 15:55:55 +08:00
CCMenuItemLabel* pMenuItem = CCMenuItemLabel::itemWithLabel(label, this, menu_selector(TestController::menuCallback));
pMenu->addChild(pMenuItem, i + 2);
pMenuItem->setPosition( CGPointMake( s.width / 2, (s.height - (i + 1) * LINE_SPACE) ));
// record the pointer of the menu item
m_pMenuItems[i] = pMenuItem;
// record the value of i as userdata
int * nIdx = new int;
*nIdx = i;
pMenuItem->setUserData((void *) nIdx);
}
// update menu items text
updateItemsText();
// add menu item to change the page-number
CCLabel* pPreLabel = CCLabel::labelWithString("PrePage", "Arial", 22);
CCMenuItemLabel* pPreItem = CCMenuItemLabel::itemWithLabel(pPreLabel, this, menu_selector(TestController::prePageCallback));
pMenu->addChild(pPreItem, ITEM_EVERYPAGE + 2);
pPreItem->setPosition(CGPointMake(s.width - 150, 20));
CCLabel* pNextLabel = CCLabel::labelWithString("NextPage", "Arial", 22);
CCMenuItemLabel* pNextItem = CCMenuItemLabel::itemWithLabel(pNextLabel, this, menu_selector(TestController::nextPageCallback));
pMenu->addChild(pNextItem, ITEM_EVERYPAGE + 3);
pNextItem->setPosition(CGPointMake(s.width - 50, 20));
addChild(pMenu, 1);
}
2010-08-19 15:55:55 +08:00
TestController::~TestController()
{
for (int i = 0; i < ITEM_EVERYPAGE; ++i)
{
if (m_pMenuItems[i])
{
// delete the userdata have recorded
if (m_pMenuItems[i]->getUserData())
{
delete m_pMenuItems[i]->getUserData();
m_pMenuItems[i]->setUserData(NULL);
}
}
}
removeAllChildrenWithCleanup(true);
}
void TestController::menuCallback(NSObject * pSender)
{
2010-08-19 15:55:55 +08:00
// get the userdata, it's the index of the menu item clicked
CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
2010-08-19 15:55:55 +08:00
void * pUserData = pMenuItem->getUserData();
int * nIdx = (int *) pUserData;
2010-08-19 15:55:55 +08:00
// create the test scene and run it
TestScene* pScene = CreateTestScene(s_nPageIndex * ITEM_EVERYPAGE + (*nIdx));
if (pScene)
{
pScene->runThisTest();
2010-09-10 10:29:39 +08:00
pScene->release();
2010-08-19 15:55:55 +08:00
}
}
2010-08-17 17:38:26 +08:00
void TestController::closeCallback(NSObject * pSender)
{
CCDirector::getSharedDirector()->end();
2010-08-19 15:55:55 +08:00
}
void TestController::nextPageCallback(NSObject * pSender)
{
int nPageCount = TESTS_COUNT / ITEM_EVERYPAGE;
if (TESTS_COUNT % ITEM_EVERYPAGE != 0)
{
nPageCount += 1;
}
// compute the current page number
s_nPageIndex = (s_nPageIndex + 1) % nPageCount;
// update menu items text
updateItemsText();
}
void TestController::prePageCallback(NSObject * pSender)
{
int nPageCount = TESTS_COUNT / ITEM_EVERYPAGE;
if (TESTS_COUNT % ITEM_EVERYPAGE != 0)
{
nPageCount += 1;
}
// compute the current page number
s_nPageIndex -= 1;
if (s_nPageIndex < 0)
{
s_nPageIndex = nPageCount - 1;
}
// update menu items text
updateItemsText();
}
void TestController::updateItemsText()
{
int nStartIndex = s_nPageIndex * ITEM_EVERYPAGE;
for (int i = 0; i < ITEM_EVERYPAGE; ++i)
{
m_pMenuItems[i]->setIsVisible(false);
int nIdx = nStartIndex + i;
if (nIdx < TESTS_COUNT)
{
std::string menuText = g_aTestNames[nIdx];
2010-08-28 17:08:10 +08:00
if (! menuText.empty())
{
m_pMenuItems[i]->setString(menuText.c_str());
m_pMenuItems[i]->setIsVisible(true);
}
2010-08-19 15:55:55 +08:00
}
}
2010-08-17 17:38:26 +08:00
}