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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include <cstring>
|
|
|
|
#include "CCMenuItem.h"
|
|
|
|
#include "CCPointExtension.h"
|
|
|
|
#include "CCActionInterval.h"
|
|
|
|
#include "CCSprite.h"
|
|
|
|
#include "CCLabelAtlas.h"
|
|
|
|
#include "CCLabelTTF.h"
|
2012-02-09 14:07:11 +08:00
|
|
|
#include "CCScriptSupport.h"
|
2012-02-01 16:45:23 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
static unsigned int _fontSize = kCCItemSize;
|
|
|
|
static std::string _fontName = "Marker Felt";
|
|
|
|
static bool _fontNameRelease = false;
|
|
|
|
|
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
|
|
|
//
|
|
|
|
// CCMenuItem
|
|
|
|
//
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItem * CCMenuItem::itemWithTarget(CCObject *rec, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
return CCMenuItem::create(rec, selector);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItem * CCMenuItem::create(CCObject *rec, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItem *pRet = new CCMenuItem();
|
|
|
|
pRet->initWithTarget(rec, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
bool CCMenuItem::initWithTarget(CCObject *rec, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
setAnchorPoint(ccp(0.5f, 0.5f));
|
|
|
|
m_pListener = rec;
|
|
|
|
m_pfnSelector = selector;
|
|
|
|
m_bIsEnabled = true;
|
|
|
|
m_bIsSelected = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCMenuItem::~CCMenuItem()
|
|
|
|
{
|
|
|
|
unregisterScriptHandler();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItem::selected()
|
|
|
|
{
|
|
|
|
m_bIsSelected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItem::unselected()
|
|
|
|
{
|
|
|
|
m_bIsSelected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItem::registerScriptHandler(int nHandler)
|
|
|
|
{
|
|
|
|
unregisterScriptHandler();
|
|
|
|
m_nScriptHandler = nHandler;
|
|
|
|
LUALOG("[LUA] Add CCMenuItem script handler: %d", m_nScriptHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItem::unregisterScriptHandler(void)
|
|
|
|
{
|
|
|
|
if (m_nScriptHandler)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeLuaHandler(m_nScriptHandler);
|
|
|
|
LUALOG("[LUA] Remove CCMenuItem script handler: %d", m_nScriptHandler);
|
|
|
|
m_nScriptHandler = 0;
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItem::activate()
|
|
|
|
{
|
|
|
|
if (m_bIsEnabled)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
if (m_pListener && m_pfnSelector)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
(m_pListener->*m_pfnSelector)(this);
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
|
|
if (m_nScriptHandler)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeFunctionWithIntegerData(m_nScriptHandler, getTag());
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItem::setIsEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
m_bIsEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCMenuItem::isEnabled()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
return m_bIsEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCRect CCMenuItem::rect()
|
|
|
|
{
|
|
|
|
return CCRectMake( m_tPosition.x - m_tContentSize.width * m_tAnchorPoint.x,
|
|
|
|
m_tPosition.y - m_tContentSize.height * m_tAnchorPoint.y,
|
|
|
|
m_tContentSize.width, m_tContentSize.height);
|
|
|
|
}
|
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
bool CCMenuItem::isSelected()
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
return m_bIsSelected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItem::setTarget(CCObject *rec, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
m_pListener = rec;
|
|
|
|
m_pfnSelector = selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//CCMenuItemLabel
|
|
|
|
//
|
|
|
|
const ccColor3B& CCMenuItemLabel::getDisabledColor()
|
|
|
|
{
|
|
|
|
return m_tDisabledColor;
|
|
|
|
}
|
|
|
|
void CCMenuItemLabel::setDisabledColor(const ccColor3B& var)
|
|
|
|
{
|
|
|
|
m_tDisabledColor = var;
|
|
|
|
}
|
|
|
|
CCNode *CCMenuItemLabel::getLabel()
|
|
|
|
{
|
|
|
|
return m_pLabel;
|
|
|
|
}
|
|
|
|
void CCMenuItemLabel::setLabel(CCNode* var)
|
|
|
|
{
|
|
|
|
if (var)
|
|
|
|
{
|
|
|
|
addChild(var);
|
|
|
|
var->setAnchorPoint(ccp(0, 0));
|
|
|
|
setContentSize(var->getContentSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pLabel)
|
|
|
|
{
|
|
|
|
removeChild(m_pLabel, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pLabel = var;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemLabel * CCMenuItemLabel::itemWithLabel(CCNode*label, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
return CCMenuItemLabel::create(label, target, selector);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemLabel * CCMenuItemLabel::create(CCNode*label, CCObject* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemLabel *pRet = new CCMenuItemLabel();
|
|
|
|
pRet->initWithLabel(label, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemLabel* CCMenuItemLabel::itemWithLabel(CCNode *label)
|
|
|
|
{
|
|
|
|
return CCMenuItemLabel::create(label);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemLabel* CCMenuItemLabel::create(CCNode *label)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemLabel *pRet = new CCMenuItemLabel();
|
|
|
|
pRet->initWithLabel(label, NULL, NULL);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
bool CCMenuItemLabel::initWithLabel(CCNode* label, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
CCMenuItem::initWithTarget(target, selector);
|
|
|
|
m_fOriginalScale = 1.0f;
|
|
|
|
m_tColorBackup = ccWHITE;
|
|
|
|
m_tDisabledColor = ccc3(126,126,126);
|
|
|
|
this->setLabel(label);
|
|
|
|
return true;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCMenuItemLabel::~CCMenuItemLabel()
|
|
|
|
{
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCMenuItemLabel::setString(const char * label)
|
|
|
|
{
|
|
|
|
dynamic_cast<CCLabelProtocol*>(m_pLabel)->setString(label);
|
|
|
|
this->setContentSize(m_pLabel->getContentSize());
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCMenuItemLabel::activate()
|
|
|
|
{
|
|
|
|
if(m_bIsEnabled)
|
|
|
|
{
|
|
|
|
this->stopAllActions();
|
|
|
|
this->setScale( m_fOriginalScale );
|
|
|
|
CCMenuItem::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
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCMenuItemLabel::selected()
|
|
|
|
{
|
|
|
|
// subclass to change the default action
|
|
|
|
if(m_bIsEnabled)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCMenuItem::selected();
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCAction *action = getActionByTag(kZoomActionTag);
|
|
|
|
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
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
m_fOriginalScale = this->getScale();
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCAction *zoomAction = CCScaleTo::create(0.1f, m_fOriginalScale * 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
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCMenuItemLabel::unselected()
|
|
|
|
{
|
|
|
|
// subclass to change the default action
|
|
|
|
if(m_bIsEnabled)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCMenuItem::unselected();
|
|
|
|
this->stopActionByTag(kZoomActionTag);
|
2012-06-14 15:13:16 +08:00
|
|
|
CCAction *zoomAction = CCScaleTo::create(0.1f, m_fOriginalScale);
|
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
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCMenuItemLabel::setIsEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if( m_bIsEnabled != 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
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
m_tColorBackup = dynamic_cast<CCRGBAProtocol*>(m_pLabel)->getColor();
|
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pLabel)->setColor(m_tDisabledColor);
|
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
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pLabel)->setColor(m_tColorBackup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CCMenuItem::setIsEnabled(enabled);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCMenuItemLabel::setOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pLabel)->setOpacity(opacity);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
GLubyte CCMenuItemLabel::getOpacity()
|
|
|
|
{
|
|
|
|
return dynamic_cast<CCRGBAProtocol*>(m_pLabel)->getOpacity();
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCMenuItemLabel::setColor(const ccColor3B& color)
|
|
|
|
{
|
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pLabel)->setColor(color);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
const ccColor3B& CCMenuItemLabel::getColor()
|
|
|
|
{
|
|
|
|
return dynamic_cast<CCRGBAProtocol*>(m_pLabel)->getColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//CCMenuItemAtlasFont
|
|
|
|
//
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemAtlasFont * CCMenuItemAtlasFont::itemWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap)
|
|
|
|
{
|
|
|
|
return CCMenuItemAtlasFont::create(value, charMapFile, itemWidth, itemHeight, startCharMap);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemAtlasFont * CCMenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
return CCMenuItemAtlasFont::create(value, charMapFile, itemWidth, itemHeight, startCharMap, NULL, NULL);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemAtlasFont * CCMenuItemAtlasFont::itemWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
return CCMenuItemAtlasFont::create(value, charMapFile, itemWidth, itemHeight, startCharMap, target, selector);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemAtlasFont * CCMenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, CCObject* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemAtlasFont *pRet = new CCMenuItemAtlasFont();
|
|
|
|
pRet->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
bool CCMenuItemAtlasFont::initWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
CCAssert( value != NULL && strlen(value) != 0, "value length must be greater than 0");
|
|
|
|
CCLabelAtlas *label = new CCLabelAtlas();
|
|
|
|
label->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap);
|
|
|
|
label->autorelease();
|
|
|
|
if (CCMenuItemLabel::initWithLabel(label, target, selector))
|
|
|
|
{
|
|
|
|
// do something ?
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
//CCMenuItemFont
|
|
|
|
//
|
|
|
|
void CCMenuItemFont::setFontSize(unsigned int s)
|
|
|
|
{
|
|
|
|
_fontSize = s;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
unsigned int CCMenuItemFont::fontSize()
|
|
|
|
{
|
|
|
|
return _fontSize;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCMenuItemFont::setFontName(const char *name)
|
|
|
|
{
|
|
|
|
if( _fontNameRelease )
|
|
|
|
{
|
|
|
|
_fontName.clear();
|
|
|
|
}
|
|
|
|
_fontName = name;
|
|
|
|
_fontNameRelease = true;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
const char * CCMenuItemFont::fontName()
|
|
|
|
{
|
|
|
|
return _fontName.c_str();
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemFont * CCMenuItemFont::itemWithString(const char *value, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
return CCMenuItemFont::create(value, target, selector);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemFont * CCMenuItemFont::create(const char *value, CCObject* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemFont *pRet = new CCMenuItemFont();
|
|
|
|
pRet->initWithString(value, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemFont * CCMenuItemFont::itemWithString(const char *value)
|
|
|
|
{
|
|
|
|
return CCMenuItemFont::create(value);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemFont * CCMenuItemFont::create(const char *value)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemFont *pRet = new CCMenuItemFont();
|
|
|
|
pRet->initWithString(value, NULL, NULL);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
bool CCMenuItemFont::initWithString(const char *value, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
CCAssert( value != NULL && strlen(value) != 0, "Value length must be greater than 0");
|
|
|
|
|
|
|
|
m_strFontName = _fontName;
|
|
|
|
m_uFontSize = _fontSize;
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF *label = CCLabelTTF::create(value, m_strFontName.c_str(), (float)m_uFontSize);
|
2012-03-20 15:04:53 +08:00
|
|
|
if (CCMenuItemLabel::initWithLabel(label, target, selector))
|
|
|
|
{
|
|
|
|
// do something ?
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItemFont::recreateLabel()
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF *label = CCLabelTTF::create(dynamic_cast<CCLabelProtocol*>(m_pLabel)->getString(),
|
2012-03-20 15:04:53 +08:00
|
|
|
m_strFontName.c_str(), (float)m_uFontSize);
|
|
|
|
this->setLabel(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItemFont::setFontSizeObj(unsigned int s)
|
|
|
|
{
|
|
|
|
m_uFontSize = s;
|
|
|
|
recreateLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int CCMenuItemFont::fontSizeObj()
|
|
|
|
{
|
|
|
|
return m_uFontSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItemFont::setFontNameObj(const char* name)
|
|
|
|
{
|
|
|
|
m_strFontName = name;
|
|
|
|
recreateLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCMenuItemFont::fontNameObj()
|
|
|
|
{
|
|
|
|
return m_strFontName.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//CCMenuItemSprite
|
|
|
|
//
|
|
|
|
CCNode * CCMenuItemSprite::getNormalImage()
|
|
|
|
{
|
|
|
|
return m_pNormalImage;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-08 14:50:41 +08:00
|
|
|
void CCMenuItemSprite::setNormalImage(CCNode* pImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (pImage != m_pNormalImage)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (pImage)
|
|
|
|
{
|
|
|
|
addChild(pImage, 0, kNormalTag);
|
|
|
|
pImage->setAnchorPoint(ccp(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pNormalImage)
|
|
|
|
{
|
|
|
|
removeChild(m_pNormalImage, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pNormalImage = pImage;
|
|
|
|
this->setContentSize(m_pNormalImage->getContentSize());
|
|
|
|
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
|
|
|
CCNode * CCMenuItemSprite::getSelectedImage()
|
|
|
|
{
|
|
|
|
return m_pSelectedImage;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-08 14:50:41 +08:00
|
|
|
void CCMenuItemSprite::setSelectedImage(CCNode* pImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (pImage != m_pNormalImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (pImage)
|
|
|
|
{
|
|
|
|
addChild(pImage, 0, kSelectedTag);
|
|
|
|
pImage->setAnchorPoint(ccp(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pSelectedImage)
|
|
|
|
{
|
|
|
|
removeChild(m_pSelectedImage, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pSelectedImage = pImage;
|
|
|
|
this->updateImagesVisibility();
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCNode * CCMenuItemSprite::getDisabledImage()
|
|
|
|
{
|
|
|
|
return m_pDisabledImage;
|
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
|
|
|
void CCMenuItemSprite::setDisabledImage(CCNode* pImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (pImage != m_pNormalImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (pImage)
|
|
|
|
{
|
|
|
|
addChild(pImage, 0, kDisableTag);
|
|
|
|
pImage->setAnchorPoint(ccp(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pDisabledImage)
|
|
|
|
{
|
|
|
|
removeChild(m_pDisabledImage, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pDisabledImage = pImage;
|
|
|
|
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
|
|
|
//
|
|
|
|
//CCMenuItemSprite - CCRGBAProtocol protocol
|
|
|
|
//
|
|
|
|
void CCMenuItemSprite::setOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pNormalImage)->setOpacity(opacity);
|
|
|
|
|
|
|
|
if (m_pSelectedImage)
|
|
|
|
{
|
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pSelectedImage)->setOpacity(opacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pDisabledImage)
|
|
|
|
{
|
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pDisabledImage)->setOpacity(opacity);
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCMenuItemSprite::setColor(const ccColor3B& color)
|
|
|
|
{
|
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pNormalImage)->setColor(color);
|
|
|
|
|
|
|
|
if (m_pSelectedImage)
|
|
|
|
{
|
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pSelectedImage)->setColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pDisabledImage)
|
|
|
|
{
|
|
|
|
dynamic_cast<CCRGBAProtocol*>(m_pDisabledImage)->setColor(color);
|
|
|
|
}
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
GLubyte CCMenuItemSprite::getOpacity()
|
|
|
|
{
|
|
|
|
return dynamic_cast<CCRGBAProtocol*>(m_pNormalImage)->getOpacity();
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
const ccColor3B& CCMenuItemSprite::getColor()
|
|
|
|
{
|
|
|
|
return dynamic_cast<CCRGBAProtocol*>(m_pNormalImage)->getColor();
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemSprite * CCMenuItemSprite::itemWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
|
|
|
|
{
|
|
|
|
return CCMenuItemSprite::create(normalSprite, selectedSprite, disabledSprite);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemSprite * CCMenuItemSprite::create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
return CCMenuItemSprite::create(normalSprite, selectedSprite, disabledSprite, NULL, NULL);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemSprite * CCMenuItemSprite::itemWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
return CCMenuItemSprite::create(normalSprite, selectedSprite, target, selector);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemSprite * CCMenuItemSprite::create(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
return CCMenuItemSprite::create(normalSprite, selectedSprite, NULL, target, selector);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemSprite * CCMenuItemSprite::itemWithNormalSprite(CCNode *normalSprite, CCNode *selectedSprite, CCNode *disabledSprite, CCObject *target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
return CCMenuItemSprite::create(normalSprite, selectedSprite, disabledSprite, target, selector);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemSprite * CCMenuItemSprite::create(CCNode *normalSprite, CCNode *selectedSprite, CCNode *disabledSprite, CCObject *target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemSprite *pRet = new CCMenuItemSprite();
|
|
|
|
pRet->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
bool CCMenuItemSprite::initWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
CCMenuItem::initWithTarget(target, selector);
|
|
|
|
setNormalImage(normalSprite);
|
|
|
|
setSelectedImage(selectedSprite);
|
|
|
|
setDisabledImage(disabledSprite);
|
2012-06-01 08:04:53 +08:00
|
|
|
|
2012-06-12 02:50:32 +08:00
|
|
|
if(m_pNormalImage)
|
|
|
|
{
|
|
|
|
this->setContentSize(m_pNormalImage->getContentSize());
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
@since v0.99.5
|
|
|
|
*/
|
|
|
|
void CCMenuItemSprite::selected()
|
|
|
|
{
|
|
|
|
CCMenuItem::selected();
|
2012-06-12 02:50:32 +08:00
|
|
|
|
2012-06-01 08:04:53 +08:00
|
|
|
if (m_pNormalImage)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-06-01 08:04:53 +08:00
|
|
|
if (m_pDisabledImage)
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
m_pDisabledImage->setVisible(false);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pSelectedImage)
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
m_pNormalImage->setVisible(false);
|
|
|
|
m_pSelectedImage->setVisible(true);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
m_pNormalImage->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
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItemSprite::unselected()
|
|
|
|
{
|
|
|
|
CCMenuItem::unselected();
|
2012-06-01 08:04:53 +08:00
|
|
|
if (m_pNormalImage)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
m_pNormalImage->setVisible(true);
|
2012-06-01 08:04:53 +08:00
|
|
|
|
|
|
|
if (m_pSelectedImage)
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
m_pSelectedImage->setVisible(false);
|
2012-06-01 08:04:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_pDisabledImage)
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
m_pDisabledImage->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
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItemSprite::setIsEnabled(bool bEnabled)
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
if( m_bIsEnabled != bEnabled )
|
|
|
|
{
|
|
|
|
CCMenuItem::setIsEnabled(bEnabled);
|
|
|
|
this->updateImagesVisibility();
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper
|
|
|
|
void CCMenuItemSprite::updateImagesVisibility()
|
|
|
|
{
|
|
|
|
if (m_bIsEnabled)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
if (m_pNormalImage) m_pNormalImage->setVisible(true);
|
|
|
|
if (m_pSelectedImage) m_pSelectedImage->setVisible(false);
|
|
|
|
if (m_pDisabledImage) m_pDisabledImage->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
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
if (m_pDisabledImage)
|
2012-02-01 16:45:23 +08:00
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
if (m_pNormalImage) m_pNormalImage->setVisible(false);
|
|
|
|
if (m_pSelectedImage) m_pSelectedImage->setVisible(false);
|
|
|
|
if (m_pDisabledImage) m_pDisabledImage->setVisible(true);
|
2012-02-01 16:45:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
if (m_pNormalImage) m_pNormalImage->setVisible(true);
|
|
|
|
if (m_pSelectedImage) m_pSelectedImage->setVisible(false);
|
|
|
|
if (m_pDisabledImage) m_pDisabledImage->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-06-01 08:04:53 +08:00
|
|
|
CCMenuItemImage* CCMenuItemImage::node()
|
2012-06-14 18:32:44 +08:00
|
|
|
{
|
|
|
|
return CCMenuItemImage::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCMenuItemImage* CCMenuItemImage::create()
|
2012-06-01 08:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemImage *pRet = new CCMenuItemImage();
|
|
|
|
if (pRet && pRet->init())
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCMenuItemImage::init(void)
|
|
|
|
{
|
|
|
|
return initWithNormalImage(NULL, NULL, NULL, NULL, NULL);
|
|
|
|
}
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemImage * CCMenuItemImage::itemWithNormalImage(const char *normalImage, const char *selectedImage)
|
|
|
|
{
|
|
|
|
return CCMenuItemImage::create(normalImage, selectedImage);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemImage * CCMenuItemImage::create(const char *normalImage, const char *selectedImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
return CCMenuItemImage::create(normalImage, selectedImage, NULL, NULL, NULL);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemImage * CCMenuItemImage::itemWithNormalImage(const char *normalImage, const char *selectedImage, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
return CCMenuItemImage::create(normalImage, selectedImage, target, selector);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemImage * CCMenuItemImage::create(const char *normalImage, const char *selectedImage, CCObject* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
return CCMenuItemImage::create(normalImage, selectedImage, NULL, target, selector);
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
2012-06-08 14:50:41 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemImage * CCMenuItemImage::itemWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
|
|
|
return CCMenuItemImage::create(normalImage, selectedImage, disabledImage, target, selector);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemImage * CCMenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemImage *pRet = new CCMenuItemImage();
|
|
|
|
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
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemImage * CCMenuItemImage::itemWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage)
|
|
|
|
{
|
|
|
|
return CCMenuItemImage::create(normalImage, selectedImage, disabledImage);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemImage * CCMenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemImage *pRet = new CCMenuItemImage();
|
|
|
|
if (pRet && pRet->initWithNormalImage(normalImage, selectedImage, disabledImage, NULL, NULL))
|
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
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
bool CCMenuItemImage::initWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector)
|
|
|
|
{
|
2012-06-01 08:04:53 +08:00
|
|
|
CCNode *normalSprite = NULL;
|
2012-03-20 15:04:53 +08:00
|
|
|
CCNode *selectedSprite = NULL;
|
|
|
|
CCNode *disabledSprite = NULL;
|
2012-06-01 08:04:53 +08:00
|
|
|
|
|
|
|
if (normalImage)
|
|
|
|
{
|
2012-06-14 17:36:31 +08:00
|
|
|
normalSprite = CCSprite::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
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
selectedSprite = CCSprite::create(selectedImage);
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
if(disabledImage)
|
2012-02-02 10:39:36 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
disabledSprite = CCSprite::create(disabledImage);
|
2012-02-02 10:39:36 +08:00
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Setter of sprite frames
|
|
|
|
//
|
|
|
|
void CCMenuItemImage::setNormalSpriteFrame(CCSpriteFrame * frame)
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
setNormalImage(CCSprite::createWithSpriteFrame(frame));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItemImage::setSelectedSpriteFrame(CCSpriteFrame * frame)
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
setSelectedImage(CCSprite::createWithSpriteFrame(frame));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItemImage::setDisabledSpriteFrame(CCSpriteFrame * frame)
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
setDisabledImage(CCSprite::createWithSpriteFrame(frame));
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// MenuItemToggle
|
|
|
|
//
|
|
|
|
void CCMenuItemToggle::setSubItems(CCArray* var)
|
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(var);
|
|
|
|
CC_SAFE_RELEASE(m_pSubItems);
|
|
|
|
m_pSubItems = var;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCArray* CCMenuItemToggle::getSubItems()
|
|
|
|
{
|
|
|
|
return m_pSubItems;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemToggle * CCMenuItemToggle::itemWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, item);
|
|
|
|
CCMenuItemToggle *pRet = new CCMenuItemToggle();
|
|
|
|
pRet->initWithTarget(target, selector, item, args);
|
|
|
|
pRet->autorelease();
|
|
|
|
va_end(args);
|
|
|
|
return pRet;
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemToggle * CCMenuItemToggle::create(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, ...)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, item);
|
|
|
|
CCMenuItemToggle *pRet = new CCMenuItemToggle();
|
|
|
|
pRet->initWithTarget(target, selector, item, args);
|
|
|
|
pRet->autorelease();
|
|
|
|
va_end(args);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCMenuItemToggle::initWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, va_list args)
|
|
|
|
{
|
|
|
|
CCMenuItem::initWithTarget(target, selector);
|
2012-06-14 16:05:58 +08:00
|
|
|
this->m_pSubItems = CCArray::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
this->m_pSubItems->retain();
|
2012-03-20 15:04:53 +08:00
|
|
|
int z = 0;
|
|
|
|
CCMenuItem *i = item;
|
|
|
|
while(i)
|
|
|
|
{
|
|
|
|
z++;
|
|
|
|
m_pSubItems->addObject(i);
|
|
|
|
i = va_arg(args, CCMenuItem*);
|
|
|
|
}
|
|
|
|
m_uSelectedIndex = UINT_MAX;
|
|
|
|
this->setSelectedIndex(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCMenuItemToggle* CCMenuItemToggle::itemWithItem(CCMenuItem *item)
|
|
|
|
{
|
|
|
|
return CCMenuItemToggle::create(item);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCMenuItemToggle* CCMenuItemToggle::create(CCMenuItem *item)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
CCMenuItemToggle *pRet = new CCMenuItemToggle();
|
|
|
|
pRet->initWithItem(item);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCMenuItemToggle::initWithItem(CCMenuItem *item)
|
|
|
|
{
|
|
|
|
CCMenuItem::initWithTarget(NULL, NULL);
|
2012-06-14 16:05:58 +08:00
|
|
|
this->m_pSubItems = CCArray::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
this->m_pSubItems->retain();
|
2012-03-20 15:04:53 +08:00
|
|
|
m_pSubItems->addObject(item);
|
|
|
|
m_uSelectedIndex = UINT_MAX;
|
|
|
|
this->setSelectedIndex(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenuItemToggle::addSubItem(CCMenuItem *item)
|
|
|
|
{
|
|
|
|
m_pSubItems->addObject(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
CCMenuItemToggle::~CCMenuItemToggle()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(m_pSubItems);
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::setSelectedIndex(unsigned int index)
|
|
|
|
{
|
|
|
|
if( index != m_uSelectedIndex )
|
|
|
|
{
|
|
|
|
m_uSelectedIndex = index;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCMenuItem *currentItem = (CCMenuItem*)getChildByTag(kCurrentItem);
|
|
|
|
if( currentItem )
|
|
|
|
{
|
|
|
|
currentItem->removeFromParentAndCleanup(false);
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
|
|
CCMenuItem* item = (CCMenuItem*)m_pSubItems->objectAtIndex(m_uSelectedIndex);
|
|
|
|
this->addChild(item, 0, kCurrentItem);
|
|
|
|
const CCSize& s = item->getContentSize();
|
|
|
|
this->setContentSize(s);
|
|
|
|
item->setPosition( ccp( s.width/2, s.height/2 ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned int CCMenuItemToggle::getSelectedIndex()
|
|
|
|
{
|
|
|
|
return m_uSelectedIndex;
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::selected()
|
|
|
|
{
|
|
|
|
CCMenuItem::selected();
|
|
|
|
((CCMenuItem*)(m_pSubItems->objectAtIndex(m_uSelectedIndex)))->selected();
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::unselected()
|
|
|
|
{
|
|
|
|
CCMenuItem::unselected();
|
|
|
|
((CCMenuItem*)(m_pSubItems->objectAtIndex(m_uSelectedIndex)))->unselected();
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::activate()
|
|
|
|
{
|
|
|
|
// update index
|
|
|
|
if( m_bIsEnabled )
|
|
|
|
{
|
|
|
|
unsigned int newIndex = (m_uSelectedIndex + 1) % m_pSubItems->count();
|
|
|
|
this->setSelectedIndex(newIndex);
|
|
|
|
}
|
|
|
|
CCMenuItem::activate();
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::setIsEnabled(bool enabled)
|
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
if (m_bIsEnabled != enabled)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
CCMenuItem::setIsEnabled(enabled);
|
|
|
|
|
|
|
|
if(m_pSubItems && m_pSubItems->count() > 0)
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
2012-06-08 14:50:41 +08:00
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pSubItems, pObj)
|
|
|
|
{
|
|
|
|
CCMenuItem* pItem = (CCMenuItem*)pObj;
|
|
|
|
pItem->setIsEnabled(enabled);
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCMenuItem* CCMenuItemToggle::selectedItem()
|
|
|
|
{
|
|
|
|
return (CCMenuItem*)m_pSubItems->objectAtIndex(m_uSelectedIndex);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
//CCMenuItemToggle - CCRGBAProtocol protocol
|
|
|
|
//
|
|
|
|
GLubyte CCMenuItemToggle::getOpacity()
|
|
|
|
{
|
|
|
|
return m_cOpacity;
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::setOpacity(GLubyte opacity)
|
|
|
|
{
|
|
|
|
m_cOpacity = opacity;
|
|
|
|
if(m_pSubItems && m_pSubItems->count() > 0)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pSubItems, pObj)
|
|
|
|
{
|
|
|
|
CCMenuItem* pItem = (CCMenuItem*)pObj;
|
2012-03-20 15:04:53 +08:00
|
|
|
dynamic_cast<CCRGBAProtocol*>(pItem)->setOpacity(opacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const ccColor3B& CCMenuItemToggle::getColor()
|
|
|
|
{
|
|
|
|
return m_tColor;
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::setColor(const ccColor3B& color)
|
|
|
|
{
|
|
|
|
m_tColor = color;
|
|
|
|
if(m_pSubItems && m_pSubItems->count() > 0)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pSubItems, pObj)
|
|
|
|
{
|
|
|
|
CCMenuItem* pItem = (CCMenuItem*)pObj;
|
2012-03-20 15:04:53 +08:00
|
|
|
dynamic_cast<CCRGBAProtocol*>(pItem)->setColor(color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|