axmol/cocos2dx/menu_nodes/CCMenuItem.h

413 lines
17 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
2011-07-04 14:47:32 +08:00
Copyright (c) 2008-2011 Ricardo Quesada
Copyright (c) 2011 Zynga Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCMENU_ITEM_H__
#define __CCMENU_ITEM_H__
// C++ includes
#include <functional>
// cocos2d includes
#include "base_nodes/CCNode.h"
2010-08-10 12:49:16 +08:00
#include "CCProtocols.h"
#include "cocoa/CCArray.h"
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
typedef std::function<void(CCObject*)> ccMenuCallback;
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-06-20 18:09:11 +08:00
/**
* @addtogroup GUI
* @{
* @addtogroup menu
* @{
*/
2012-04-18 18:43:45 +08:00
/** @brief CCMenuItem base class
*
* Subclass CCMenuItem (or any subclass) to create your custom CCMenuItem objects.
*/
2013-02-27 17:23:39 +08:00
class CC_DLL CCMenuItem : public CCNodeRGBA
2012-04-18 18:43:45 +08:00
{
public:
CCMenuItem()
: _selected(false)
, _enabled(false)
, _scriptTapHandler(0)
, _callback(nullptr)
, _target(NULL)
2012-04-18 18:43:45 +08:00
{}
virtual ~CCMenuItem();
2012-07-18 08:08:45 +08:00
/** Creates a CCMenuItem with no target/selector */
static CCMenuItem* create();
2012-04-18 18:43:45 +08:00
/** Creates a CCMenuItem with a target/selector */
CC_DEPRECATED_ATTRIBUTE static CCMenuItem* create(CCObject *rec, SEL_MenuHandler selector);
/** Creates a CCMenuItem with a target/selector */
static CCMenuItem* create(const ccMenuCallback& callback);
/** Initializes a CCMenuItem with a target/selector */
bool initWithCallback(const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
/** Initializes a CCMenuItem with a target/selector */
CC_DEPRECATED_ATTRIBUTE bool initWithTarget( CCObject *rec, SEL_MenuHandler selector);
2012-04-18 18:43:45 +08:00
/** 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 registerScriptTapHandler(int nHandler);
virtual void unregisterScriptTapHandler(void);
int getScriptTapHandler() { return _scriptTapHandler; };
virtual bool isEnabled();
//@note: It's 'setIsEnable' in cocos2d-iphone.
virtual void setEnabled(bool value);
virtual bool isSelected();
2013-02-27 17:23:39 +08:00
virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
2012-04-18 18:43:45 +08:00
/** set the target/selector of the menu item*/
CC_DEPRECATED_ATTRIBUTE void setTarget(CCObject *rec, SEL_MenuHandler selector);
/** set the callback to the menu item */
void setCallback(const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
protected:
bool _selected;
bool _enabled;
int _scriptTapHandler;
// callback
ccMenuCallback _callback;
// If using the old API, the _target needs to be retained / released
CCObject *_target;
2012-04-18 18:43:45 +08:00
};
/** @brief An abstract class for "label" CCMenuItemLabel items
Any CCNode that supports the CCLabelProtocol protocol can be added.
Supported nodes:
- CCBitmapFontAtlas
- CCLabelAtlas
- CCLabelTTF
*/
2013-02-27 17:23:39 +08:00
class CC_DLL CCMenuItemLabel : public CCMenuItem
2012-04-18 18:43:45 +08:00
{
/** the color that will be used to disable the item */
CC_PROPERTY_PASS_BY_REF(ccColor3B, _disabledColor, DisabledColor);
2012-04-18 18:43:45 +08:00
/** Label that is rendered. It can be any CCNode that implements the CCLabelProtocol */
CC_PROPERTY(CCNode*, _label, Label);
2012-04-18 18:43:45 +08:00
public:
CCMenuItemLabel()
: _label(NULL)
, _originalScale(0.0)
2012-04-18 18:43:45 +08:00
{}
virtual ~CCMenuItemLabel();
2012-04-18 18:43:45 +08:00
/** creates a CCMenuItemLabel with a Label, target and selector */
CC_DEPRECATED_ATTRIBUTE static CCMenuItemLabel * create(CCNode*label, CCObject* target, SEL_MenuHandler selector);
/** creates a CCMenuItemLabel with a Label and a callback */
static CCMenuItemLabel * create(CCNode*label, const ccMenuCallback& callback);
/** creates a CCMenuItemLabel with a Label. Target and selector will be nil */
static CCMenuItemLabel* create(CCNode *label);
2012-04-18 18:43:45 +08:00
/** initializes a CCMenuItemLabel with a Label, target and selector */
CC_DEPRECATED_ATTRIBUTE bool initWithLabel(CCNode* label, CCObject* target, SEL_MenuHandler selector);
/** initializes a CCMenuItemLabel with a Label, target and selector */
bool initWithLabel(CCNode* label, const ccMenuCallback& callback);
2012-04-18 18:43:45 +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 setEnabled changes the RGB color of the font
2012-02-02 10:39:36 +08:00
*/
virtual void setEnabled(bool enabled);
2012-04-18 18:43:45 +08:00
protected:
ccColor3B _colorBackup;
float _originalScale;
2012-04-18 18:43:45 +08:00
};
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(){}
2012-04-18 18:43:45 +08:00
/** 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);
2012-04-18 18:43:45 +08:00
/** creates a menu item from a string and atlas. Use it with MenuItemToggle */
CC_DEPRECATED_ATTRIBUTE static CCMenuItemAtlasFont* create(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. Use it with MenuItemToggle */
static CCMenuItemAtlasFont* create(const char *value, const char *charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
/** initializes a menu item from a string and atlas with a target/selector */
CC_DEPRECATED_ATTRIBUTE bool initWithString(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, const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
};
2012-04-18 18:43:45 +08:00
/** @brief A CCMenuItemFont
Helper class that creates a CCMenuItemLabel class with a Label
*/
class CC_DLL CCMenuItemFont : public CCMenuItemLabel
{
public:
CCMenuItemFont() : _fontSize(0), _fontName(""){}
2012-04-18 18:43:45 +08:00
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();
2012-04-18 18:43:45 +08:00
/** creates a menu item from a string without target/selector. To be used with CCMenuItemToggle */
static CCMenuItemFont * create(const char *value);
2012-04-18 18:43:45 +08:00
/** creates a menu item from a string with a target/selector */
CC_DEPRECATED_ATTRIBUTE static CCMenuItemFont * create(const char *value, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item from a string with a target/selector */
static CCMenuItemFont * create(const char *value, const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
/** initializes a menu item from a string with a target/selector */
CC_DEPRECATED_ATTRIBUTE bool initWithString(const char *value, CCObject* target, SEL_MenuHandler selector);
/** initializes a menu item from a string with a target/selector */
bool initWithString(const char *value, const ccMenuCallback& callback);
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
unsigned int _fontSize;
std::string _fontName;
2012-04-18 18:43:45 +08:00
};
2012-04-18 18:43:45 +08:00
/** @brief CCMenuItemSprite accepts CCNode<CCRGBAProtocol> objects as items.
The images has 3 different states:
- unselected image
- selected image
- disabled image
@since v0.8.0
*/
2013-02-27 17:23:39 +08:00
class CC_DLL CCMenuItemSprite : public CCMenuItem
2012-04-18 18:43:45 +08:00
{
/** the image used when the item is not selected */
CC_PROPERTY(CCNode*, _normalImage, NormalImage);
2012-04-18 18:43:45 +08:00
/** the image used when the item is selected */
CC_PROPERTY(CCNode*, _selectedImage, SelectedImage);
2012-04-18 18:43:45 +08:00
/** the image used when the item is disabled */
CC_PROPERTY(CCNode*, _disabledImage, DisabledImage);
2012-04-18 18:43:45 +08:00
public:
CCMenuItemSprite()
:_normalImage(NULL)
,_selectedImage(NULL)
,_disabledImage(NULL)
2012-04-18 18:43:45 +08:00
{}
2012-04-18 18:43:45 +08:00
/** creates a menu item with a normal, selected and disabled image*/
static CCMenuItemSprite * create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite = NULL);
2012-04-18 18:43:45 +08:00
/** creates a menu item with a normal and selected image with target/selector */
CC_DEPRECATED_ATTRIBUTE 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 */
CC_DEPRECATED_ATTRIBUTE static CCMenuItemSprite * create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a normal and selected image with a callable object */
static CCMenuItemSprite * create(CCNode* normalSprite, CCNode* selectedSprite, const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
/** creates a menu item with a normal,selected and disabled image with target/selector */
static CCMenuItemSprite * create(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
/** initializes a menu item with a normal, selected and disabled image with target/selector */
CC_DEPRECATED_ATTRIBUTE bool initWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, CCObject* target, SEL_MenuHandler selector);
/** initializes a menu item with a normal, selected and disabled image with a callable object */
bool initWithNormalSprite(CCNode* normalSprite, CCNode* selectedSprite, CCNode* disabledSprite, const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
/**
@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 setEnabled(bool bEnabled);
2012-02-02 10:39:36 +08:00
virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
protected:
virtual void updateImagesVisibility();
2012-04-18 18:43:45 +08:00
};
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(){}
2012-04-18 18:43:45 +08:00
/** creates a menu item with a normal and selected image*/
static CCMenuItemImage* create(const char *normalImage, const char *selectedImage);
2012-04-18 18:43:45 +08:00
/** creates a menu item with a normal,selected and disabled image*/
static CCMenuItemImage* create(const char *normalImage, const char *selectedImage, const char *disabledImage);
2012-04-18 18:43:45 +08:00
/** creates a menu item with a normal and selected image with target/selector */
CC_DEPRECATED_ATTRIBUTE static CCMenuItemImage* create(const char *normalImage, const char *selectedImage, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a normal and selected image with a callable object */
static CCMenuItemImage* create(const char *normalImage, const char *selectedImage, const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
/** creates a menu item with a normal,selected and disabled image with target/selector */
CC_DEPRECATED_ATTRIBUTE static CCMenuItemImage* create(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector);
/** creates a menu item with a normal,selected and disabled image with a callable object */
static CCMenuItemImage* create(const char *normalImage, const char *selectedImage, const char *disabledImage, const ccMenuCallback& callback);
bool init();
2012-04-18 18:43:45 +08:00
/** initializes a menu item with a normal, selected and disabled image with target/selector */
CC_DEPRECATED_ATTRIBUTE bool initWithNormalImage(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 a callable object */
bool initWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, const ccMenuCallback& callback);
/** 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.
*/
static CCMenuItemImage* create();
2012-04-18 18:43:45 +08:00
};
2012-04-18 18:43:45 +08:00
/** @brief A CCMenuItemToggle
A simple container class that "toggles" it's inner items
The inner items can be any MenuItem
2012-04-18 18:43:45 +08:00
*/
2013-02-27 17:23:39 +08:00
class CC_DLL CCMenuItemToggle : public CCMenuItem
2012-04-18 18:43:45 +08:00
{
/** returns the selected item */
CC_PROPERTY(unsigned int, _selectedIndex, SelectedIndex);
2012-04-18 18:43:45 +08:00
/** 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
*/
CC_PROPERTY(CCArray*, _subItems, SubItems);
2012-04-18 18:43:45 +08:00
public:
CCMenuItemToggle()
: _selectedIndex(0)
, _subItems(NULL)
2012-04-18 18:43:45 +08:00
{}
virtual ~CCMenuItemToggle();
2013-05-07 15:51:58 +08:00
/** creates a menu item from a CCArray with a target selector */
CC_DEPRECATED_ATTRIBUTE static CCMenuItemToggle * createWithTarget(CCObject* target, SEL_MenuHandler selector, CCArray* menuItems);
/** creates a menu item from a CCArray with a callable object */
static CCMenuItemToggle * createWithCallback(const ccMenuCallback& callback, CCArray* menuItems);
2012-04-18 18:43:45 +08:00
/** creates a menu item from a list of items with a target/selector */
CC_DEPRECATED_ATTRIBUTE static CCMenuItemToggle* createWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, ...);
/** creates a menu item from a list of items with a callable object */
static CCMenuItemToggle* createWithCallback(const ccMenuCallback& callback, CCMenuItem* item, ...);
/** creates a menu item with no target/selector and no items */
static CCMenuItemToggle* create();
2012-04-18 18:43:45 +08:00
/** initializes a menu item from a list of items with a target selector */
CC_DEPRECATED_ATTRIBUTE bool initWithTarget(CCObject* target, SEL_MenuHandler selector, CCMenuItem* item, va_list args);
/** initializes a menu item from a list of items with a callable object */
bool initWithCallback(const ccMenuCallback& callback, CCMenuItem* item, va_list args);
2012-04-18 18:43:45 +08:00
/** creates a menu item with a item */
static CCMenuItemToggle* create(CCMenuItem *item);
2012-04-18 18:43:45 +08:00
/** 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);
2012-04-18 18:43:45 +08:00
virtual void setOpacityModifyRGB(bool bValue) {CC_UNUSED_PARAM(bValue);}
virtual bool isOpacityModifyRGB(void) { return false;}
2012-04-18 18:43:45 +08:00
};
2012-06-20 18:09:11 +08:00
// end of GUI group
/// @}
/// @}
2012-04-18 18:43:45 +08:00
NS_CC_END
2011-07-26 18:41:58 +08:00
#endif //__CCMENU_ITEM_H__