Cocos2d-x学习笔记(18)(TestCpp源代码分析-2)

时间:2021-05-25 15:08:33

本章主要讲controller.h/cpp文件的分析,该文件主要用于演示样例场景管理类TestController,用于显示全部演示样例的菜单。

//controller.cpp
#include "controller.h"
#include "testResource.h"
#include "tests.h" #define LINE_SPACE 40 static CCPoint s_tCurPos = CCPointZero;//设置当前位置为屏幕左下角 static TestScene* CreateTestScene(int nIdx) //静态函数,创建TestScene,nldx与test.h中的枚举变量做比較。确定新建的场景。
{
CCDirector::sharedDirector()->purgeCachedData(); TestScene* pScene = NULL; switch (nIdx) //依据传入的nldx,通过switch函数创建对应的场景
{
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_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;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) //注意
case TEST_CHIPMUNK:
pScene = new ChipmunkAccelTouchTestScene(); break;
#endif
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;
// bada don't support libcurl
#if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA && CC_TARGET_PLATFORM != CC_PLATFORM_NACL && CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
case TEST_CURL:
pScene = new CurlTestScene(); break;
#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;
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE)
case TEST_CLIPPINGNODE:
pScene = new ClippingNodeTestScene();
break;
#endif
case TEST_FILEUTILS:
pScene = new FileUtilsTestScene();
break;
case TEST_SPINE:
pScene = new SpineTestScene();
break;
default:
break;
} return pScene;
} TestController::TestController()
: m_tBeginPos(CCPointZero) //构造函数 {
// add close menu 创建菜单图片,s_pPathClose为testResource.h中的宏定义。menu_selector为TestController的closeCallback函数,相似Qt的信号与槽
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(s_pPathClose, s_pPathClose, this, menu_selector(TestController::closeCallback) );
CCMenu* pMenu =CCMenu::create(pCloseItem, NULL); //创建菜单,用上面创建的CCMenuItemImage pMenu->setPosition( CCPointZero );//设置菜单位置
pCloseItem->setPosition(ccp( VisibleRect::right().x - 30, VisibleRect::top().y - 30)); //设置pCloseItem的位置
//距离右边30,上边30
// add menu items for tests
m_pItemMenu = CCMenu::create(); //m_pItemMenu为 CCMenu*指针变量,用于存储46个演示样例的标题
for (int i = 0; i < TESTS_COUNT; ++i) //TESTS_COUNT为test.h中的枚举变量成员,最后一个成员。
{
// #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); //g_aTestNames是一个string数组
// #endif
CCMenuItemLabel* pMenuItem = CCMenuItemLabel::create(label, this, menu_selector(TestController::menuCallback)); m_pItemMenu->addChild(pMenuItem, i + 10000); //zOrder指定元素先后顺序,为i+10000
pMenuItem->setPosition( ccp( VisibleRect::center().x, (VisibleRect::top().y - (i + 1) * LINE_SPACE) )); //LINE_SPACE是该cpp文件的宏定义,为40
} m_pItemMenu->setContentSize(CCSizeMake(VisibleRect::getVisibleRect().size.width, (TESTS_COUNT + 1) * (LINE_SPACE)));
m_pItemMenu->setPosition(s_tCurPos); //设置该m_pItemMenu位置
addChild(m_pItemMenu); //加入到层 setTouchEnabled(true); //设置触摸功能 addChild(pMenu, 1); //加入关闭菜单到层 } TestController::~TestController() //析构函数
{
} void TestController::menuCallback(CCObject * pSender) //參数为与label连接的节点
{
// get the userdata, it's the index of the menu item clicked
CCMenuItem* pMenuItem = (CCMenuItem *)(pSender);
int nIdx = pMenuItem->getZOrder() - 10000; //获得节点的zOrder值,由于创建时是i+10000,此处减去10000; // create the test scene and run it 依据nldx值传递到CreateTestScene函数中,创建对应的场景
TestScene* pScene = CreateTestScene(nIdx); //CreateTestScene为static函数,为整个类共同拥有
if (pScene)
{
pScene->runThisTest(); //创建成功执行该场景
pScene->release(); //通过回收池释放场景内存
}
} void TestController::closeCallback(CCObject * pSender) //pCloseItem调用的关闭函数
{
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); if (nextPos.y < 0.0f)
{
m_pItemMenu->setPosition(CCPointZero);
return;
} if (nextPos.y > ((TESTS_COUNT + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height))
{
m_pItemMenu->setPosition(ccp(0, ((TESTS_COUNT + 1)* LINE_SPACE - VisibleRect::getVisibleRect().size.height)));
return;
} m_pItemMenu->setPosition(nextPos);
m_tBeginPos = touchLocation;
s_tCurPos = nextPos;
}

//controller.h

#ifndef _CONTROLLER_H_
#define _CONTROLLER_H_ #include "cocos2d.h" USING_NS_CC; class TestController : public CCLayer //创建全部演示样例的菜单。继承自CCLayer层
{
public:
TestController();
~TestController(); void menuCallback(CCObject * pSender); //返回主菜单函数
void closeCallback(CCObject * pSender); //关闭应用程序函数 virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); //重写该函数
virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent); //重写该函数 private:
CCPoint m_tBeginPos; //起始位置
CCMenu* m_pItemMenu; //菜单
}; #endif