2012-02-01 16:45:23 +08:00
|
|
|
/****************************************************************************
|
2012-06-14 15:13:16 +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-06-19 16:20:46 +08:00
|
|
|
|
2012-02-01 16:45:23 +08:00
|
|
|
#include "CCMenuItem.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#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>
|
2012-06-19 16:20:46 +08:00
|
|
|
#include <cstring>
|
2012-02-01 16:45:23 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2013-06-20 14:13:12 +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;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
const unsigned int kCurrentItem = 0xc0c05001;
|
|
|
|
const unsigned int kZoomActionTag = 0xc0c05002;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
const unsigned int kNormalTag = 0x1;
|
|
|
|
const unsigned int kSelectedTag = 0x2;
|
|
|
|
const unsigned int kDisableTag = 0x3;
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// MenuItem
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
2012-07-19 05:40:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem* MenuItem::create()
|
2012-07-18 08:08:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return MenuItem::create((const ccMenuCallback&)nullptr);
|
2012-07-18 08:08:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-05 09:29:26 +08:00
|
|
|
// XXX deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem* MenuItem::create(Object *target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem *pRet = new MenuItem();
|
2013-06-05 09:29:26 +08:00
|
|
|
pRet->initWithTarget(target, selector);
|
2012-03-20 15:04:53 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem* MenuItem::create( const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem *pRet = new MenuItem();
|
2013-06-05 09:29:26 +08:00
|
|
|
pRet->initWithCallback(callback);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItem::initWithTarget(cocos2d::Object *target, SEL_MenuHandler selector )
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
|
|
|
_target = target;
|
|
|
|
CC_SAFE_RETAIN(_target);
|
|
|
|
return initWithCallback( std::bind(selector,target, std::placeholders::_1) );
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItem::initWithCallback(const ccMenuCallback& callback)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
setAnchorPoint(Point(0.5f, 0.5f));
|
2013-06-05 09:29:26 +08:00
|
|
|
_callback = callback;
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabled = true;
|
|
|
|
_selected = false;
|
2012-03-20 15:04:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::~MenuItem()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-05 09:29:26 +08:00
|
|
|
CC_SAFE_RELEASE(_target);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItem::selected()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selected = true;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItem::unselected()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selected = false;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItem::activate()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_enabled)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-05 09:29:26 +08:00
|
|
|
if( _callback )
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2013-06-05 09:29:26 +08:00
|
|
|
_callback(this);
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2013-07-02 15:23:51 +08:00
|
|
|
if (kScriptTypeLua == _scriptType)
|
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
BasicScriptData data((void*)this);
|
|
|
|
ScriptEvent scriptEvent(kMenuClickedEvent,&data);
|
|
|
|
ScriptEngineManager::sharedManager()->getScriptEngine()->sendEvent(&scriptEvent);
|
2013-07-02 15:23:51 +08:00
|
|
|
}
|
|
|
|
else if (kScriptTypeJavascript == _scriptType)
|
2012-08-31 17:55:45 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
ScriptEngineManager::sharedManager()->getScriptEngine()->executeMenuItemEvent(this);
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItem::setEnabled(bool enabled)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabled = enabled;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 12:50:17 +08:00
|
|
|
bool MenuItem::isEnabled() const
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _enabled;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Rect MenuItem::rect()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-07-12 14:30:26 +08:00
|
|
|
return Rect( _position.x - _contentSize.width * _anchorPoint.x,
|
2013-06-15 14:03:30 +08:00
|
|
|
_position.y - _contentSize.height * _anchorPoint.y,
|
|
|
|
_contentSize.width, _contentSize.height);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-07-04 12:50:17 +08:00
|
|
|
bool MenuItem::isSelected() const
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _selected;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 08:51:40 +08:00
|
|
|
// XXX deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItem::setTarget(Object *target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-05 09:29:26 +08:00
|
|
|
_target = target;
|
|
|
|
CC_SAFE_RETAIN(_target);
|
|
|
|
setCallback( std::bind( selector, target, std::placeholders::_1) );
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItem::setCallback(const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
|
|
|
_callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
|
|
|
//CCMenuItemLabel
|
|
|
|
//
|
2012-07-19 05:40:09 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
const Color3B& MenuItemLabel::getDisabledColor() const
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _disabledColor;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-07-05 16:49:22 +08:00
|
|
|
void MenuItemLabel::setDisabledColor(const Color3B& var)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_disabledColor = var;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
Node *MenuItemLabel::getLabel()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _label;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemLabel::setLabel(Node* var)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
if (var)
|
|
|
|
{
|
|
|
|
addChild(var);
|
2013-07-12 14:11:55 +08:00
|
|
|
var->setAnchorPoint(Point(0, 0));
|
2012-03-20 15:04:53 +08:00
|
|
|
setContentSize(var->getContentSize());
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_label)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
removeChild(_label, true);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_label = var;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-05 09:29:26 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemLabel * MenuItemLabel::create(Node*label, Object* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemLabel *pRet = new MenuItemLabel();
|
2012-03-20 15:04:53 +08:00
|
|
|
pRet->initWithLabel(label, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemLabel * MenuItemLabel::create(Node*label, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemLabel *pRet = new MenuItemLabel();
|
2013-06-05 09:29:26 +08:00
|
|
|
pRet->initWithLabel(label, callback);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemLabel* MenuItemLabel::create(Node *label)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemLabel *pRet = new MenuItemLabel();
|
2013-06-07 04:08:59 +08:00
|
|
|
pRet->initWithLabel(label, (const ccMenuCallback&) nullptr);
|
2012-03-20 15:04:53 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-05 09:29:26 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemLabel::initWithLabel(Node* label, Object* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-05 09:29:26 +08:00
|
|
|
_target = target;
|
|
|
|
CC_SAFE_RETAIN(_target);
|
|
|
|
return initWithLabel(label, std::bind(selector,target, std::placeholders::_1) );
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemLabel::initWithLabel(Node* label, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::initWithCallback(callback);
|
2013-06-15 14:03:30 +08:00
|
|
|
_originalScale = 1.0f;
|
2013-07-08 18:11:32 +08:00
|
|
|
_colorBackup = Color3B::WHITE;
|
2013-07-05 16:49:22 +08:00
|
|
|
setDisabledColor(Color3B(126,126,126));
|
2012-03-20 15:04:53 +08:00
|
|
|
this->setLabel(label);
|
2013-06-05 09:29:26 +08:00
|
|
|
|
2013-02-27 17:23:39 +08:00
|
|
|
setCascadeColorEnabled(true);
|
|
|
|
setCascadeOpacityEnabled(true);
|
2013-06-05 09:29:26 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-05 09:29:26 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemLabel::~MenuItemLabel()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemLabel::setString(const char * label)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
dynamic_cast<LabelProtocol*>(_label)->setString(label);
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setContentSize(_label->getContentSize());
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemLabel::activate()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_enabled)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
this->stopAllActions();
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setScale( _originalScale );
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::activate();
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemLabel::selected()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
// subclass to change the default action
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_enabled)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::selected();
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Action *action = getActionByTag(kZoomActionTag);
|
2012-03-20 15:04:53 +08:00
|
|
|
if (action)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
this->stopAction(action);
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
else
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_originalScale = this->getScale();
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Action *zoomAction = ScaleTo::create(0.1f, _originalScale * 1.2f);
|
2012-03-20 15:04:53 +08:00
|
|
|
zoomAction->setTag(kZoomActionTag);
|
|
|
|
this->runAction(zoomAction);
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemLabel::unselected()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
// subclass to change the default action
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_enabled)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::unselected();
|
2012-03-20 15:04:53 +08:00
|
|
|
this->stopActionByTag(kZoomActionTag);
|
2013-06-20 14:13:12 +08:00
|
|
|
Action *zoomAction = ScaleTo::create(0.1f, _originalScale);
|
2012-03-20 15:04:53 +08:00
|
|
|
zoomAction->setTag(kZoomActionTag);
|
|
|
|
this->runAction(zoomAction);
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemLabel::setEnabled(bool enabled)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _enabled != enabled )
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
if(enabled == false)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_colorBackup = dynamic_cast<RGBAProtocol*>(_label)->getColor();
|
|
|
|
dynamic_cast<RGBAProtocol*>(_label)->setColor(_disabledColor);
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
else
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
dynamic_cast<RGBAProtocol*>(_label)->setColor(_colorBackup);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::setEnabled(enabled);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
|
|
|
//CCMenuItemAtlasFont
|
|
|
|
//
|
2012-07-19 05:40:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemAtlasFont * MenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return MenuItemAtlasFont::create(value, charMapFile, itemWidth, itemHeight, startCharMap, (const ccMenuCallback&)nullptr);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-05 09:29:26 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemAtlasFont * MenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, Object* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemAtlasFont *pRet = new MenuItemAtlasFont();
|
2012-03-20 15:04:53 +08:00
|
|
|
pRet->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemAtlasFont * MenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemAtlasFont *pRet = new MenuItemAtlasFont();
|
2013-06-05 09:29:26 +08:00
|
|
|
pRet->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, callback);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemAtlasFont::initWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, Object* target, SEL_MenuHandler selector)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
|
|
|
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) );
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemAtlasFont::initWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCAssert( value != NULL && strlen(value) != 0, "value length must be greater than 0");
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelAtlas *label = new LabelAtlas();
|
2012-03-20 15:04:53 +08:00
|
|
|
label->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap);
|
|
|
|
label->autorelease();
|
2013-06-20 14:13:12 +08:00
|
|
|
if (MenuItemLabel::initWithLabel(label, callback))
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
// do something ?
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-05 09:29:26 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
|
|
|
//CCMenuItemFont
|
|
|
|
//
|
2012-07-19 05:40:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemFont::setFontSize(unsigned int s)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-02-27 17:23:39 +08:00
|
|
|
_globalFontSize = s;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int MenuItemFont::fontSize()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-02-27 17:23:39 +08:00
|
|
|
return _globalFontSize;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemFont::setFontName(const char *name)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-02-27 17:23:39 +08:00
|
|
|
if (_globalFontNameRelease)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-02-27 17:23:39 +08:00
|
|
|
_globalFontName.clear();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-02-27 17:23:39 +08:00
|
|
|
_globalFontName = name;
|
|
|
|
_globalFontNameRelease = true;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const char * MenuItemFont::fontName()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-02-27 17:23:39 +08:00
|
|
|
return _globalFontName.c_str();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-05 09:29:26 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemFont * MenuItemFont::create(const char *value, Object* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemFont *pRet = new MenuItemFont();
|
2012-03-20 15:04:53 +08:00
|
|
|
pRet->initWithString(value, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemFont * MenuItemFont::create(const char *value, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemFont *pRet = new MenuItemFont();
|
2013-06-05 09:29:26 +08:00
|
|
|
pRet->initWithString(value, callback);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemFont * MenuItemFont::create(const char *value)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemFont *pRet = new MenuItemFont();
|
2013-06-07 04:08:59 +08:00
|
|
|
pRet->initWithString(value, (const ccMenuCallback&)nullptr);
|
2012-03-20 15:04:53 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-06 08:51:40 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemFont::initWithString(const char *value, Object* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCAssert( value != NULL && strlen(value) != 0, "Value length must be greater than 0");
|
2013-06-05 09:29:26 +08:00
|
|
|
|
|
|
|
_target = target;
|
|
|
|
CC_SAFE_RETAIN(target);
|
|
|
|
return initWithString(value, std::bind(selector,target, std::placeholders::_1) );
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemFont::initWithString(const char *value, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
|
|
|
CCAssert( value != NULL && strlen(value) != 0, "Value length must be greater than 0");
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_fontName = _globalFontName;
|
|
|
|
_fontSize = _globalFontSize;
|
2013-06-05 09:29:26 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF *label = LabelTTF::create(value, _fontName.c_str(), (float)_fontSize);
|
|
|
|
if (MenuItemLabel::initWithLabel(label, callback))
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
// do something ?
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemFont::recreateLabel()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
LabelTTF *label = LabelTTF::create(dynamic_cast<LabelProtocol*>(_label)->getString(),
|
2013-06-15 14:03:30 +08:00
|
|
|
_fontName.c_str(), (float)_fontSize);
|
2012-03-20 15:04:53 +08:00
|
|
|
this->setLabel(label);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemFont::setFontSizeObj(unsigned int s)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_fontSize = s;
|
2012-03-20 15:04:53 +08:00
|
|
|
recreateLabel();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int MenuItemFont::fontSizeObj()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _fontSize;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemFont::setFontNameObj(const char* name)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_fontName = name;
|
2012-03-20 15:04:53 +08:00
|
|
|
recreateLabel();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
const char* MenuItemFont::fontNameObj()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _fontName.c_str();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//CCMenuItemSprite
|
|
|
|
//
|
2012-07-19 05:40:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node * MenuItemSprite::getNormalImage()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _normalImage;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemSprite::setNormalImage(Node* pImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (pImage != _normalImage)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (pImage)
|
|
|
|
{
|
|
|
|
addChild(pImage, 0, kNormalTag);
|
2013-07-12 14:11:55 +08:00
|
|
|
pImage->setAnchorPoint(Point(0, 0));
|
2012-06-08 14:50:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_normalImage)
|
2012-06-08 14:50:41 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
removeChild(_normalImage, true);
|
2012-06-08 14:50:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_normalImage = pImage;
|
|
|
|
this->setContentSize(_normalImage->getContentSize());
|
2012-06-08 14:50:41 +08:00
|
|
|
this->updateImagesVisibility();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node * MenuItemSprite::getSelectedImage()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _selectedImage;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemSprite::setSelectedImage(Node* pImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (pImage != _normalImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (pImage)
|
|
|
|
{
|
|
|
|
addChild(pImage, 0, kSelectedTag);
|
2013-07-12 14:11:55 +08:00
|
|
|
pImage->setAnchorPoint(Point(0, 0));
|
2012-06-08 14:50:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_selectedImage)
|
2012-06-08 14:50:41 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
removeChild(_selectedImage, true);
|
2012-06-08 14:50:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedImage = pImage;
|
2012-06-08 14:50:41 +08:00
|
|
|
this->updateImagesVisibility();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node * MenuItemSprite::getDisabledImage()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _disabledImage;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemSprite::setDisabledImage(Node* pImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (pImage != _normalImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (pImage)
|
|
|
|
{
|
|
|
|
addChild(pImage, 0, kDisableTag);
|
2013-07-12 14:11:55 +08:00
|
|
|
pImage->setAnchorPoint(Point(0, 0));
|
2012-06-08 14:50:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_disabledImage)
|
2012-06-08 14:50:41 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
removeChild(_disabledImage, true);
|
2012-06-08 14:50:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_disabledImage = pImage;
|
2012-06-08 14:50:41 +08:00
|
|
|
this->updateImagesVisibility();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
2013-02-27 17:23:39 +08:00
|
|
|
//CCMenuItemSprite
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
2012-07-19 05:40:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return MenuItemSprite::create(normalSprite, selectedSprite, disabledSprite, (const ccMenuCallback&)nullptr);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-05 09:29:26 +08:00
|
|
|
// XXX deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, Object* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return MenuItemSprite::create(normalSprite, selectedSprite, NULL, target, selector);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemSprite * MenuItemSprite::create(Node* normalSprite, Node* selectedSprite, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return MenuItemSprite::create(normalSprite, selectedSprite, NULL, callback);
|
2013-06-05 09:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemSprite * MenuItemSprite::create(Node *normalSprite, Node *selectedSprite, Node *disabledSprite, Object *target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemSprite *pRet = new MenuItemSprite();
|
2012-03-20 15:04:53 +08:00
|
|
|
pRet->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemSprite * MenuItemSprite::create(Node *normalSprite, Node *selectedSprite, Node *disabledSprite, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemSprite *pRet = new MenuItemSprite();
|
2013-06-05 09:29:26 +08:00
|
|
|
pRet->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, callback);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemSprite::initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Object* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-05 09:29:26 +08:00
|
|
|
_target = target;
|
|
|
|
CC_SAFE_RETAIN(_target);
|
|
|
|
return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, std::bind(selector,target, std::placeholders::_1) );
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemSprite::initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::initWithCallback(callback);
|
2012-03-20 15:04:53 +08:00
|
|
|
setNormalImage(normalSprite);
|
|
|
|
setSelectedImage(selectedSprite);
|
|
|
|
setDisabledImage(disabledSprite);
|
2012-06-01 08:04:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_normalImage)
|
2012-06-12 02:50:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
this->setContentSize(_normalImage->getContentSize());
|
2012-06-12 02:50:32 +08:00
|
|
|
}
|
2013-06-05 09:29:26 +08:00
|
|
|
|
2013-02-27 17:23:39 +08:00
|
|
|
setCascadeColorEnabled(true);
|
|
|
|
setCascadeOpacityEnabled(true);
|
2013-06-05 09:29:26 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@since v0.99.5
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemSprite::selected()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::selected();
|
2012-06-12 02:50:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_normalImage)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_disabledImage)
|
2012-06-01 08:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_disabledImage->setVisible(false);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_selectedImage)
|
2012-06-01 08:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_normalImage->setVisible(false);
|
|
|
|
_selectedImage->setVisible(true);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_normalImage->setVisible(true);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemSprite::unselected()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::unselected();
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_normalImage)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_normalImage->setVisible(true);
|
2012-06-01 08:04:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_selectedImage)
|
2012-06-01 08:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedImage->setVisible(false);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_disabledImage)
|
2012-06-01 08:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_disabledImage->setVisible(false);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemSprite::setEnabled(bool bEnabled)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _enabled != bEnabled )
|
2012-06-15 15:10:40 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::setEnabled(bEnabled);
|
2012-06-15 15:10:40 +08:00
|
|
|
this->updateImagesVisibility();
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemSprite::updateImagesVisibility()
|
2012-06-08 14:50:41 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_enabled)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +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
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
else
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_disabledImage)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +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
|
|
|
|
{
|
2013-06-15 14:03:30 +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-03-20 15:04:53 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-07-19 05:40:09 +08:00
|
|
|
///
|
2013-06-20 14:13:12 +08:00
|
|
|
/// MenuItemImage
|
2012-07-19 05:40:09 +08:00
|
|
|
///
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage* MenuItemImage::create()
|
2012-06-01 08:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage *pRet = new MenuItemImage();
|
2012-06-01 08:04:53 +08:00
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemImage::init(void)
|
2012-06-01 08:04:53 +08:00
|
|
|
{
|
2013-06-07 04:08:59 +08:00
|
|
|
return initWithNormalImage(NULL, NULL, NULL, (const ccMenuCallback&)nullptr);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return MenuItemImage::create(normalImage, selectedImage, NULL, (const ccMenuCallback&)nullptr);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2013-06-05 09:29:26 +08:00
|
|
|
// XXX deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, Object* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return MenuItemImage::create(normalImage, selectedImage, NULL, target, selector);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return MenuItemImage::create(normalImage, selectedImage, NULL, callback);
|
2013-06-05 09:29:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage, Object* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage *pRet = new MenuItemImage();
|
2012-03-20 15:04:53 +08:00
|
|
|
if (pRet && pRet->initWithNormalImage(normalImage, selectedImage, disabledImage, target, selector))
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage, const ccMenuCallback& callback)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage *pRet = new MenuItemImage();
|
2013-06-05 09:29:26 +08:00
|
|
|
if (pRet && pRet->initWithNormalImage(normalImage, selectedImage, disabledImage, callback))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage * MenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemImage *pRet = new MenuItemImage();
|
2013-06-07 04:08:59 +08:00
|
|
|
if (pRet && pRet->initWithNormalImage(normalImage, selectedImage, disabledImage, (const ccMenuCallback&)nullptr))
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2013-06-05 09:29:26 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemImage::initWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, Object* target, SEL_MenuHandler selector)
|
2013-06-05 09:29:26 +08:00
|
|
|
{
|
|
|
|
_target = target;
|
|
|
|
CC_SAFE_RETAIN(_target);
|
|
|
|
return initWithNormalImage(normalImage, selectedImage, disabledImage, std::bind(selector,target, std::placeholders::_1) );
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemImage::initWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, const ccMenuCallback& callback)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Node *normalSprite = NULL;
|
|
|
|
Node *selectedSprite = NULL;
|
|
|
|
Node *disabledSprite = NULL;
|
2012-06-01 08:04:53 +08:00
|
|
|
|
|
|
|
if (normalImage)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
normalSprite = Sprite::create(normalImage);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
if (selectedImage)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
selectedSprite = Sprite::create(selectedImage);
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2013-06-05 09:29:26 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
if(disabledImage)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
disabledSprite = Sprite::create(disabledImage);
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2013-06-05 09:29:26 +08:00
|
|
|
return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, callback);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-06-05 09:29:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Setter of sprite frames
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemImage::setNormalSpriteFrame(SpriteFrame * frame)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
setNormalImage(Sprite::createWithSpriteFrame(frame));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemImage::setSelectedSpriteFrame(SpriteFrame * frame)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
setSelectedImage(Sprite::createWithSpriteFrame(frame));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemImage::setDisabledSpriteFrame(SpriteFrame * frame)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
setDisabledImage(Sprite::createWithSpriteFrame(frame));
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-07-19 05:40:09 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
//
|
|
|
|
// MenuItemToggle
|
|
|
|
//
|
2012-07-19 05:40:09 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemToggle::setSubItems(Array* var)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(var);
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_subItems);
|
|
|
|
_subItems = var;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* MenuItemToggle::getSubItems()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _subItems;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-06-06 08:51:40 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle * MenuItemToggle::createWithTarget(Object* target, SEL_MenuHandler selector, Array* menuItems)
|
2013-03-12 15:11:41 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle *pRet = new MenuItemToggle();
|
|
|
|
pRet->MenuItem::initWithTarget(target, selector);
|
|
|
|
pRet->_subItems = Array::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
pRet->_subItems->retain();
|
2013-03-12 15:11:41 +08:00
|
|
|
|
2013-05-07 15:51:58 +08:00
|
|
|
for (unsigned int z=0; z < menuItems->count(); z++)
|
2013-03-12 15:11:41 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem* menuItem = (MenuItem*)menuItems->objectAtIndex(z);
|
2013-06-15 14:03:30 +08:00
|
|
|
pRet->_subItems->addObject(menuItem);
|
2013-03-12 15:11:41 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pRet->_selectedIndex = UINT_MAX;
|
2013-03-12 15:11:41 +08:00
|
|
|
pRet->setSelectedIndex(0);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle * MenuItemToggle::createWithCallback(const ccMenuCallback &callback, Array* menuItems)
|
2013-06-06 08:51:40 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle *pRet = new MenuItemToggle();
|
|
|
|
pRet->MenuItem::initWithCallback(callback);
|
|
|
|
pRet->_subItems = Array::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
pRet->_subItems->retain();
|
2013-06-06 08:51:40 +08:00
|
|
|
|
|
|
|
for (unsigned int z=0; z < menuItems->count(); z++)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem* menuItem = (MenuItem*)menuItems->objectAtIndex(z);
|
2013-06-15 14:03:30 +08:00
|
|
|
pRet->_subItems->addObject(menuItem);
|
2013-06-06 08:51:40 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
pRet->_selectedIndex = UINT_MAX;
|
2013-06-06 08:51:40 +08:00
|
|
|
pRet->setSelectedIndex(0);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle * MenuItemToggle::createWithTarget(Object* target, SEL_MenuHandler selector, MenuItem* item, ...)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, item);
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle *pRet = new MenuItemToggle();
|
2012-03-20 15:04:53 +08:00
|
|
|
pRet->initWithTarget(target, selector, item, args);
|
|
|
|
pRet->autorelease();
|
|
|
|
va_end(args);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle * MenuItemToggle::createWithCallback(const ccMenuCallback &callback, MenuItem* item, ...)
|
2013-06-06 08:51:40 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, item);
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle *pRet = new MenuItemToggle();
|
2013-06-06 08:51:40 +08:00
|
|
|
pRet->initWithCallback(callback, item, args);
|
|
|
|
pRet->autorelease();
|
|
|
|
va_end(args);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle * MenuItemToggle::create()
|
2012-08-01 08:25:38 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle *pRet = new MenuItemToggle();
|
2012-08-04 06:24:07 +08:00
|
|
|
pRet->initWithItem(NULL);
|
2012-08-01 08:25:38 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-06 08:51:40 +08:00
|
|
|
// XXX: deprecated
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemToggle::initWithTarget(Object* target, SEL_MenuHandler selector, MenuItem* item, va_list args)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-06 08:51:40 +08:00
|
|
|
_target = target;
|
|
|
|
CC_SAFE_RETAIN(_target);
|
|
|
|
return initWithCallback(std::bind( selector, target, std::placeholders::_1), item, args);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemToggle::initWithCallback(const ccMenuCallback &callback, MenuItem *item, va_list args)
|
2013-06-06 08:51:40 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::initWithCallback(callback);
|
|
|
|
this->_subItems = Array::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
this->_subItems->retain();
|
2012-03-20 15:04:53 +08:00
|
|
|
int z = 0;
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem *i = item;
|
2013-06-06 08:51:40 +08:00
|
|
|
while(i)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
z++;
|
2013-06-15 14:03:30 +08:00
|
|
|
_subItems->addObject(i);
|
2013-06-20 14:13:12 +08:00
|
|
|
i = va_arg(args, MenuItem*);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedIndex = UINT_MAX;
|
2012-03-20 15:04:53 +08:00
|
|
|
this->setSelectedIndex(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle* MenuItemToggle::create(MenuItem *item)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle *pRet = new MenuItemToggle();
|
2012-03-20 15:04:53 +08:00
|
|
|
pRet->initWithItem(item);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool MenuItemToggle::initWithItem(MenuItem *item)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::initWithCallback((const ccMenuCallback&)nullptr);
|
|
|
|
setSubItems(Array::create());
|
2012-12-03 22:17:53 +08:00
|
|
|
|
2013-02-27 17:23:39 +08:00
|
|
|
if (item)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_subItems->addObject(item);
|
2012-08-11 05:17:53 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedIndex = UINT_MAX;
|
2012-03-20 15:04:53 +08:00
|
|
|
this->setSelectedIndex(0);
|
2013-02-27 17:23:39 +08:00
|
|
|
|
|
|
|
setCascadeColorEnabled(true);
|
|
|
|
setCascadeOpacityEnabled(true);
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemToggle::addSubItem(MenuItem *item)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_subItems->addObject(item);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItemToggle::~MenuItemToggle()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_subItems);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemToggle::setSelectedIndex(unsigned int index)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if( index != _selectedIndex && _subItems->count() > 0 )
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedIndex = index;
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem *currentItem = (MenuItem*)getChildByTag(kCurrentItem);
|
2012-04-19 14:35:52 +08:00
|
|
|
if( currentItem )
|
|
|
|
{
|
|
|
|
currentItem->removeFromParentAndCleanup(false);
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem* item = (MenuItem*)_subItems->objectAtIndex(_selectedIndex);
|
2012-03-20 15:04:53 +08:00
|
|
|
this->addChild(item, 0, kCurrentItem);
|
2013-06-20 14:13:12 +08:00
|
|
|
Size s = item->getContentSize();
|
2012-03-20 15:04:53 +08:00
|
|
|
this->setContentSize(s);
|
2013-07-12 14:11:55 +08:00
|
|
|
item->setPosition( Point( s.width/2, s.height/2 ) );
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int MenuItemToggle::getSelectedIndex()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _selectedIndex;
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemToggle::selected()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::selected();
|
|
|
|
((MenuItem*)(_subItems->objectAtIndex(_selectedIndex)))->selected();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemToggle::unselected()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::unselected();
|
|
|
|
((MenuItem*)(_subItems->objectAtIndex(_selectedIndex)))->unselected();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemToggle::activate()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
// update index
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _enabled )
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int newIndex = (_selectedIndex + 1) % _subItems->count();
|
2012-03-20 15:04:53 +08:00
|
|
|
this->setSelectedIndex(newIndex);
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::activate();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
void MenuItemToggle::setEnabled(bool enabled)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_enabled != enabled)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem::setEnabled(enabled);
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_subItems && _subItems->count() > 0)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_subItems, pObj)
|
2012-06-08 14:50:41 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
MenuItem* pItem = static_cast<MenuItem*>(pObj);
|
2012-06-15 16:47:30 +08:00
|
|
|
pItem->setEnabled(enabled);
|
2012-06-08 14:50:41 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem* MenuItemToggle::selectedItem()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return (MenuItem*)_subItems->objectAtIndex(_selectedIndex);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|