Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@haxpor
Last active December 21, 2015 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haxpor/6282289 to your computer and use it in GitHub Desktop.
Save haxpor/6282289 to your computer and use it in GitHub Desktop.
Added holding touch functionality to CCMenuItem, thus related to CCMenu. It can return time passed since the previous holding touch. Holding touch functionality can be turned on/off on the fly during run-time. The benefit is that we can use CCMenu with holding touch functionality without a need to implement on our own in external checking inside…
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
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 "CCMenu.h"
#include "CCDirector.h"
#include "CCApplication.h"
#include "support/CCPointExtension.h"
#include "touch_dispatcher/CCTouchDispatcher.h"
#include "touch_dispatcher/CCTouch.h"
#include "CCStdC.h"
#include <vector>
#include <stdarg.h>
using namespace std;
NS_CC_BEGIN
enum
{
kDefaultPadding = 5,
};
//
//CCMenu
//
CCMenu* CCMenu::node()
{
return CCMenu::create();
}
CCMenu* CCMenu::create()
{
return CCMenu::create(NULL, NULL);
}
CCMenu * CCMenu::menuWithItems(CCMenuItem* item, ...)
{
va_list args;
va_start(args,item);
CCMenu *pRet = new CCMenu();
if (pRet && pRet->initWithItems(item, args))
{
pRet->autorelease();
va_end(args);
return pRet;
}
va_end(args);
CC_SAFE_DELETE(pRet);
return NULL;
}
CCMenu * CCMenu::create(CCMenuItem* item, ...)
{
va_list args;
va_start(args,item);
CCMenu *pRet = new CCMenu();
if (pRet && pRet->initWithItems(item, args))
{
pRet->autorelease();
va_end(args);
return pRet;
}
va_end(args);
CC_SAFE_DELETE(pRet);
return NULL;
}
CCMenu* CCMenu::menuWithArray(CCArray* pArrayOfItems)
{
return CCMenu::createWithArray(pArrayOfItems);
}
CCMenu* CCMenu::createWithArray(CCArray* pArrayOfItems)
{
CCMenu *pRet = new CCMenu();
if (pRet && pRet->initWithArray(pArrayOfItems))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
}
return pRet;
}
CCMenu* CCMenu::menuWithItem(CCMenuItem* item)
{
return CCMenu::createWithItem(item);
}
CCMenu* CCMenu::createWithItem(CCMenuItem* item)
{
return CCMenu::create(item, NULL);
}
bool CCMenu::init()
{
return initWithArray(NULL);
}
bool CCMenu::initWithItems(CCMenuItem* item, va_list args)
{
CCArray* pArray = NULL;
if( item )
{
pArray = CCArray::create(item, NULL);
CCMenuItem *i = va_arg(args, CCMenuItem*);
while(i)
{
pArray->addObject(i);
i = va_arg(args, CCMenuItem*);
}
}
return initWithArray(pArray);
}
bool CCMenu::initWithArray(CCArray* pArrayOfItems)
{
if (CCLayer::init())
{
setTouchEnabled(true);
m_bEnabled = true;
// menu in the center of the screen
CCSize s = CCDirector::sharedDirector()->getWinSize();
this->ignoreAnchorPointForPosition(true);
setAnchorPoint(ccp(0.5f, 0.5f));
this->setContentSize(s);
setPosition(ccp(s.width/2, s.height/2));
if (pArrayOfItems != NULL)
{
int z=0;
CCObject* pObj = NULL;
CCARRAY_FOREACH(pArrayOfItems, pObj)
{
CCMenuItem* item = (CCMenuItem*)pObj;
this->addChild(item, z);
z++;
}
}
// [self alignItemsVertically];
m_pSelectedItem = NULL;
m_eState = kCCMenuStateWaiting;
return true;
}
return false;
}
/*
* override add:
*/
void CCMenu::addChild(CCNode * child)
{
CCLayer::addChild(child);
}
void CCMenu::addChild(CCNode * child, int zOrder)
{
CCLayer::addChild(child, zOrder);
}
void CCMenu::addChild(CCNode * child, int zOrder, int tag)
{
CCAssert( dynamic_cast<CCMenuItem*>(child) != NULL, "Menu only supports MenuItem objects as children");
CCLayer::addChild(child, zOrder, tag);
}
void CCMenu::onExit()
{
if (m_eState == kCCMenuStateTrackingTouch)
{
m_pSelectedItem->unselected();
m_eState = kCCMenuStateWaiting;
m_pSelectedItem = NULL;
}
CCLayer::onExit();
}
//Menu - Events
void CCMenu::setHandlerPriority(int newPriority)
{
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
pDispatcher->setPriority(newPriority, this);
}
void CCMenu::registerWithTouchDispatcher()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority, true);
}
bool CCMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
CC_UNUSED_PARAM(event);
if (m_eState != kCCMenuStateWaiting || ! m_bIsVisible || !m_bEnabled)
{
return false;
}
for (CCNode *c = this->m_pParent; c != NULL; c = c->getParent())
{
if (c->isVisible() == false)
{
return false;
}
}
m_pSelectedItem = this->itemForTouch(touch);
if (m_pSelectedItem)
{
m_eState = kCCMenuStateTrackingTouch;
m_pSelectedItem->selected();
if(m_isActivatedByPressEvent)
m_pSelectedItem->activate();
// track holding (if enabled)
if(m_pSelectedItem->isHoldingEnabled())
{
m_pSelectedItem->setHolding(true);
m_pSelectedItem->resetTimePassedSincePreviousHolding();
}
return true;
}
return false;
}
void CCMenu::ccTouchEnded(CCTouch *touch, CCEvent* event)
{
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");
if (m_pSelectedItem)
{
// stop tracking holding-event (if enabled)
if(m_pSelectedItem->isHoldingEnabled())
// reset holding flag, but left time passed intact to allow others to use its information
m_pSelectedItem->setHolding(false);
m_pSelectedItem->unselected();
if(!m_isActivatedByPressEvent)
m_pSelectedItem->activate();
}
m_eState = kCCMenuStateWaiting;
}
void CCMenu::ccTouchCancelled(CCTouch *touch, CCEvent* event)
{
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchCancelled] -- invalid state");
if (m_pSelectedItem)
{
// cancel holding (if enabled)
if(m_pSelectedItem->isHoldingEnabled())
{
m_pSelectedItem->setHolding(false);
m_pSelectedItem->resetTimePassedSincePreviousHolding();
}
m_pSelectedItem->unselected();
}
m_eState = kCCMenuStateWaiting;
}
void CCMenu::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchMoved] -- invalid state");
CCMenuItem *currentItem = this->itemForTouch(touch);
if (currentItem != m_pSelectedItem)
{
if (m_pSelectedItem)
{
// reset holding (if enabled)
if(m_pSelectedItem->isHoldingEnabled())
m_pSelectedItem->setHolding(false);
m_pSelectedItem->unselected();
}
m_pSelectedItem = currentItem;
if (m_pSelectedItem)
{
m_pSelectedItem->selected();
}
}
}
//Menu - Alignment
void CCMenu::alignItemsVertically()
{
this->alignItemsVerticallyWithPadding(kDefaultPadding);
}
void CCMenu::alignItemsVerticallyWithPadding(float padding)
{
float height = -padding;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
height += pChild->getContentSize().height * pChild->getScaleY() + padding;
}
}
}
float y = height / 2.0f;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
pChild->setPosition(ccp(0, y - pChild->getContentSize().height * pChild->getScaleY() / 2.0f));
y -= pChild->getContentSize().height * pChild->getScaleY() + padding;
}
}
}
}
void CCMenu::alignItemsHorizontally(void)
{
this->alignItemsHorizontallyWithPadding(kDefaultPadding);
}
void CCMenu::alignItemsHorizontallyWithPadding(float padding)
{
float width = -padding;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
width += pChild->getContentSize().width * pChild->getScaleX() + padding;
}
}
}
float x = -width / 2.0f;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
pChild->setPosition(ccp(x + pChild->getContentSize().width * pChild->getScaleX() / 2.0f, 0));
x += pChild->getContentSize().width * pChild->getScaleX() + padding;
}
}
}
}
void CCMenu::alignItemsInColumns(unsigned int columns, ...)
{
va_list args;
va_start(args, columns);
this->alignItemsInColumns(columns, args);
va_end(args);
}
void CCMenu::alignItemsInColumns(unsigned int columns, va_list args)
{
vector<unsigned int> rows;
while (columns)
{
rows.push_back(columns);
columns = va_arg(args, unsigned int);
}
int height = -5;
unsigned int row = 0;
unsigned int rowHeight = 0;
unsigned int columnsOccupied = 0;
unsigned int rowColumns;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
CCAssert(row < rows.size(), "");
rowColumns = rows[row];
// can not have zero columns on a row
CCAssert(rowColumns, "");
float tmp = pChild->getContentSize().height;
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
++columnsOccupied;
if (columnsOccupied >= rowColumns)
{
height += rowHeight + 5;
columnsOccupied = 0;
rowHeight = 0;
++row;
}
}
}
}
// check if too many rows/columns for available menu items
CCAssert(! columnsOccupied, "");
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
row = 0;
rowHeight = 0;
rowColumns = 0;
float w = 0.0;
float x = 0.0;
float y = (float)(height / 2);
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
if (rowColumns == 0)
{
rowColumns = rows[row];
w = winSize.width / (1 + rowColumns);
x = w;
}
float tmp = pChild->getContentSize().height;
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
pChild->setPosition(ccp(x - winSize.width / 2,
y - pChild->getContentSize().height / 2));
x += w;
++columnsOccupied;
if (columnsOccupied >= rowColumns)
{
y -= rowHeight + 5;
columnsOccupied = 0;
rowColumns = 0;
rowHeight = 0;
++row;
}
}
}
}
}
void CCMenu::alignItemsInRows(unsigned int rows, ...)
{
va_list args;
va_start(args, rows);
this->alignItemsInRows(rows, args);
va_end(args);
}
void CCMenu::alignItemsInRows(unsigned int rows, va_list args)
{
vector<unsigned int> columns;
while (rows)
{
columns.push_back(rows);
rows = va_arg(args, unsigned int);
}
vector<unsigned int> columnWidths;
vector<unsigned int> columnHeights;
int width = -10;
int columnHeight = -5;
unsigned int column = 0;
unsigned int columnWidth = 0;
unsigned int rowsOccupied = 0;
unsigned int columnRows;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
// check if too many menu items for the amount of rows/columns
CCAssert(column < columns.size(), "");
columnRows = columns[column];
// can't have zero rows on a column
CCAssert(columnRows, "");
// columnWidth = fmaxf(columnWidth, [item contentSize].width);
float tmp = pChild->getContentSize().width;
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
columnHeight += (int)(pChild->getContentSize().height + 5);
++rowsOccupied;
if (rowsOccupied >= columnRows)
{
columnWidths.push_back(columnWidth);
columnHeights.push_back(columnHeight);
width += columnWidth + 10;
rowsOccupied = 0;
columnWidth = 0;
columnHeight = -5;
++column;
}
}
}
}
// check if too many rows/columns for available menu items.
CCAssert(! rowsOccupied, "");
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
column = 0;
columnWidth = 0;
columnRows = 0;
float x = (float)(-width / 2);
float y = 0.0;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
if (columnRows == 0)
{
columnRows = columns[column];
y = (float) columnHeights[column];
}
// columnWidth = fmaxf(columnWidth, [item contentSize].width);
float tmp = pChild->getContentSize().width;
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
pChild->setPosition(ccp(x + columnWidths[column] / 2,
y - winSize.height / 2));
y -= pChild->getContentSize().height + 10;
++rowsOccupied;
if (rowsOccupied >= columnRows)
{
x += columnWidth + 5;
rowsOccupied = 0;
columnRows = 0;
columnWidth = 0;
++column;
}
}
}
}
}
// Opacity Protocol
/** Override synthesized setOpacity to recurse items */
void CCMenu::setOpacity(GLubyte var)
{
m_cOpacity = var;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(pChild);
if (pRGBAProtocol)
{
pRGBAProtocol->setOpacity(m_cOpacity);
}
}
}
}
}
GLubyte CCMenu::getOpacity(void)
{
return m_cOpacity;
}
void CCMenu::setColor(const ccColor3B& var)
{
m_tColor = var;
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild)
{
CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(pChild);
if (pRGBAProtocol)
{
pRGBAProtocol->setColor(m_tColor);
}
}
}
}
}
const ccColor3B& CCMenu::getColor(void)
{
return m_tColor;
}
CCMenuItem* CCMenu::itemForTouch(CCTouch *touch)
{
CCPoint touchLocation = touch->getLocation();
if (m_pChildren && m_pChildren->count() > 0)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
if (pChild && pChild->isVisible() && ((CCMenuItem*)pChild)->isEnabled())
{
CCPoint local = pChild->convertToNodeSpace(touchLocation);
CCRect r = ((CCMenuItem*)pChild)->rect();
r.origin = CCPointZero;
if (r.containsPoint(local))
{
return (CCMenuItem*)pChild;
}
}
}
}
return NULL;
}
NS_CC_END
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
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.
****************************************************************************/
#ifndef __CCMENU_H_
#define __CCMENU_H_
#include "CCMenuItem.h"
#include "layers_scenes_transitions_nodes/CCLayer.h"
NS_CC_BEGIN
/**
* @addtogroup GUI
* @{
* @addtogroup menu
* @{
*/
typedef enum
{
kCCMenuStateWaiting,
kCCMenuStateTrackingTouch
} tCCMenuState;
enum {
//* priority used by the menu for the event handler
kCCMenuHandlerPriority = -128,
};
/** @brief A CCMenu
*
* Features and Limitation:
* - You can add MenuItem objects in runtime using addChild:
* - But the only accepted children are MenuItem objects
*/
class CC_DLL CCMenu : public CCLayer, public CCRGBAProtocol
{
/** Color: conforms with CCRGBAProtocol protocol */
CC_PROPERTY_PASS_BY_REF(ccColor3B, m_tColor, Color);
/** Opacity: conforms with CCRGBAProtocol protocol */
CC_PROPERTY(GLubyte, m_cOpacity, Opacity);
/** whether or not the menu will receive events */
bool m_bEnabled;
public:
CCMenu()
: m_cOpacity(0)
, m_pSelectedItem(NULL)
, m_isActivatedByPressEvent(false)
{}
virtual ~CCMenu(){}
/** creates an empty CCMenu
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenu* node();
/** creates a CCMenu with it's items
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenu* menuWithItems(CCMenuItem* item, ...);
/** creates a CCMenu with a NSArray of CCMenuItem objects
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenu* menuWithArray(CCArray* pArrayOfItems);
/** creates a CCMenu with it's item, then use addChild() to add
* other items. It is used for script, it can't init with undetermined
* number of variables.
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenu* menuWithItem(CCMenuItem* item);
/** creates an empty CCMenu */
static CCMenu* create();
/** creates a CCMenu with it's items */
static CCMenu* create(CCMenuItem* item, ...);
/** creates a CCMenu with a CCArray of CCMenuItem objects */
static CCMenu* createWithArray(CCArray* pArrayOfItems);
/** creates a CCMenu with it's item, then use addChild() to add
* other items. It is used for script, it can't init with undetermined
* number of variables.
*/
static CCMenu* createWithItem(CCMenuItem* item);
/** initializes an empty CCMenu */
bool init();
/** initializes a CCMenu with it's items */
bool initWithItems(CCMenuItem* item, va_list args);
/** initializes a CCMenu with a NSArray of CCMenuItem objects */
bool initWithArray(CCArray* pArrayOfItems);
/** align items vertically */
void alignItemsVertically();
/** align items vertically with padding
@since v0.7.2
*/
void alignItemsVerticallyWithPadding(float padding);
/** align items horizontally */
void alignItemsHorizontally();
/** align items horizontally with padding
@since v0.7.2
*/
void alignItemsHorizontallyWithPadding(float padding);
/** align items in rows of columns */
void alignItemsInColumns(unsigned int columns, ...);
void alignItemsInColumns(unsigned int columns, va_list args);
/** align items in columns of rows */
void alignItemsInRows(unsigned int rows, ...);
void alignItemsInRows(unsigned int rows, va_list args);
/** set event handler priority. By default it is: kCCMenuTouchPriority */
void setHandlerPriority(int newPriority);
//super methods
virtual void addChild(CCNode * child);
virtual void addChild(CCNode * child, int zOrder);
virtual void addChild(CCNode * child, int zOrder, int tag);
virtual void registerWithTouchDispatcher();
/**
@brief For phone event handle functions
*/
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
virtual void ccTouchCancelled(CCTouch *touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
/**
@since v0.99.5
override onExit
*/
virtual void onExit();
virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
virtual bool isEnabled() { return m_bEnabled; }
virtual void setEnabled(bool value) { m_bEnabled = value; };
/* Set to true to activate receiving activation event via pressing. Otherwise, setting to false is the normal behavior receiving activation event via touch.
Default value is false. */
void setActivatedByPressEvent(bool b) { m_isActivatedByPressEvent = b; };
bool isActivatedByPressEvent() { return m_isActivatedByPressEvent; };
protected:
CCMenuItem* itemForTouch(CCTouch * touch);
tCCMenuState m_eState;
CCMenuItem *m_pSelectedItem;
bool m_isActivatedByPressEvent;
};
// end of GUI group
/// @}
/// @}
NS_CC_END
#endif//__CCMENU_H_
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
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 "CCMenuItem.h"
#include "support/CCPointExtension.h"
#include "actions/CCActionInterval.h"
#include "sprite_nodes/CCSprite.h"
#include "label_nodes/CCLabelAtlas.h"
#include "label_nodes/CCLabelTTF.h"
#include "script_support/CCScriptSupport.h"
#include <stdarg.h>
#include <cstring>
NS_CC_BEGIN
static unsigned int _fontSize = kCCItemSize;
static std::string _fontName = "Marker Felt";
static bool _fontNameRelease = false;
const unsigned int kCurrentItem = 0xc0c05001;
const unsigned int kZoomActionTag = 0xc0c05002;
const unsigned int kNormalTag = 0x1;
const unsigned int kSelectedTag = 0x2;
const unsigned int kDisableTag = 0x3;
//
// CCMenuItem
//
CCMenuItem* CCMenuItem::itemWithTarget(CCObject *rec, SEL_MenuHandler selector)
{
return CCMenuItem::create(rec, selector);
}
CCMenuItem* CCMenuItem::create()
{
return CCMenuItem::create(NULL, NULL);
}
CCMenuItem* CCMenuItem::create(CCObject *rec, SEL_MenuHandler selector)
{
CCMenuItem *pRet = new CCMenuItem();
pRet->initWithTarget(rec, selector);
pRet->autorelease();
return pRet;
}
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()
{
unregisterScriptTapHandler();
// only when it's enabled as of now
if(m_isHoldingEnabled)
unscheduleUpdate();
}
void CCMenuItem::selected()
{
m_bIsSelected = true;
}
void CCMenuItem::unselected()
{
m_bIsSelected = false;
}
void CCMenuItem::registerScriptTapHandler(int nHandler)
{
unregisterScriptTapHandler();
m_nScriptTapHandler = nHandler;
LUALOG("[LUA] Add CCMenuItem script handler: %d", m_nScriptTapHandler);
}
void CCMenuItem::unregisterScriptTapHandler(void)
{
if (m_nScriptTapHandler)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nScriptTapHandler);
LUALOG("[LUA] Remove CCMenuItem script handler: %d", m_nScriptTapHandler);
m_nScriptTapHandler = 0;
}
}
void CCMenuItem::activate()
{
if (m_bIsEnabled)
{
if (m_pListener && m_pfnSelector)
{
(m_pListener->*m_pfnSelector)(this);
}
if (kScriptTypeNone != m_eScriptType)
{
CCScriptEngineManager::sharedManager()->getScriptEngine()->executeMenuItemEvent(this);
}
}
}
void CCMenuItem::setEnabled(bool enabled)
{
m_bIsEnabled = enabled;
}
bool CCMenuItem::isEnabled()
{
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);
}
bool CCMenuItem::isSelected()
{
return m_bIsSelected;
}
void CCMenuItem::update(float dt)
{
if(m_isHoldingEnabled && m_isHolding)
{
m_timePassedSincePreviousHolding += dt;
}
}
void CCMenuItem::setTarget(CCObject *rec, SEL_MenuHandler selector)
{
m_pListener = rec;
m_pfnSelector = selector;
}
bool CCMenuItem::isHoldingEnabled()
{
return m_isHoldingEnabled;
}
void CCMenuItem::setHoldingEnabled(bool value)
{
if(value != m_isHoldingEnabled)
{
m_isHoldingEnabled = value;
if(m_isHoldingEnabled)
scheduleUpdate();
else
unscheduleUpdate();
}
}
bool CCMenuItem::isHolding()
{
return m_isHolding;
}
void CCMenuItem::setHolding(bool value)
{
m_isHolding = value;
}
float CCMenuItem::getTimePassedSincePreviousHolding()
{
return m_timePassedSincePreviousHolding;
}
void CCMenuItem::resetTimePassedSincePreviousHolding()
{
m_timePassedSincePreviousHolding = 0.0f;
}
//
//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;
}
CCMenuItemLabel * CCMenuItemLabel::itemWithLabel(CCNode*label, CCObject* target, SEL_MenuHandler selector)
{
return CCMenuItemLabel::create(label, target, selector);
}
CCMenuItemLabel * CCMenuItemLabel::create(CCNode*label, CCObject* target, SEL_MenuHandler selector)
{
CCMenuItemLabel *pRet = new CCMenuItemLabel();
pRet->initWithLabel(label, target, selector);
pRet->autorelease();
return pRet;
}
CCMenuItemLabel* CCMenuItemLabel::itemWithLabel(CCNode *label)
{
return CCMenuItemLabel::create(label);
}
CCMenuItemLabel* CCMenuItemLabel::create(CCNode *label)
{
CCMenuItemLabel *pRet = new CCMenuItemLabel();
pRet->initWithLabel(label, NULL, NULL);
pRet->autorelease();
return pRet;
}
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;
}
CCMenuItemLabel::~CCMenuItemLabel()
{
}
void CCMenuItemLabel::setString(const char * label)
{
dynamic_cast<CCLabelProtocol*>(m_pLabel)->setString(label);
this->setContentSize(m_pLabel->getContentSize());
}
void CCMenuItemLabel::activate()
{
if(m_bIsEnabled)
{
this->stopAllActions();
this->setScale( m_fOriginalScale );
CCMenuItem::activate();
}
}
void CCMenuItemLabel::selected()
{
// subclass to change the default action
if(m_bIsEnabled)
{
CCMenuItem::selected();
CCAction *action = getActionByTag(kZoomActionTag);
if (action)
{
this->stopAction(action);
}
else
{
m_fOriginalScale = this->getScale();
}
CCAction *zoomAction = CCScaleTo::create(0.1f, m_fOriginalScale * 1.2f);
zoomAction->setTag(kZoomActionTag);
this->runAction(zoomAction);
}
}
void CCMenuItemLabel::unselected()
{
// subclass to change the default action
if(m_bIsEnabled)
{
CCMenuItem::unselected();
this->stopActionByTag(kZoomActionTag);
CCAction *zoomAction = CCScaleTo::create(0.1f, m_fOriginalScale);
zoomAction->setTag(kZoomActionTag);
this->runAction(zoomAction);
}
}
void CCMenuItemLabel::setEnabled(bool enabled)
{
if( m_bIsEnabled != enabled )
{
if(enabled == false)
{
m_tColorBackup = dynamic_cast<CCRGBAProtocol*>(m_pLabel)->getColor();
dynamic_cast<CCRGBAProtocol*>(m_pLabel)->setColor(m_tDisabledColor);
}
else
{
dynamic_cast<CCRGBAProtocol*>(m_pLabel)->setColor(m_tColorBackup);
}
}
CCMenuItem::setEnabled(enabled);
}
void CCMenuItemLabel::setOpacity(GLubyte opacity)
{
dynamic_cast<CCRGBAProtocol*>(m_pLabel)->setOpacity(opacity);
}
GLubyte CCMenuItemLabel::getOpacity()
{
return dynamic_cast<CCRGBAProtocol*>(m_pLabel)->getOpacity();
}
void CCMenuItemLabel::setColor(const ccColor3B& color)
{
dynamic_cast<CCRGBAProtocol*>(m_pLabel)->setColor(color);
}
const ccColor3B& CCMenuItemLabel::getColor()
{
return dynamic_cast<CCRGBAProtocol*>(m_pLabel)->getColor();
}
//
//CCMenuItemAtlasFont
//
CCMenuItemAtlasFont * CCMenuItemAtlasFont::itemWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap)
{
return CCMenuItemAtlasFont::create(value, charMapFile, itemWidth, itemHeight, startCharMap);
}
CCMenuItemAtlasFont * CCMenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap)
{
return CCMenuItemAtlasFont::create(value, charMapFile, itemWidth, itemHeight, startCharMap, NULL, NULL);
}
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);
}
CCMenuItemAtlasFont * CCMenuItemAtlasFont::create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, CCObject* target, SEL_MenuHandler selector)
{
CCMenuItemAtlasFont *pRet = new CCMenuItemAtlasFont();
pRet->initWithString(value, charMapFile, itemWidth, itemHeight, startCharMap, target, selector);
pRet->autorelease();
return pRet;
}
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;
}
unsigned int CCMenuItemFont::fontSize()
{
return _fontSize;
}
void CCMenuItemFont::setFontName(const char *name)
{
if( _fontNameRelease )
{
_fontName.clear();
}
_fontName = name;
_fontNameRelease = true;
}
const char * CCMenuItemFont::fontName()
{
return _fontName.c_str();
}
CCMenuItemFont * CCMenuItemFont::itemWithString(const char *value, CCObject* target, SEL_MenuHandler selector)
{
return CCMenuItemFont::create(value, target, selector);
}
CCMenuItemFont * CCMenuItemFont::create(const char *value, CCObject* target, SEL_MenuHandler selector)
{
CCMenuItemFont *pRet = new CCMenuItemFont();
pRet->initWithString(value, target, selector);
pRet->autorelease();
return pRet;
}
CCMenuItemFont * CCMenuItemFont::itemWithString(const char *value)
{
return CCMenuItemFont::create(value);
}
CCMenuItemFont * CCMenuItemFont::create(const char *value)
{
CCMenuItemFont *pRet = new CCMenuItemFont();
pRet->initWithString(value, NULL, NULL);
pRet->autorelease();
return pRet;
}
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;
CCLabelTTF *label = CCLabelTTF::create(value, m_strFontName.c_str(), (float)m_uFontSize);
if (CCMenuItemLabel::initWithLabel(label, target, selector))
{
// do something ?
}
return true;
}
void CCMenuItemFont::recreateLabel()
{
CCLabelTTF *label = CCLabelTTF::create(dynamic_cast<CCLabelProtocol*>(m_pLabel)->getString(),
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;
}
void CCMenuItemSprite::setNormalImage(CCNode* pImage)
{
if (pImage != m_pNormalImage)
{
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();
}
}
CCNode * CCMenuItemSprite::getSelectedImage()
{
return m_pSelectedImage;
}
void CCMenuItemSprite::setSelectedImage(CCNode* pImage)
{
if (pImage != m_pNormalImage)
{
if (pImage)
{
addChild(pImage, 0, kSelectedTag);
pImage->setAnchorPoint(ccp(0, 0));
}
if (m_pSelectedImage)
{
removeChild(m_pSelectedImage, true);
}
m_pSelectedImage = pImage;
this->updateImagesVisibility();
}
}
CCNode * CCMenuItemSprite::getDisabledImage()
{
return m_pDisabledImage;
}
void CCMenuItemSprite::setDisabledImage(CCNode* pImage)
{
if (pImage != m_pNormalImage)
{
if (pImage)
{
addChild(pImage, 0, kDisableTag);
pImage->setAnchorPoint(ccp(0, 0));
}
if (m_pDisabledImage)
{
removeChild(m_pDisabledImage, true);
}
m_pDisabledImage = pImage;
this->updateImagesVisibility();
}
}
//
//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);
}
}
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);
}
}
GLubyte CCMenuItemSprite::getOpacity()
{
return dynamic_cast<CCRGBAProtocol*>(m_pNormalImage)->getOpacity();
}
const ccColor3B& CCMenuItemSprite::getColor()
{
return dynamic_cast<CCRGBAProtocol*>(m_pNormalImage)->getColor();
}
CCMenuItemSprite * CCMenuItemSprite::itemWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
{
return CCMenuItemSprite::create(normalSprite, selectedSprite, disabledSprite);
}
CCMenuItemSprite * CCMenuItemSprite::create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite)
{
return CCMenuItemSprite::create(normalSprite, selectedSprite, disabledSprite, NULL, NULL);
}
CCMenuItemSprite * CCMenuItemSprite::itemWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector)
{
return CCMenuItemSprite::create(normalSprite, selectedSprite, target, selector);
}
CCMenuItemSprite * CCMenuItemSprite::create(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector)
{
return CCMenuItemSprite::create(normalSprite, selectedSprite, NULL, target, selector);
}
CCMenuItemSprite * CCMenuItemSprite::itemWithNormalSprite(CCNode *normalSprite, CCNode *selectedSprite, CCNode *disabledSprite, CCObject *target, SEL_MenuHandler selector)
{
return CCMenuItemSprite::create(normalSprite, selectedSprite, disabledSprite, target, selector);
}
CCMenuItemSprite * CCMenuItemSprite::create(CCNode *normalSprite, CCNode *selectedSprite, CCNode *disabledSprite, CCObject *target, SEL_MenuHandler selector)
{
CCMenuItemSprite *pRet = new CCMenuItemSprite();
pRet->initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
pRet->autorelease();
return pRet;
}
bool CCMenuItemSprite::initWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector)
{
CCMenuItem::initWithTarget(target, selector);
setNormalImage(normalSprite);
setSelectedImage(selectedSprite);
setDisabledImage(disabledSprite);
if(m_pNormalImage)
{
this->setContentSize(m_pNormalImage->getContentSize());
}
return true;
}
/**
@since v0.99.5
*/
void CCMenuItemSprite::selected()
{
CCMenuItem::selected();
if (m_pNormalImage)
{
if (m_pDisabledImage)
{
m_pDisabledImage->setVisible(false);
}
if (m_pSelectedImage)
{
m_pNormalImage->setVisible(false);
m_pSelectedImage->setVisible(true);
}
else
{
m_pNormalImage->setVisible(true);
}
}
}
void CCMenuItemSprite::unselected()
{
CCMenuItem::unselected();
if (m_pNormalImage)
{
m_pNormalImage->setVisible(true);
if (m_pSelectedImage)
{
m_pSelectedImage->setVisible(false);
}
if (m_pDisabledImage)
{
m_pDisabledImage->setVisible(false);
}
}
}
void CCMenuItemSprite::setEnabled(bool bEnabled)
{
if( m_bIsEnabled != bEnabled )
{
CCMenuItem::setEnabled(bEnabled);
this->updateImagesVisibility();
}
}
// Helper
void CCMenuItemSprite::updateImagesVisibility()
{
if (m_bIsEnabled)
{
if (m_pNormalImage) m_pNormalImage->setVisible(true);
if (m_pSelectedImage) m_pSelectedImage->setVisible(false);
if (m_pDisabledImage) m_pDisabledImage->setVisible(false);
}
else
{
if (m_pDisabledImage)
{
if (m_pNormalImage) m_pNormalImage->setVisible(false);
if (m_pSelectedImage) m_pSelectedImage->setVisible(false);
if (m_pDisabledImage) m_pDisabledImage->setVisible(true);
}
else
{
if (m_pNormalImage) m_pNormalImage->setVisible(true);
if (m_pSelectedImage) m_pSelectedImage->setVisible(false);
if (m_pDisabledImage) m_pDisabledImage->setVisible(false);
}
}
}
///
/// CCMenuItemImage
///
CCMenuItemImage* CCMenuItemImage::node()
{
return CCMenuItemImage::create();
}
CCMenuItemImage* CCMenuItemImage::create()
{
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);
}
CCMenuItemImage * CCMenuItemImage::itemWithNormalImage(const char *normalImage, const char *selectedImage)
{
return CCMenuItemImage::create(normalImage, selectedImage);
}
CCMenuItemImage * CCMenuItemImage::create(const char *normalImage, const char *selectedImage)
{
return CCMenuItemImage::create(normalImage, selectedImage, NULL, NULL, NULL);
}
CCMenuItemImage * CCMenuItemImage::itemWithNormalImage(const char *normalImage, const char *selectedImage, CCObject* target, SEL_MenuHandler selector)
{
return CCMenuItemImage::create(normalImage, selectedImage, target, selector);
}
CCMenuItemImage * CCMenuItemImage::create(const char *normalImage, const char *selectedImage, CCObject* target, SEL_MenuHandler selector)
{
return CCMenuItemImage::create(normalImage, selectedImage, NULL, target, selector);
}
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);
}
CCMenuItemImage * CCMenuItemImage::create(CCSpriteFrame* normalFrame, CCSpriteFrame* selectedFrame, CCSpriteFrame* disabledFrame, CCObject* target, SEL_MenuHandler selector)
{
CCMenuItemImage *pRet = new CCMenuItemImage();
if(pRet && pRet->initWithFrames(normalFrame, selectedFrame, disabledFrame, target, selector))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
CCMenuItemImage * CCMenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector)
{
CCMenuItemImage *pRet = new CCMenuItemImage();
if (pRet && pRet->initWithNormalImage(normalImage, selectedImage, disabledImage, target, selector))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
CCMenuItemImage * CCMenuItemImage::itemWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage)
{
return CCMenuItemImage::create(normalImage, selectedImage, disabledImage);
}
CCMenuItemImage * CCMenuItemImage::create(const char *normalImage, const char *selectedImage, const char *disabledImage)
{
CCMenuItemImage *pRet = new CCMenuItemImage();
if (pRet && pRet->initWithNormalImage(normalImage, selectedImage, disabledImage, NULL, NULL))
{
pRet->autorelease();
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
bool CCMenuItemImage::initWithFrames(CCSpriteFrame* normalFrame, CCSpriteFrame* selectedFrame, CCSpriteFrame* disabledFrame, CCObject* target, SEL_MenuHandler selector)
{
CCNode *normalSprite = NULL;
CCNode *selectedSprite = NULL;
CCNode *disabledSprite = NULL;
if(normalFrame)
{
normalSprite = CCSprite::createWithSpriteFrame(normalFrame);
}
if(selectedFrame)
{
selectedSprite = CCSprite::createWithSpriteFrame(selectedFrame);
}
if(disabledFrame)
{
disabledSprite = CCSprite::createWithSpriteFrame(disabledFrame);
}
return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
}
bool CCMenuItemImage::initWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector)
{
CCNode *normalSprite = NULL;
CCNode *selectedSprite = NULL;
CCNode *disabledSprite = NULL;
if (normalImage)
{
normalSprite = CCSprite::create(normalImage);
}
if (selectedImage)
{
selectedSprite = CCSprite::create(selectedImage);
}
if(disabledImage)
{
disabledSprite = CCSprite::create(disabledImage);
}
return initWithNormalSprite(normalSprite, selectedSprite, disabledSprite, target, selector);
}
//
// Setter of sprite frames
//
void CCMenuItemImage::setNormalSpriteFrame(CCSpriteFrame * frame)
{
setNormalImage(CCSprite::createWithSpriteFrame(frame));
}
void CCMenuItemImage::setSelectedSpriteFrame(CCSpriteFrame * frame)
{
setSelectedImage(CCSprite::createWithSpriteFrame(frame));
}
void CCMenuItemImage::setDisabledSpriteFrame(CCSpriteFrame * frame)
{
setDisabledImage(CCSprite::createWithSpriteFrame(frame));
}
//
// MenuItemToggle
//
void CCMenuItemToggle::setSubItems(CCArray* var)
{
CC_SAFE_RETAIN(var);
CC_SAFE_RELEASE(m_pSubItems);
m_pSubItems = var;
}
CCArray* CCMenuItemToggle::getSubItems()
{
return m_pSubItems;
}
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;
}
CCMenuItemToggle * CCMenuItemToggle::createWithTarget(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;
}
CCMenuItemToggle * CCMenuItemToggle::create()
{
CCMenuItemToggle *pRet = new CCMenuItemToggle();
pRet->initWithItem(NULL);
pRet->autorelease();
return pRet;
}
bool CCMenuItemToggle::initWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, va_list args)
{
CCMenuItem::initWithTarget(target, selector);
this->m_pSubItems = CCArray::create();
this->m_pSubItems->retain();
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;
}
CCMenuItemToggle* CCMenuItemToggle::itemWithItem(CCMenuItem *item)
{
return CCMenuItemToggle::create(item);
}
CCMenuItemToggle* CCMenuItemToggle::create(CCMenuItem *item)
{
CCMenuItemToggle *pRet = new CCMenuItemToggle();
pRet->initWithItem(item);
pRet->autorelease();
return pRet;
}
bool CCMenuItemToggle::initWithItem(CCMenuItem *item)
{
CCMenuItem::initWithTarget(NULL, NULL);
this->m_pSubItems = CCArray::create();
this->m_pSubItems->retain();
if (item) {
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_pSubItems->count() > 0 )
{
m_uSelectedIndex = index;
CCMenuItem *currentItem = (CCMenuItem*)getChildByTag(kCurrentItem);
if( currentItem )
{
currentItem->removeFromParentAndCleanup(false);
}
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::setEnabled(bool enabled)
{
if (m_bIsEnabled != enabled)
{
CCMenuItem::setEnabled(enabled);
if(m_pSubItems && m_pSubItems->count() > 0)
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pSubItems, pObj)
{
CCMenuItem* pItem = (CCMenuItem*)pObj;
pItem->setEnabled(enabled);
}
}
}
}
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)
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pSubItems, pObj)
{
CCMenuItem* pItem = (CCMenuItem*)pObj;
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)
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pSubItems, pObj)
{
CCMenuItem* pItem = (CCMenuItem*)pObj;
dynamic_cast<CCRGBAProtocol*>(pItem)->setColor(color);
}
}
}
NS_CC_END
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
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.
****************************************************************************/
#ifndef __CCMENU_ITEM_H__
#define __CCMENU_ITEM_H__
#include "base_nodes/CCNode.h"
#include "CCProtocols.h"
#include "cocoa/CCArray.h"
NS_CC_BEGIN
class CCLabelTTF;
class CCLabelAtlas;
class CCSprite;
class CCSpriteFrame;
#define kCCItemSize 32
/**
* @addtogroup GUI
* @{
* @addtogroup menu
* @{
*/
/** @brief CCMenuItem base class
*
* Subclass CCMenuItem (or any subclass) to create your custom CCMenuItem objects.
*/
class CC_DLL CCMenuItem : public CCNode
{
protected:
/** whether or not the item is selected
@since v0.8.2
*/
bool m_bIsSelected;
bool m_bIsEnabled;
public:
CCMenuItem()
: m_bIsSelected(false)
, m_bIsEnabled(false)
, m_pListener(NULL)
, m_pfnSelector(NULL)
, m_nScriptTapHandler(0)
, m_isHoldingEnabled(false)
, m_isHolding(false)
, m_timePassedSincePreviousHolding(0.0f)
{}
virtual ~CCMenuItem();
/** Creates a CCMenuItem with a target/selector
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItem * itemWithTarget(CCObject *rec, SEL_MenuHandler selector);
/** Creates a CCMenuItem with no target/selector */
static CCMenuItem* create();
/** Creates a CCMenuItem with a target/selector */
static CCMenuItem* create(CCObject *rec, SEL_MenuHandler selector);
/** Initializes a CCMenuItem with a target/selector */
bool initWithTarget(CCObject *rec, SEL_MenuHandler selector);
/** Returns the outside box */
CCRect rect();
/** Activate the item */
virtual void activate();
/** The item was selected (not activated), similar to "mouse-over" */
virtual void selected();
/** The item was unselected */
virtual void unselected();
/** Register menu handler script function */
virtual void registerScriptTapHandler(int nHandler);
virtual void unregisterScriptTapHandler(void);
int getScriptTapHandler() { return m_nScriptTapHandler; };
virtual bool isEnabled();
//@note: It's 'setIsEnable' in cocos2d-iphone.
virtual void setEnabled(bool value);
virtual bool isSelected();
virtual void update(float dt);
/** set the target/selector of the menu item*/
void setTarget(CCObject *rec, SEL_MenuHandler selector);
bool isHoldingEnabled();
void setHoldingEnabled(bool value);
bool isHolding();
void setHolding(bool value);
float getTimePassedSincePreviousHolding();
void resetTimePassedSincePreviousHolding();
protected:
CCObject* m_pListener;
SEL_MenuHandler m_pfnSelector;
int m_nScriptTapHandler;
// holding functionality
bool m_isHoldingEnabled;
bool m_isHolding;
float m_timePassedSincePreviousHolding;
};
/** @brief An abstract class for "label" CCMenuItemLabel items
Any CCNode that supports the CCLabelProtocol protocol can be added.
Supported nodes:
- CCBitmapFontAtlas
- CCLabelAtlas
- CCLabelTTF
*/
class CC_DLL CCMenuItemLabel : public CCMenuItem, public CCRGBAProtocol
{
/** the color that will be used to disable the item */
CC_PROPERTY_PASS_BY_REF(ccColor3B, m_tDisabledColor, DisabledColor);
/** Label that is rendered. It can be any CCNode that implements the CCLabelProtocol */
CC_PROPERTY(CCNode*, m_pLabel, Label);
public:
CCMenuItemLabel()
: m_pLabel(NULL)
, m_fOriginalScale(0.0)
{}
virtual ~CCMenuItemLabel();
/** creates a CCMenuItemLabel with a Label, target and selector
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemLabel * itemWithLabel(CCNode*label, CCObject* target, SEL_MenuHandler selector);
/** creates a CCMenuItemLabel with a Label. Target and selector will be nil
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemLabel* itemWithLabel(CCNode *label);
/** creates a CCMenuItemLabel with a Label, target and selector */
static CCMenuItemLabel * create(CCNode*label, CCObject* target, SEL_MenuHandler selector);
/** creates a CCMenuItemLabel with a Label. Target and selector will be nil */
static CCMenuItemLabel* create(CCNode *label);
/** initializes a CCMenuItemLabel with a Label, target and selector */
bool initWithLabel(CCNode* label, CCObject* target, SEL_MenuHandler selector);
/** sets a new string to the inner label */
void setString(const char * label);
// super methods
virtual void activate();
virtual void selected();
virtual void unselected();
/** Enable or disabled the CCMenuItemFont
@warning setEnabled changes the RGB color of the font
*/
virtual void setEnabled(bool enabled);
virtual void setOpacity(GLubyte opacity);
virtual GLubyte getOpacity();
virtual void setColor(const ccColor3B& color);
virtual const ccColor3B& getColor();
virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
protected:
ccColor3B m_tColorBackup;
float m_fOriginalScale;
};
/** @brief A CCMenuItemAtlasFont
Helper class that creates a MenuItemLabel class with a LabelAtlas
*/
class CC_DLL CCMenuItemAtlasFont : public CCMenuItemLabel
{
public:
CCMenuItemAtlasFont(){}
virtual ~CCMenuItemAtlasFont(){}
/** creates a menu item from a string and atlas with a target/selector
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemAtlasFont* itemWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap);
/** creates a menu item from a string and atlas. Use it with MenuItemToggle
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemAtlasFont* itemWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item from a string and atlas with a target/selector */
static CCMenuItemAtlasFont* create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap);
/** creates a menu item from a string and atlas. Use it with MenuItemToggle */
static CCMenuItemAtlasFont* create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, CCObject* target, SEL_MenuHandler selector);
/** initializes a menu item from a string and atlas with a target/selector */
bool initWithString(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, CCObject* target, SEL_MenuHandler selector);
};
/** @brief A CCMenuItemFont
Helper class that creates a CCMenuItemLabel class with a Label
*/
class CC_DLL CCMenuItemFont : public CCMenuItemLabel
{
public:
CCMenuItemFont() : m_uFontSize(0), m_strFontName(""){}
virtual ~CCMenuItemFont(){}
/** set default font size */
static void setFontSize(unsigned int s);
/** get default font size */
static unsigned int fontSize();
/** set the default font name */
static void setFontName(const char *name);
/** get the default font name */
static const char *fontName();
/** creates a menu item from a string without target/selector. To be used with CCMenuItemToggle
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemFont * itemWithString(const char *value);
/** creates a menu item from a string with a target/selector
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemFont * itemWithString(const char *value, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item from a string without target/selector. To be used with CCMenuItemToggle */
static CCMenuItemFont * create(const char *value);
/** creates a menu item from a string with a target/selector */
static CCMenuItemFont * create(const char *value, CCObject* target, SEL_MenuHandler selector);
/** initializes a menu item from a string with a target/selector */
bool initWithString(const char *value, CCObject* target, SEL_MenuHandler selector);
/** set font size
* c++ can not overload static and non-static member functions with the same parameter types
* so change the name to setFontSizeObj
*/
void setFontSizeObj(unsigned int s);
/** get font size */
unsigned int fontSizeObj();
/** set the font name
* c++ can not overload static and non-static member functions with the same parameter types
* so change the name to setFontNameObj
*/
void setFontNameObj(const char* name);
const char* fontNameObj();
protected:
void recreateLabel();
unsigned int m_uFontSize;
std::string m_strFontName;
};
/** @brief CCMenuItemSprite accepts CCNode<CCRGBAProtocol> objects as items.
The images has 3 different states:
- unselected image
- selected image
- disabled image
@since v0.8.0
*/
class CC_DLL CCMenuItemSprite : public CCMenuItem, public CCRGBAProtocol
{
/** the image used when the item is not selected */
CC_PROPERTY(CCNode*, m_pNormalImage, NormalImage);
/** the image used when the item is selected */
CC_PROPERTY(CCNode*, m_pSelectedImage, SelectedImage);
/** the image used when the item is disabled */
CC_PROPERTY(CCNode*, m_pDisabledImage, DisabledImage);
public:
CCMenuItemSprite()
:m_pNormalImage(NULL)
,m_pSelectedImage(NULL)
,m_pDisabledImage(NULL)
{}
/** creates a menu item with a normal, selected and disabled image
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemSprite * itemWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite = NULL);
/** creates a menu item with a normal and selected image with target/selector
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemSprite * itemWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a normal,selected and disabled image with target/selector
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemSprite * itemWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a normal, selected and disabled image*/
static CCMenuItemSprite * create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite = NULL);
/** creates a menu item with a normal and selected image with target/selector */
static CCMenuItemSprite * create(CCNode* normalSprite, CCNode* selectedSprite, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a normal,selected and disabled image with target/selector */
static CCMenuItemSprite * create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector);
/** initializes a menu item with a normal, selected and disabled image with target/selector */
bool initWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector);
// super methods
virtual void setColor(const ccColor3B& color);
virtual const ccColor3B& getColor();
virtual void setOpacity(GLubyte opacity);
virtual GLubyte getOpacity();
/**
@since v0.99.5
*/
virtual void selected();
virtual void unselected();
virtual void setEnabled(bool bEnabled);
virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
protected:
virtual void updateImagesVisibility();
};
/** @brief CCMenuItemImage accepts images as items.
The images has 3 different states:
- unselected image
- selected image
- disabled image
For best results try that all images are of the same size
*/
class CC_DLL CCMenuItemImage : public CCMenuItemSprite
{
public:
CCMenuItemImage(){}
virtual ~CCMenuItemImage(){}
/** creates a menu item with a normal and selected image
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemImage* itemWithNormalImage(const char *normalImage, const char *selectedImage);
/** creates a menu item with a normal,selected and disabled image
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemImage* itemWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage);
/** creates a menu item with a normal and selected image with target/selector
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemImage* itemWithNormalImage(const char *normalImage, const char *selectedImage, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a normal,selected and disabled image with target/selector
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemImage* itemWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a normal and selected image*/
static CCMenuItemImage* create(const char *normalImage, const char *selectedImage);
/** creates a menu item with a normal,selected and disabled image*/
static CCMenuItemImage* create(const char *normalImage, const char *selectedImage, const char *disabledImage);
/** creates a menu item with a normal and selected image with target/selector */
static CCMenuItemImage* create(const char *normalImage, const char *selectedImage, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a normal,selected and disabled image with target/selector */
static CCMenuItemImage* create(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a spriteframe of normal, selected, and disabled image with target/selector */
static CCMenuItemImage* create(CCSpriteFrame* normalFrame, CCSpriteFrame* selectedFrame, CCSpriteFrame* disabledFrame, CCObject* target, SEL_MenuHandler selector);
bool init();
/** initializes a menu item with a normal, selected and disabled image with target/selector */
bool initWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector);
bool initWithFrames(CCSpriteFrame* normalFrame, CCSpriteFrame* selectedFrame, CCSpriteFrame* disabledFrame, CCObject* target, SEL_MenuHandler selector);
/** sets the sprite frame for the normal image */
void setNormalSpriteFrame(CCSpriteFrame* frame);
/** sets the sprite frame for the selected image */
void setSelectedSpriteFrame(CCSpriteFrame* frame);
/** sets the sprite frame for the disabled image */
void setDisabledSpriteFrame(CCSpriteFrame* frame);
/** Creates an CCMenuItemImage.
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemImage* node();
/** Creates an CCMenuItemImage.
*/
static CCMenuItemImage* create();
};
/** @brief A CCMenuItemToggle
A simple container class that "toggles" it's inner items
The inner items can be any MenuItem
*/
class CC_DLL CCMenuItemToggle : public CCMenuItem, public CCRGBAProtocol
{
/** conforms with CCRGBAProtocol protocol */
CC_PROPERTY(GLubyte, m_cOpacity, Opacity);
/** conforms with CCRGBAProtocol protocol */
CC_PROPERTY_PASS_BY_REF(ccColor3B, m_tColor, Color);
/** returns the selected item */
CC_PROPERTY(unsigned int, m_uSelectedIndex, SelectedIndex);
/** CCMutableArray that contains the subitems. You can add/remove items in runtime, and you can replace the array with a new one.
@since v0.7.2
*/
CC_PROPERTY(CCArray*, m_pSubItems, SubItems);
public:
CCMenuItemToggle()
: m_cOpacity(0)
, m_uSelectedIndex(0)
, m_pSubItems(NULL)
{}
virtual ~CCMenuItemToggle();
/** creates a menu item from a list of items with a target/selector
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemToggle* itemWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, ...);
/** creates a menu item from a list of items with a target/selector */
static CCMenuItemToggle* createWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, ...);
/** creates a menu item with no target/selector and no items */
static CCMenuItemToggle* create();
/** initializes a menu item from a list of items with a target selector */
bool initWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, va_list args);
// The follow methods offered to lua
/** creates a menu item with a item
@deprecated: This interface will be deprecated sooner or later.
*/
CC_DEPRECATED_ATTRIBUTE static CCMenuItemToggle* itemWithItem(CCMenuItem *item);
/** creates a menu item with a item */
static CCMenuItemToggle* create(CCMenuItem *item);
/** initializes a menu item with a item */
bool initWithItem(CCMenuItem *item);
/** add more menu item */
void addSubItem(CCMenuItem *item);
/** return the selected item */
CCMenuItem* selectedItem();
// super methods
virtual void activate();
virtual void selected();
virtual void unselected();
virtual void setEnabled(bool var);
virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
};
// end of GUI group
/// @}
/// @}
NS_CC_END
#endif //__CCMENU_ITEM_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment