2014-01-14 06:48:12 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-01-14 06:48:12 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2014-01-14 06:48:12 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "MenuTest.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2015-04-09 08:37:30 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
kTagMenu = 1,
|
|
|
|
kTagMenu0 = 0,
|
2012-04-19 14:35:52 +08:00
|
|
|
kTagMenu1 = 1,
|
|
|
|
};
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
#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
|
2012-04-19 14:35:52 +08:00
|
|
|
#define MID_BACKCALLBACK 1009
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
MenuTests::MenuTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(MenuTest);
|
|
|
|
}
|
2010-08-28 15:47:51 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
2012-03-22 14:22:06 +08:00
|
|
|
// MenuLayerMainMenu
|
2010-08-28 15:47:51 +08:00
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
2012-03-22 14:22:06 +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);
|
2022-07-15 19:17:01 +08:00
|
|
|
_touchListener->onTouchBegan = AX_CALLBACK_2(MenuLayerMainMenu::touchBegan, this);
|
|
|
|
_touchListener->onTouchMoved = AX_CALLBACK_2(MenuLayerMainMenu::touchMoved, this);
|
|
|
|
_touchListener->onTouchEnded = AX_CALLBACK_2(MenuLayerMainMenu::touchEnded, this);
|
|
|
|
_touchListener->onTouchCancelled = AX_CALLBACK_2(MenuLayerMainMenu::touchCancelled, this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(_touchListener, 1);
|
2021-12-28 16:06:23 +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,
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_CALLBACK_1(MenuLayerMainMenu::menuCallback, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Image Item
|
2021-12-28 16:06:23 +08:00
|
|
|
auto item2 =
|
2022-07-15 19:17:01 +08:00
|
|
|
MenuItemImage::create(s_SendScore, s_PressSendScore, AX_CALLBACK_1(MenuLayerMainMenu::menuCallback2, this));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Label Item (LabelAtlas)
|
2013-08-16 16:05:27 +08:00
|
|
|
auto labelAtlas = LabelAtlas::create("0123456789", "fonts/labelatlas.png", 16, 24, '.');
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item3 = MenuItemLabel::create(labelAtlas, AX_CALLBACK_1(MenuLayerMainMenu::menuCallbackDisabled, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
item3->setDisabledColor(Color3B(32, 32, 64));
|
|
|
|
item3->setColor(Color3B(200, 200, 255));
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("test MenuItem Label getString: %s", item3->getString().data());
|
2012-04-19 14:35:52 +08:00
|
|
|
// Font Item
|
2021-12-28 16:06:23 +08:00
|
|
|
auto item4 = MenuItemFont::create("I toggle enable items",
|
|
|
|
[&](Ref* sender) { _disabledItem->setEnabled(!_disabledItem->isEnabled()); });
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
item4->setFontSizeObj(20);
|
2014-03-26 23:33:58 +08:00
|
|
|
item4->setFontName("fonts/Marker Felt.ttf");
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// Label Item (LabelBMFont)
|
2014-03-28 10:28:44 +08:00
|
|
|
auto label = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", "configuration");
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item5 = MenuItemLabel::create(label, AX_CALLBACK_1(MenuLayerMainMenu::menuCallbackConfig, this));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Testing issue #500
|
2021-12-28 16:06:23 +08:00
|
|
|
item5->setScale(0.8f);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Events
|
2014-03-26 23:33:58 +08:00
|
|
|
MenuItemFont::setFontName("fonts/Marker Felt.ttf");
|
2012-11-19 13:57:54 +08:00
|
|
|
// Bugs Item
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item6 = MenuItemFont::create("Bugs", AX_CALLBACK_1(MenuLayerMainMenu::menuCallbackBugsTest, this));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Font Item
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item7 = MenuItemFont::create("Quit", AX_CALLBACK_1(MenuLayerMainMenu::onQuit, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
auto item8 = MenuItemFont::create("Remove menu item when moving",
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_CALLBACK_1(MenuLayerMainMenu::menuMovingCallback, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto color_action = TintBy::create(0.5f, 0, -255, -255);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto color_back = color_action->reverse();
|
|
|
|
auto seq = Sequence::create(color_action, color_back, nullptr);
|
2013-11-19 14:32:17 +08:00
|
|
|
item7->runAction(RepeatForever::create(seq));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menu = Menu::create(item1, item2, item3, item4, item5, item6, item7, item8, nullptr);
|
2012-04-19 14:35:52 +08:00
|
|
|
menu->alignItemsVertically();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// elastic effect
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (const auto& child : menu->getChildren())
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto dstPoint = child->getPosition();
|
2021-12-28 16:06:23 +08:00
|
|
|
int offset = (int)(s.width / 2 + 50);
|
|
|
|
if (i % 2 == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
offset = -offset;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
child->setPosition(Vec2(dstPoint.x + offset, dstPoint.y));
|
|
|
|
child->runAction(EaseElasticOut::create(MoveBy::create(2, Vec2(dstPoint.x - offset, 0.0f)), 0.35f));
|
2012-04-19 14:35:52 +08:00
|
|
|
i++;
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
_disabledItem = item3;
|
|
|
|
item3->retain();
|
|
|
|
_disabledItem->setEnabled(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
addChild(menu);
|
2017-09-08 09:28:57 +08:00
|
|
|
menu->setTag(100);
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(Vec2(s.width / 2, s.height / 2));
|
2014-03-19 16:23:14 +08:00
|
|
|
menu->setScale(0);
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->runAction(ScaleTo::create(1, 1));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
bool MenuLayerMainMenu::touchBegan(Touch* touch, Event* event)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void MenuLayerMainMenu::touchEnded(Touch* touch, Event* event) {}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void MenuLayerMainMenu::touchCancelled(Touch* touch, Event* event) {}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void MenuLayerMainMenu::touchMoved(Touch* touch, Event* event) {}
|
2011-07-08 15:57:46 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
MenuLayerMainMenu::~MenuLayerMainMenu()
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
2013-06-15 14:03:30 +08:00
|
|
|
_disabledItem->release();
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayerMainMenu::menuCallback(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
static_cast<LayerMultiplex*>(_parent)->switchTo(1, false);
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayerMainMenu::menuCallbackConfig(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
static_cast<LayerMultiplex*>(_parent)->switchTo(3, false);
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void MenuLayerMainMenu::allowTouches(float dt)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->setPriority(_touchListener, 1);
|
2014-10-04 00:38:36 +08:00
|
|
|
unscheduleAllCallbacks();
|
2013-07-24 06:20:22 +08:00
|
|
|
log("TOUCHES ALLOWED AGAIN");
|
2011-07-08 15:57:46 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void MenuLayerMainMenu::menuCallbackDisabled(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// hijack all touch events for 5 seconds
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->setPriority(_touchListener, -1);
|
2022-07-15 19:17:01 +08:00
|
|
|
schedule(AX_SCHEDULE_SELECTOR(MenuLayerMainMenu::allowTouches), 5.0f);
|
2013-07-24 06:20:22 +08:00
|
|
|
log("TOUCHES DISABLED FOR 5 SECONDS");
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayerMainMenu::menuCallback2(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
static_cast<LayerMultiplex*>(_parent)->switchTo(2, false);
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void MenuLayerMainMenu::menuCallbackBugsTest(Ref* pSender)
|
2012-11-19 13:57:54 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
static_cast<LayerMultiplex*>(_parent)->switchTo(4, false);
|
2012-11-19 13:57:54 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayerMainMenu::onQuit(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
//[[Director sharedDirector] end];
|
2021-12-28 16:06:23 +08:00
|
|
|
// getCocosApp()->exit();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void MenuLayerMainMenu::menuMovingCallback(Ref* pSender)
|
2013-01-17 15:04:48 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
static_cast<LayerMultiplex*>(_parent)->switchTo(5, false);
|
2013-01-17 15:04:48 +08:00
|
|
|
}
|
|
|
|
|
2010-08-28 15:47:51 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MenuLayer2
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
MenuLayer2::MenuLayer2()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < 2; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item1 = MenuItemImage::create(s_PlayNormal, s_PlaySelect, AX_CALLBACK_1(MenuLayer2::menuCallback, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
auto item2 =
|
2022-07-15 19:17:01 +08:00
|
|
|
MenuItemImage::create(s_HighNormal, s_HighSelect, AX_CALLBACK_1(MenuLayer2::menuCallbackOpacity, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
auto item3 =
|
2022-07-15 19:17:01 +08:00
|
|
|
MenuItemImage::create(s_AboutNormal, s_AboutSelect, AX_CALLBACK_1(MenuLayer2::menuCallbackAlign, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
item1->setScaleX(1.5f);
|
|
|
|
item2->setScaleX(0.5f);
|
|
|
|
item3->setScaleX(0.5f);
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
auto menu = Menu::create(item1, item2, item3, nullptr);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(Vec2(s.width / 2, s.height / 2));
|
|
|
|
|
|
|
|
menu->setTag(kTagMenu);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
addChild(menu, 0, 100 + i);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_centeredMenu = menu->getPosition();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_alignedH = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
alignMenusH();
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
MenuLayer2::~MenuLayer2() {}
|
2010-08-28 15:47:51 +08:00
|
|
|
|
|
|
|
void MenuLayer2::alignMenusH()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < 2; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menu = static_cast<Menu*>(getChildByTag(100 + i));
|
|
|
|
menu->setPosition(_centeredMenu);
|
|
|
|
if (i == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// TIP: if no padding, padding = 5
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->alignItemsHorizontally();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto p = menu->getPosition();
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(p + Vec2(0.0f, 30.0f));
|
|
|
|
}
|
|
|
|
else
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// TIP: but padding is configurable
|
|
|
|
menu->alignItemsHorizontallyWithPadding(40);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto p = menu->getPosition();
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(p - Vec2(0.0f, 30.0f));
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MenuLayer2::alignMenusV()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < 2; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menu = static_cast<Menu*>(getChildByTag(100 + i));
|
|
|
|
menu->setPosition(_centeredMenu);
|
|
|
|
if (i == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// TIP: if no padding, padding = 5
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->alignItemsVertically();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto p = menu->getPosition();
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(p + Vec2(100.0f, 0.0f));
|
|
|
|
}
|
|
|
|
else
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// TIP: but padding is configurable
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->alignItemsVerticallyWithPadding(40);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto p = menu->getPosition();
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(p - Vec2(100.0f, 0.0f));
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayer2::menuCallback(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
static_cast<LayerMultiplex*>(_parent)->switchTo(0, false);
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayer2::menuCallbackOpacity(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menu = static_cast<Menu*>(static_cast<Node*>(sender)->getParent());
|
2019-06-05 17:58:33 +08:00
|
|
|
uint8_t opacity = menu->getOpacity();
|
2021-12-28 16:06:23 +08:00
|
|
|
if (opacity == 128)
|
2012-04-19 14:35:52 +08:00
|
|
|
menu->setOpacity(255);
|
|
|
|
else
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setOpacity(128);
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayer2::menuCallbackAlign(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_alignedH = !_alignedH;
|
|
|
|
|
|
|
|
if (_alignedH)
|
2012-04-19 14:35:52 +08:00
|
|
|
alignMenusH();
|
|
|
|
else
|
2021-12-28 16:06:23 +08:00
|
|
|
alignMenusV();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2010-08-28 15:47:51 +08:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MenuLayer3
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
MenuLayer3::MenuLayer3()
|
|
|
|
{
|
2014-03-26 23:33:58 +08:00
|
|
|
MenuItemFont::setFontName("fonts/Marker Felt.ttf");
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontSize(28);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-03-28 10:28:44 +08:00
|
|
|
auto label = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", "Enable AtlasItem");
|
2021-12-28 16:06:23 +08:00
|
|
|
auto item1 = MenuItemLabel::create(label, [&](Ref* sender) {
|
2022-07-16 10:43:05 +08:00
|
|
|
// AXLOG("Label clicked. Toggling AtlasSprite");
|
2021-12-28 16:06:23 +08:00
|
|
|
_disabledItem->setEnabled(!_disabledItem->isEnabled());
|
|
|
|
_disabledItem->stopAllActions();
|
|
|
|
});
|
|
|
|
auto item2 = MenuItemFont::create("--- Go Back ---",
|
|
|
|
[&](Ref* sender) { static_cast<LayerMultiplex*>(_parent)->switchTo(0, false); });
|
|
|
|
|
|
|
|
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,
|
|
|
|
[](Ref* sender) { log("sprite clicked!"); });
|
|
|
|
_disabledItem = item3;
|
|
|
|
item3->retain();
|
|
|
|
_disabledItem->setEnabled(false);
|
|
|
|
|
|
|
|
auto menu = Menu::create(item1, item2, item3, nullptr);
|
|
|
|
menu->setPosition(Vec2(0, 0));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
item1->setPosition(Vec2(s.width / 2 - 150, s.height / 2));
|
|
|
|
item2->setPosition(Vec2(s.width / 2 - 200, s.height / 2));
|
|
|
|
item3->setPosition(Vec2(s.width / 2, s.height / 2 - 100));
|
|
|
|
|
|
|
|
auto jump = JumpBy::create(3, Vec2(400, 0), 50, 4);
|
|
|
|
item2->runAction(RepeatForever::create(Sequence::create(jump, jump->reverse(), nullptr)));
|
2012-12-10 14:12:56 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto spin1 = RotateBy::create(3, 360);
|
|
|
|
auto spin2 = spin1->clone();
|
|
|
|
auto spin3 = spin1->clone();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
item1->runAction(RepeatForever::create(spin1));
|
|
|
|
item2->runAction(RepeatForever::create(spin2));
|
|
|
|
item3->runAction(RepeatForever::create(spin3));
|
|
|
|
|
|
|
|
addChild(menu);
|
|
|
|
|
|
|
|
menu->setPosition(Vec2(0, 0));
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MenuLayer3::~MenuLayer3()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_disabledItem->release();
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// MenuLayer4
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
MenuLayer4::MenuLayer4()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontName("American Typewriter");
|
|
|
|
MenuItemFont::setFontSize(18);
|
2013-08-16 16:05:27 +08:00
|
|
|
auto title1 = MenuItemFont::create("Sound");
|
2012-06-15 16:47:30 +08:00
|
|
|
title1->setEnabled(false);
|
2021-12-28 16:06:23 +08:00
|
|
|
MenuItemFont::setFontName("fonts/Marker Felt.ttf");
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontSize(34);
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item1 = MenuItemToggle::createWithCallback(AX_CALLBACK_1(MenuLayer4::menuCallback, this),
|
2021-12-28 16:06:23 +08:00
|
|
|
MenuItemFont::create("On"), MenuItemFont::create("Off"), nullptr);
|
|
|
|
|
|
|
|
MenuItemFont::setFontName("American Typewriter");
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontSize(18);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto title2 = MenuItemFont::create("Music");
|
2012-06-15 16:47:30 +08:00
|
|
|
title2->setEnabled(false);
|
2021-12-28 16:06:23 +08:00
|
|
|
MenuItemFont::setFontName("fonts/Marker Felt.ttf");
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontSize(34);
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item2 = MenuItemToggle::createWithCallback(AX_CALLBACK_1(MenuLayer4::menuCallback, this),
|
2021-12-28 16:06:23 +08:00
|
|
|
MenuItemFont::create("On"), MenuItemFont::create("Off"), nullptr);
|
|
|
|
|
|
|
|
MenuItemFont::setFontName("American Typewriter");
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontSize(18);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto title3 = MenuItemFont::create("Quality");
|
|
|
|
title3->setEnabled(false);
|
|
|
|
MenuItemFont::setFontName("fonts/Marker Felt.ttf");
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontSize(34);
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item3 = MenuItemToggle::createWithCallback(AX_CALLBACK_1(MenuLayer4::menuCallback, this),
|
2021-12-28 16:06:23 +08:00
|
|
|
MenuItemFont::create("High"), MenuItemFont::create("Low"), nullptr);
|
|
|
|
|
|
|
|
MenuItemFont::setFontName("American Typewriter");
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontSize(18);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto title4 = MenuItemFont::create("Orientation");
|
2012-06-15 16:47:30 +08:00
|
|
|
title4->setEnabled(false);
|
2021-12-28 16:06:23 +08:00
|
|
|
MenuItemFont::setFontName("fonts/Marker Felt.ttf");
|
2013-06-20 14:17:10 +08:00
|
|
|
MenuItemFont::setFontSize(34);
|
2022-07-15 19:17:01 +08:00
|
|
|
auto item4 = MenuItemToggle::createWithCallback(AX_CALLBACK_1(MenuLayer4::menuCallback, this),
|
2021-12-28 16:06:23 +08:00
|
|
|
MenuItemFont::create("Off"), nullptr);
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// TIP: you can manipulate the items like any other MutableArray
|
2021-12-28 16:06:23 +08:00
|
|
|
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
|
2021-12-28 16:06:23 +08:00
|
|
|
item4->setSelectedIndex(2);
|
|
|
|
|
|
|
|
MenuItemFont::setFontName("fonts/Marker Felt.ttf");
|
|
|
|
MenuItemFont::setFontSize(34);
|
|
|
|
|
|
|
|
auto label = Label::createWithBMFont("fonts/bitmapFontTest3.fnt", "go back");
|
2022-07-15 19:17:01 +08:00
|
|
|
auto back = MenuItemLabel::create(label, AX_CALLBACK_1(MenuLayer4::backCallback, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(title1, title2, item1, item2, title3, title4, item3, item4, back, nullptr); // 9 items.
|
|
|
|
|
2014-07-11 15:10:19 +08:00
|
|
|
menu->alignItemsInColumns(2, 2, 2, 2, 1, NULL);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
addChild(menu);
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(Vec2(s.width / 2, s.height / 2));
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
MenuLayer4::~MenuLayer4() {}
|
2010-08-28 15:47:51 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayer4::menuCallback(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
// AXLOG("selected item: %x index:%d", dynamic_cast<MenuItemToggle*>(sender)->selectedItem(),
|
2021-12-28 16:06:23 +08:00
|
|
|
// dynamic_cast<MenuItemToggle*>(sender)->selectedIndex() );
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void MenuLayer4::backCallback(Ref* sender)
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
static_cast<LayerMultiplex*>(_parent)->switchTo(0, false);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 13:57:54 +08:00
|
|
|
// BugsTest
|
|
|
|
BugsTest::BugsTest()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
auto issue1410 = MenuItemFont::create("Issue 1410", AX_CALLBACK_1(BugsTest::issue1410MenuCallback, this));
|
|
|
|
auto issue1410_2 = MenuItemFont::create("Issue 1410 #2", AX_CALLBACK_1(BugsTest::issue1410v2MenuCallback, this));
|
|
|
|
auto back = MenuItemFont::create("Back", AX_CALLBACK_1(BugsTest::backMenuCallback, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
auto menu = Menu::create(issue1410, issue1410_2, back, nullptr);
|
2012-11-19 13:57:54 +08:00
|
|
|
addChild(menu);
|
|
|
|
menu->alignItemsVertically();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
menu->setPosition(Vec2(s.width / 2, s.height / 2));
|
2012-11-19 13:57:54 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void BugsTest::issue1410MenuCallback(Ref* sender)
|
2012-11-19 13:57:54 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menu = static_cast<Menu*>(static_cast<Node*>(sender)->getParent());
|
2013-10-23 16:14:03 +08:00
|
|
|
menu->setEnabled(false);
|
|
|
|
menu->setEnabled(true);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-07-24 06:20:22 +08:00
|
|
|
log("NO CRASHES");
|
2012-11-19 13:57:54 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void BugsTest::issue1410v2MenuCallback(axis::Ref* pSender)
|
2012-11-19 13:57:54 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto menu = static_cast<Menu*>(static_cast<MenuItem*>(pSender)->getParent());
|
2013-10-23 16:14:03 +08:00
|
|
|
menu->setEnabled(true);
|
|
|
|
menu->setEnabled(false);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-07-24 06:20:22 +08:00
|
|
|
log("NO CRASHES. AND MENU SHOULD STOP WORKING");
|
2012-11-19 13:57:54 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void BugsTest::backMenuCallback(axis::Ref* pSender)
|
2012-11-19 13:57:54 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
static_cast<LayerMultiplex*>(_parent)->switchTo(0, false);
|
2012-11-19 13:57:54 +08:00
|
|
|
}
|
|
|
|
|
2013-01-17 15:04:48 +08:00
|
|
|
RemoveMenuItemWhenMove::RemoveMenuItemWhenMove()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithTTF("click item and move, should not crash", "fonts/arial.ttf", 20);
|
2021-12-28 16:06:23 +08:00
|
|
|
label->setPosition(Vec2(s.width / 2, s.height - 30));
|
2013-01-17 15:04:48 +08:00
|
|
|
addChild(label);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
item = MenuItemFont::create("item 1");
|
2013-01-17 15:04:48 +08:00
|
|
|
item->retain();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
auto back = MenuItemFont::create("go back", AX_CALLBACK_1(RemoveMenuItemWhenMove::goBack, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
auto menu = Menu::create(item, back, nullptr);
|
2013-01-17 15:04:48 +08:00
|
|
|
addChild(menu);
|
|
|
|
menu->alignItemsVertically();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
menu->setPosition(Vec2(s.width / 2, s.height / 2));
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
// Register Touch Event
|
2013-10-23 11:27:24 +08:00
|
|
|
_touchListener = EventListenerTouchOneByOne::create();
|
2013-09-17 10:46:31 +08:00
|
|
|
_touchListener->setSwallowTouches(false);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
_touchListener->onTouchBegan = AX_CALLBACK_2(RemoveMenuItemWhenMove::onTouchBegan, this);
|
|
|
|
_touchListener->onTouchMoved = AX_CALLBACK_2(RemoveMenuItemWhenMove::onTouchMoved, this);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithFixedPriority(_touchListener, -129);
|
2013-01-17 15:04:48 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void RemoveMenuItemWhenMove::goBack(Ref* pSender)
|
2013-01-17 15:04:48 +08:00
|
|
|
{
|
2017-09-08 09:28:57 +08:00
|
|
|
static_cast<LayerMultiplex*>(_parent)->switchTo(0, false);
|
2013-01-17 15:04:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
RemoveMenuItemWhenMove::~RemoveMenuItemWhenMove()
|
|
|
|
{
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->removeEventListener(_touchListener);
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(item);
|
2013-01-17 15:04:48 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
bool RemoveMenuItemWhenMove::onTouchBegan(Touch* touch, Event* event)
|
2013-01-17 15:04:48 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void RemoveMenuItemWhenMove::onTouchMoved(Touch* touch, Event* event)
|
2013-01-17 15:04:48 +08:00
|
|
|
{
|
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
item->removeFromParentAndCleanup(true);
|
|
|
|
item->release();
|
2014-07-10 00:45:27 +08:00
|
|
|
item = nullptr;
|
2013-01-17 15:04:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
bool MenuTest::init()
|
2010-08-28 15:47:51 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
if (TestCase::init())
|
|
|
|
{
|
|
|
|
MenuItemFont::setFontSize(20);
|
|
|
|
|
2021-12-08 00:11:53 +08:00
|
|
|
auto layer1 = new MenuLayerMainMenu();
|
|
|
|
auto layer2 = new MenuLayer2();
|
|
|
|
auto layer3 = new MenuLayer3();
|
|
|
|
auto layer4 = new MenuLayer4();
|
|
|
|
auto layer5 = new BugsTest();
|
|
|
|
auto layer6 = new RemoveMenuItemWhenMove();
|
2015-04-03 14:31:03 +08:00
|
|
|
|
|
|
|
auto layer = LayerMultiplex::create(layer1, layer2, layer3, layer4, layer5, layer6, nullptr);
|
|
|
|
addChild(layer, 0);
|
|
|
|
|
|
|
|
layer1->release();
|
|
|
|
layer2->release();
|
|
|
|
layer3->release();
|
|
|
|
layer4->release();
|
|
|
|
layer5->release();
|
|
|
|
layer6->release();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
return false;
|
2010-08-28 15:47:51 +08:00
|
|
|
}
|