axmol/test_uphone/tests/controller.cpp

203 lines
6.1 KiB
C++
Raw Normal View History

#include "tests.h"
#include "controller.h"
2010-08-27 11:53:35 +08:00
#include "testResource.h"
2010-08-19 15:55:55 +08:00
#define LINE_SPACE 30
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-25 17:44:52 +08:00
/**
@todo
2010-08-23 10:17:29 +08:00
case TEST_EFFECTS:
pScene = new EffectTestScene(); break;
2010-08-25 17:44:52 +08:00
*/
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:
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;
// case TEST_INTERVAL:
// pScene = new IntervalTestScene(); break;
2010-09-04 18:18:14 +08:00
case TESTS_CHIPMUNK:
CCDirector::getSharedDirector()->setDeviceOrientation(kCCDeviceOrientationPortrait);
pScene = new ChipmunkTestScene(); 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
2010-08-19 15:55:55 +08:00
TestController::TestController()
{
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)
{
CCLabel* label = CCLabel::labelWithString("For Test", "Arial", 22);
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();
pScene->autorelease();
}
}
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
}