axmol/core/2d/MenuItem.h

515 lines
16 KiB
C
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2019-11-23 20:27:39 +08:00
https://axmol.dev/
2019-11-23 20:27:39 +08:00
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>
Release 2.1.5 (#2076) * Fix unexpected libpng used * Fix string format incorrect for tests * Fix #1751, use coroutine control AutoTest flow * Update CHANGELOG.md * Added OpenType font (.otf) to the noCompress list. (#2077) * Update 1k & copyright notice in some sources * Move doctest to axmol 3rdparty * Fix ci * Update 1kdist to v90 * Update 1kiss.ps1 * DrawNodeV2 0.95.1 (#2079) * Rename remaining legacy engine related spells and improve code style * Update 3rdparty README.md * Fix checkReallySupportsASTC does not work on ios device reported by @BIGCATDOG in https://github.com/axmolengine/axmol/issues/2078 * Fix ci * FastRNG: add missing include for AXASSERT (#2081) * Delete unused files * Improve FileUtils - Rename FileUtils::createDirectory to FileUtils::createDirectories - Use splitpath_cb to optimize FileUtils::createDirectories - Rename FileUtils::getFileShortName to FileUtils::getPathBaseName - Rename FileUtils::getFileExtension to FileUtils::getPathExtension - Add FileUtils::getPathDirName - Add FileUtils::getPathBaseNameNoExtension - Mark all renamed FileUtils stubs old name deprecated - Mark all FileUtils offthread APIs deprecated * Update box2d to v2.4.2 * Disable /sdl checks explicitly for winuwp For axmol deprecated policy, we need disable /sdl checks explicitly to avoid compiler traits invoking deprecated functions as error * Update cppwinrt to 2.0.240405.15 * Update simdjson to 3.10.0 * Fix box2d testbed compile error * Improve file path to url * Fix FileUtils::createDirectories unix logic * axmol-cmdline: remove arch suffix for host build output directory * Update CHANGELOG.md * Update lua bindings --------- Co-authored-by: Dani Alias <danielgutierrezalias@gmail.com> Co-authored-by: aismann <icesoft@freenet.de> Co-authored-by: smilediver <smilediver@outlook.com>
2024-08-11 21:11:35 +08:00
// axmol includes
#include "2d/Node.h"
#include "base/Protocols.h"
2019-11-23 20:27:39 +08:00
namespace ax
{
2019-11-23 20:27:39 +08:00
typedef std::function<void(Object*)> ccMenuCallback;
2019-11-23 20:27:39 +08:00
class Label;
class LabelAtlas;
class Sprite;
class SpriteFrame;
#define kItemSize 32
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _2d
* @{
*/
/** @brief MenuItem base class.
*
* Subclass MenuItem (or any subclass) to create your custom MenuItem objects.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL MenuItem : public Node
2019-11-23 20:27:39 +08:00
{
public:
/** Creates a MenuItem with no target/selector. */
static MenuItem* create();
/** Creates a MenuItem with a target/selector. */
static MenuItem* create(const ccMenuCallback& callback);
/** Returns the outside box. */
Rect rect() const;
/** 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. */
virtual bool isEnabled() const;
/** Enables or disables the item. */
virtual void setEnabled(bool value);
/** Returns whether or not the item is selected. */
virtual bool isSelected() const;
/** Set the callback to the menu item.
2021-12-25 10:04:45 +08:00
* @code
* In js,can contain two params,the second param is jsptr.
* @endcode
* @lua NA
*/
2019-11-23 20:27:39 +08:00
void setCallback(const ccMenuCallback& callback);
/**
* @js NA
*/
virtual std::string getDescription() const override;
2021-12-25 10:04:45 +08:00
/**
* @js ctor
*/
MenuItem() : _selected(false), _enabled(false), _callback(nullptr) {}
2019-11-23 20:27:39 +08:00
/**
* @js NA
* @lua NA
*/
virtual ~MenuItem();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Initializes a MenuItem with a target/selector.
* @lua NA
*/
bool initWithCallback(const ccMenuCallback& callback);
protected:
2021-12-25 10:04:45 +08:00
bool _selected;
bool _enabled;
2019-11-23 20:27:39 +08:00
// callback
ccMenuCallback _callback;
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(MenuItem);
2019-11-23 20:27:39 +08:00
};
/** @brief An abstract class for "label" MenuItemLabel items.
Any Node that supports the LabelProtocol protocol can be added.
Supported nodes:
- BitmapFontAtlas
- LabelAtlas
- LabelTTF
- Label
*/
2022-07-16 10:43:05 +08:00
class AX_DLL MenuItemLabel : public MenuItem
2019-11-23 20:27:39 +08:00
{
public:
/** Creates a MenuItemLabel with a Label and a callback. */
2021-12-25 10:04:45 +08:00
static MenuItemLabel* create(Node* label, const ccMenuCallback& callback);
2019-11-23 20:27:39 +08:00
/** Creates a MenuItemLabel with a Label. Target and selector will be nil. */
2021-12-25 10:04:45 +08:00
static MenuItemLabel* create(Node* label);
2019-11-23 20:27:39 +08:00
/** Sets a new string to the inner label. */
void setString(std::string_view label);
2019-11-23 20:27:39 +08:00
/** Get the inner string of the inner label. */
std::string_view getString() const;
2019-11-23 20:27:39 +08:00
/** Gets the color that will be used when the item is disabled. */
const Color3B& getDisabledColor() const { return _disabledColor; }
/** Sets the color that will be used when the item is disabled. */
void setDisabledColor(const Color3B& color) { _disabledColor = color; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Gets the label that is rendered. */
Node* getLabel() const { return _label; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Sets the label that is rendered. */
void setLabel(Node* node);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
// Overrides
virtual void activate() override;
virtual void selected() override;
virtual void unselected() override;
virtual void setEnabled(bool enabled) override;
2021-12-25 10:04:45 +08:00
/**
* @js ctor
*/
MenuItemLabel() : _originalScale(0.0), _label(nullptr) {}
2019-11-23 20:27:39 +08:00
/**
* @js NA
* @lua NA
*/
virtual ~MenuItemLabel();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Initializes a MenuItemLabel with a Label, target and selector. */
bool initWithLabel(Node* label, const ccMenuCallback& callback);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
2021-12-25 10:04:45 +08:00
Color3B _colorBackup;
float _originalScale;
2019-11-23 20:27:39 +08:00
/** 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:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(MenuItemLabel);
2019-11-23 20:27:39 +08:00
};
/** @brief A MenuItemAtlasFont.
Helper class that creates a MenuItemLabel class with a LabelAtlas.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL MenuItemAtlasFont : public MenuItemLabel
2019-11-23 20:27:39 +08:00
{
public:
/** Creates a menu item from a string and atlas with a target/selector. */
static MenuItemAtlasFont* create(std::string_view value,
std::string_view charMapFile,
2021-12-25 10:04:45 +08:00
int itemWidth,
int itemHeight,
char startCharMap);
2019-11-23 20:27:39 +08:00
/** Creates a menu item from a string and atlas. Use it with MenuItemToggle. */
static MenuItemAtlasFont* create(std::string_view value,
std::string_view charMapFile,
2021-12-25 10:04:45 +08:00
int itemWidth,
int itemHeight,
char startCharMap,
const ccMenuCallback& callback);
/**
* @js ctor
*/
MenuItemAtlasFont() {}
2019-11-23 20:27:39 +08:00
/**
* @js NA
* @lua NA
*/
2021-12-25 10:04:45 +08:00
virtual ~MenuItemAtlasFont() {}
2019-11-23 20:27:39 +08:00
/** Initializes a menu item from a string and atlas with a target/selector. */
bool initWithString(std::string_view value,
std::string_view charMapFile,
2021-12-25 10:04:45 +08:00
int itemWidth,
int itemHeight,
char startCharMap,
const ccMenuCallback& callback);
2019-11-23 20:27:39 +08:00
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(MenuItemAtlasFont);
2019-11-23 20:27:39 +08:00
};
/** @brief A MenuItemFont.
Helper class that creates a MenuItemLabel class with a Label.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL MenuItemFont : public MenuItemLabel
2019-11-23 20:27:39 +08:00
{
public:
/** Creates a menu item from a string without target/selector. To be used with MenuItemToggle. */
static MenuItemFont* create(std::string_view value = "");
2019-11-23 20:27:39 +08:00
/** Creates a menu item from a string with a target/selector. */
static MenuItemFont* create(std::string_view value, const ccMenuCallback& callback);
2019-11-23 20:27:39 +08:00
/** Set default font size. */
static void setFontSize(int size);
/** Get default font size. */
static int getFontSize();
/** Set the default font name. */
static void setFontName(std::string_view name);
2019-11-23 20:27:39 +08:00
/** Get the default font name. */
static std::string_view getFontName();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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
* @js NA
*/
void setFontSizeObj(int size);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** get font size .
* @js getFontSize
* @js NA
*/
int getFontSizeObj() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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
* @js NA
*/
void setFontNameObj(std::string_view name);
2019-11-23 20:27:39 +08:00
/** Returns the name of the Font.
* @js getFontNameObj
* @js NA
*/
std::string_view getFontNameObj() const;
2021-12-25 10:04:45 +08:00
/**
* @js ctor
*/
MenuItemFont();
2019-11-23 20:27:39 +08:00
/**
* @js NA
* @lua NA
*/
virtual ~MenuItemFont();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Initializes a menu item from a string with a target/selector. */
bool initWithString(std::string_view value, const ccMenuCallback& callback);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
int _fontSize;
std::string _fontName;
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(MenuItemFont);
2019-11-23 20:27:39 +08:00
};
/** @brief MenuItemSprite accepts Node<RGBAProtocol> objects as items.
The images has 3 different states:
- unselected image
- selected image
- disabled image
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
@since v0.8.0
*/
2022-07-16 10:43:05 +08:00
class AX_DLL MenuItemSprite : public MenuItem
2019-11-23 20:27:39 +08:00
{
public:
/** Creates a menu item with a normal, selected and disabled image.*/
2021-12-25 10:04:45 +08:00
static MenuItemSprite* create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite = nullptr);
2019-11-23 20:27:39 +08:00
/** Creates a menu item with a normal and selected image with a callable object. */
2021-12-25 10:04:45 +08:00
static MenuItemSprite* create(Node* normalSprite, Node* selectedSprite, const ccMenuCallback& callback);
2019-11-23 20:27:39 +08:00
/** Creates a menu item with a normal,selected and disabled image with target/selector. */
2021-12-25 10:04:45 +08:00
static MenuItemSprite* create(Node* normalSprite,
Node* selectedSprite,
Node* disabledSprite,
const ccMenuCallback& callback);
2019-11-23 20:27:39 +08:00
/** Gets the image used when the item is not selected. */
Node* getNormalImage() const { return _normalImage; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Sets the image used when the item is not selected. */
void setNormalImage(Node* image);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Gets the image used when the item is selected. */
Node* getSelectedImage() const { return _selectedImage; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Sets the image used when the item is selected. */
void setSelectedImage(Node* image);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Gets the image used when the item is disabled. */
Node* getDisabledImage() const { return _disabledImage; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Sets the image used when the item is disabled. */
void setDisabledImage(Node* image);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* The item was selected (not activated), similar to "mouse-over".
@since v0.99.5
*/
virtual void selected();
/** The item was unselected. */
virtual void unselected();
/** Enables or disables the item. */
virtual void setEnabled(bool bEnabled);
2021-12-25 10:04:45 +08:00
MenuItemSprite() : _normalImage(nullptr), _selectedImage(nullptr), _disabledImage(nullptr) {}
2019-11-23 20:27:39 +08:00
/** Initializes a menu item with a normal, selected and disabled image with a callable object. */
2021-12-25 10:04:45 +08:00
bool initWithNormalSprite(Node* normalSprite,
Node* selectedSprite,
Node* disabledSprite,
const ccMenuCallback& callback);
2019-11-23 20:27:39 +08:00
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:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(MenuItemSprite);
2019-11-23 20:27:39 +08:00
};
/** @brief MenuItemImage accepts images as items.
The images has 3 different states:
- unselected image
- selected image
- disabled image
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
For best results try that all images are of the same size.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL MenuItemImage : public MenuItemSprite
2019-11-23 20:27:39 +08:00
{
public:
/** Creates an MenuItemImage. */
static MenuItemImage* create();
/** Creates a menu item with a normal and selected image.*/
static MenuItemImage* create(std::string_view normalImage, std::string_view selectedImage);
2019-11-23 20:27:39 +08:00
/** Creates a menu item with a normal,selected and disabled image.*/
static MenuItemImage* create(std::string_view normalImage,
std::string_view selectedImage,
std::string_view disabledImage);
2019-11-23 20:27:39 +08:00
/** Creates a menu item with a normal and selected image with a callable object. */
static MenuItemImage* create(std::string_view normalImage,
std::string_view selectedImage,
2021-12-25 10:04:45 +08:00
const ccMenuCallback& callback);
2019-11-23 20:27:39 +08:00
/** Creates a menu item with a normal,selected and disabled image with a callable object. */
static MenuItemImage* create(std::string_view normalImage,
std::string_view selectedImage,
std::string_view disabledImage,
2021-12-25 10:04:45 +08:00
const ccMenuCallback& callback);
2019-11-23 20:27:39 +08:00
/** 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);
2021-12-25 10:04:45 +08:00
/**
* @js ctor
*/
MenuItemImage() {}
2019-11-23 20:27:39 +08:00
/**
* @js NA
* @lua NA
*/
2021-12-25 10:04:45 +08:00
virtual ~MenuItemImage() {}
2019-11-23 20:27:39 +08:00
bool init();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Initializes a menu item with a normal, selected and disabled image with a callable object. */
bool initWithNormalImage(std::string_view normalImage,
std::string_view selectedImage,
std::string_view disabledImage,
2021-12-25 10:04:45 +08:00
const ccMenuCallback& callback);
2019-11-23 20:27:39 +08:00
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(MenuItemImage);
2019-11-23 20:27:39 +08:00
};
/** @brief A MenuItemToggle.
A simple container class that "toggles" it's inner items.
The inner items can be any MenuItem.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL MenuItemToggle : public MenuItem
2019-11-23 20:27:39 +08:00
{
public:
/**
*@brief Creates a menu item from a Array with a callable object.
*/
2021-12-25 10:04:45 +08:00
static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, const Vector<MenuItem*>& menuItems);
2019-11-23 20:27:39 +08:00
/** Creates a menu item from a list of items with a callable object. */
2021-12-25 10:04:45 +08:00
static MenuItemToggle* createWithCallback(const ccMenuCallback& callback,
MenuItem* item,
2022-07-16 10:43:05 +08:00
...) AX_REQUIRES_NULL_TERMINATION;
2019-11-23 20:27:39 +08:00
/** Creates a menu item with no target/selector and no items. */
static MenuItemToggle* create();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Creates a menu item with a item. */
2021-12-25 10:04:45 +08:00
static MenuItemToggle* create(MenuItem* item);
2019-11-23 20:27:39 +08:00
/** Add more menu item. */
2021-12-25 10:04:45 +08:00
void addSubItem(MenuItem* item);
2019-11-23 20:27:39 +08:00
/** Return the selected item. */
MenuItem* getSelectedItem();
/** Gets the index of the selected item. */
unsigned int getSelectedIndex() const { return _selectedIndex; }
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Sets the index of the selected item. */
void setSelectedIndex(unsigned int index);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** 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
*/
const Vector<MenuItem*>& getSubItems() const { return _subItems; }
Vector<MenuItem*>& getSubItems() { return _subItems; }
/** Sets the array that contains the subitems. */
2021-12-25 10:04:45 +08:00
void setSubItems(const Vector<MenuItem*>& items) { _subItems = items; }
2019-11-23 20:27:39 +08:00
// Overrides
virtual void activate() override;
virtual void selected() override;
virtual void unselected() override;
virtual void setEnabled(bool var) override;
virtual void cleanup() override;
2021-12-25 10:04:45 +08:00
/**
* @js ctor
*/
MenuItemToggle() : _selectedIndex(0), _selectedItem(nullptr) {}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Initializes a menu item from a list of items with a callable object. */
bool initWithCallback(const ccMenuCallback& callback, MenuItem* item, va_list args);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/** Initializes a menu item with a item. */
2021-12-25 10:04:45 +08:00
bool initWithItem(MenuItem* item);
2019-11-23 20:27:39 +08:00
protected:
/** Returns the selected item. */
unsigned int _selectedIndex;
MenuItem* _selectedItem;
2021-12-25 10:04:45 +08:00
/** Array that contains the subitems. You can add/remove items in runtime, and you can replace the array with a new
one.
2019-11-23 20:27:39 +08:00
@since v0.7.2
*/
Vector<MenuItem*> _subItems;
private:
2022-07-16 10:43:05 +08:00
AX_DISALLOW_COPY_AND_ASSIGN(MenuItemToggle);
2019-11-23 20:27:39 +08:00
};
// end of 2d group
/// @}
}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
#endif //__CCMENU_ITEM_H__