axmol/core/2d/CCMenuItem.cpp

871 lines
21 KiB
C++
Raw Normal View History

2012-02-01 16:45:23 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
2012-02-01 16:45:23 +08:00
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2012-02-01 16:45:23 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2012-02-01 16:45:23 +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.
****************************************************************************/
#include "2d/CCMenuItem.h"
#include "2d/CCActionInterval.h"
#include "2d/CCSprite.h"
2014-08-28 17:03:29 +08:00
#include "2d/CCLabelAtlas.h"
#include "2d/CCLabel.h"
#include "base/ccUTF8.h"
2012-02-01 16:45:23 +08:00
#include <stdarg.h>
NS_AX_BEGIN
2021-12-25 10:04:45 +08:00
static int _globalFontSize = kItemSize;
2013-02-27 17:23:39 +08:00
static std::string _globalFontName = "Marker Felt";
static bool _globalFontNameRelease = false;
2021-12-25 10:04:45 +08:00
const unsigned int kZoomActionTag = 0xc0c05002;
//
// MenuItem
//
2012-07-19 05:40:09 +08:00
MenuItem* MenuItem::create()
2012-07-18 08:08:45 +08:00
{
return MenuItem::create((const ccMenuCallback&)nullptr);
2012-07-18 08:08:45 +08:00
}
2021-12-25 10:04:45 +08:00
MenuItem* MenuItem::create(const ccMenuCallback& callback)
{
2021-12-25 10:04:45 +08:00
MenuItem* ret = new MenuItem();
ret->initWithCallback(callback);
ret->autorelease();
return ret;
}
bool MenuItem::initWithCallback(const ccMenuCallback& callback)
{
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
setAnchorPoint(Vec2(0.5f, 0.5f));
_callback = callback;
2021-12-25 10:04:45 +08:00
_enabled = true;
_selected = false;
return true;
}
2021-12-25 10:04:45 +08:00
MenuItem::~MenuItem() {}
void MenuItem::selected()
{
_selected = true;
}
void MenuItem::unselected()
{
_selected = false;
}
void MenuItem::activate()
{
if (_enabled)
2012-02-02 10:39:36 +08:00
{
2021-12-25 10:04:45 +08:00
if (_callback)
2012-02-01 16:45:23 +08:00
{
_callback(this);
2012-02-01 16:45:23 +08:00
}
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_SCRIPT_BINDING
BasicScriptData data(this);
ScriptEvent scriptEvent(kMenuClickedEvent, &data);
ScriptEngineManager::sendEventToLua(scriptEvent);
#endif
2012-02-02 10:39:36 +08:00
}
}
void MenuItem::setEnabled(bool enabled)
{
_enabled = enabled;
}
2013-07-04 12:50:17 +08:00
bool MenuItem::isEnabled() const
{
return _enabled;
}
Rect MenuItem::rect() const
{
2021-12-25 10:04:45 +08:00
return Rect(_position.x - _contentSize.width * _anchorPoint.x, _position.y - _contentSize.height * _anchorPoint.y,
_contentSize.width, _contentSize.height);
}
2013-07-04 12:50:17 +08:00
bool MenuItem::isSelected() const
{
return _selected;
}
void MenuItem::setCallback(const ccMenuCallback& callback)
{
_callback = callback;
}
std::string MenuItem::getDescription() const
{
return StringUtils::format("<MenuItem | tag = %d>", _tag);
}
//
2021-12-25 10:04:45 +08:00
// CCMenuItemLabel
//
2012-07-19 05:40:09 +08:00
void MenuItemLabel::setLabel(Node* var)
{
if (var)
{
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
var->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
2021-12-25 10:04:45 +08:00
setContentSize(var->getContentSize());
addChild(var);
}
2021-12-25 10:04:45 +08:00
if (_label)
{
removeChild(_label, true);
}
2021-12-25 10:04:45 +08:00
_label = var;
}
2021-12-25 10:04:45 +08:00
MenuItemLabel* MenuItemLabel::create(Node* label, const ccMenuCallback& callback)
{
2021-12-25 10:04:45 +08:00
MenuItemLabel* ret = new MenuItemLabel();
ret->initWithLabel(label, callback);
ret->autorelease();
return ret;
}
2021-12-25 10:04:45 +08:00
MenuItemLabel* MenuItemLabel::create(Node* label)
{
2021-12-25 10:04:45 +08:00
MenuItemLabel* ret = new MenuItemLabel();
ret->initWithLabel(label, (const ccMenuCallback&)nullptr);
ret->autorelease();
return ret;
}
bool MenuItemLabel::initWithLabel(Node* label, const ccMenuCallback& callback)
{
MenuItem::initWithCallback(callback);
_originalScale = 1.0f;
2021-12-25 10:04:45 +08:00
_colorBackup = Color3B::WHITE;
setDisabledColor(Color3B(126, 126, 126));
this->setLabel(label);
2013-02-27 17:23:39 +08:00
setCascadeColorEnabled(true);
setCascadeOpacityEnabled(true);
return true;
}
2021-12-25 10:04:45 +08:00
MenuItemLabel::~MenuItemLabel() {}
2021-12-26 23:26:34 +08:00
void MenuItemLabel::setString(std::string_view label)
{
dynamic_cast<LabelProtocol*>(_label)->setString(label);
this->setContentSize(_label->getContentSize());
}
2021-12-26 23:26:34 +08:00
std::string_view MenuItemLabel::getString() const
{
auto label = dynamic_cast<LabelProtocol*>(_label);
return label->getString();
}
void MenuItemLabel::activate()
{
2021-12-25 10:04:45 +08:00
if (_enabled)
{
this->stopAllActions();
2021-12-25 10:04:45 +08:00
this->setScale(_originalScale);
MenuItem::activate();
2012-02-02 10:39:36 +08:00
}
}
void MenuItemLabel::selected()
{
// subclass to change the default action
2021-12-25 10:04:45 +08:00
if (_enabled)
2012-02-02 10:39:36 +08:00
{
MenuItem::selected();
2021-12-25 10:04:45 +08:00
Action* action = getActionByTag(kZoomActionTag);
if (action)
2012-02-01 16:45:23 +08:00
{
this->stopAction(action);
2012-02-01 16:45:23 +08:00
}
else
2012-02-02 10:39:36 +08:00
{
_originalScale = this->getScale();
2012-02-02 10:39:36 +08:00
}
2021-12-25 10:04:45 +08:00
Action* zoomAction = ScaleTo::create(0.1f, _originalScale * 1.2f);
zoomAction->setTag(kZoomActionTag);
this->runAction(zoomAction);
2012-02-02 10:39:36 +08:00
}
}
void MenuItemLabel::unselected()
{
// subclass to change the default action
2021-12-25 10:04:45 +08:00
if (_enabled)
2012-02-02 10:39:36 +08:00
{
MenuItem::unselected();
this->stopActionByTag(kZoomActionTag);
2021-12-25 10:04:45 +08:00
Action* zoomAction = ScaleTo::create(0.1f, _originalScale);
zoomAction->setTag(kZoomActionTag);
this->runAction(zoomAction);
2012-02-02 10:39:36 +08:00
}
}
void MenuItemLabel::setEnabled(bool enabled)
{
2021-12-25 10:04:45 +08:00
if (_enabled != enabled)
2012-02-02 10:39:36 +08:00
{
2021-12-25 10:04:45 +08:00
if (enabled == false)
2012-02-02 10:39:36 +08:00
{
_colorBackup = this->getColor();
this->setColor(_disabledColor);
2012-02-02 10:39:36 +08:00
}
else
2012-02-02 10:39:36 +08:00
{
this->setColor(_colorBackup);
}
}
MenuItem::setEnabled(enabled);
}
//
2021-12-25 10:04:45 +08:00
// CCMenuItemAtlasFont
//
2012-07-19 05:40:09 +08:00
2021-12-26 23:26:34 +08:00
MenuItemAtlasFont* MenuItemAtlasFont::create(std::string_view value,
std::string_view charMapFile,
2021-12-25 10:04:45 +08:00
int itemWidth,
int itemHeight,
char startCharMap)
{
2021-12-25 10:04:45 +08:00
return MenuItemAtlasFont::create(value, charMapFile, itemWidth, itemHeight, startCharMap,
(const ccMenuCallback&)nullptr);
}
2021-12-26 23:26:34 +08:00
MenuItemAtlasFont* MenuItemAtlasFont::create(std::string_view value,
std::string_view charMapFile,
2021-12-25 10:04:45 +08:00
int itemWidth,
int itemHeight,
char startCharMap,
const ccMenuCallback& callback)
{
2021-12-25 10:04:45 +08:00
MenuItemAtlasFont* ret = new MenuItemAtlasFont();
ret->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, callback);
ret->autorelease();
return ret;
}
2021-12-26 23:26:34 +08:00
bool MenuItemAtlasFont::initWithString(std::string_view value,
std::string_view charMapFile,
2021-12-25 10:04:45 +08:00
int itemWidth,
int itemHeight,
char startCharMap,
const ccMenuCallback& callback)
{
2022-07-16 10:43:05 +08:00
AXASSERT(value.size() != 0, "value length must be greater than 0");
2021-12-25 10:04:45 +08:00
LabelAtlas* label = LabelAtlas::create(value, charMapFile, itemWidth, itemHeight, startCharMap);
if (MenuItemLabel::initWithLabel(label, callback))
{
// do something ?
}
return true;
}
//
2021-12-25 10:04:45 +08:00
// CCMenuItemFont
//
2012-07-19 05:40:09 +08:00
2013-12-05 17:19:01 +08:00
void MenuItemFont::setFontSize(int s)
{
2013-02-27 17:23:39 +08:00
_globalFontSize = s;
}
2013-12-05 17:19:01 +08:00
int MenuItemFont::getFontSize()
{
2013-02-27 17:23:39 +08:00
return _globalFontSize;
}
2021-12-26 23:26:34 +08:00
void MenuItemFont::setFontName(std::string_view name)
{
2013-02-27 17:23:39 +08:00
if (_globalFontNameRelease)
{
2013-02-27 17:23:39 +08:00
_globalFontName.clear();
}
2021-12-25 10:04:45 +08:00
_globalFontName = name;
2013-02-27 17:23:39 +08:00
_globalFontNameRelease = true;
}
2021-12-26 23:26:34 +08:00
std::string_view MenuItemFont::getFontName()
{
return _globalFontName;
}
2021-12-26 23:26:34 +08:00
MenuItemFont* MenuItemFont::create(std::string_view value, const ccMenuCallback& callback)
{
2021-12-25 10:04:45 +08:00
MenuItemFont* ret = new MenuItemFont();
ret->initWithString(value, callback);
ret->autorelease();
return ret;
}
2021-12-26 23:26:34 +08:00
MenuItemFont* MenuItemFont::create(std::string_view value)
{
2021-12-25 10:04:45 +08:00
MenuItemFont* ret = new MenuItemFont();
ret->initWithString(value, (const ccMenuCallback&)nullptr);
ret->autorelease();
return ret;
}
2021-12-25 10:04:45 +08:00
MenuItemFont::MenuItemFont() : _fontSize(0), _fontName("") {}
2013-10-24 17:27:57 +08:00
MenuItemFont::~MenuItemFont()
{
2022-07-16 10:43:05 +08:00
AXLOGINFO("In the destructor of MenuItemFont (%p).", this);
2013-10-24 17:27:57 +08:00
}
2021-12-26 23:26:34 +08:00
bool MenuItemFont::initWithString(std::string_view value, const ccMenuCallback& callback)
{
2022-07-16 10:43:05 +08:00
AXASSERT(!value.empty(), "Value length must be greater than 0");
_fontName = _globalFontName;
_fontSize = _globalFontSize;
2021-12-25 10:04:45 +08:00
Label* label = Label::createWithSystemFont(value, _fontName, _fontSize);
if (MenuItemLabel::initWithLabel(label, callback))
{
// do something ?
}
return true;
}
2013-12-05 17:19:01 +08:00
void MenuItemFont::setFontSizeObj(int s)
{
_fontSize = s;
dynamic_cast<Label*>(_label)->setSystemFontSize(_fontSize);
this->setContentSize(dynamic_cast<Label*>(_label)->getContentSize());
}
2013-12-05 17:19:01 +08:00
int MenuItemFont::getFontSizeObj() const
{
return _fontSize;
}
2021-12-26 23:26:34 +08:00
void MenuItemFont::setFontNameObj(std::string_view name)
{
_fontName = name;
dynamic_cast<Label*>(_label)->setSystemFontName(_fontName);
this->setContentSize(dynamic_cast<Label*>(_label)->getContentSize());
}
2021-12-26 23:26:34 +08:00
std::string_view MenuItemFont::getFontNameObj() const
{
return _fontName;
}
//
2021-12-25 10:04:45 +08:00
// CCMenuItemSprite
//
2012-07-19 05:40:09 +08:00
void MenuItemSprite::setNormalImage(Node* image)
{
if (image != _normalImage)
2012-02-02 10:39:36 +08:00
{
if (image)
{
addChild(image);
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
image->setAnchorPoint(Vec2(0, 0));
}
if (_normalImage)
{
removeChild(_normalImage, true);
}
_normalImage = image;
if (_normalImage != nullptr)
{
this->setContentSize(_normalImage->getContentSize());
}
this->updateImagesVisibility();
}
}
void MenuItemSprite::setSelectedImage(Node* image)
{
if (image != _normalImage)
{
if (image)
{
addChild(image);
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
image->setAnchorPoint(Vec2(0, 0));
}
if (_selectedImage)
{
removeChild(_selectedImage, true);
}
_selectedImage = image;
this->updateImagesVisibility();
}
}
void MenuItemSprite::setDisabledImage(Node* image)
{
if (image != _normalImage)
{
if (image)
{
addChild(image);
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
image->setAnchorPoint(Vec2(0, 0));
}
if (_disabledImage)
{
removeChild(_disabledImage, true);
}
_disabledImage = image;
this->updateImagesVisibility();
}
}
//
2021-12-25 10:04:45 +08:00
// CCMenuItemSprite
//
2012-07-19 05:40:09 +08:00
2021-12-25 10:04:45 +08:00
MenuItemSprite* MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite)
{
return MenuItemSprite::create(normalSprite, selectedSprite, disabledSprite, (const ccMenuCallback&)nullptr);
}
2021-12-25 10:04:45 +08:00
MenuItemSprite* MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, const ccMenuCallback& callback)
{
return MenuItemSprite::create(normalSprite, selectedSprite, nullptr, callback);
}
2021-12-25 10:04:45 +08:00
MenuItemSprite* MenuItemSprite::create(Node* normalSprite,
Node* selectedSprite,
Node* disabledSprite,
const ccMenuCallback& callback)
{
2021-12-25 10:04:45 +08:00
MenuItemSprite* ret = new MenuItemSprite();
ret->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, callback);
ret->autorelease();
return ret;
}
2021-12-25 10:04:45 +08:00
bool MenuItemSprite::initWithNormalSprite(Node* normalSprite,
Node* selectedSprite,
Node* disabledSprite,
const ccMenuCallback& callback)
{
MenuItem::initWithCallback(callback);
setNormalImage(normalSprite);
setSelectedImage(selectedSprite);
setDisabledImage(disabledSprite);
2021-12-25 10:04:45 +08:00
if (_normalImage)
2012-06-12 02:50:32 +08:00
{
this->setContentSize(_normalImage->getContentSize());
2012-06-12 02:50:32 +08:00
}
2013-02-27 17:23:39 +08:00
setCascadeColorEnabled(true);
setCascadeOpacityEnabled(true);
return true;
}
/**
@since v0.99.5
*/
void MenuItemSprite::selected()
{
MenuItem::selected();
2012-06-12 02:50:32 +08:00
if (_normalImage)
2012-02-02 10:39:36 +08:00
{
if (_disabledImage)
{
_disabledImage->setVisible(false);
}
if (_selectedImage)
{
_normalImage->setVisible(false);
_selectedImage->setVisible(true);
}
else
{
_normalImage->setVisible(true);
}
2012-02-02 10:39:36 +08:00
}
}
void MenuItemSprite::unselected()
{
MenuItem::unselected();
this->updateImagesVisibility();
}
void MenuItemSprite::setEnabled(bool bEnabled)
{
2021-12-25 10:04:45 +08:00
if (_enabled != bEnabled)
{
MenuItem::setEnabled(bEnabled);
this->updateImagesVisibility();
2012-02-01 16:45:23 +08:00
}
}
2021-12-25 10:04:45 +08:00
// Helper
void MenuItemSprite::updateImagesVisibility()
{
if (_enabled)
2012-02-01 16:45:23 +08:00
{
2021-12-25 10:04:45 +08:00
if (_normalImage)
_normalImage->setVisible(true);
if (_selectedImage)
_selectedImage->setVisible(false);
if (_disabledImage)
_disabledImage->setVisible(false);
2012-02-01 16:45:23 +08:00
}
else
2012-02-01 16:45:23 +08:00
{
if (_disabledImage)
2012-02-01 16:45:23 +08:00
{
2021-12-25 10:04:45 +08:00
if (_normalImage)
_normalImage->setVisible(false);
if (_selectedImage)
_selectedImage->setVisible(false);
if (_disabledImage)
_disabledImage->setVisible(true);
2012-02-01 16:45:23 +08:00
}
else
{
2021-12-25 10:04:45 +08:00
if (_normalImage)
_normalImage->setVisible(true);
if (_selectedImage)
_selectedImage->setVisible(false);
if (_disabledImage)
_disabledImage->setVisible(false);
2012-02-02 10:39:36 +08:00
}
}
}
2012-07-19 05:40:09 +08:00
///
/// MenuItemImage
2012-07-19 05:40:09 +08:00
///
MenuItemImage* MenuItemImage::create()
{
2021-12-25 10:04:45 +08:00
MenuItemImage* ret = new MenuItemImage();
2021-12-08 00:11:53 +08:00
if (ret->init())
{
ret->autorelease();
return ret;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
return nullptr;
}
bool MenuItemImage::init()
{
return initWithNormalImage("", "", "", (const ccMenuCallback&)nullptr);
}
2021-12-26 23:26:34 +08:00
MenuItemImage* MenuItemImage::create(std::string_view normalImage, std::string_view selectedImage)
{
return MenuItemImage::create(normalImage, selectedImage, "", (const ccMenuCallback&)nullptr);
}
2021-12-26 23:26:34 +08:00
MenuItemImage* MenuItemImage::create(std::string_view normalImage,
std::string_view selectedImage,
2021-12-25 10:04:45 +08:00
const ccMenuCallback& callback)
{
return MenuItemImage::create(normalImage, selectedImage, "", callback);
}
2021-12-26 23:26:34 +08:00
MenuItemImage* MenuItemImage::create(std::string_view normalImage,
std::string_view selectedImage,
std::string_view disabledImage,
2021-12-25 10:04:45 +08:00
const ccMenuCallback& callback)
{
2021-12-25 10:04:45 +08:00
MenuItemImage* ret = new MenuItemImage();
2021-12-08 00:11:53 +08:00
if (ret->initWithNormalImage(normalImage, selectedImage, disabledImage, callback))
{
ret->autorelease();
return ret;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
return nullptr;
}
2021-12-26 23:26:34 +08:00
MenuItemImage* MenuItemImage::create(std::string_view normalImage,
std::string_view selectedImage,
std::string_view disabledImage)
{
2021-12-25 10:04:45 +08:00
MenuItemImage* ret = new MenuItemImage();
2021-12-08 00:11:53 +08:00
if (ret->initWithNormalImage(normalImage, selectedImage, disabledImage, (const ccMenuCallback&)nullptr))
2012-02-02 10:39:36 +08:00
{
ret->autorelease();
return ret;
2012-02-02 10:39:36 +08:00
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
return nullptr;
}
2021-12-26 23:26:34 +08:00
bool MenuItemImage::initWithNormalImage(std::string_view normalImage,
std::string_view selectedImage,
std::string_view disabledImage,
2021-12-25 10:04:45 +08:00
const ccMenuCallback& callback)
{
2021-12-25 10:04:45 +08:00
Node* normalSprite = nullptr;
Node* selectedSprite = nullptr;
Node* disabledSprite = nullptr;
2021-12-25 10:04:45 +08:00
if (normalImage.size() > 0)
{
normalSprite = Sprite::create(normalImage);
}
2021-12-25 10:04:45 +08:00
if (selectedImage.size() > 0)
2012-02-02 10:39:36 +08:00
{
selectedSprite = Sprite::create(selectedImage);
2012-02-02 10:39:36 +08:00
}
2021-12-25 10:04:45 +08:00
if (disabledImage.size() > 0)
2012-02-02 10:39:36 +08:00
{
disabledSprite = Sprite::create(disabledImage);
2012-02-02 10:39:36 +08:00
}
return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, callback);
}
//
// Setter of sprite frames
//
2021-12-25 10:04:45 +08:00
void MenuItemImage::setNormalSpriteFrame(SpriteFrame* frame)
{
setNormalImage(Sprite::createWithSpriteFrame(frame));
}
2021-12-25 10:04:45 +08:00
void MenuItemImage::setSelectedSpriteFrame(SpriteFrame* frame)
{
setSelectedImage(Sprite::createWithSpriteFrame(frame));
}
2021-12-25 10:04:45 +08:00
void MenuItemImage::setDisabledSpriteFrame(SpriteFrame* frame)
{
setDisabledImage(Sprite::createWithSpriteFrame(frame));
}
2012-07-19 05:40:09 +08:00
//
// MenuItemToggle
//
2012-07-19 05:40:09 +08:00
2021-12-25 10:04:45 +08:00
MenuItemToggle* MenuItemToggle::createWithCallback(const ccMenuCallback& callback, const Vector<MenuItem*>& menuItems)
{
2021-12-25 10:04:45 +08:00
MenuItemToggle* ret = new MenuItemToggle();
ret->MenuItem::initWithCallback(callback);
ret->autorelease();
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
2015-12-17 21:55:47 +08:00
{
2021-12-25 10:04:45 +08:00
for (const auto& item : menuItems)
{
if (item)
{
sEngine->retainScriptObject(ret, item);
}
}
2015-12-17 21:55:47 +08:00
}
2022-07-15 19:17:01 +08:00
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
2021-12-25 10:04:45 +08:00
ret->_subItems = menuItems;
ret->_selectedIndex = UINT_MAX;
ret->setSelectedIndex(0);
return ret;
}
2021-12-25 10:04:45 +08:00
MenuItemToggle* MenuItemToggle::createWithCallback(const ccMenuCallback& callback, MenuItem* item, ...)
{
va_list args;
va_start(args, item);
2021-12-25 10:04:45 +08:00
MenuItemToggle* ret = new MenuItemToggle();
ret->initWithCallback(callback, item, args);
ret->autorelease();
va_end(args);
return ret;
}
2021-12-25 10:04:45 +08:00
MenuItemToggle* MenuItemToggle::create()
{
2021-12-25 10:04:45 +08:00
MenuItemToggle* ret = new MenuItemToggle();
ret->initWithItem(nullptr);
ret->autorelease();
return ret;
}
2021-12-25 10:04:45 +08:00
bool MenuItemToggle::initWithCallback(const ccMenuCallback& callback, MenuItem* item, va_list args)
{
MenuItem::initWithCallback(callback);
2021-12-25 10:04:45 +08:00
int z = 0;
MenuItem* i = item;
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
2022-07-15 19:17:01 +08:00
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
2021-12-25 10:04:45 +08:00
while (i)
{
z++;
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
if (sEngine)
{
sEngine->retainScriptObject(this, i);
}
2022-07-15 19:17:01 +08:00
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
_subItems.pushBack(i);
i = va_arg(args, MenuItem*);
}
_selectedIndex = UINT_MAX;
this->setSelectedIndex(0);
return true;
}
2021-12-25 10:04:45 +08:00
MenuItemToggle* MenuItemToggle::create(MenuItem* item)
{
2021-12-25 10:04:45 +08:00
MenuItemToggle* ret = new MenuItemToggle();
ret->initWithItem(item);
ret->autorelease();
return ret;
}
2021-12-25 10:04:45 +08:00
bool MenuItemToggle::initWithItem(MenuItem* item)
{
MenuItem::initWithCallback((const ccMenuCallback&)nullptr);
2013-02-27 17:23:39 +08:00
if (item)
{
2015-12-17 21:55:47 +08:00
addSubItem(item);
}
_selectedIndex = UINT_MAX;
this->setSelectedIndex(0);
2021-12-25 10:04:45 +08:00
2013-02-27 17:23:39 +08:00
setCascadeColorEnabled(true);
setCascadeOpacityEnabled(true);
2021-12-25 10:04:45 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
void MenuItemToggle::addSubItem(MenuItem* item)
{
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_GC_FOR_NATIVE_OBJECTS
auto sEngine = ScriptEngineManager::getInstance()->getScriptEngine();
if (sEngine)
{
sEngine->retainScriptObject(this, item);
}
2022-07-15 19:17:01 +08:00
#endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS
_subItems.pushBack(item);
}
2015-12-17 21:55:47 +08:00
void MenuItemToggle::cleanup()
{
2021-12-25 10:04:45 +08:00
for (const auto& item : _subItems)
{
2022-07-15 19:17:01 +08:00
#if defined(AX_NATIVE_CONTROL_SCRIPT) && !AX_NATIVE_CONTROL_SCRIPT
2015-12-17 21:55:47 +08:00
ScriptEngineManager::getInstance()->getScriptEngine()->releaseScriptObject(this, item);
#endif
item->cleanup();
}
2015-12-17 21:55:47 +08:00
MenuItem::cleanup();
}
void MenuItemToggle::setSelectedIndex(unsigned int index)
{
2021-12-25 10:04:45 +08:00
if (index != _selectedIndex && !_subItems.empty())
{
_selectedIndex = index;
if (_selectedItem)
{
_selectedItem->removeFromParentAndCleanup(false);
}
_selectedItem = _subItems.at(_selectedIndex);
this->addChild(_selectedItem);
2021-10-23 23:27:14 +08:00
Vec2 s = _selectedItem->getContentSize();
this->setContentSize(s);
_selectedItem->setPosition(s.width / 2, s.height / 2);
}
}
void MenuItemToggle::selected()
{
MenuItem::selected();
_subItems.at(_selectedIndex)->selected();
}
void MenuItemToggle::unselected()
{
MenuItem::unselected();
_subItems.at(_selectedIndex)->unselected();
}
void MenuItemToggle::activate()
{
// update index
2021-12-25 10:04:45 +08:00
if (_enabled)
{
unsigned int newIndex = (_selectedIndex + 1) % _subItems.size();
this->setSelectedIndex(newIndex);
}
MenuItem::activate();
}
void MenuItemToggle::setEnabled(bool enabled)
{
if (_enabled != enabled)
{
MenuItem::setEnabled(enabled);
2021-12-25 10:04:45 +08:00
for (const auto& item : _subItems)
{
item->setEnabled(enabled);
}
}
}
MenuItem* MenuItemToggle::getSelectedItem()
{
return _subItems.at(_selectedIndex);
}
NS_AX_END