2010-08-10 12:49:16 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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"
|
2010-08-25 10:19:20 +08:00
|
|
|
#include "CGPointExtension.h"
|
2010-12-22 15:15:04 +08:00
|
|
|
#include "CCActionInterval.h"
|
2010-08-10 12:49:16 +08:00
|
|
|
#include "CCSprite.h"
|
2010-08-13 17:15:50 +08:00
|
|
|
#include "CCLabelAtlas.h"
|
2010-12-24 14:00:49 +08:00
|
|
|
#include "CCLabelTTF.h"
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-25 14:14:31 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
namespace cocos2d{
|
|
|
|
|
|
|
|
static int _fontSize = kItemSize;
|
|
|
|
static std::string _fontName = "Marker Felt";
|
|
|
|
static bool _fontNameRelease = false;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kCurrentItem = 0xc0c05001,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kZoomActionTag = 0xc0c05002,
|
|
|
|
};
|
|
|
|
//
|
|
|
|
// CCMenuItem
|
|
|
|
//
|
2010-11-17 16:31:48 +08:00
|
|
|
CCMenuItem * CCMenuItem::itemWithTarget(SelectorProtocol *rec, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
CCMenuItem *pRet = new CCMenuItem();
|
|
|
|
pRet->initWithTarget(rec, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
bool CCMenuItem::initWithTarget(SelectorProtocol *rec, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
m_tAnchorPoint = ccp(0.5f, 0.5f);
|
|
|
|
m_pListener = rec;
|
|
|
|
m_pfnSelector = selector;
|
|
|
|
m_bIsEnabled = true;
|
|
|
|
m_bIsSelected = false;
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
void CCMenuItem::selected()
|
|
|
|
{
|
|
|
|
m_bIsSelected = true;
|
|
|
|
}
|
|
|
|
void CCMenuItem::unselected()
|
|
|
|
{
|
|
|
|
m_bIsSelected = false;
|
|
|
|
}
|
|
|
|
void CCMenuItem::activate()
|
|
|
|
{
|
|
|
|
if (m_bIsEnabled && m_pListener)
|
|
|
|
{
|
|
|
|
(m_pListener->*m_pfnSelector)(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CCMenuItem::setIsEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
m_bIsEnabled = enabled;
|
|
|
|
}
|
|
|
|
bool CCMenuItem::getIsEnabled()
|
|
|
|
{
|
|
|
|
return m_bIsEnabled;
|
|
|
|
}
|
|
|
|
CGRect CCMenuItem::rect()
|
|
|
|
{
|
|
|
|
return CGRectMake( m_tPosition.x - m_tContentSize.width * m_tAnchorPoint.x,
|
|
|
|
m_tPosition.y - m_tContentSize.height * m_tAnchorPoint.y,
|
|
|
|
m_tContentSize.width, m_tContentSize.height);
|
|
|
|
}
|
|
|
|
bool CCMenuItem::getIsSelected()
|
|
|
|
{
|
|
|
|
return m_bIsSelected;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
//CCMenuItemLabel
|
|
|
|
//
|
|
|
|
ccColor3B CCMenuItemLabel::getDisabledColor()
|
|
|
|
{
|
|
|
|
return m_tDisabledColor;
|
|
|
|
}
|
|
|
|
void CCMenuItemLabel::setDisabledColor(ccColor3B var)
|
|
|
|
{
|
|
|
|
m_tDisabledColor = var;
|
|
|
|
}
|
2010-09-01 16:23:30 +08:00
|
|
|
CCNode *CCMenuItemLabel::getLabel()
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
return m_pLabel;
|
|
|
|
}
|
2010-09-01 16:23:30 +08:00
|
|
|
void CCMenuItemLabel::setLabel(CCNode* var)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-09-27 18:19:10 +08:00
|
|
|
CCX_SAFE_RETAIN(var);
|
2010-08-13 16:55:46 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pLabel);
|
2010-08-10 12:49:16 +08:00
|
|
|
m_pLabel = var;
|
|
|
|
this->setContentSize(m_pLabel->getContentSize());
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
CCMenuItemLabel * CCMenuItemLabel::itemWithLabel(CCNode*label, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
CCMenuItemLabel *pRet = new CCMenuItemLabel();
|
|
|
|
pRet->initWithLabel(label, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
bool CCMenuItemLabel::initWithLabel(CCNode* label, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::initWithTarget(target, selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
m_fOriginalScale = 1.0f;
|
|
|
|
m_tColorBackup = ccWHITE;
|
|
|
|
m_tDisabledColor = ccc3(126,126,126);
|
2010-09-01 16:23:30 +08:00
|
|
|
this->setLabel(label);
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
CCMenuItemLabel::~CCMenuItemLabel()
|
|
|
|
{
|
|
|
|
CCX_SAFE_RELEASE(m_pLabel);
|
|
|
|
}
|
|
|
|
void CCMenuItemLabel::setString(const char * label)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
m_pLabel->convertToLabelProtocol()->setString(label);
|
2010-08-13 16:55:46 +08:00
|
|
|
this->setContentSize(m_pLabel->getContentSize());
|
|
|
|
// [label_ setString:string];
|
|
|
|
// [self setContentSize: [label_ contentSize]];
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
void CCMenuItemLabel::activate()
|
|
|
|
{
|
|
|
|
if(m_bIsEnabled)
|
|
|
|
{
|
|
|
|
this->stopAllActions();
|
2010-08-19 15:29:38 +08:00
|
|
|
this->setScale( m_fOriginalScale );
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::activate();
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void CCMenuItemLabel::selected()
|
|
|
|
{
|
|
|
|
// subclass to change the default action
|
|
|
|
if(m_bIsEnabled)
|
|
|
|
{
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::selected();
|
2010-08-10 12:49:16 +08:00
|
|
|
this->stopActionByTag(kZoomActionTag);
|
2010-08-19 15:29:38 +08:00
|
|
|
m_fOriginalScale = this->getScale();
|
2010-08-10 12:49:16 +08:00
|
|
|
CCAction *zoomAction = CCScaleTo::actionWithDuration(0.1f, m_fOriginalScale * 1.2f);
|
|
|
|
zoomAction->setTag(kZoomActionTag);
|
|
|
|
this->runAction(zoomAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CCMenuItemLabel::unselected()
|
|
|
|
{
|
|
|
|
// subclass to change the default action
|
|
|
|
if(m_bIsEnabled)
|
|
|
|
{
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::unselected();
|
2010-08-10 12:49:16 +08:00
|
|
|
this->stopActionByTag(kZoomActionTag);
|
|
|
|
CCAction *zoomAction = CCScaleTo::actionWithDuration(0.1f, m_fOriginalScale);
|
|
|
|
zoomAction->setTag(kZoomActionTag);
|
|
|
|
this->runAction(zoomAction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void CCMenuItemLabel::setIsEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if( m_bIsEnabled != enabled )
|
|
|
|
{
|
|
|
|
if(enabled == false)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
m_tColorBackup = m_pLabel->convertToRGBAProtocol()->getColor();
|
|
|
|
m_pLabel->convertToRGBAProtocol()->setColor(m_tDisabledColor);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
m_pLabel->convertToRGBAProtocol()->setColor(m_tColorBackup);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::setIsEnabled(enabled);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
void CCMenuItemLabel::draw()
|
|
|
|
{
|
|
|
|
m_pLabel->draw();
|
|
|
|
}
|
|
|
|
void CCMenuItemLabel::setOpacity(GLubyte opacity)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
m_pLabel->convertToRGBAProtocol()->setOpacity(opacity);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
GLubyte CCMenuItemLabel::getOpacity()
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
return m_pLabel->convertToRGBAProtocol()->getOpacity();
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
void CCMenuItemLabel::setColor(ccColor3B color)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
m_pLabel->convertToRGBAProtocol()->setColor(color);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
ccColor3B CCMenuItemLabel::getColor()
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
return m_pLabel->convertToRGBAProtocol()->getColor();
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//CCMenuItemAtlasFont
|
|
|
|
//
|
|
|
|
CCMenuItemAtlasFont * CCMenuItemAtlasFont::itemFromString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap)
|
|
|
|
{
|
|
|
|
return CCMenuItemAtlasFont::itemFromString(value, charMapFile, itemWidth, itemHeight, startCharMap, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2010-11-17 16:31:48 +08:00
|
|
|
CCMenuItemAtlasFont * CCMenuItemAtlasFont::itemFromString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
CCMenuItemAtlasFont *pRet = new CCMenuItemAtlasFont();
|
|
|
|
pRet->initFromString(value, charMapFile, itemWidth, itemHeight, startCharMap, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
bool CCMenuItemAtlasFont::initFromString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
NSAssert( value != NULL && strlen(value) != 0, "value lenght must be greater than 0");
|
2010-08-13 16:55:46 +08:00
|
|
|
CCLabelAtlas *label = new CCLabelAtlas();
|
|
|
|
label->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap);
|
|
|
|
label->autorelease();
|
2010-09-02 14:54:42 +08:00
|
|
|
if (CCMenuItemLabel::initWithLabel(label, target, selector))
|
2010-08-13 16:55:46 +08:00
|
|
|
{
|
|
|
|
// do something ?
|
|
|
|
}
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
//
|
|
|
|
//CCMenuItemFont
|
|
|
|
//
|
|
|
|
void CCMenuItemFont::setFontSize(int s)
|
|
|
|
{
|
|
|
|
_fontSize = s;
|
|
|
|
}
|
|
|
|
int CCMenuItemFont::fontSize()
|
|
|
|
{
|
|
|
|
return _fontSize;
|
|
|
|
}
|
|
|
|
void CCMenuItemFont::setFontName(const char *name)
|
|
|
|
{
|
|
|
|
if( _fontNameRelease )
|
2010-08-13 16:55:46 +08:00
|
|
|
{
|
2010-08-10 12:49:16 +08:00
|
|
|
_fontName.clear();
|
2010-08-13 16:55:46 +08:00
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
_fontName = name;
|
|
|
|
_fontNameRelease = true;
|
|
|
|
}
|
|
|
|
const char * CCMenuItemFont::fontName()
|
|
|
|
{
|
|
|
|
return _fontName.c_str();
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
CCMenuItemFont * CCMenuItemFont::itemFromString(const char *value, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
CCMenuItemFont *pRet = new CCMenuItemFont();
|
|
|
|
pRet->initFromString(value, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CCMenuItemFont * CCMenuItemFont::itemFromString(const char *value)
|
|
|
|
{
|
|
|
|
CCMenuItemFont *pRet = new CCMenuItemFont();
|
|
|
|
pRet->initFromString(value, NULL, NULL);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
bool CCMenuItemFont::initFromString(const char *value, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
NSAssert( value != NULL && strlen(value) != 0, "Value lenght must be greater than 0");
|
2010-08-13 17:15:50 +08:00
|
|
|
CCLabel *label = CCLabel::labelWithString(value, _fontName.c_str(), (float)_fontSize);
|
2010-09-02 14:54:42 +08:00
|
|
|
if (CCMenuItemLabel::initWithLabel(label, target, selector))
|
2010-08-13 16:55:46 +08:00
|
|
|
{
|
|
|
|
// do something ?
|
|
|
|
}
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
//
|
|
|
|
//CCMenuItemSprite
|
|
|
|
//
|
|
|
|
CCNode * CCMenuItemSprite::getNormalImage()
|
|
|
|
{
|
|
|
|
return m_pNormalImage;
|
|
|
|
}
|
|
|
|
void CCMenuItemSprite::setNormalImage(CCNode* var)
|
|
|
|
{
|
2010-09-27 18:19:10 +08:00
|
|
|
CCX_SAFE_RETAIN(var);
|
2010-09-06 13:52:57 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pNormalImage);
|
2010-08-10 12:49:16 +08:00
|
|
|
m_pNormalImage = var;
|
|
|
|
}
|
|
|
|
CCNode * CCMenuItemSprite::getSelectedImage()
|
|
|
|
{
|
|
|
|
return m_pSelectedImage;
|
|
|
|
}
|
|
|
|
void CCMenuItemSprite::setSelectedImage(CCNode* var)
|
|
|
|
{
|
2010-09-27 18:19:10 +08:00
|
|
|
CCX_SAFE_RETAIN(var);
|
2010-09-06 13:52:57 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pSelectedImage);
|
2010-08-10 12:49:16 +08:00
|
|
|
m_pSelectedImage = var;
|
|
|
|
}
|
|
|
|
CCNode * CCMenuItemSprite::getDisabledImage()
|
|
|
|
{
|
|
|
|
return m_pDisabledImage;
|
|
|
|
}
|
|
|
|
void CCMenuItemSprite::setDisabledImage(CCNode* var)
|
|
|
|
{
|
2010-09-27 18:19:10 +08:00
|
|
|
CCX_SAFE_RETAIN(var);
|
2010-09-06 13:52:57 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pDisabledImage);
|
2010-08-10 12:49:16 +08:00
|
|
|
m_pDisabledImage = var;
|
|
|
|
}
|
|
|
|
CCMenuItemSprite * CCMenuItemSprite::itemFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite)
|
|
|
|
{
|
|
|
|
return CCMenuItemSprite::itemFromNormalSprite(normalSprite, selectedSprite, NULL, NULL, NULL);
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
CCMenuItemSprite * CCMenuItemSprite::itemFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
return CCMenuItemSprite::itemFromNormalSprite(normalSprite, selectedSprite, NULL, target, selector);
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
CCMenuItemSprite * CCMenuItemSprite::itemFromNormalSprite(CCNode *normalSprite, CCNode *selectedSprite, CCNode *disabledSprite, SelectorProtocol *target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
CCMenuItemSprite *pRet = new CCMenuItemSprite();
|
|
|
|
pRet->initFromNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
bool CCMenuItemSprite::initFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-09-04 12:02:52 +08:00
|
|
|
assert(normalSprite != NULL);
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::initWithTarget(target, selector);
|
2010-09-03 18:00:09 +08:00
|
|
|
this->m_pNormalImage = normalSprite; CCX_SAFE_RETAIN(normalSprite);
|
|
|
|
this->m_pSelectedImage = selectedSprite; CCX_SAFE_RETAIN(selectedSprite);
|
|
|
|
this->m_pDisabledImage = disabledSprite; CCX_SAFE_RETAIN(disabledSprite);
|
2010-08-10 12:49:16 +08:00
|
|
|
this->setContentSize(m_pNormalImage->getContentSize());
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
CCMenuItemSprite::~CCMenuItemSprite()
|
|
|
|
{
|
2010-09-06 18:01:50 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pNormalImage);
|
|
|
|
CCX_SAFE_RELEASE(m_pSelectedImage);
|
|
|
|
CCX_SAFE_RELEASE(m_pDisabledImage);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
void CCMenuItemSprite::draw()
|
|
|
|
{
|
|
|
|
if(m_bIsEnabled)
|
|
|
|
{
|
|
|
|
if( m_bIsSelected )
|
|
|
|
{
|
|
|
|
m_pSelectedImage->draw();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pNormalImage->draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(m_pDisabledImage != NULL)
|
|
|
|
{
|
|
|
|
m_pDisabledImage->draw();
|
|
|
|
}
|
|
|
|
// disabled image was not provided
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_pNormalImage->draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
//CCMenuItemImage - CCRGBAProtocol protocol
|
|
|
|
//
|
|
|
|
void CCMenuItemImage::setOpacity(GLubyte opacity)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
m_pNormalImage->convertToRGBAProtocol()->setOpacity(opacity);
|
|
|
|
m_pSelectedImage->convertToRGBAProtocol()->setOpacity(opacity);
|
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
if (m_pDisabledImage)
|
2010-08-31 11:20:37 +08:00
|
|
|
{
|
|
|
|
m_pDisabledImage->convertToRGBAProtocol()->setOpacity(opacity);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void CCMenuItemImage::setColor(ccColor3B color)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
m_pNormalImage->convertToRGBAProtocol()->setColor(color);
|
|
|
|
m_pSelectedImage->convertToRGBAProtocol()->setColor(color);
|
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
if (m_pDisabledImage)
|
2010-08-31 11:20:37 +08:00
|
|
|
{
|
|
|
|
m_pDisabledImage->convertToRGBAProtocol()->setColor(color);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
GLubyte CCMenuItemImage::getOpacity()
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
return m_pNormalImage->convertToRGBAProtocol()->getOpacity();
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
ccColor3B CCMenuItemImage::getColor()
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
return m_pNormalImage->convertToRGBAProtocol()->getColor();
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
CCMenuItemImage * CCMenuItemImage::itemFromNormalImage(const char *normalImage, const char *selectedImage)
|
|
|
|
{
|
|
|
|
return CCMenuItemImage::itemFromNormalImage(normalImage, selectedImage, NULL, NULL, NULL);
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
CCMenuItemImage * CCMenuItemImage::itemFromNormalImage(const char *normalImage, const char *selectedImage, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
return CCMenuItemImage::itemFromNormalImage(normalImage, selectedImage, NULL, target, selector);
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
CCMenuItemImage * CCMenuItemImage::itemFromNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
CCMenuItemImage *pRet = new CCMenuItemImage();
|
2010-09-04 12:02:52 +08:00
|
|
|
if (pRet && pRet->initFromNormalImage(normalImage, selectedImage, disabledImage, target, selector))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CCX_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
CCMenuItemImage * CCMenuItemImage::itemFromNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage)
|
|
|
|
{
|
|
|
|
CCMenuItemImage *pRet = new CCMenuItemImage();
|
2010-09-04 12:02:52 +08:00
|
|
|
if (pRet && pRet->initFromNormalImage(normalImage, selectedImage, disabledImage, NULL, NULL))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CCX_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
bool CCMenuItemImage::initFromNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, SelectorProtocol* target, SEL_MenuHandler selector)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
CCNode *normalSprite = CCSprite::spriteWithFile(normalImage);
|
|
|
|
CCNode *selectedSprite = CCSprite::spriteWithFile(selectedImage);
|
|
|
|
CCNode *disabledSprite = NULL;
|
|
|
|
|
|
|
|
if(disabledImage)
|
2010-09-04 12:02:52 +08:00
|
|
|
{
|
2010-08-10 12:49:16 +08:00
|
|
|
disabledSprite = CCSprite::spriteWithFile(disabledImage);
|
2010-09-04 12:02:52 +08:00
|
|
|
}
|
|
|
|
return initFromNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// MenuItemToggle
|
|
|
|
//
|
|
|
|
void CCMenuItemToggle::setSubItems(NSMutableArray<CCMenuItem*>* var)
|
|
|
|
{
|
2010-09-27 18:19:10 +08:00
|
|
|
CCX_SAFE_RETAIN(var);
|
2010-09-06 13:52:57 +08:00
|
|
|
CCX_SAFE_RELEASE(m_pSubItems);
|
2010-08-10 12:49:16 +08:00
|
|
|
m_pSubItems = var;
|
|
|
|
}
|
|
|
|
NSMutableArray<CCMenuItem*> *CCMenuItemToggle::getSubItems()
|
|
|
|
{
|
|
|
|
return m_pSubItems;
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
CCMenuItemToggle * CCMenuItemToggle::itemWithTarget(SelectorProtocol* target, SEL_MenuHandler selector, CCMenuItem* item, ...)
|
2010-08-10 12:49:16 +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;
|
|
|
|
}
|
2010-11-17 16:31:48 +08:00
|
|
|
bool CCMenuItemToggle::initWithTarget(SelectorProtocol* target, SEL_MenuHandler selector, CCMenuItem* item, va_list args)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::initWithTarget(target, selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
this->m_pSubItems = new NSMutableArray<CCMenuItem*>();
|
|
|
|
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);
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
CCMenuItemToggle::~CCMenuItemToggle()
|
|
|
|
{
|
|
|
|
m_pSubItems->release();
|
|
|
|
}
|
2010-08-25 14:14:31 +08:00
|
|
|
void CCMenuItemToggle::setSelectedIndex(unsigned int index)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
if( index != m_uSelectedIndex )
|
|
|
|
{
|
|
|
|
m_uSelectedIndex = index;
|
|
|
|
this->removeChildByTag(kCurrentItem, false);
|
|
|
|
CCMenuItem *item = m_pSubItems->getObjectAtIndex(m_uSelectedIndex);
|
|
|
|
this->addChild(item, 0, kCurrentItem);
|
|
|
|
CGSize s = item->getContentSize();
|
|
|
|
this->setContentSize(s);
|
|
|
|
item->setPosition( ccp( s.width/2, s.height/2 ) );
|
|
|
|
}
|
|
|
|
}
|
2010-08-25 14:14:31 +08:00
|
|
|
unsigned int CCMenuItemToggle::getSelectedIndex()
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
return m_uSelectedIndex;
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::selected()
|
|
|
|
{
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::selected();
|
2010-08-10 12:49:16 +08:00
|
|
|
m_pSubItems->getObjectAtIndex(m_uSelectedIndex)->selected();
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::unselected()
|
|
|
|
{
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::unselected();
|
2010-08-10 12:49:16 +08:00
|
|
|
m_pSubItems->getObjectAtIndex(m_uSelectedIndex)->unselected();
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::activate()
|
|
|
|
{
|
|
|
|
// update index
|
|
|
|
if( m_bIsEnabled )
|
|
|
|
{
|
2010-08-25 14:14:31 +08:00
|
|
|
unsigned int newIndex = (m_uSelectedIndex + 1) % m_pSubItems->count();
|
2010-08-10 12:49:16 +08:00
|
|
|
this->setSelectedIndex(newIndex);
|
|
|
|
}
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::activate();
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
void CCMenuItemToggle::setIsEnabled(bool enabled)
|
|
|
|
{
|
2010-09-02 14:54:42 +08:00
|
|
|
CCMenuItem::setIsEnabled(enabled);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
|
|
|
if(m_pSubItems && m_pSubItems->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCMenuItem*>::NSMutableArrayIterator it;
|
|
|
|
for( it = m_pSubItems->begin(); it != m_pSubItems->end(); ++it)
|
|
|
|
{
|
|
|
|
(*it)->setIsEnabled(enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CCMenuItem * CCMenuItemToggle::selectedItem()
|
|
|
|
{
|
|
|
|
return m_pSubItems->getObjectAtIndex(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)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCMenuItem*>::NSMutableArrayIterator it;
|
|
|
|
for( it = m_pSubItems->begin(); it != m_pSubItems->end(); ++it)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
(*it)->convertToRGBAProtocol()->setOpacity(opacity);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ccColor3B CCMenuItemToggle::getColor()
|
|
|
|
{
|
|
|
|
return m_tColor;
|
|
|
|
}
|
|
|
|
void CCMenuItemToggle::setColor(ccColor3B color)
|
|
|
|
{
|
|
|
|
m_tColor = color;
|
|
|
|
if(m_pSubItems && m_pSubItems->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCMenuItem*>::NSMutableArrayIterator it;
|
|
|
|
for( it = m_pSubItems->begin(); it != m_pSubItems->end(); ++it)
|
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
(*it)->convertToRGBAProtocol()->setColor(color);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cocos2d
|