axmol/samples/Cpp/TestCpp/Classes/MenuTest/MenuTest.cpp

562 lines
18 KiB
C++
Raw Normal View History

#include "MenuTest.h"
#include "../testResource.h"
2010-08-28 15:47:51 +08:00
enum {
kTagMenu = 1,
kTagMenu0 = 0,
kTagMenu1 = 1,
};
#define MID_CALLBACK 1000
#define MID_CALLBACK2 1001
#define MID_DISABLED 1002
#define MID_ENABLE 1003
#define MID_CONFIG 1004
#define MID_QUIT 1005
#define MID_OPACITY 1006
#define MID_ALIGN 1007
#define MID_CALLBACK3 1008
#define MID_BACKCALLBACK 1009
2010-08-28 15:47:51 +08:00
//------------------------------------------------------------------
//
// MenuLayerMainMenu
2010-08-28 15:47:51 +08:00
//
//------------------------------------------------------------------
MenuLayerMainMenu::MenuLayerMainMenu()
2010-08-28 15:47:51 +08:00
{
2013-10-23 16:28:54 +08:00
_touchListener = EventListenerTouchOneByOne::create();
_touchListener->setSwallowTouches(true);
_touchListener->onTouchBegan = CC_CALLBACK_2(MenuLayerMainMenu::onTouchBegan, this);
_touchListener->onTouchMoved = CC_CALLBACK_2(MenuLayerMainMenu::onTouchMoved, this);
_touchListener->onTouchEnded = CC_CALLBACK_2(MenuLayerMainMenu::onTouchEnded, this);
_touchListener->onTouchCancelled = CC_CALLBACK_2(MenuLayerMainMenu::onTouchCancelled, this);
_eventDispatcher->addEventListenerWithFixedPriority(_touchListener, 1);
2012-11-19 13:57:54 +08:00
// Font Item
auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23));
auto spriteSelected = Sprite::create(s_MenuItem, Rect(0,23*1,115,23));
auto spriteDisabled = Sprite::create(s_MenuItem, Rect(0,23*0,115,23));
auto item1 = MenuItemSprite::create(spriteNormal, spriteSelected, spriteDisabled, CC_CALLBACK_1(MenuLayerMainMenu::menuCallback, this) );
// Image Item
auto item2 = MenuItemImage::create(s_SendScore, s_PressSendScore, CC_CALLBACK_1(MenuLayerMainMenu::menuCallback2, this) );
// Label Item (LabelAtlas)
auto labelAtlas = LabelAtlas::create("0123456789", "fonts/labelatlas.png", 16, 24, '.');
auto item3 = MenuItemLabel::create(labelAtlas, CC_CALLBACK_1(MenuLayerMainMenu::menuCallbackDisabled, this) );
item3->setDisabledColor( Color3B(32,32,64) );
item3->setColor( Color3B(200,200,255) );
// Font Item
auto item4 = MenuItemFont::create("I toggle enable items", [&](Object *sender) {
_disabledItem->setEnabled(! _disabledItem->isEnabled() );
});
item4->setFontSizeObj(20);
item4->setFontName("Marker Felt");
// Label Item (LabelBMFont)
auto label = LabelBMFont::create("configuration", "fonts/bitmapFontTest3.fnt");
auto item5 = MenuItemLabel::create(label, CC_CALLBACK_1(MenuLayerMainMenu::menuCallbackConfig, this));
// Testing issue #500
item5->setScale( 0.8f );
// Events
MenuItemFont::setFontName("Marker Felt");
2012-11-19 13:57:54 +08:00
// Bugs Item
auto item6 = MenuItemFont::create("Bugs", CC_CALLBACK_1(MenuLayerMainMenu::menuCallbackBugsTest, this));
// Font Item
auto item7= MenuItemFont::create("Quit", CC_CALLBACK_1(MenuLayerMainMenu::onQuit, this));
auto item8 = MenuItemFont::create("Remove menu item when moving", CC_CALLBACK_1(MenuLayerMainMenu::menuMovingCallback, this));
auto color_action = TintBy::create(0.5f, 0, -255, -255);
auto color_back = color_action->reverse();
auto seq = Sequence::create(color_action, color_back, NULL);
item7->runAction(RepeatForever::create(seq));
auto menu = Menu::create( item1, item2, item3, item4, item5, item6, item7, item8, NULL);
menu->alignItemsVertically();
// elastic effect
auto s = Director::getInstance()->getWinSize();
int i=0;
auto& children = menu->getChildren();
std::for_each(children.begin(), children.end(), [&i, &s](Node* child){
auto dstPoint = child->getPosition();
int offset = (int) (s.width/2 + 50);
if( i % 2 == 0)
offset = -offset;
2013-07-12 14:11:55 +08:00
child->setPosition( Point( dstPoint.x + offset, dstPoint.y) );
child->runAction(
EaseElasticOut::create(MoveBy::create(2, Point(dstPoint.x - offset,0)), 0.35f)
);
i++;
});
_disabledItem = item3; item3->retain();
_disabledItem->setEnabled( false );
addChild(menu);
2013-07-12 14:11:55 +08:00
menu->setPosition(Point(s.width/2, s.height/2));
}
bool MenuLayerMainMenu::onTouchBegan(Touch *touch, Event * event)
{
return true;
}
void MenuLayerMainMenu::onTouchEnded(Touch *touch, Event * event)
{
}
void MenuLayerMainMenu::onTouchCancelled(Touch *touch, Event * event)
{
}
void MenuLayerMainMenu::onTouchMoved(Touch *touch, Event * event)
{
}
2011-07-08 15:57:46 +08:00
MenuLayerMainMenu::~MenuLayerMainMenu()
2010-08-28 15:47:51 +08:00
{
_eventDispatcher->removeEventListener(_touchListener);
_disabledItem->release();
2010-08-28 15:47:51 +08:00
}
void MenuLayerMainMenu::menuCallback(Object* sender)
2010-08-28 15:47:51 +08:00
{
static_cast<LayerMultiplex*>(_parent)->switchTo(1);
2010-08-28 15:47:51 +08:00
}
void MenuLayerMainMenu::menuCallbackConfig(Object* sender)
2010-08-28 15:47:51 +08:00
{
static_cast<LayerMultiplex*>(_parent)->switchTo(3);
2010-08-28 15:47:51 +08:00
}
2012-06-08 13:55:28 +08:00
void MenuLayerMainMenu::allowTouches(float dt)
{
_eventDispatcher->setPriority(_touchListener, 1);
unscheduleAllSelectors();
log("TOUCHES ALLOWED AGAIN");
2011-07-08 15:57:46 +08:00
}
void MenuLayerMainMenu::menuCallbackDisabled(Object* sender)
2010-08-28 15:47:51 +08:00
{
// hijack all touch events for 5 seconds
_eventDispatcher->setPriority(_touchListener, -1);
schedule(schedule_selector(MenuLayerMainMenu::allowTouches), 5.0f);
log("TOUCHES DISABLED FOR 5 SECONDS");
2010-08-28 15:47:51 +08:00
}
void MenuLayerMainMenu::menuCallback2(Object* sender)
2010-08-28 15:47:51 +08:00
{
static_cast<LayerMultiplex*>(_parent)->switchTo(2);
2010-08-28 15:47:51 +08:00
}
void MenuLayerMainMenu::menuCallbackBugsTest(Object *pSender)
2012-11-19 13:57:54 +08:00
{
static_cast<LayerMultiplex*>(_parent)->switchTo(4);
2012-11-19 13:57:54 +08:00
}
void MenuLayerMainMenu::onQuit(Object* sender)
2010-08-28 15:47:51 +08:00
{
//[[Director sharedDirector] end];
//getCocosApp()->exit();
}
void MenuLayerMainMenu::menuMovingCallback(Object *pSender)
{
static_cast<LayerMultiplex*>(_parent)->switchTo(5);
}
2010-08-28 15:47:51 +08:00
//------------------------------------------------------------------
//
// MenuLayer2
//
//------------------------------------------------------------------
MenuLayer2::MenuLayer2()
{
for( int i=0;i < 2;i++ )
{
auto item1 = MenuItemImage::create(s_PlayNormal, s_PlaySelect, CC_CALLBACK_1(MenuLayer2::menuCallback, this));
auto item2 = MenuItemImage::create(s_HighNormal, s_HighSelect, CC_CALLBACK_1(MenuLayer2::menuCallbackOpacity, this));
auto item3 = MenuItemImage::create(s_AboutNormal, s_AboutSelect, CC_CALLBACK_1(MenuLayer2::menuCallbackAlign, this));
item1->setScaleX( 1.5f );
item2->setScaleX( 0.5f );
item3->setScaleX( 0.5f );
auto menu = Menu::create(item1, item2, item3, NULL);
auto s = Director::getInstance()->getWinSize();
2013-07-12 14:11:55 +08:00
menu->setPosition(Point(s.width/2, s.height/2));
menu->setTag( kTagMenu );
addChild(menu, 0, 100+i);
_centeredMenu = menu->getPosition();
}
_alignedH = true;
alignMenusH();
2010-08-28 15:47:51 +08:00
}
MenuLayer2::~MenuLayer2()
{
}
void MenuLayer2::alignMenusH()
{
for(int i=0;i<2;i++)
{
auto menu = static_cast<Menu*>( getChildByTag(100+i) );
menu->setPosition( _centeredMenu );
if(i==0)
{
// TIP: if no padding, padding = 5
menu->alignItemsHorizontally();
auto p = menu->getPosition();
2013-07-12 14:11:55 +08:00
menu->setPosition(p + Point(0,30));
}
else
{
// TIP: but padding is configurable
menu->alignItemsHorizontallyWithPadding(40);
auto p = menu->getPosition();
2013-07-12 14:11:55 +08:00
menu->setPosition(p - Point(0,30));
}
}
2010-08-28 15:47:51 +08:00
}
void MenuLayer2::alignMenusV()
{
for(int i=0;i<2;i++)
{
auto menu = static_cast<Menu*>( getChildByTag(100+i) );
menu->setPosition( _centeredMenu );
if(i==0)
{
// TIP: if no padding, padding = 5
menu->alignItemsVertically();
auto p = menu->getPosition();
2013-07-12 14:11:55 +08:00
menu->setPosition(p + Point(100,0));
}
else
{
// TIP: but padding is configurable
menu->alignItemsVerticallyWithPadding(40);
auto p = menu->getPosition();
2013-07-12 14:11:55 +08:00
menu->setPosition(p - Point(100,0));
}
}
2010-08-28 15:47:51 +08:00
}
void MenuLayer2::menuCallback(Object* sender)
2010-08-28 15:47:51 +08:00
{
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
2010-08-28 15:47:51 +08:00
}
void MenuLayer2::menuCallbackOpacity(Object* sender)
2010-08-28 15:47:51 +08:00
{
auto menu = static_cast<Menu*>( static_cast<Node*>(sender)->getParent() );
GLubyte opacity = menu->getOpacity();
if( opacity == 128 )
menu->setOpacity(255);
else
menu->setOpacity(128);
2010-08-28 15:47:51 +08:00
}
void MenuLayer2::menuCallbackAlign(Object* sender)
2010-08-28 15:47:51 +08:00
{
_alignedH = ! _alignedH;
if( _alignedH )
alignMenusH();
else
alignMenusV();
}
2010-08-28 15:47:51 +08:00
//------------------------------------------------------------------
//
// MenuLayer3
//
//------------------------------------------------------------------
MenuLayer3::MenuLayer3()
{
MenuItemFont::setFontName("Marker Felt");
MenuItemFont::setFontSize(28);
auto label = LabelBMFont::create("Enable AtlasItem", "fonts/bitmapFontTest3.fnt");
auto item1 = MenuItemLabel::create(label, [&](Object *sender) {
//CCLOG("Label clicked. Toogling AtlasSprite");
_disabledItem->setEnabled( ! _disabledItem->isEnabled() );
_disabledItem->stopAllActions();
});
auto item2 = MenuItemFont::create("--- Go Back ---", [&](Object *sender) {
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
});
auto spriteNormal = Sprite::create(s_MenuItem, Rect(0,23*2,115,23));
auto spriteSelected = Sprite::create(s_MenuItem, Rect(0,23*1,115,23));
auto spriteDisabled = Sprite::create(s_MenuItem, Rect(0,23*0,115,23));
auto item3 = MenuItemSprite::create(spriteNormal, spriteSelected, spriteDisabled, [](Object *sender) {
log("sprite clicked!");
});
_disabledItem = item3; item3->retain();
_disabledItem->setEnabled( false );
auto menu = Menu::create( item1, item2, item3, NULL);
2013-07-12 14:11:55 +08:00
menu->setPosition( Point(0,0) );
auto s = Director::getInstance()->getWinSize();
2013-07-12 14:11:55 +08:00
item1->setPosition( Point(s.width/2 - 150, s.height/2) );
item2->setPosition( Point(s.width/2 - 200, s.height/2) );
item3->setPosition( Point(s.width/2, s.height/2 - 100) );
auto jump = JumpBy::create(3, Point(400,0), 50, 4);
item2->runAction( RepeatForever::create(Sequence::create( jump, jump->reverse(), NULL)));
auto spin1 = RotateBy::create(3, 360);
auto spin2 = spin1->clone();
auto spin3 = spin1->clone();
item1->runAction( RepeatForever::create(spin1) );
item2->runAction( RepeatForever::create(spin2) );
item3->runAction( RepeatForever::create(spin3) );
addChild( menu );
2013-07-12 14:11:55 +08:00
menu->setPosition(Point(0,0));
2010-08-28 15:47:51 +08:00
}
MenuLayer3::~MenuLayer3()
{
_disabledItem->release();
2010-08-28 15:47:51 +08:00
}
//------------------------------------------------------------------
//
// MenuLayer4
//
//------------------------------------------------------------------
MenuLayer4::MenuLayer4()
{
MenuItemFont::setFontName("American Typewriter");
MenuItemFont::setFontSize(18);
auto title1 = MenuItemFont::create("Sound");
title1->setEnabled(false);
MenuItemFont::setFontName( "Marker Felt" );
MenuItemFont::setFontSize(34);
auto item1 = MenuItemToggle::createWithCallback( CC_CALLBACK_1(MenuLayer4::menuCallback, this),
MenuItemFont::create( "On" ),
MenuItemFont::create( "Off"),
NULL );
2010-08-28 15:47:51 +08:00
MenuItemFont::setFontName( "American Typewriter" );
MenuItemFont::setFontSize(18);
auto title2 = MenuItemFont::create( "Music" );
title2->setEnabled(false);
MenuItemFont::setFontName( "Marker Felt" );
MenuItemFont::setFontSize(34);
auto item2 = MenuItemToggle::createWithCallback(CC_CALLBACK_1(MenuLayer4::menuCallback, this),
MenuItemFont::create( "On" ),
MenuItemFont::create( "Off"),
NULL );
2010-08-28 15:47:51 +08:00
MenuItemFont::setFontName( "American Typewriter" );
MenuItemFont::setFontSize(18);
auto title3 = MenuItemFont::create( "Quality" );
title3->setEnabled( false );
MenuItemFont::setFontName( "Marker Felt" );
MenuItemFont::setFontSize(34);
auto item3 = MenuItemToggle::createWithCallback(CC_CALLBACK_1(MenuLayer4::menuCallback, this),
MenuItemFont::create( "High" ),
MenuItemFont::create( "Low" ),
NULL );
2010-08-28 15:47:51 +08:00
MenuItemFont::setFontName( "American Typewriter" );
MenuItemFont::setFontSize(18);
auto title4 = MenuItemFont::create( "Orientation" );
title4->setEnabled(false);
MenuItemFont::setFontName( "Marker Felt" );
MenuItemFont::setFontSize(34);
auto item4 = MenuItemToggle::createWithCallback(CC_CALLBACK_1(MenuLayer4::menuCallback, this),
MenuItemFont::create( "Off" ),
NULL );
// TIP: you can manipulate the items like any other MutableArray
item4->getSubItems().pushBack( MenuItemFont::create( "33%" ) );
item4->getSubItems().pushBack( MenuItemFont::create( "66%" ) );
item4->getSubItems().pushBack( MenuItemFont::create( "100%" ) );
2010-08-28 15:47:51 +08:00
// you can change the one of the items by doing this
item4->setSelectedIndex( 2 );
MenuItemFont::setFontName( "Marker Felt" );
MenuItemFont::setFontSize( 34 );
auto label = LabelBMFont::create( "go back", "fonts/bitmapFontTest3.fnt" );
auto back = MenuItemLabel::create(label, CC_CALLBACK_1(MenuLayer4::backCallback, this) );
2010-08-28 15:47:51 +08:00
auto menu = Menu::create(
2010-08-28 15:47:51 +08:00
title1, title2,
item1, item2,
title3, title4,
item3, item4,
back, NULL ); // 9 items.
2010-08-28 15:47:51 +08:00
menu->alignItemsInColumns(2, 2, 2, 2, 1, NULL);
2010-08-28 15:47:51 +08:00
addChild( menu );
auto s = Director::getInstance()->getWinSize();
2013-07-12 14:11:55 +08:00
menu->setPosition(Point(s.width/2, s.height/2));
2010-08-28 15:47:51 +08:00
}
MenuLayer4::~MenuLayer4()
{
}
void MenuLayer4::menuCallback(Object* sender)
2010-08-28 15:47:51 +08:00
{
//CCLOG("selected item: %x index:%d", dynamic_cast<MenuItemToggle*>(sender)->selectedItem(), dynamic_cast<MenuItemToggle*>(sender)->selectedIndex() );
2010-08-28 15:47:51 +08:00
}
void MenuLayer4::backCallback(Object* sender)
2010-08-28 15:47:51 +08:00
{
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
}
2012-11-19 13:57:54 +08:00
// BugsTest
BugsTest::BugsTest()
{
auto issue1410 = MenuItemFont::create("Issue 1410", CC_CALLBACK_1(BugsTest::issue1410MenuCallback, this));
auto issue1410_2 = MenuItemFont::create("Issue 1410 #2", CC_CALLBACK_1(BugsTest::issue1410v2MenuCallback, this));
auto back = MenuItemFont::create("Back", CC_CALLBACK_1(BugsTest::backMenuCallback, this));
2012-11-19 13:57:54 +08:00
auto menu = Menu::create(issue1410, issue1410_2, back, NULL);
2012-11-19 13:57:54 +08:00
addChild(menu);
menu->alignItemsVertically();
auto s = Director::getInstance()->getWinSize();
2013-07-12 14:11:55 +08:00
menu->setPosition(Point(s.width/2, s.height/2));
2012-11-19 13:57:54 +08:00
}
void BugsTest::issue1410MenuCallback(Object *sender)
2012-11-19 13:57:54 +08:00
{
auto menu = static_cast<Menu*>( static_cast<Node*>(sender)->getParent() );
menu->setEnabled(false);
menu->setEnabled(true);
2012-11-19 13:57:54 +08:00
log("NO CRASHES");
2012-11-19 13:57:54 +08:00
}
void BugsTest::issue1410v2MenuCallback(cocos2d::Object *pSender)
2012-11-19 13:57:54 +08:00
{
auto menu = static_cast<Menu*>( static_cast<MenuItem*>(pSender)->getParent() );
menu->setEnabled(true);
menu->setEnabled(false);
2012-11-19 13:57:54 +08:00
log("NO CRASHES. AND MENU SHOULD STOP WORKING");
2012-11-19 13:57:54 +08:00
}
void BugsTest::backMenuCallback(cocos2d::Object *pSender)
2012-11-19 13:57:54 +08:00
{
2013-07-09 05:42:08 +08:00
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
2012-11-19 13:57:54 +08:00
}
RemoveMenuItemWhenMove::RemoveMenuItemWhenMove()
{
auto s = Director::getInstance()->getWinSize();
auto label = LabelTTF::create("click item and move, should not crash", "Arial", 20);
2013-07-12 14:11:55 +08:00
label->setPosition(Point(s.width/2, s.height - 30));
addChild(label);
item = MenuItemFont::create("item 1");
item->retain();
auto back = MenuItemFont::create("go back", CC_CALLBACK_1(RemoveMenuItemWhenMove::goBack, this));
auto menu = Menu::create(item, back, NULL);
addChild(menu);
menu->alignItemsVertically();
2013-07-12 14:11:55 +08:00
menu->setPosition(Point(s.width/2, s.height/2));
// Register Touch Event
_touchListener = EventListenerTouchOneByOne::create();
_touchListener->setSwallowTouches(false);
_touchListener->onTouchBegan = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this);
_touchListener->onTouchMoved = CC_CALLBACK_2(RemoveMenuItemWhenMove::onTouchMoved, this);
_eventDispatcher->addEventListenerWithFixedPriority(_touchListener, -129);
}
void RemoveMenuItemWhenMove::goBack(Object *pSender)
{
2013-07-09 05:42:08 +08:00
static_cast<LayerMultiplex*>(_parent)->switchTo(0);
}
RemoveMenuItemWhenMove::~RemoveMenuItemWhenMove()
{
_eventDispatcher->removeEventListener(_touchListener);
CC_SAFE_RELEASE(item);
}
bool RemoveMenuItemWhenMove::onTouchBegan(Touch *touch, Event *event)
{
return true;
}
void RemoveMenuItemWhenMove::onTouchMoved(Touch *touch, Event *event)
{
if (item)
{
item->removeFromParentAndCleanup(true);
item->release();
item = NULL;
}
}
2010-08-28 15:47:51 +08:00
void MenuTestScene::runThisTest()
{
2013-09-18 23:43:04 +08:00
MenuItemFont::setFontSize(20);
auto layer1 = new MenuLayerMainMenu();
auto layer2 = new MenuLayer2();
auto layer3 = new MenuLayer3();
auto layer4 = new MenuLayer4();
auto layer5 = new BugsTest();
auto layer6 = new RemoveMenuItemWhenMove();
2010-08-28 15:47:51 +08:00
auto layer = LayerMultiplex::create(layer1, layer2, layer3, layer4, layer5, layer6, NULL);
2010-08-28 15:47:51 +08:00
addChild(layer, 0);
layer1->release();
layer2->release();
layer3->release();
layer4->release();
layer5->release();
layer6->release();
Director::getInstance()->replaceScene(this);
2010-08-28 15:47:51 +08:00
}