From 26513d6b7efb76490f420e66f0b68d8a07273e00 Mon Sep 17 00:00:00 2001 From: natural-law Date: Thu, 7 Jul 2011 14:55:11 +0800 Subject: [PATCH] issue #574, Add DirectorTest for win32. --- tests/test.win32/test.win32.vcproj | 12 ++ tests/tests/DirectorTest/DirectorTest.cpp | 216 ++++++++++++++++++++++ tests/tests/DirectorTest/DirectorTest.h | 38 ++++ tests/tests/controller.cpp | 9 +- tests/tests/tests.h | 5 +- 5 files changed, 273 insertions(+), 7 deletions(-) create mode 100644 tests/tests/DirectorTest/DirectorTest.cpp create mode 100644 tests/tests/DirectorTest/DirectorTest.h diff --git a/tests/test.win32/test.win32.vcproj b/tests/test.win32/test.win32.vcproj index 41653133f2..8883e288c2 100644 --- a/tests/test.win32/test.win32.vcproj +++ b/tests/test.win32/test.win32.vcproj @@ -963,6 +963,18 @@ > + + + + + + diff --git a/tests/tests/DirectorTest/DirectorTest.cpp b/tests/tests/DirectorTest/DirectorTest.cpp new file mode 100644 index 0000000000..0b13f838a2 --- /dev/null +++ b/tests/tests/DirectorTest/DirectorTest.cpp @@ -0,0 +1,216 @@ +#include "DirectorTest.h" +#include "../testResource.h" + +#define MAX_LAYER 1 + +static int sceneIdx=-1; +static ccDeviceOrientation s_currentOrientation = CCDeviceOrientationPortrait; + +CCLayer* createTestCaseLayer(int index) +{ + switch (index) + { + case 0: + { + Director1* pRet = new Director1(); + pRet->init(); + pRet->autorelease(); + return pRet; + } + default: + return NULL; + } +} + +CCLayer* nextDirectorTestCase() +{ + sceneIdx++; + sceneIdx = sceneIdx % MAX_LAYER; + + return createTestCaseLayer(sceneIdx); +} + +CCLayer* backDirectorTestCase() +{ + sceneIdx--; + if( sceneIdx < 0 ) + sceneIdx += MAX_LAYER; + + return createTestCaseLayer(sceneIdx); +} + +CCLayer* restartDirectorTestCase() +{ + return createTestCaseLayer(sceneIdx); +} + +///--------------------------------------- +// +// DirectorTest +// +///--------------------------------------- +bool DirectorTest::init() +{ + bool bRet = false; + do + { + CC_BREAK_IF(! CCLayer::init()); + CCSize s = CCDirector::sharedDirector()->getWinSize(); + + CCLabelTTF *label = CCLabelTTF::labelWithString(title().c_str(), "Arial", 26); + addChild(label, 1); + label->setPosition(ccp(s.width/2, s.height-50)); + + std::string sSubtitle = subtitle(); + if (sSubtitle.length()) + { + CCLabelTTF *l = CCLabelTTF::labelWithString(sSubtitle.c_str(), "Thonburi", 16); + addChild(l, 1); + l->setPosition(ccp(s.width/2, s.height-80)); + } + + CCMenuItemImage *item1 = CCMenuItemImage::itemFromNormalImage(s_pPathB1, s_pPathB2, this, menu_selector(DirectorTest::backCallback)); + CCMenuItemImage *item2 = CCMenuItemImage::itemFromNormalImage(s_pPathR1, s_pPathR2, this, menu_selector(DirectorTest::restartCallback)); + CCMenuItemImage *item3 = CCMenuItemImage::itemFromNormalImage(s_pPathF1, s_pPathF2, this, menu_selector(DirectorTest::nextCallback)); + + CCMenu *menu = CCMenu::menuWithItems(item1, item2, item3, NULL); + menu->setPosition(CCPointZero); + item1->setPosition(ccp( s.width/2 - 100,30)); + item2->setPosition(ccp( s.width/2, 30)); + item3->setPosition(ccp( s.width/2 + 100,30)); + + bRet = true; + } while (0); + + return bRet; +} + +void DirectorTest::restartCallback(CCObject* pSender) +{ + CCScene *s = new DirectorTestScene(); + s->addChild(restartDirectorTestCase()); + CCDirector::sharedDirector()->replaceScene(s); + s->autorelease(); +} + +void DirectorTest::nextCallback(CCObject* pSender) +{ + CCScene *s = new DirectorTestScene(); + s->addChild(nextDirectorTestCase()); + CCDirector::sharedDirector()->replaceScene(s); + s->autorelease(); +} + +void DirectorTest::backCallback(CCObject* pSender) +{ + CCScene *s = new DirectorTestScene(); + s->addChild(backDirectorTestCase()); + CCDirector::sharedDirector()->replaceScene(s); + s->autorelease(); +} + +std::string DirectorTest::title() +{ + return "No title"; +} + +std::string DirectorTest::subtitle() +{ + return ""; +} + +///--------------------------------------- +// +// Director1 +// +///--------------------------------------- +bool Director1::init() +{ + bool bRet = false; + + do + { + CC_BREAK_IF(! DirectorTest::init()); + + setIsTouchEnabled(true); + CCSize s = CCDirector::sharedDirector()->getWinSize(); + + CCMenuItem *item = CCMenuItemFont::itemFromString("Rotate Device", this, menu_selector(Director1::rotateDevice)); + CCMenu *menu = CCMenu::menuWithItems(item, NULL); + menu->setPosition(ccp( s.width/2, s.height/2)); + addChild(menu); + + bRet = true; + } while (0); + + return bRet; +} + +void Director1::newOrientation() +{ + switch (s_currentOrientation) + { + case CCDeviceOrientationLandscapeLeft: + s_currentOrientation = CCDeviceOrientationPortrait; + break; + case CCDeviceOrientationPortrait: + s_currentOrientation = CCDeviceOrientationLandscapeRight; + break; + case CCDeviceOrientationLandscapeRight: + s_currentOrientation = CCDeviceOrientationPortraitUpsideDown; + break; + case CCDeviceOrientationPortraitUpsideDown: + s_currentOrientation = CCDeviceOrientationLandscapeLeft; + break; + } + CCDirector::sharedDirector()->setDeviceOrientation(s_currentOrientation); +} + +void Director1::rotateDevice(CCObject* pSender) +{ + newOrientation(); + restartCallback(NULL); +} + +void Director1::ccTouchesEnded(CCSet * touches, CCEvent* event) +{ + CCSetIterator it; + CCTouch* touch; + + for( it = touches->begin(); it != touches->end(); it++) + { + touch = (CCTouch*)(*it); + + if(!touch) + break; + CCPoint a = touch->locationInView(touch->view()); + + CCDirector *director = CCDirector::sharedDirector(); + CCPoint b = director->convertToUI(director->convertToGL(a)); + CCLog("(%d,%d) == (%d,%d)", (int) a.x, (int)a.y, (int)b.x, (int)b.y ); + } +} + +std::string Director1::title() +{ + return "Testing conversion"; +} + +std::string Director1::subtitle() +{ + return "Tap screen and see the debug console"; +} + +///--------------------------------------- +// +// DirectorTestScene +// +///--------------------------------------- +void DirectorTestScene::runThisTest() +{ + s_currentOrientation = CCDeviceOrientationPortrait; + CCLayer* pLayer = nextDirectorTestCase(); + addChild(pLayer); + + CCDirector::sharedDirector()->replaceScene(this); +} diff --git a/tests/tests/DirectorTest/DirectorTest.h b/tests/tests/DirectorTest/DirectorTest.h new file mode 100644 index 0000000000..ee058c025f --- /dev/null +++ b/tests/tests/DirectorTest/DirectorTest.h @@ -0,0 +1,38 @@ +#ifndef __DIRECTOR_TEST_H__ +#define __DIRECTOR_TEST_H__ + +#include "../testBasic.h" + +class DirectorTest: public CCLayer +{ +public: + virtual bool init(); + + void restartCallback(CCObject* pSender); + void nextCallback(CCObject* pSender); + void backCallback(CCObject* pSender); + + virtual std::string title(); + virtual std::string subtitle(); +}; + +class Director1 : public DirectorTest +{ +public: + virtual bool init(); + + void newOrientation(); + void rotateDevice(CCObject* pSender); + void ccTouchesEnded(CCSet * touches, CCEvent* event); + + virtual std::string title(); + virtual std::string subtitle(); +}; + +class DirectorTestScene : public TestScene +{ +public: + virtual void runThisTest(); +}; + +#endif diff --git a/tests/tests/controller.cpp b/tests/tests/controller.cpp index db53d92ce7..9885fd0c07 100644 --- a/tests/tests/controller.cpp +++ b/tests/tests/controller.cpp @@ -5,14 +5,9 @@ static CCPoint s_tCurPos = CCPointZero; -static ccDeviceOrientation s_eOrientation = CCDeviceOrientationPortrait; static void ChangeOrientation(ccDeviceOrientation eOrientation) { - if (s_eOrientation != eOrientation) - { - s_eOrientation = eOrientation; - CCDirector::sharedDirector()->setDeviceOrientation(eOrientation); - } + CCDirector::sharedDirector()->setDeviceOrientation(eOrientation); } static TestScene* CreateTestScene(int nIdx) @@ -110,6 +105,8 @@ static TestScene* CreateTestScene(int nIdx) case TEST_USERDEFAULT: pScene = new UserDefaultTestScene(); break; #endif + case TEST_DIRECTOR: + pScene = new DirectorTestScene(); break; default: break; } diff --git a/tests/tests/tests.h b/tests/tests/tests.h index 4b0f02520d..467910cfcc 100644 --- a/tests/tests/tests.h +++ b/tests/tests/tests.h @@ -39,6 +39,7 @@ #include "CocosDenshionTest/CocosDenshionTest.h" #include "CurlTest/CurlTest.h" #include "UserDefaultTest/UserDefaultTest.h" +#include "DirectorTest/DirectorTest.h" #if (CC_TARGET_PLATFORM != CC_PLATFORM_AIRPLAY) #include "ChipmunkTest/cocos2dChipmunkDemo.h" @@ -88,6 +89,7 @@ enum TEST_ZWOPTEX, TEST_CURL, TEST_USERDEFAULT, + TEST_DIRECTOR, TESTS_COUNT, }; @@ -128,7 +130,8 @@ const std::string g_aTestNames[TESTS_COUNT] = { "PerformanceTest", "ZwoptexTest", "CurlTest", - "UserDefaultTest" + "UserDefaultTest", + "DirectorTest" }; #endif