axmol/cocos/2d/CCMenuItem.h

566 lines
20 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
2011-07-04 14:47:32 +08:00
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies 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 "2d/CCNode.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "base/CCProtocols.h"
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
typedef std::function<void(Ref*)> ccMenuCallback;
class Label;
class LabelAtlas;
class Sprite;
class SpriteFrame;
#define kItemSize 32
2012-02-02 10:39:36 +08:00
2012-06-20 18:09:11 +08:00
/**
* @addtogroup GUI
* @{
* @addtogroup menu
* @{
*/
/** @brief MenuItem base class
2012-04-18 18:43:45 +08:00
*
* Subclass MenuItem (or any subclass) to create your custom MenuItem objects.
2012-04-18 18:43:45 +08:00
*/
class CC_DLL MenuItem : public Node
2012-04-18 18:43:45 +08:00
{
public:
/** Creates a MenuItem with no target/selector */
static MenuItem* create();
/** Creates a MenuItem with a target/selector */
CC_DEPRECATED_ATTRIBUTE static MenuItem* create(Ref *rec, SEL_MenuHandler selector);
/** Creates a MenuItem with a target/selector */
static MenuItem* create(const ccMenuCallback& callback);
2012-04-18 18:43:45 +08:00
/** Returns the outside box */
Rect rect() const;
2012-04-18 18:43:45 +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();
/** returns whether or not the item is enabled */
2013-07-04 12:50:17 +08:00
virtual bool isEnabled() const;
/** enables or disables the item */
virtual void setEnabled(bool value);
/** returns whether or not the item is selected */
2013-07-04 12:50:17 +08:00
virtual bool isSelected() const;
/** set the callback to the menu item
* @code
* In js,can contain two params,the second param is jsptr
* @endcode
* @lua NA
*/
void setCallback(const ccMenuCallback& callback);
/** set the target/selector of the menu item
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE void setTarget(Ref *rec, SEL_MenuHandler selector);
virtual std::string getDescription() const override;
CC_CONSTRUCTOR_ACCESS:
/**
* @js ctor
*/
MenuItem()
: _selected(false)
, _enabled(false)
, _callback(nullptr)
, _target(nullptr)
{}
/**
* @js NA
* @lua NA
*/
virtual ~MenuItem();
/** Initializes a MenuItem with a target/selector
* @js NA
* @lua NA
*/
bool initWithCallback(const ccMenuCallback& callback);
/** Initializes a MenuItem with a target/selector
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Ref *rec, SEL_MenuHandler selector);
protected:
bool _selected;
bool _enabled;
// callback
ccMenuCallback _callback;
// If using the old API, the _target needs to be retained / released
Ref *_target;
private:
CC_DISALLOW_COPY_AND_ASSIGN(MenuItem);
2012-04-18 18:43:45 +08:00
};
/** @brief An abstract class for "label" MenuItemLabel items
Any Node that supports the LabelProtocol protocol can be added.
2012-04-18 18:43:45 +08:00
Supported nodes:
- BitmapFontAtlas
- LabelAtlas
- LabelTTF
- Label
2012-04-18 18:43:45 +08:00
*/
class CC_DLL MenuItemLabel : public MenuItem
2012-04-18 18:43:45 +08:00
{
public:
/** creates a MenuItemLabel with a Label, target and selector */
CC_DEPRECATED_ATTRIBUTE static MenuItemLabel * create(Node*label, Ref* target, SEL_MenuHandler selector);
/** creates a MenuItemLabel with a Label and a callback */
static MenuItemLabel * create(Node*label, const ccMenuCallback& callback);
/** creates a MenuItemLabel with a Label. Target and selector will be nil */
static MenuItemLabel* create(Node *label);
2012-04-18 18:43:45 +08:00
/** sets a new string to the inner label */
void setString(const std::string& label);
/** Gets the color that will be used to disable the item */
inline const Color3B& getDisabledColor() const { return _disabledColor; };
/** Sets the color that will be used to disable the item */
inline void setDisabledColor(const Color3B& color) { _disabledColor = color; };
/** Gets the label that is rendered. */
inline Node* getLabel() const { return _label; };
/** Sets the label that is rendered. */
void setLabel(Node* node);
// Overrides
virtual void activate() override;
virtual void selected() override;
virtual void unselected() override;
virtual void setEnabled(bool enabled) override;
2012-04-18 18:43:45 +08:00
CC_CONSTRUCTOR_ACCESS:
/**
* @js ctor
*/
MenuItemLabel()
: _originalScale(0.0)
, _label(nullptr)
{}
/**
* @js NA
* @lua NA
*/
virtual ~MenuItemLabel();
/** initializes a MenuItemLabel with a Label, target and selector */
bool initWithLabel(Node* label, const ccMenuCallback& callback);
/** initializes a MenuItemLabel with a Label, target and selector */
CC_DEPRECATED_ATTRIBUTE bool initWithLabel(Node* label, Ref* target, SEL_MenuHandler selector);
protected:
Color3B _colorBackup;
float _originalScale;
/** the color that will be used to disable the item */
Color3B _disabledColor;
/** Label that is rendered. It can be any Node that implements the LabelProtocol */
Node* _label;
private:
CC_DISALLOW_COPY_AND_ASSIGN(MenuItemLabel);
2012-04-18 18:43:45 +08:00
};
/** @brief A MenuItemAtlasFont
2012-04-18 18:43:45 +08:00
Helper class that creates a MenuItemLabel class with a LabelAtlas
*/
class CC_DLL MenuItemAtlasFont : public MenuItemLabel
2012-04-18 18:43:45 +08:00
{
public:
/** creates a menu item from a string and atlas with a target/selector */
2013-12-05 17:19:01 +08:00
static MenuItemAtlasFont* create(const std::string& value, const std::string& 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 MenuItemAtlasFont* create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, Ref* target, SEL_MenuHandler selector);
/** creates a menu item from a string and atlas. Use it with MenuItemToggle */
2013-12-05 17:19:01 +08:00
static MenuItemAtlasFont* create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback);
CC_CONSTRUCTOR_ACCESS:
/**
* @js ctor
*/
MenuItemAtlasFont(){}
/**
* @js NA
* @lua NA
*/
virtual ~MenuItemAtlasFont(){}
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 std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, Ref* target, SEL_MenuHandler selector);
/** initializes a menu item from a string and atlas with a target/selector */
2013-12-05 17:19:01 +08:00
bool initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback);
private:
CC_DISALLOW_COPY_AND_ASSIGN(MenuItemAtlasFont);
2012-04-18 18:43:45 +08:00
};
/** @brief A MenuItemFont
Helper class that creates a MenuItemLabel class with a Label
2012-04-18 18:43:45 +08:00
*/
class CC_DLL MenuItemFont : public MenuItemLabel
2012-04-18 18:43:45 +08:00
{
public:
/** creates a menu item from a string without target/selector. To be used with MenuItemToggle */
static MenuItemFont * create(const std::string& value = "");
2012-04-18 18:43:45 +08:00
/** creates a menu item from a string with a target/selector */
CC_DEPRECATED_ATTRIBUTE static MenuItemFont * create(const std::string& value, Ref* target, SEL_MenuHandler selector);
/** creates a menu item from a string with a target/selector */
static MenuItemFont * create(const std::string& value, const ccMenuCallback& callback);
/** set default font size */
2013-12-05 17:19:01 +08:00
static void setFontSize(int size);
/** get default font size */
2013-12-05 17:19:01 +08:00
static int getFontSize();
CC_DEPRECATED_ATTRIBUTE static int fontSize() { return MenuItemFont::getFontSize(); };
/** set the default font name */
static void setFontName(const std::string& name);
/** get the default font name */
static const std::string& getFontName();
CC_DEPRECATED_ATTRIBUTE static const std::string& fontName() { return MenuItemFont::getFontName(); };
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
* @js setFontSize
2012-02-02 10:39:36 +08:00
*/
2013-12-05 17:19:01 +08:00
void setFontSizeObj(int size);
2012-04-18 18:43:45 +08:00
/** get font size
* @js getFontSize
*/
2013-12-05 17:19:01 +08:00
int getFontSizeObj() const;
CC_DEPRECATED_ATTRIBUTE int fontSizeObj() const { return getFontSizeObj(); };
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
* @js setFontName
2012-02-02 10:39:36 +08:00
*/
void setFontNameObj(const std::string& name);
/** returns the name of the Font
* @js getFontNameObj
*/
const std::string& getFontNameObj() const;
/** deprecated Use getFontNameObj() instead */
CC_DEPRECATED_ATTRIBUTE const std::string& fontNameObj() const { return getFontNameObj(); }
2012-04-18 18:43:45 +08:00
CC_CONSTRUCTOR_ACCESS:
/**
* @js ctor
*/
MenuItemFont();
/**
* @js NA
* @lua NA
*/
virtual ~MenuItemFont();
/** initializes a menu item from a string with a target/selector */
CC_DEPRECATED_ATTRIBUTE bool initWithString(const std::string& value, Ref* target, SEL_MenuHandler selector);
/** initializes a menu item from a string with a target/selector */
bool initWithString(const std::string& value, const ccMenuCallback& callback);
2012-02-02 10:39:36 +08:00
protected:
2013-12-05 17:19:01 +08:00
int _fontSize;
std::string _fontName;
private:
CC_DISALLOW_COPY_AND_ASSIGN(MenuItemFont);
2012-04-18 18:43:45 +08:00
};
/** @brief MenuItemSprite accepts Node<RGBAProtocol> objects as items.
2012-04-18 18:43:45 +08:00
The images has 3 different states:
- unselected image
- selected image
- disabled image
@since v0.8.0
*/
class CC_DLL MenuItemSprite : public MenuItem
2012-04-18 18:43:45 +08:00
{
public:
/** creates a menu item with a normal, selected and disabled image*/
static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite = nullptr);
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 MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Ref* target, SEL_MenuHandler selector);
/** creates a menu item with a normal,selected and disabled image with target/selector */
CC_DEPRECATED_ATTRIBUTE static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Ref* target, SEL_MenuHandler selector);
/** creates a menu item with a normal and selected image with a callable object */
static MenuItemSprite * create(Node* normalSprite, Node* 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 MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, const ccMenuCallback& callback);
/** Gets the image used when the item is not selected */
inline Node* getNormalImage() const { return _normalImage; };
/** Sets the image used when the item is not selected */
void setNormalImage(Node* image);
/** Gets the image used when the item is selected */
inline Node* getSelectedImage() const { return _selectedImage; };
/** Sets the image used when the item is selected */
void setSelectedImage(Node* image);
/** Gets the image used when the item is disabled */
inline Node* getDisabledImage() const { return _disabledImage; };
/** Sets the image used when the item is disabled */
void setDisabledImage(Node* image);
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
CC_CONSTRUCTOR_ACCESS:
MenuItemSprite()
:_normalImage(nullptr)
,_selectedImage(nullptr)
,_disabledImage(nullptr)
{}
/** initializes a menu item with a normal, selected and disabled image with target/selector */
CC_DEPRECATED_ATTRIBUTE bool initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Ref* target, SEL_MenuHandler selector);
/** initializes a menu item with a normal, selected and disabled image with a callable object */
bool initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, const ccMenuCallback& callback);
protected:
virtual void updateImagesVisibility();
/** the image used when the item is not selected */
Node* _normalImage;
/** the image used when the item is selected */
Node* _selectedImage;
/** the image used when the item is disabled */
Node* _disabledImage;
private:
CC_DISALLOW_COPY_AND_ASSIGN(MenuItemSprite);
2012-04-18 18:43:45 +08:00
};
/** @brief MenuItemImage accepts images as items.
2012-04-18 18:43:45 +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
*/
class CC_DLL MenuItemImage : public MenuItemSprite
2012-04-18 18:43:45 +08:00
{
public:
/** Creates an MenuItemImage. */
static MenuItemImage* create();
2012-04-18 18:43:45 +08:00
/** creates a menu item with a normal and selected image*/
static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage);
2012-04-18 18:43:45 +08:00
/** creates a menu item with a normal,selected and disabled image*/
static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage, const std::string& 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 MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage, Ref* target, SEL_MenuHandler selector);
/** creates a menu item with a normal and selected image with a callable object */
static MenuItemImage* create(const std::string&normalImage, const std::string&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 MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector);
/** creates a menu item with a normal,selected and disabled image with a callable object */
static MenuItemImage* create(const std::string&normalImage, const std::string&selectedImage, const std::string&disabledImage, const ccMenuCallback& callback);
/** sets the sprite frame for the normal image */
void setNormalSpriteFrame(SpriteFrame* frame);
/** sets the sprite frame for the selected image */
void setSelectedSpriteFrame(SpriteFrame* frame);
/** sets the sprite frame for the disabled image */
void setDisabledSpriteFrame(SpriteFrame* frame);
CC_CONSTRUCTOR_ACCESS:
/**
* @js ctor
*/
MenuItemImage(){}
/**
* @js NA
* @lua NA
*/
virtual ~MenuItemImage(){}
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 std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector);
/** initializes a menu item with a normal, selected and disabled image with a callable object */
bool initWithNormalImage(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, const ccMenuCallback& callback);
private:
CC_DISALLOW_COPY_AND_ASSIGN(MenuItemImage);
2012-04-18 18:43:45 +08:00
};
/** @brief A MenuItemToggle
2012-04-18 18:43:45 +08:00
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
*/
class CC_DLL MenuItemToggle : public MenuItem
2012-04-18 18:43:45 +08:00
{
public:
2013-12-07 14:24:31 +08:00
/** creates a menu item from a Array with a target selector
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE static MenuItemToggle * createWithTarget(Ref* target, SEL_MenuHandler selector, const Vector<MenuItem*>& menuItems);
2013-12-07 14:24:31 +08:00
/** creates a menu item from a list of items with a target/selector
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE static MenuItemToggle* createWithTarget(Ref* target, SEL_MenuHandler selector, MenuItem* item, ...)CC_REQUIRES_NULL_TERMINATION;
2013-12-07 14:24:31 +08:00
/** creates a menu item from a Array with a callable object */
2013-12-07 14:24:31 +08:00
static MenuItemToggle * createWithCallback(const ccMenuCallback& callback, const Vector<MenuItem*>& menuItems);
/** creates a menu item from a list of items with a callable object */
static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, MenuItem* item, ...) CC_REQUIRES_NULL_TERMINATION;
/** creates a menu item with no target/selector and no items */
static MenuItemToggle* create();
/** creates a menu item with a item */
static MenuItemToggle* create(MenuItem *item);
2012-04-18 18:43:45 +08:00
/** add more menu item */
void addSubItem(MenuItem *item);
2012-04-18 18:43:45 +08:00
/** return the selected item */
MenuItem* getSelectedItem();
/** @deprecated Use getSelectedItem() instead */
CC_DEPRECATED_ATTRIBUTE MenuItem* selectedItem() { return getSelectedItem(); }
/** Gets the index of the selected item */
inline unsigned int getSelectedIndex() const { return _selectedIndex; };
/** Sets the index of the selected item */
void setSelectedIndex(unsigned int index);
/** Gets the array 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
* @js NA
* @lua NA
*/
inline const Vector<MenuItem*>& getSubItems() const { return _subItems; };
inline Vector<MenuItem*>& getSubItems() { return _subItems; };
/** Sets the array that contains the subitems. */
inline void setSubItems(const Vector<MenuItem*>& items) {
_subItems = items;
}
// Overrides
virtual void activate() override;
virtual void selected() override;
virtual void unselected() override;
virtual void setEnabled(bool var) override;
CC_CONSTRUCTOR_ACCESS:
/**
* @js ctor
*/
MenuItemToggle()
: _selectedIndex(0)
{}
/**
* @js NA
* @lua NA
*/
virtual ~MenuItemToggle();
/** initializes a menu item from a list of items with a target selector
* @js NA
* @lua NA
*/
CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Ref* target, SEL_MenuHandler selector, MenuItem* item, va_list args);
/** initializes a menu item from a list of items with a callable object */
bool initWithCallback(const ccMenuCallback& callback, MenuItem* item, va_list args);
/** initializes a menu item with a item */
bool initWithItem(MenuItem *item);
protected:
/** returns the selected item */
unsigned int _selectedIndex;
/** Array 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
*/
Vector<MenuItem*> _subItems;
private:
CC_DISALLOW_COPY_AND_ASSIGN(MenuItemToggle);
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__