#include "controller.h"
#include "testResource.h"#include "tests.h"
#define LINE_SPACE 40 //總共有40個test場景
static CCPoint s_tCurPos = CCPointZero;//創建一個點初始位置爲0點。全局的。
static TestScene* CreateTestScene(int nIdx)
{
CCDirector::sharedDirector()->purgeCachedData();//清楚所有緩存數據
TestScene* pScene = NULL;//設置一個場景,後期所有場景可以用此替換
switch (nIdx)
{//選中的是哪個場景,然後創建
case TEST_ACTIONS:
pScene = new ActionsTestScene(); break;
case TEST_TRANSITIONS:
pScene = new TransitionsTestScene(); break;
case TEST_PROGRESS_ACTIONS:
pScene = new ProgressActionsTestScene(); break;
case TEST_EFFECTS:
pScene = new EffectTestScene(); break;
case TEST_CLICK_AND_MOVE:
pScene = new ClickAndMoveTestScene(); break;
case TEST_ROTATE_WORLD:
pScene = new RotateWorldTestScene(); break;
case TEST_PARTICLE:
pScene = new ParticleTestScene(); break;
case TEST_EASE_ACTIONS:
pScene = new ActionsEaseTestScene(); break;
case TEST_MOTION_STREAK:
pScene = new MotionStreakTestScene(); break;
case TEST_DRAW_PRIMITIVES:
pScene = new DrawPrimitivesTestScene(); break;
case TEST_COCOSNODE:
pScene = new CocosNodeTestScene(); break;
case TEST_TOUCHES:
pScene = new PongScene(); break;
case TEST_MENU:
pScene = new MenuTestScene(); break;
case TEST_ACTION_MANAGER:
pScene = new ActionManagerTestScene(); break;
case TEST_LAYER:
pScene = new LayerTestScene(); break;
case TEST_SCENE:
pScene = new SceneTestScene(); break;
case TEST_PARALLAX:
pScene = new ParallaxTestScene(); break;
case TEST_TILE_MAP:
pScene = new TileMapTestScene(); break;
case TEST_INTERVAL:
pScene = new IntervalTestScene(); break;
case TEST_CHIPMUNKACCELTOUCH:
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
pScene = new ChipmunkAccelTouchTestScene(); break;
#else
#ifdef MARMALADEUSECHIPMUNK
#if (MARMALADEUSECHIPMUNK == 1)
pScene = new ChipmunkAccelTouchTestScene();
#endif
break;
#endif
#endif
case TEST_LABEL:
pScene = new AtlasTestScene(); break;
case TEST_TEXT_INPUT:
pScene = new TextInputTestScene(); break;
case TEST_SPRITE:
pScene = new SpriteTestScene(); break;
case TEST_SCHEDULER:
pScene = new SchedulerTestScene(); break;
case TEST_RENDERTEXTURE:
pScene = new RenderTextureScene(); break;
case TEST_TEXTURE2D:
pScene = new TextureTestScene(); break;
case TEST_BOX2D:
pScene = new Box2DTestScene(); break;
case TEST_BOX2DBED:
pScene = new Box2dTestBedScene(); break;
case TEST_EFFECT_ADVANCE:
pScene = new EffectAdvanceScene(); break;
case TEST_ACCELEROMRTER:
pScene = new AccelerometerTestScene(); break;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
case TEST_KEYPAD:
pScene = new KeypadTestScene(); break;
#endif
case TEST_COCOSDENSHION:
pScene = new CocosDenshionTestScene(); break;
case TEST_PERFORMANCE:
pScene = new PerformanceTestScene(); break;
case TEST_ZWOPTEX:
pScene = new ZwoptexTestScene(); break;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
// bada don't support libcurl
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
case TEST_CURL:
pScene = new CurlTestScene(); break;
#endif
#endif
case TEST_USERDEFAULT:
pScene = new UserDefaultTestScene(); break;
case TEST_BUGS:
pScene = new BugsTestScene(); break;
case TEST_FONTS:
pScene = new FontTestScene(); break;
case TEST_CURRENT_LANGUAGE:
pScene = new CurrentLanguageTestScene(); break;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
case TEST_TEXTURECACHE: pScene = new TextureCacheTestScene(); break;
#endif
case TEST_EXTENSIONS:
{
pScene = new ExtensionsTestScene();
}
break;
case TEST_SHADER:
pScene = new ShaderTestScene();
break;
case TEST_MUTITOUCH:
pScene = new MutiTouchTestScene();
break;
default:
break;
}
return pScene;
}
TestController::TestController()
: m_tBeginPos(CCPointZero)//開始時預先賦值
{
// add close menu
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(s_pPathClose, s_pPathClose, this, menu_selector(TestController::closeCallback) );//用圖片創建菜單項,資源全在Resource.h內定義、第一個爲點擊前,第二個爲點擊後。
CCMenu* pMenu =CCMenu::create(pCloseItem, NULL);//以上面的菜單項創建一個菜單
CCSize s = CCDirector::sharedDirector()->getWinSize();
pMenu->setPosition( CCPointZero );//設置菜單點爲0
pCloseItem->setPosition(CCPointMake( s.width - 30, s.height - 30));//在設置點爲右上角
// add menu items for tests
m_pItemMenu = CCMenu::create();//創建一個空菜單
for (int i = 0; i < TESTS_COUNT; ++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].c_str(), "Arial", 24);
// #endif
CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestController::menuCallback));
m_pItemMenu->addChild(pMenuItem, i + 10000);//天價菜單時,菜單項的排序
pMenuItem->setPosition( CCPointMake( s.width / 2, (s.height - (i + 1) * LINE_SPACE) ));//設置菜單項在菜單裏的位置位置爲中間
}
m_pItemMenu->setContentSize(CCSizeMake(s.width, (TESTS_COUNT + 1) * (LINE_SPACE)));//設置菜單內容的寛高
m_pItemMenu->setPosition(s_tCurPos);//設置菜單位置
addChild(m_pItemMenu);//添加到佈局
setTouchEnabled(true);//設置接收觸控事件
addChild(pMenu, 1);添加關閉按鈕。
}
TestController::~TestController()
{
}
void TestController::menuCallback(CCObject * pSender)
{
// get the userdata, it's the index of the menu item clicked
CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);//菜單項
int nIdx = pMenuItem->getZOrder() - 10000;//獲取菜單項的排序
// create the test scene and run it
TestScene* pScene = CreateTestScene(nIdx);//調用上邊創建新場景
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);//觸控
m_tBeginPos = 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 - m_tBeginPos.y;//獲取移動到的做表單
CCPoint curPos = m_pItemMenu->getPosition();//獲取當前菜單
CCPoint nextPos = ccp(curPos.x, curPos.y + nMoveY);;//獲取移動了多少高度
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
if (nextPos.y < 0.0f)//如果移動到最上邊就停止
{
m_pItemMenu->setPosition(CCPointZero);
return;
}
if (nextPos.y > ((TESTS_COUNT + 1)* LINE_SPACE - winSize.height))//如果移動到最下邊
{
m_pItemMenu->setPosition(ccp(0, ((TESTS_COUNT + 1)* LINE_SPACE - winSize.height)));
return;
}
m_pItemMenu->setPosition(nextPos);//設置菜單項顯示到的項
m_tBeginPos = touchLocation;//設置觸摸到的目前的位置
s_tCurPos = nextPos;//存儲已經移動到的點
}
這個主要是學習菜單的操作,