2011-03-19 10:59:01 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 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_ITEM_H__
|
|
|
|
#define __CCMENU_ITEM_H__
|
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
#include "CCNode.h"
|
|
|
|
#include "CCProtocols.h"
|
2011-03-19 10:59:01 +08:00
|
|
|
#include "selector_protocol.h"
|
|
|
|
|
|
|
|
namespace cocos2d{
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-12-24 17:07:31 +08:00
|
|
|
class CCLabelTTF;
|
2010-08-10 12:49:16 +08:00
|
|
|
class CCLabelAtlas;
|
|
|
|
class CCSprite;
|
|
|
|
|
|
|
|
#define kItemSize 32
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief CCMenuItem base class
|
2010-08-10 12:49:16 +08:00
|
|
|
*
|
|
|
|
* Subclass CCMenuItem (or any subclass) to create your custom CCMenuItem objects.
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCMenuItem : public CCNode
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
/** whether or not the item is selected
|
|
|
|
@since v0.8.2
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY_READONLY(bool, m_bIsSelected, IsSelected);
|
|
|
|
CC_PROPERTY(bool, m_bIsEnabled, IsEnabled);
|
2010-08-10 12:49:16 +08:00
|
|
|
public:
|
|
|
|
CCMenuItem()
|
2011-06-10 17:51:37 +08:00
|
|
|
: m_bIsSelected(false)
|
|
|
|
, m_bIsEnabled(false)
|
|
|
|
, m_pListener(NULL)
|
2011-04-01 16:06:53 +08:00
|
|
|
, m_pfnSelector(NULL)
|
2010-08-10 12:49:16 +08:00
|
|
|
{}
|
|
|
|
virtual ~CCMenuItem(){}
|
|
|
|
/** Creates a CCMenuItem with a target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
static CCMenuItem * itemWithTarget(SelectorProtocol *rec, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** Initializes a CCMenuItem with a target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
bool initWithTarget(SelectorProtocol *rec, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** Returns the outside box */
|
2011-03-07 17:11:57 +08:00
|
|
|
CCRect rect();
|
2010-08-10 12:49:16 +08:00
|
|
|
/** 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();
|
2011-06-20 17:31:38 +08:00
|
|
|
/** Register a script function, the function is called in activete
|
|
|
|
* If pszFunctionName is NULL, then unregister it.
|
|
|
|
*/
|
|
|
|
virtual void registerScriptHandler(const char* pszFunctionName);
|
2010-08-10 12:49:16 +08:00
|
|
|
protected:
|
|
|
|
SelectorProtocol* m_pListener;
|
2010-11-17 16:31:48 +08:00
|
|
|
SEL_MenuHandler m_pfnSelector;
|
2011-06-20 17:31:38 +08:00
|
|
|
std::string m_functionName;
|
2010-08-10 12:49:16 +08:00
|
|
|
};
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief An abstract class for "label" CCMenuItemLabel items
|
2010-08-10 12:49:16 +08:00
|
|
|
Any CCNode that supports the CCLabelProtocol protocol can be added.
|
|
|
|
Supported nodes:
|
|
|
|
- CCBitmapFontAtlas
|
|
|
|
- CCLabelAtlas
|
2010-12-24 17:07:31 +08:00
|
|
|
- CCLabelTTF
|
2010-08-10 12:49:16 +08:00
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCMenuItemLabel : public CCMenuItem, public CCRGBAProtocol
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
/** the color that will be used to disable the item */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(ccColor3B, m_tDisabledColor, DisabledColor);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** Label that is rendered. It can be any CCNode that implements the CCLabelProtocol */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(CCNode*, m_pLabel, Label);
|
2010-08-10 12:49:16 +08:00
|
|
|
public:
|
|
|
|
CCMenuItemLabel()
|
2011-04-01 16:06:53 +08:00
|
|
|
: m_pLabel(NULL)
|
|
|
|
, m_fOriginalScale(0.0)
|
2010-08-10 12:49:16 +08:00
|
|
|
{}
|
|
|
|
virtual ~CCMenuItemLabel();
|
|
|
|
/** creates a CCMenuItemLabel with a Label, target and selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
static CCMenuItemLabel * itemWithLabel(CCNode*label, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** initializes a CCMenuItemLabel with a Label, target and selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
bool initWithLabel(CCNode* label, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** 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
|
|
|
|
*/
|
|
|
|
virtual void setIsEnabled(bool enabled);
|
|
|
|
virtual void setOpacity(GLubyte opacity);
|
|
|
|
virtual GLubyte getOpacity();
|
|
|
|
virtual void setColor(ccColor3B color);
|
|
|
|
virtual ccColor3B getColor();
|
2010-08-31 11:20:37 +08:00
|
|
|
|
|
|
|
virtual CCRGBAProtocol* convertToRGBAProtocol() { return (CCRGBAProtocol*)this; }
|
2010-08-10 12:49:16 +08:00
|
|
|
protected:
|
|
|
|
ccColor3B m_tColorBackup;
|
|
|
|
float m_fOriginalScale;
|
|
|
|
};
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief A CCMenuItemAtlasFont
|
2010-08-10 12:49:16 +08:00
|
|
|
Helper class that creates a MenuItemLabel class with a LabelAtlas
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCMenuItemAtlasFont : public CCMenuItemLabel
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCMenuItemAtlasFont(){}
|
|
|
|
virtual ~CCMenuItemAtlasFont(){}
|
|
|
|
/** creates a menu item from a string and atlas with a target/selector */
|
|
|
|
static CCMenuItemAtlasFont* itemFromString(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 */
|
2010-11-17 16:31:48 +08:00
|
|
|
static 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
|
|
|
/** initializes a menu item from a string and atlas with a target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
bool 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-29 09:44:57 +08:00
|
|
|
/** @brief A CCMenuItemFont
|
2010-08-10 12:49:16 +08:00
|
|
|
Helper class that creates a CCMenuItemLabel class with a Label
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCMenuItemFont : public CCMenuItemLabel
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCMenuItemFont(){}
|
|
|
|
virtual ~CCMenuItemFont(){}
|
|
|
|
/** set font size */
|
|
|
|
static void setFontSize(int s);
|
|
|
|
/** get font size */
|
|
|
|
static int fontSize();
|
|
|
|
/** set the font name */
|
|
|
|
static void setFontName(const char *name);
|
|
|
|
/** get the font name */
|
|
|
|
static const char *fontName();
|
|
|
|
/** creates a menu item from a string without target/selector. To be used with CCMenuItemToggle */
|
|
|
|
static CCMenuItemFont * itemFromString(const char *value);
|
|
|
|
/** creates a menu item from a string with a target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
static CCMenuItemFont * itemFromString(const char *value, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** initializes a menu item from a string with a target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
bool initFromString(const char *value, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
};
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief CCMenuItemSprite accepts CCNode<CCRGBAProtocol> objects as items.
|
2010-08-10 12:49:16 +08:00
|
|
|
The images has 3 different states:
|
|
|
|
- unselected image
|
|
|
|
- selected image
|
|
|
|
- disabled image
|
|
|
|
|
|
|
|
@since v0.8.0
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCMenuItemSprite : public CCMenuItem, public CCRGBAProtocol
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
/** the image used when the item is not selected */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(CCNode*, m_pNormalImage, NormalImage);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** the image used when the item is selected */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(CCNode*, m_pSelectedImage, SelectedImage);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** the image used when the item is disabled */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(CCNode*, m_pDisabledImage, DisabledImage);
|
2010-08-10 12:49:16 +08:00
|
|
|
public:
|
|
|
|
CCMenuItemSprite()
|
|
|
|
:m_pNormalImage(NULL)
|
|
|
|
,m_pSelectedImage(NULL)
|
|
|
|
,m_pDisabledImage(NULL)
|
|
|
|
{}
|
|
|
|
/** creates a menu item with a normal and selected image*/
|
|
|
|
static CCMenuItemSprite * itemFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite);
|
|
|
|
/** creates a menu item with a normal and selected image with target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
static CCMenuItemSprite * itemFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** creates a menu item with a normal,selected and disabled image with target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
static CCMenuItemSprite * itemFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** initializes a menu item with a normal, selected and disabled image with target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
bool initFromNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
// super methods
|
2011-05-19 17:00:13 +08:00
|
|
|
virtual void setColor(ccColor3B color);
|
|
|
|
virtual ccColor3B getColor();
|
|
|
|
virtual void setOpacity(GLubyte opacity);
|
|
|
|
virtual GLubyte getOpacity();
|
2010-08-31 11:20:37 +08:00
|
|
|
|
2010-12-25 11:15:19 +08:00
|
|
|
/**
|
|
|
|
@since v0.99.5
|
|
|
|
*/
|
|
|
|
virtual void selected();
|
|
|
|
virtual void unselected();
|
|
|
|
virtual void setIsEnabled(bool bEnabled);
|
|
|
|
|
2010-08-31 11:20:37 +08:00
|
|
|
virtual CCRGBAProtocol* convertToRGBAProtocol() { return (CCRGBAProtocol*)this; }
|
2010-08-10 12:49:16 +08:00
|
|
|
};
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief CCMenuItemImage accepts images as items.
|
2010-08-10 12:49:16 +08:00
|
|
|
The images has 3 different states:
|
|
|
|
- unselected image
|
|
|
|
- selected image
|
|
|
|
- disabled image
|
|
|
|
|
|
|
|
For best results try that all images are of the same size
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCMenuItemImage : public CCMenuItemSprite
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CCMenuItemImage(){}
|
|
|
|
virtual ~CCMenuItemImage(){}
|
|
|
|
/** creates a menu item with a normal and selected image*/
|
|
|
|
static CCMenuItemImage* itemFromNormalImage(const char *normalImage, const char *selectedImage);
|
|
|
|
/** creates a menu item with a normal,selected and disabled image*/
|
|
|
|
static CCMenuItemImage* itemFromNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage);
|
|
|
|
/** creates a menu item with a normal and selected image with target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
static CCMenuItemImage* itemFromNormalImage(const char *normalImage, const char *selectedImage, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** creates a menu item with a normal,selected and disabled image with target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
static CCMenuItemImage* itemFromNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** initializes a menu item with a normal, selected and disabled image with target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
bool initFromNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, SelectorProtocol* target, SEL_MenuHandler selector);
|
2010-08-10 12:49:16 +08:00
|
|
|
};
|
|
|
|
|
2010-09-29 09:44:57 +08:00
|
|
|
/** @brief A CCMenuItemToggle
|
2010-08-10 12:49:16 +08:00
|
|
|
A simple container class that "toggles" it's inner items
|
|
|
|
The inner itmes can be any MenuItem
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
class CC_DLL CCMenuItemToggle : public CCMenuItem, public CCRGBAProtocol
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
/** conforms with CCRGBAProtocol protocol */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(GLubyte, m_cOpacity, Opacity);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** conforms with CCRGBAProtocol protocol */
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(ccColor3B, m_tColor, Color);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** returns the selected item */
|
2011-03-07 17:11:57 +08:00
|
|
|
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.
|
2010-08-10 12:49:16 +08:00
|
|
|
@since v0.7.2
|
|
|
|
*/
|
2011-03-07 17:11:57 +08:00
|
|
|
CC_PROPERTY(CCMutableArray<CCMenuItem*>*, m_pSubItems, SubItems);
|
2010-08-10 12:49:16 +08:00
|
|
|
public:
|
2011-04-01 16:06:53 +08:00
|
|
|
CCMenuItemToggle()
|
|
|
|
: m_cOpacity(0)
|
2011-06-10 17:51:37 +08:00
|
|
|
, m_uSelectedIndex(0)
|
|
|
|
, m_pSubItems(NULL)
|
2011-04-01 16:06:53 +08:00
|
|
|
{}
|
2010-08-10 12:49:16 +08:00
|
|
|
virtual ~CCMenuItemToggle();
|
|
|
|
/** creates a menu item from a list of items with a target/selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
static CCMenuItemToggle* itemWithTarget(SelectorProtocol* target, SEL_MenuHandler selector, CCMenuItem* item, ...);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** initializes a menu item from a list of items with a target selector */
|
2010-11-17 16:31:48 +08:00
|
|
|
bool initWithTarget(SelectorProtocol* target, SEL_MenuHandler selector, CCMenuItem* item, va_list args);
|
2010-08-10 12:49:16 +08:00
|
|
|
/** return the selected item */
|
|
|
|
CCMenuItem* selectedItem();
|
|
|
|
// super methods
|
|
|
|
virtual void activate();
|
|
|
|
virtual void selected();
|
|
|
|
virtual void unselected();
|
|
|
|
virtual void setIsEnabled(bool var);
|
2010-08-31 11:20:37 +08:00
|
|
|
|
|
|
|
virtual CCRGBAProtocol* convertToRGBAProtocol() { return (CCRGBAProtocol*)this; }
|
2010-08-10 12:49:16 +08:00
|
|
|
};
|
2011-03-19 10:59:01 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
#endif //__CCMENU_ITEM_H__
|