Updated CCControlExtension

This commit is contained in:
James Chen 2012-04-16 21:51:06 +08:00
parent 959acd556f
commit ac6e263621
8 changed files with 201 additions and 173 deletions

View File

@ -106,6 +106,8 @@ public:
};
#define CCStringMake(str) CCString::stringWithCString(str)
#define ccs CCStringMake
NS_CC_END

View File

@ -25,47 +25,52 @@
#include "ExtensionsTest/ControlExtensionTest/CCControlButtonTest/CCControlButtonTest.h"
CCControlButtonTest_HelloVariableSize::CCControlButtonTest_HelloVariableSize()
bool CCControlButtonTest_HelloVariableSize::init()
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
// Defines an array of title to create buttons dynamically
CCArray *stringArray = CCArray::arrayWithObjects(
CCStringMake("Hello"),
CCStringMake("Variable"),
CCStringMake("Size"),
CCStringMake("!"),
NULL);
CCNode *layer = CCNode::node();
addChild(layer, 1);
double total_width = 0, height = 0;
// For each title in the array
CCObject* pObj = NULL;
CCARRAY_FOREACH(stringArray, pObj)
{
CCString* title = (CCString*)pObj;
// Creates a button with this string as title
CCControlButton *button = standardButtonWithTitle(title->getCString());
button->setPosition(ccp (total_width + button->getContentSize().width / 2, button->getContentSize().height / 2));
layer->addChild(button);
// Compute the size of the layer
height = button->getContentSize().height;
total_width += button->getContentSize().width;
}
if (CCControlScene::init())
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
// Defines an array of title to create buttons dynamically
CCArray *stringArray = CCArray::arrayWithObjects(
ccs("Hello"),
ccs("Variable"),
ccs("Size"),
ccs("!"),
NULL);
CCNode *layer = CCNode::node();
addChild(layer, 1);
double total_width = 0, height = 0;
// For each title in the array
CCObject* pObj = NULL;
CCARRAY_FOREACH(stringArray, pObj)
{
CCString* title = (CCString*)pObj;
// Creates a button with this string as title
CCControlButton *button = standardButtonWithTitle(title->getCString());
button->setPosition(ccp (total_width + button->getContentSize().width / 2, button->getContentSize().height / 2));
layer->addChild(button);
// Compute the size of the layer
height = button->getContentSize().height;
total_width += button->getContentSize().width;
}
layer->setAnchorPoint(ccp (0.5, 0.5));
layer->setContentSize(CCSizeMake(total_width, height));
layer->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
// Add the black background
CCScale9Sprite *background = CCScale9Sprite::spriteWithFile("buttonBackground.png");
background->setContentSize(CCSizeMake(total_width + 14, height + 14));
background->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(background);
layer->setAnchorPoint(ccp (0.5, 0.5));
layer->setContentSize(CCSizeMake(total_width, height));
layer->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
// Add the black background
CCScale9Sprite *background = CCScale9Sprite::spriteWithFile("buttonBackground.png");
background->setContentSize(CCSizeMake(total_width + 14, height + 14));
background->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(background);
return true;
}
return false;
}
CCControlButton *CCControlButtonTest_HelloVariableSize::standardButtonWithTitle(const char * title)
@ -90,48 +95,52 @@ CCControlButtonTest_Event::~CCControlButtonTest_Event()
CC_SAFE_RELEASE_NULL(m_pDisplayValueLabel);
}
CCControlButtonTest_Event::CCControlButtonTest_Event()
bool CCControlButtonTest_Event::init()
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
if (CCControlScene::init())
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
// Add a label in which the button events will be displayed
setDisplayValueLabel(CCLabelTTF::labelWithString("No Event", "Marker Felt", 32));
m_pDisplayValueLabel->setAnchorPoint(ccp(0.5f, -1));
m_pDisplayValueLabel->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(m_pDisplayValueLabel, 1);
// Add the button
CCScale9Sprite *backgroundButton = CCScale9Sprite::spriteWithFile("button.png");
CCScale9Sprite *backgroundHighlightedButton = CCScale9Sprite::spriteWithFile("buttonHighlighted.png");
CCLabelTTF *titleButton = CCLabelTTF::labelWithString("Touch Me!", "Marker Felt", 30);
// Add a label in which the button events will be displayed
setDisplayValueLabel(CCLabelTTF::labelWithString("No Event", "Marker Felt", 32));
m_pDisplayValueLabel->setAnchorPoint(ccp(0.5f, -1));
m_pDisplayValueLabel->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(m_pDisplayValueLabel, 1);
// Add the button
CCScale9Sprite *backgroundButton = CCScale9Sprite::spriteWithFile("button.png");
CCScale9Sprite *backgroundHighlightedButton = CCScale9Sprite::spriteWithFile("buttonHighlighted.png");
CCLabelTTF *titleButton = CCLabelTTF::labelWithString("Touch Me!", "Marker Felt", 30);
titleButton->setColor(ccc3(159, 168, 176));
CCControlButton *controlButton = CCControlButton::buttonWithLabelAndBackgroundSprite(titleButton
backgroundButton);
controlButton->setBackgroundSpriteForState(backgroundHighlightedButton, CCControlStateHighlighted);
controlButton->setTitleColorForState(ccWHITE, CCControlStateHighlighted);
controlButton->setAnchorPoint(ccp(0.5f, 1));
controlButton->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(controlButton, 1);
titleButton->setColor(ccc3(159, 168, 176));
CCControlButton *controlButton = CCControlButton::buttonWithLabelAndBackgroundSprite(titleButton, backgroundButton);
controlButton->setBackgroundSpriteForState(backgroundHighlightedButton, CCControlStateHighlighted);
controlButton->setTitleColorForState(ccWHITE, CCControlStateHighlighted);
controlButton->setAnchorPoint(ccp(0.5f, 1));
controlButton->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(controlButton, 1);
// Add the black background
CCScale9Sprite *background = CCScale9Sprite::spriteWithFile("buttonBackground.png");
background->setContentSize(CCSizeMake(300, 170));
background->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(background);
// Sets up event handlers
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDownAction), CCControlEventTouchDown);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDragInsideAction), CCControlEventTouchDragInside);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDragOutsideAction), CCControlEventTouchDragOutside);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDragEnterAction), CCControlEventTouchDragEnter);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDragExitAction), CCControlEventTouchDragExit);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchUpInsideAction), CCControlEventTouchUpInside);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchUpOutsideAction), CCControlEventTouchUpOutside);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchCancelAction), CCControlEventTouchCancel);
// Add the black background
CCScale9Sprite *background = CCScale9Sprite::spriteWithFile("buttonBackground.png");
background->setContentSize(CCSizeMake(300, 170));
background->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(background);
// Sets up event handlers
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDownAction), CCControlEventTouchDown);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDragInsideAction), CCControlEventTouchDragInside);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDragOutsideAction), CCControlEventTouchDragOutside);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDragEnterAction), CCControlEventTouchDragEnter);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchDragExitAction), CCControlEventTouchDragExit);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchUpInsideAction), CCControlEventTouchUpInside);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchUpOutsideAction), CCControlEventTouchUpOutside);
controlButton->addTargetWithActionForControlEvent(this, menu_selector(CCControlButtonTest_Event::touchCancelAction), CCControlEventTouchCancel);
return true;
}
return false;
}
void CCControlButtonTest_Event::touchDownAction(CCObject *sender)
@ -177,42 +186,47 @@ void CCControlButtonTest_Event::touchCancelAction(CCObject *sender)
//CCControlButtonTest_Styling
CCControlButtonTest_Styling::CCControlButtonTest_Styling()
bool CCControlButtonTest_Styling::init()
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
if (CCControlScene::init())
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
CCNode *layer = CCNode::node();
addChild(layer, 1);
int space = 10; // px
double max_w = 0, max_h = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
// Add the buttons
CCControlButton *button = standardButtonWithTitle(CCString::stringWithFormat("%d",rand() % 30)->getCString());
button->setAdjustBackgroundImage(false); // Tells the button that the background image must not be adjust
// It'll use the prefered size of the background image
button->setPosition(ccp (button->getContentSize().width / 2 + (button->getContentSize().width + space) * i,
button->getContentSize().height / 2 + (button->getContentSize().height + space) * j);
layer->addChild(button);
max_w = MAX(button->getContentSize().width * (i + 1) + space * i, max_w);
max_h = MAX(button->getContentSize().height * (j + 1) + space * j, max_h);
}
}
layer->setAnchorPoint(ccp (0.5, 0.5));
layer->setContentSize(CCSizeMake(max_w, max_h));
layer->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
// Add the black background
CCScale9Sprite *backgroundButton = CCScale9Sprite::spriteWithFile("buttonBackground.png");
backgroundButton->setContentSize(CCSizeMake(max_w + 14, max_h + 14));
backgroundButton->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(backgroundButton);
CCNode *layer = CCNode::node();
addChild(layer, 1);
int space = 10; // px
double max_w = 0, max_h = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
// Add the buttons
CCControlButton *button = standardButtonWithTitle(CCString::stringWithFormat("%d",rand() % 30)->getCString());
button->setAdjustBackgroundImage(false); // Tells the button that the background image must not be adjust
// It'll use the prefered size of the background image
button->setPosition(ccp (button->getContentSize().width / 2 + (button->getContentSize().width + space) * i,
button->getContentSize().height / 2 + (button->getContentSize().height + space) * j));
layer->addChild(button);
max_w = MAX(button->getContentSize().width * (i + 1) + space * i, max_w);
max_h = MAX(button->getContentSize().height * (j + 1) + space * j, max_h);
}
}
layer->setAnchorPoint(ccp (0.5, 0.5));
layer->setContentSize(CCSizeMake(max_w, max_h));
layer->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
// Add the black background
CCScale9Sprite *backgroundButton = CCScale9Sprite::spriteWithFile("buttonBackground.png");
backgroundButton->setContentSize(CCSizeMake(max_w + 14, max_h + 14));
backgroundButton->setPosition(ccp(screenSize.width / 2.0f, screenSize.height / 2.0f));
addChild(backgroundButton);
return true;
}
return false;
}

View File

@ -23,22 +23,26 @@
*
*/
#include "ExtensionsTest/ControlExtensionTest/CCControlScene.h"
#ifndef __CCCONTROLBUTTONTEST_H__
#define __CCCONTROLBUTTONTEST_H__
#include "../CCControlScene.h"
class CCControlButtonTest_HelloVariableSize : public CCControlScene
{
public:
CCControlButtonTest_HelloVariableSize();
bool init();
/** Creates and return a button with a default background and title color. */
CCControlButton *standardButtonWithTitle(const char * title);
SCENE_NODE_FUNC(CCControlButtonTest_HelloVariableSize)
CONTROL_SCENE_NODE_FUNC(CCControlButtonTest_HelloVariableSize)
};
class CCControlButtonTest_Event : public CCControlScene
{
public:
CCControlButtonTest_Event();
~CCControlButtonTest_Event();
bool init();
void touchDownAction(CCObject *sender);
void touchDragInsideAction(CCObject *sender);
void touchDragOutsideAction(CCObject *sender);
@ -50,15 +54,17 @@ public:
protected:
CCLabelTTF *displayEvents;
CC_SYNTHESIZE_RETAIN(CCLabelTTF *, m_pDisplayValueLabel, DisplayValueLabel)
SCENE_NODE_FUNC(CCControlButtonTest_Event)
CONTROL_SCENE_NODE_FUNC(CCControlButtonTest_Event)
};
class CCControlButtonTest_Styling : public CCControlScene
{
public:
CCControlButtonTest_Styling();
bool init();
CCControlButton *standardButtonWithTitle(const char *title);
SCENE_NODE_FUNC(CCControlButtonTest_Styling)
CONTROL_SCENE_NODE_FUNC(CCControlButtonTest_Styling)
};
#endif /* __CCCONTROLBUTTONTEST_H__ */

View File

@ -47,7 +47,7 @@ bool CCControlScene::init()
CCSize screensize = CCDirector::sharedDirector()->getWinSize();
// Add the generated background
CCSprite *background = CCSprite spriteWithFile("background.png");
CCSprite *background = CCSprite::spriteWithFile("background.png");
background->setPosition(ccp(screensize.width / 2, screensize.height / 2));
addChild(background);
@ -59,19 +59,19 @@ bool CCControlScene::init()
// Add the title
setSceneTitleLabel(CCLabelTTF::labelWithString("Title", "Arial", 12));
m_pSceneTitleLabel->setPosition(ccp (screensize.width / 2, screensize.height - m_pSceneTitleLabel->getContentSize().height / 2 - 5)];
m_pSceneTitleLabel->setPosition(ccp (screensize.width / 2, screensize.height - m_pSceneTitleLabel->getContentSize().height / 2 - 5));
addChild(m_pSceneTitleLabel, 1);
// Add the menu
CCMenuItemImage *item1 = CCMenuItemImage::itemWithNormalImage("b1.png", "b2.png", this, menu_selector(CCControlScene::previousCallback)));
CCMenuItemImage *item2 = CCMenuItemImage::itemWithNormalImage("r1.png", "r2.png", this, menu_selector(CCControlScene::restartCallback)));
CCMenuItemImage *item3 = CCMenuItemImage::itemWithNormalImage("f1.png", "f2.png", this, menu_selector(CCControlScene::nextCallback)));
CCMenuItemImage *item1 = CCMenuItemImage::itemWithNormalImage("b1.png", "b2.png", this, menu_selector(CCControlScene::previousCallback));
CCMenuItemImage *item2 = CCMenuItemImage::itemWithNormalImage("r1.png", "r2.png", this, menu_selector(CCControlScene::restartCallback));
CCMenuItemImage *item3 = CCMenuItemImage::itemWithNormalImage("f1.png", "f2.png", this, menu_selector(CCControlScene::nextCallback));
CCMenu *menu = CCMenu::menuWithItems(item1, item3, item2, NULL);
menu->setPosition(CCPointZero);
item1 setPosition(ccp(screensize.width / 2 - 100, 37));
item2 setPosition(ccp(screensize.width / 2, 35));
item3 setPosition(ccp(screensize.width / 2 + 100, 37));
item1->setPosition(ccp(screensize.width / 2 - 100, 37));
item2->setPosition(ccp(screensize.width / 2, 35));
item3->setPosition(ccp(screensize.width / 2 + 100, 37));
addChild(menu ,1);
return true;
@ -79,18 +79,17 @@ bool CCControlScene::init()
return false;
}
void CCControlScene::previousCallback(CCNode* sender)
void CCControlScene::previousCallback(CCObject* sender)
{
CCDirector::sharedDirector()->replaceScene(CCControlSceneManager::sharedControlSceneManager()->previousControlScene());
}
void CCControlScene::restartCallback(CCNode* sender)
void CCControlScene::restartCallback(CCObject* sender)
{
CCDirector::sharedDirector()->replaceScene(CCControlSceneManager::sharedControlSceneManager()->currentControlScene());
}
void CCControlScene::nextCallback(CCNode* sender)
void CCControlScene::nextCallback(CCObject* sender)
{
CCDirector::sharedDirector()->replaceScene(CCControlSceneManager::sharedControlSceneManager()->nextControlScene());
}

View File

@ -23,56 +23,49 @@
*
*/
#ifndef __CCCONTROLSCENE_H__
#define __CCCONTROLSCENE_H__
#include "cocos2d.h"
// 'scene' is an autorelease object.
USING_NS_CC;
// 'layer' is an autorelease object.
CCControlScene *controlLayer = CCControlScene::node();
controlLayer->getSceneTitleLabel()->setString(title);
// add layer as a child to scene
scene->addChild(controlLayer);
// return the scene
return scene;
#define CONTROL_SCENE_NODE_FUNC(scene) \
static scene* sceneWithTitle(const char * title) \
#define CONTROL_SCENE_NODE_FUNC(controlScene) \
public: \
static CCScene* sceneWithTitle(const char * title) \
{ \
scene *pRet = new scene(); \
if (pRet && pRet->init()) \
{ \
pRet->autorelease(); \
} \
else \
{ \
CC_SAFE_DELETE(pRet); \
} \
return pRet; \
CCScene* pScene = CCScene::node(); \
controlScene* controlLayer = new controlScene(); \
if (controlLayer && controlLayer->init()) \
{ \
controlLayer->autorelease(); \
controlLayer->getSceneTitleLabel()->setString(title); \
pScene->addChild(controlLayer); \
} \
else \
{ \
CC_SAFE_DELETE(controlLayer); \
} \
return pScene; \
}
class CCControlScene : public cocos2d::CCLayer
{
public:
CCControlScene();
~CCControlScene();
bool init();
/**
* Returns a CCScene that contains the CCControl example layer.
*
* @param title The title of the scene.
*/
static cocos2d::CCScene* sceneWithTitle(const char * title);
bool init();
// Menu Callbacks
void previousCallback(cocos2d::CCNode* sender);
void restartCallback(cocos2d::CCNode* sender);
void nextCallback(cocos2d::CCNode* sender);
void previousCallback(cocos2d::CCObject* sender);
void restartCallback(cocos2d::CCObject* sender);
void nextCallback(cocos2d::CCObject* sender);
/** Title label of the scene. */
CC_SYNTHESIZE_RETAIN(cocos2d::CCLabelTTF*, m_pSceneTitleLabel, SceneTitleLabel)
LAYER_NODE_FUNC(CCControlScene);
CONTROL_SCENE_NODE_FUNC(CCControlScene);
};
#endif /* __CCCONTROLSCENE_H__ */

View File

@ -25,6 +25,7 @@
#include "CCControlSceneManager.h"
#include "CCControlScene.h"
#include "CCControlButtonTest/CCControlButtonTest.h"
USING_NS_CC;
@ -95,9 +96,9 @@ CCScene *CCControlSceneManager::currentControlScene()
// case kCCControlSliderTest:
// case kCCControlColourPickerTest:
// case kCCControlSwitchTest:
case kCCControlButtonTest_HelloVariableSize:return CCControlButtonTest_HelloVariableSize::node();
case kCCControlButtonTest_Event:return CCControlButtonTest_Event::node();
case kCCControlButtonTest_Styling:return CCControlButtonTest_Styling::node();
case kCCControlButtonTest_HelloVariableSize:return CCControlButtonTest_HelloVariableSize::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]);
case kCCControlButtonTest_Event:return CCControlButtonTest_Event::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]);
case kCCControlButtonTest_Styling:return CCControlButtonTest_Styling::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]);
}
// return nextControlScene::sceneWithTitle(s_testArray[m_nCurrentControlSceneId]);

View File

@ -22,9 +22,13 @@
* THE SOFTWARE.
*
*/
#ifndef __CCCONTROLSCENEMANAGER_H__
#define __CCCONTROLSCENEMANAGER_H__
#include "cocos2d.h"
USING_NS_CC;
class CCControlSceneManager : public cocos2d::CCObject
{
public:
@ -46,3 +50,4 @@ public:
CC_SYNTHESIZE(int, m_nCurrentControlSceneId, CurrentControlSceneId)
};
#endif /* __CCCONTROLSCENEMANAGER_H__ */

View File

@ -1,18 +1,19 @@
#include "ExtensionsTest.h"
#include "../testResource.h"
#include "NotificationCenterTest.h"
#include "NotificationCenterTest/NotificationCenterTest.h"
#include "ControlExtensionTest/CCControlSceneManager.h"
enum
{
MAX_COUNT = 1,
MAX_COUNT = 2,
LINE_SPACE = 40,
kItemTagBasic = 1000,
};
static const std::string testsName[MAX_COUNT] =
{
"NotificationCenterTest"
"NotificationCenterTest",
"CCControlButtonTest"
};
////////////////////////////////////////////////////////
@ -51,6 +52,13 @@ void ExtensionsMainLayer::menuCallback(CCObject* pSender)
case 0:
runNotificationCenterTest();
break;
case 1:
{
CCControlSceneManager* pManager = sharedControlSceneManager();
CCScene* pScene = pManager->currentControlScene();
CCDirector::sharedDirector()->replaceScene(pScene);
}
break;
default:
break;
}