axmol/cocos2dx/menu_nodes/CCMenuItem.cpp

1032 lines
26 KiB
C++
Raw Normal View History

2012-02-01 16:45:23 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
2012-02-01 16:45:23 +08:00
Copyright (c) 2008-2011 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
http://www.cocos2d-x.org
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-02-01 16:45:23 +08:00
#include "CCMenuItem.h"
#include "actions/CCActionInterval.h"
#include "sprite_nodes/CCSprite.h"
#include "label_nodes/CCLabelAtlas.h"
#include "label_nodes/CCLabelTTF.h"
#include "script_support/CCScriptSupport.h"
2012-02-01 16:45:23 +08:00
#include <stdarg.h>
#include <cstring>
2012-02-01 16:45:23 +08:00
NS_CC_BEGIN
2012-02-02 10:39:36 +08:00
static unsigned int _globalFontSize = kItemSize;
2013-02-27 17:23:39 +08:00
static std::string _globalFontName = "Marker Felt";
static bool _globalFontNameRelease = false;
const unsigned int kCurrentItem = 0xc0c05001;
const unsigned int kZoomActionTag = 0xc0c05002;
const unsigned int kNormalTag = 0x1;
const unsigned int kSelectedTag = 0x2;
const unsigned int kDisableTag = 0x3;
//
// 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
}
// XXX deprecated
MenuItem* MenuItem::create(Object *target, SEL_MenuHandler selector)
{
MenuItem *pRet = new MenuItem();
pRet->initWithTarget(target, selector);
pRet->autorelease();
return pRet;
}
MenuItem* MenuItem::create( const ccMenuCallback& callback)
{
MenuItem *pRet = new MenuItem();
pRet->initWithCallback(callback);
pRet->autorelease();
return pRet;
}
// XXX deprecated
bool MenuItem::initWithTarget(cocos2d::Object *target, SEL_MenuHandler selector )
{
_target = target;
CC_SAFE_RETAIN(_target);
return initWithCallback( std::bind(selector,target, std::placeholders::_1) );
}
bool MenuItem::initWithCallback(const ccMenuCallback& callback)
{
2013-07-12 14:11:55 +08:00
setAnchorPoint(Point(0.5f, 0.5f));
_callback = callback;
_enabled = true;
_selected = false;
return true;
}
MenuItem::~MenuItem()
{
CC_SAFE_RELEASE(_target);
}
void MenuItem::selected()
{
_selected = true;
}
void MenuItem::unselected()
{
_selected = false;
}
void MenuItem::activate()
{
if (_enabled)
2012-02-02 10:39:36 +08:00
{
if( _callback )
2012-02-01 16:45:23 +08:00
{
_callback(this);
2012-02-01 16:45:23 +08:00
}
if (kScriptTypeLua == _scriptType)
{
BasicScriptData data((void*)this);
ScriptEvent scriptEvent(kMenuClickedEvent,&data);
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent);
}
else if (kScriptTypeJavascript == _scriptType)
{
ScriptEngineManager::sharedManager()->getScriptEngine()->executeMenuItemEvent(this);
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()
{
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;
}
// XXX deprecated
void MenuItem::setTarget(Object *target, SEL_MenuHandler selector)
{
_target = target;
CC_SAFE_RETAIN(_target);
setCallback( std::bind( selector, target, std::placeholders::_1) );
}
void MenuItem::setCallback(const ccMenuCallback& callback)
{
_callback = callback;
}
//
//CCMenuItemLabel
//
2012-07-19 05:40:09 +08:00
const Color3B& MenuItemLabel::getDisabledColor() const
{
return _disabledColor;
}
void MenuItemLabel::setDisabledColor(const Color3B& var)
{
_disabledColor = var;
}
Node *MenuItemLabel::getLabel()
{
return _label;
}
void MenuItemLabel::setLabel(Node* var)
{
if (var)
{
addChild(var);
2013-07-12 14:11:55 +08:00
var->setAnchorPoint(Point(0, 0));
setContentSize(var->getContentSize());
}
if (_label)
{
removeChild(_label, true);
}
_label = var;
}
// XXX: deprecated
MenuItemLabel * MenuItemLabel::create(Node*label, Object* target, SEL_MenuHandler selector)
{
MenuItemLabel *pRet = new MenuItemLabel();
pRet->initWithLabel(label, target, selector);
pRet->autorelease();
return pRet;
}
MenuItemLabel * MenuItemLabel::create(Node*label, const ccMenuCallback& callback)
{
MenuItemLabel *pRet = new MenuItemLabel();
pRet->initWithLabel(label, callback);
pRet->autorelease();
return pRet;
}
MenuItemLabel* MenuItemLabel::create(Node *label)
{
MenuItemLabel *pRet = new MenuItemLabel();
pRet->initWithLabel(label, (const ccMenuCallback&) nullptr);
pRet->autorelease();
return pRet;
}
// XXX: deprecated
bool MenuItemLabel::initWithLabel(Node* label, Object* target, SEL_MenuHandler selector)
{
_target = target;
CC_SAFE_RETAIN(_target);
return initWithLabel(label, std::bind(selector,target, std::placeholders::_1) );
}
bool MenuItemLabel::initWithLabel(Node* label, const ccMenuCallback& callback)
{
MenuItem::initWithCallback(callback);
_originalScale = 1.0f;
_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;
}
MenuItemLabel::~MenuItemLabel()
{
}
void MenuItemLabel::setString(const char * label)
{
dynamic_cast<LabelProtocol*>(_label)->setString(label);
this->setContentSize(_label->getContentSize());
}
void MenuItemLabel::activate()
{
if(_enabled)
{
this->stopAllActions();
this->setScale( _originalScale );
MenuItem::activate();
2012-02-02 10:39:36 +08:00
}
}
void MenuItemLabel::selected()
{
// subclass to change the default action
if(_enabled)
2012-02-02 10:39:36 +08:00
{
MenuItem::selected();
2012-02-02 10:39:36 +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
}
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
if(_enabled)
2012-02-02 10:39:36 +08:00
{
MenuItem::unselected();
this->stopActionByTag(kZoomActionTag);
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)
{
if( _enabled != enabled )
2012-02-02 10:39:36 +08:00
{
if(enabled == false)
2012-02-02 10:39:36 +08:00
{
_colorBackup = dynamic_cast<RGBAProtocol*>(_label)->getColor();
dynamic_cast<RGBAProtocol*>(_label)->setColor(_disabledColor);
2012-02-02 10:39:36 +08:00
}
else
2012-02-02 10:39:36 +08:00
{
dynamic_cast<RGBAProtocol*>(_label)->setColor(_colorBackup);
}
}
MenuItem::setEnabled(enabled);
}
//
//CCMenuItemAtlasFont
//
2012-07-19 05:40:09 +08:00
MenuItemAtlasFont * MenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap)
{
return MenuItemAtlasFont::create(value, charMapFile, itemWidth, itemHeight, startCharMap, (const ccMenuCallback&)nullptr);
}
// XXX: deprecated
MenuItemAtlasFont * MenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, Object* target, SEL_MenuHandler selector)
{
MenuItemAtlasFont *pRet = new MenuItemAtlasFont();
pRet->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, target, selector);
pRet->autorelease();
return pRet;
}
MenuItemAtlasFont * MenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback)
{
MenuItemAtlasFont *pRet = new MenuItemAtlasFont();
pRet->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, callback);
pRet->autorelease();
return pRet;
}
// XXX: deprecated
bool MenuItemAtlasFont::initWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, Object* target, SEL_MenuHandler selector)
{
CCAssert( value != NULL && strlen(value) != 0, "value length must be greater than 0");
_target = target;
CC_SAFE_RETAIN(_target);
return initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, std::bind(selector,target, std::placeholders::_1) );
}
bool MenuItemAtlasFont::initWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback)
{
CCAssert( value != NULL && strlen(value) != 0, "value length must be greater than 0");
LabelAtlas *label = new LabelAtlas();
label->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap);
label->autorelease();
if (MenuItemLabel::initWithLabel(label, callback))
{
// do something ?
}
return true;
}
//
//CCMenuItemFont
//
2012-07-19 05:40:09 +08:00
void MenuItemFont::setFontSize(unsigned int s)
{
2013-02-27 17:23:39 +08:00
_globalFontSize = s;
}
unsigned int MenuItemFont::fontSize()
{
2013-02-27 17:23:39 +08:00
return _globalFontSize;
}
void MenuItemFont::setFontName(const char *name)
{
2013-02-27 17:23:39 +08:00
if (_globalFontNameRelease)
{
2013-02-27 17:23:39 +08:00
_globalFontName.clear();
}
2013-02-27 17:23:39 +08:00
_globalFontName = name;
_globalFontNameRelease = true;
}
const char * MenuItemFont::fontName()
{
2013-02-27 17:23:39 +08:00
return _globalFontName.c_str();
}
// XXX: deprecated
MenuItemFont * MenuItemFont::create(const char *value, Object* target, SEL_MenuHandler selector)
{
MenuItemFont *pRet = new MenuItemFont();
pRet->initWithString(value, target, selector);
pRet->autorelease();
return pRet;
}
MenuItemFont * MenuItemFont::create(const char *value, const ccMenuCallback& callback)
{
MenuItemFont *pRet = new MenuItemFont();
pRet->initWithString(value, callback);
pRet->autorelease();
return pRet;
}
MenuItemFont * MenuItemFont::create(const char *value)
{
MenuItemFont *pRet = new MenuItemFont();
pRet->initWithString(value, (const ccMenuCallback&)nullptr);
pRet->autorelease();
return pRet;
}
// XXX: deprecated
bool MenuItemFont::initWithString(const char *value, Object* target, SEL_MenuHandler selector)
{
CCAssert( value != NULL && strlen(value) != 0, "Value length must be greater than 0");
_target = target;
CC_SAFE_RETAIN(target);
return initWithString(value, std::bind(selector,target, std::placeholders::_1) );
}
bool MenuItemFont::initWithString(const char *value, const ccMenuCallback& callback)
{
CCAssert( value != NULL && strlen(value) != 0, "Value length must be greater than 0");
_fontName = _globalFontName;
_fontSize = _globalFontSize;
LabelTTF *label = LabelTTF::create(value, _fontName.c_str(), (float)_fontSize);
if (MenuItemLabel::initWithLabel(label, callback))
{
// do something ?
}
return true;
}
void MenuItemFont::recreateLabel()
{
LabelTTF *label = LabelTTF::create(dynamic_cast<LabelProtocol*>(_label)->getString(),
_fontName.c_str(), (float)_fontSize);
this->setLabel(label);
}
void MenuItemFont::setFontSizeObj(unsigned int s)
{
_fontSize = s;
recreateLabel();
}
unsigned int MenuItemFont::fontSizeObj()
{
return _fontSize;
}
void MenuItemFont::setFontNameObj(const char* name)
{
_fontName = name;
recreateLabel();
}
const char* MenuItemFont::fontNameObj()
{
return _fontName.c_str();
}
//
//CCMenuItemSprite
//
2012-07-19 05:40:09 +08:00
Node * MenuItemSprite::getNormalImage()
{
return _normalImage;
}
void MenuItemSprite::setNormalImage(Node* pImage)
{
if (pImage != _normalImage)
2012-02-02 10:39:36 +08:00
{
if (pImage)
{
addChild(pImage, 0, kNormalTag);
2013-07-12 14:11:55 +08:00
pImage->setAnchorPoint(Point(0, 0));
}
if (_normalImage)
{
removeChild(_normalImage, true);
}
_normalImage = pImage;
this->setContentSize(_normalImage->getContentSize());
this->updateImagesVisibility();
}
}
Node * MenuItemSprite::getSelectedImage()
{
return _selectedImage;
}
void MenuItemSprite::setSelectedImage(Node* pImage)
{
if (pImage != _normalImage)
{
if (pImage)
{
addChild(pImage, 0, kSelectedTag);
2013-07-12 14:11:55 +08:00
pImage->setAnchorPoint(Point(0, 0));
}
if (_selectedImage)
{
removeChild(_selectedImage, true);
}
_selectedImage = pImage;
this->updateImagesVisibility();
}
}
Node * MenuItemSprite::getDisabledImage()
{
return _disabledImage;
}
void MenuItemSprite::setDisabledImage(Node* pImage)
{
if (pImage != _normalImage)
{
if (pImage)
{
addChild(pImage, 0, kDisableTag);
2013-07-12 14:11:55 +08:00
pImage->setAnchorPoint(Point(0, 0));
}
if (_disabledImage)
{
removeChild(_disabledImage, true);
}
_disabledImage = pImage;
this->updateImagesVisibility();
}
}
//
2013-02-27 17:23:39 +08:00
//CCMenuItemSprite
//
2012-07-19 05:40:09 +08:00
MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite)
{
return MenuItemSprite::create(normalSprite, selectedSprite, disabledSprite, (const ccMenuCallback&)nullptr);
}
// XXX deprecated
MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, Object* target, SEL_MenuHandler selector)
{
return MenuItemSprite::create(normalSprite, selectedSprite, NULL, target, selector);
}
MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, const ccMenuCallback& callback)
{
return MenuItemSprite::create(normalSprite, selectedSprite, NULL, callback);
}
// XXX deprecated
MenuItemSprite * MenuItemSprite::create(Node *normalSprite, Node *selectedSprite, Node *disabledSprite, Object *target, SEL_MenuHandler selector)
{
MenuItemSprite *pRet = new MenuItemSprite();
pRet->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
pRet->autorelease();
return pRet;
}
MenuItemSprite * MenuItemSprite::create(Node *normalSprite, Node *selectedSprite, Node *disabledSprite, const ccMenuCallback& callback)
{
MenuItemSprite *pRet = new MenuItemSprite();
pRet->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, callback);
pRet->autorelease();
return pRet;
}
// XXX deprecated
bool MenuItemSprite::initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Object* target, SEL_MenuHandler selector)
{
_target = target;
CC_SAFE_RETAIN(_target);
return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, std::bind(selector,target, std::placeholders::_1) );
}
bool MenuItemSprite::initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, const ccMenuCallback& callback)
{
MenuItem::initWithCallback(callback);
setNormalImage(normalSprite);
setSelectedImage(selectedSprite);
setDisabledImage(disabledSprite);
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();
if (_normalImage)
2012-02-02 10:39:36 +08:00
{
_normalImage->setVisible(true);
if (_selectedImage)
{
_selectedImage->setVisible(false);
}
if (_disabledImage)
{
_disabledImage->setVisible(false);
}
2012-02-02 10:39:36 +08:00
}
}
void MenuItemSprite::setEnabled(bool bEnabled)
{
if( _enabled != bEnabled )
{
MenuItem::setEnabled(bEnabled);
this->updateImagesVisibility();
2012-02-01 16:45:23 +08:00
}
}
// Helper
void MenuItemSprite::updateImagesVisibility()
{
if (_enabled)
2012-02-01 16:45:23 +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
{
if (_normalImage) _normalImage->setVisible(false);
if (_selectedImage) _selectedImage->setVisible(false);
if (_disabledImage) _disabledImage->setVisible(true);
2012-02-01 16:45:23 +08:00
}
else
{
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()
{
MenuItemImage *pRet = new MenuItemImage();
if (pRet && pRet->init())
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool MenuItemImage::init(void)
{
return initWithNormalImage(NULL, NULL, NULL, (const ccMenuCallback&)nullptr);
}
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage)
{
return MenuItemImage::create(normalImage, selectedImage, NULL, (const ccMenuCallback&)nullptr);
}
// XXX deprecated
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, Object* target, SEL_MenuHandler selector)
{
return MenuItemImage::create(normalImage, selectedImage, NULL, target, selector);
}
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, const ccMenuCallback& callback)
{
return MenuItemImage::create(normalImage, selectedImage, NULL, callback);
}
// XXX deprecated
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage, Object* target, SEL_MenuHandler selector)
{
MenuItemImage *pRet = new MenuItemImage();
if (pRet && pRet->initWithNormalImage(normalImage, selectedImage, disabledImage, target, selector))
2012-02-02 10:39:36 +08:00
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage, const ccMenuCallback& callback)
{
MenuItemImage *pRet = new MenuItemImage();
if (pRet && pRet->initWithNormalImage(normalImage, selectedImage, disabledImage, callback))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage)
{
MenuItemImage *pRet = new MenuItemImage();
if (pRet && pRet->initWithNormalImage(normalImage, selectedImage, disabledImage, (const ccMenuCallback&)nullptr))
2012-02-02 10:39:36 +08:00
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
// XXX: deprecated
bool MenuItemImage::initWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, Object* target, SEL_MenuHandler selector)
{
_target = target;
CC_SAFE_RETAIN(_target);
return initWithNormalImage(normalImage, selectedImage, disabledImage, std::bind(selector,target, std::placeholders::_1) );
}
bool MenuItemImage::initWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, const ccMenuCallback& callback)
{
Node *normalSprite = NULL;
Node *selectedSprite = NULL;
Node *disabledSprite = NULL;
if (normalImage)
{
normalSprite = Sprite::create(normalImage);
}
if (selectedImage)
2012-02-02 10:39:36 +08:00
{
selectedSprite = Sprite::create(selectedImage);
2012-02-02 10:39:36 +08:00
}
if(disabledImage)
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
//
void MenuItemImage::setNormalSpriteFrame(SpriteFrame * frame)
{
setNormalImage(Sprite::createWithSpriteFrame(frame));
}
void MenuItemImage::setSelectedSpriteFrame(SpriteFrame * frame)
{
setSelectedImage(Sprite::createWithSpriteFrame(frame));
}
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
void MenuItemToggle::setSubItems(Array* var)
{
CC_SAFE_RETAIN(var);
CC_SAFE_RELEASE(_subItems);
_subItems = var;
}
Array* MenuItemToggle::getSubItems()
{
return _subItems;
}
// XXX: deprecated
MenuItemToggle * MenuItemToggle::createWithTarget(Object* target, SEL_MenuHandler selector, Array* menuItems)
{
MenuItemToggle *pRet = new MenuItemToggle();
pRet->MenuItem::initWithTarget(target, selector);
pRet->_subItems = Array::create();
pRet->_subItems->retain();
2013-05-07 15:51:58 +08:00
for (unsigned int z=0; z < menuItems->count(); z++)
{
MenuItem* menuItem = (MenuItem*)menuItems->objectAtIndex(z);
pRet->_subItems->addObject(menuItem);
}
pRet->_selectedIndex = UINT_MAX;
pRet->setSelectedIndex(0);
return pRet;
}
MenuItemToggle * MenuItemToggle::createWithCallback(const ccMenuCallback &callback, Array* menuItems)
{
MenuItemToggle *pRet = new MenuItemToggle();
pRet->MenuItem::initWithCallback(callback);
pRet->_subItems = Array::create();
pRet->_subItems->retain();
for (unsigned int z=0; z < menuItems->count(); z++)
{
MenuItem* menuItem = (MenuItem*)menuItems->objectAtIndex(z);
pRet->_subItems->addObject(menuItem);
}
pRet->_selectedIndex = UINT_MAX;
pRet->setSelectedIndex(0);
return pRet;
}
// XXX: deprecated
MenuItemToggle * MenuItemToggle::createWithTarget(Object* target, SEL_MenuHandler selector, MenuItem* item, ...)
{
va_list args;
va_start(args, item);
MenuItemToggle *pRet = new MenuItemToggle();
pRet->initWithTarget(target, selector, item, args);
pRet->autorelease();
va_end(args);
return pRet;
}
MenuItemToggle * MenuItemToggle::createWithCallback(const ccMenuCallback &callback, MenuItem* item, ...)
{
va_list args;
va_start(args, item);
MenuItemToggle *pRet = new MenuItemToggle();
pRet->initWithCallback(callback, item, args);
pRet->autorelease();
va_end(args);
return pRet;
}
MenuItemToggle * MenuItemToggle::create()
{
MenuItemToggle *pRet = new MenuItemToggle();
2012-08-04 06:24:07 +08:00
pRet->initWithItem(NULL);
pRet->autorelease();
return pRet;
}
// XXX: deprecated
bool MenuItemToggle::initWithTarget(Object* target, SEL_MenuHandler selector, MenuItem* item, va_list args)
{
_target = target;
CC_SAFE_RETAIN(_target);
return initWithCallback(std::bind( selector, target, std::placeholders::_1), item, args);
}
bool MenuItemToggle::initWithCallback(const ccMenuCallback &callback, MenuItem *item, va_list args)
{
MenuItem::initWithCallback(callback);
this->_subItems = Array::create();
this->_subItems->retain();
int z = 0;
MenuItem *i = item;
while(i)
{
z++;
_subItems->addObject(i);
i = va_arg(args, MenuItem*);
}
_selectedIndex = UINT_MAX;
this->setSelectedIndex(0);
return true;
}
MenuItemToggle* MenuItemToggle::create(MenuItem *item)
{
MenuItemToggle *pRet = new MenuItemToggle();
pRet->initWithItem(item);
pRet->autorelease();
return pRet;
}
bool MenuItemToggle::initWithItem(MenuItem *item)
{
MenuItem::initWithCallback((const ccMenuCallback&)nullptr);
setSubItems(Array::create());
2013-02-27 17:23:39 +08:00
if (item)
{
_subItems->addObject(item);
}
_selectedIndex = UINT_MAX;
this->setSelectedIndex(0);
2013-02-27 17:23:39 +08:00
setCascadeColorEnabled(true);
setCascadeOpacityEnabled(true);
return true;
}
void MenuItemToggle::addSubItem(MenuItem *item)
{
_subItems->addObject(item);
}
MenuItemToggle::~MenuItemToggle()
{
CC_SAFE_RELEASE(_subItems);
}
void MenuItemToggle::setSelectedIndex(unsigned int index)
{
if( index != _selectedIndex && _subItems->count() > 0 )
{
_selectedIndex = index;
MenuItem *currentItem = (MenuItem*)getChildByTag(kCurrentItem);
if( currentItem )
{
currentItem->removeFromParentAndCleanup(false);
}
MenuItem* item = (MenuItem*)_subItems->objectAtIndex(_selectedIndex);
this->addChild(item, 0, kCurrentItem);
Size s = item->getContentSize();
this->setContentSize(s);
2013-07-12 14:11:55 +08:00
item->setPosition( Point( s.width/2, s.height/2 ) );
}
}
unsigned int MenuItemToggle::getSelectedIndex()
{
return _selectedIndex;
}
void MenuItemToggle::selected()
{
MenuItem::selected();
((MenuItem*)(_subItems->objectAtIndex(_selectedIndex)))->selected();
}
void MenuItemToggle::unselected()
{
MenuItem::unselected();
((MenuItem*)(_subItems->objectAtIndex(_selectedIndex)))->unselected();
}
void MenuItemToggle::activate()
{
// update index
if( _enabled )
{
unsigned int newIndex = (_selectedIndex + 1) % _subItems->count();
this->setSelectedIndex(newIndex);
}
MenuItem::activate();
}
void MenuItemToggle::setEnabled(bool enabled)
{
if (_enabled != enabled)
{
MenuItem::setEnabled(enabled);
if(_subItems && _subItems->count() > 0)
{
Object* pObj = NULL;
CCARRAY_FOREACH(_subItems, pObj)
{
MenuItem* pItem = static_cast<MenuItem*>(pObj);
pItem->setEnabled(enabled);
}
}
}
}
MenuItem* MenuItemToggle::selectedItem()
{
return (MenuItem*)_subItems->objectAtIndex(_selectedIndex);
}
NS_CC_END