2011-03-19 10:59:01 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
2011-07-04 14:47:32 +08:00
|
|
|
Copyright (c) 2008-2011 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2011-03-19 10:59:01 +08:00
|
|
|
|
|
|
|
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__
|
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
#include "CCNode.h"
|
|
|
|
#include "CCProtocols.h"
|
2012-03-20 15:04:53 +08:00
|
|
|
#include "CCArray.h"
|
2011-03-19 10:59:01 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
class CCLabelTTF;
|
|
|
|
class CCLabelAtlas;
|
|
|
|
class CCSprite;
|
|
|
|
class CCSpriteFrame;
|
2011-07-13 10:20:24 +08:00
|
|
|
#define kCCItemSize 32
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
/** @brief CCMenuItem base class
|
|
|
|
*
|
|
|
|
* Subclass CCMenuItem (or any subclass) to create your custom CCMenuItem objects.
|
|
|
|
*/
|
|
|
|
class CC_DLL CCMenuItem : public CCNode
|
|
|
|
{
|
|
|
|
/** whether or not the item is selected
|
|
|
|
@since v0.8.2
|
2012-02-02 10:39:36 +08:00
|
|
|
*/
|
2012-04-18 18:43:45 +08:00
|
|
|
CC_PROPERTY_READONLY(bool, m_bIsSelected, IsSelected);
|
|
|
|
CC_PROPERTY(bool, m_bIsEnabled, IsEnabled);
|
|
|
|
public:
|
|
|
|
CCMenuItem()
|
|
|
|
: m_bIsSelected(false)
|
|
|
|
, m_bIsEnabled(false)
|
2012-04-19 14:35:52 +08:00
|
|
|
, m_pListener(NULL)
|
2012-04-18 18:43:45 +08:00
|
|
|
, m_pfnSelector(NULL)
|
|
|
|
, m_nScriptHandler(0)
|
|
|
|
{}
|
|
|
|
virtual ~CCMenuItem();
|
|
|
|
/** Creates a CCMenuItem with a target/selector */
|
|
|
|
static CCMenuItem * itemWithTarget(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();
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
/** Register menu handler script function */
|
|
|
|
virtual void registerScriptHandler(int nHandler);
|
|
|
|
virtual void unregisterScriptHandler(void);
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
/** set the target/selector of the menu item*/
|
|
|
|
void setTarget(CCObject *rec, SEL_MenuHandler selector);
|
|
|
|
protected:
|
|
|
|
CCObject* m_pListener;
|
2012-04-19 14:35:52 +08:00
|
|
|
SEL_MenuHandler m_pfnSelector;
|
2012-04-18 18:43:45 +08:00
|
|
|
int m_nScriptHandler;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @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 */
|
|
|
|
static CCMenuItemLabel * itemWithLabel(CCNode*label, CCObject* target, SEL_MenuHandler selector);
|
|
|
|
/** creates a CCMenuItemLabel with a Label. Target and selector will be nill */
|
|
|
|
static CCMenuItemLabel* itemWithLabel(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 setIsEnabled changes the RGB color of the font
|
2012-02-02 10:39:36 +08:00
|
|
|
*/
|
2012-04-18 18:43:45 +08:00
|
|
|
virtual void setIsEnabled(bool enabled);
|
|
|
|
virtual void setOpacity(GLubyte opacity);
|
|
|
|
virtual GLubyte getOpacity();
|
|
|
|
virtual void setColor(const ccColor3B& color);
|
|
|
|
virtual const ccColor3B& getColor();
|
|
|
|
|
|
|
|
virtual void setIsOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
|
|
|
|
virtual bool getIsOpacityModifyRGB(void) { return false;}
|
|
|
|
protected:
|
2012-04-19 14:35:52 +08:00
|
|
|
ccColor3B m_tColorBackup;
|
|
|
|
float m_fOriginalScale;
|
2012-04-18 18:43:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** @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 */
|
|
|
|
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 */
|
|
|
|
static CCMenuItemAtlasFont* itemWithString(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 */
|
|
|
|
static CCMenuItemFont * itemWithString(const char *value);
|
|
|
|
/** creates a menu item from a string with a target/selector */
|
|
|
|
static CCMenuItemFont * itemWithString(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);
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
/** set font size
|
|
|
|
* c++ can not overload static and non-static member functions with the same parameter types
|
|
|
|
* so change the name to setFontSizeObj
|
2012-02-02 10:39:36 +08:00
|
|
|
*/
|
2012-04-18 18:43:45 +08:00
|
|
|
void setFontSizeObj(unsigned int s);
|
|
|
|
|
|
|
|
/** get font size */
|
|
|
|
unsigned int fontSizeObj();
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
/** 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
|
2012-02-02 10:39:36 +08:00
|
|
|
*/
|
2012-04-18 18:43:45 +08:00
|
|
|
void setFontNameObj(const char* name);
|
|
|
|
|
|
|
|
const char* fontNameObj();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void recreateLabel();
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
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*/
|
|
|
|
static CCMenuItemSprite * itemWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite = NULL);
|
|
|
|
/** creates a menu item with a normal and selected image with target/selector */
|
|
|
|
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 */
|
|
|
|
static CCMenuItemSprite * itemWithNormalSprite(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
|
2012-02-02 10:39:36 +08:00
|
|
|
*/
|
2012-04-18 18:43:45 +08:00
|
|
|
virtual void selected();
|
|
|
|
virtual void unselected();
|
|
|
|
virtual void setIsEnabled(bool bEnabled);
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
virtual void setIsOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
|
|
|
|
virtual bool getIsOpacityModifyRGB(void) { return false;}
|
2012-06-08 14:50:41 +08:00
|
|
|
protected:
|
|
|
|
virtual void updateImagesVisibility();
|
2012-04-18 18:43:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** @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*/
|
|
|
|
static CCMenuItemImage* itemWithNormalImage(const char *normalImage, const char *selectedImage);
|
|
|
|
/** creates a menu item with a normal,selected and disabled image*/
|
|
|
|
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 */
|
|
|
|
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 */
|
|
|
|
static CCMenuItemImage* itemWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector);
|
|
|
|
/** 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);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** 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);
|
2012-04-18 18:43:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief A CCMenuItemToggle
|
|
|
|
A simple container class that "toggles" it's inner items
|
|
|
|
The inner itmes 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
|
2012-02-02 10:39:36 +08:00
|
|
|
*/
|
2012-04-18 18:43:45 +08:00
|
|
|
CC_PROPERTY(CCArray*, m_pSubItems, SubItems);
|
|
|
|
public:
|
|
|
|
CCMenuItemToggle()
|
|
|
|
: m_cOpacity(0)
|
|
|
|
, m_uSelectedIndex(0)
|
2012-04-19 14:35:52 +08:00
|
|
|
, m_pSubItems(NULL)
|
2012-04-18 18:43:45 +08:00
|
|
|
{}
|
|
|
|
virtual ~CCMenuItemToggle();
|
|
|
|
/** creates a menu item from a list of items with a target/selector */
|
2012-04-19 14:35:52 +08:00
|
|
|
static CCMenuItemToggle* itemWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, ...);
|
2012-04-18 18:43:45 +08:00
|
|
|
/** 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 */
|
|
|
|
static CCMenuItemToggle* itemWithItem(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 setIsEnabled(bool var);
|
|
|
|
|
|
|
|
virtual void setIsOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
|
|
|
|
virtual bool getIsOpacityModifyRGB(void) { return false;}
|
|
|
|
};
|
2012-02-02 10:39:36 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_END
|
2011-03-19 10:59:01 +08:00
|
|
|
|
2011-07-26 18:41:58 +08:00
|
|
|
#endif //__CCMENU_ITEM_H__
|