2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2024-07-11 23:28:31 +08:00
|
|
|
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-06-10 02:25:43 +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_H_
|
|
|
|
#define __CCMENU_H_
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/MenuItem.h"
|
|
|
|
#include "2d/Layer.h"
|
|
|
|
#include "base/Value.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
class Touch;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup _2d
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @brief A Menu for touch handling.
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
|
|
|
* Features and Limitation:
|
|
|
|
* - You can add MenuItem objects in runtime using addChild.
|
|
|
|
* - But the only accepted children are MenuItem objects.
|
|
|
|
*/
|
2022-07-16 10:43:05 +08:00
|
|
|
class AX_DLL Menu : public Node
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Menu state, it's used internally.
|
|
|
|
*/
|
|
|
|
enum class State
|
|
|
|
{
|
|
|
|
WAITING,
|
|
|
|
TRACKING_TOUCH,
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
*@brief Creates an empty Menu.
|
|
|
|
*/
|
|
|
|
static Menu* create();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** Creates a Menu with MenuItem objects. */
|
2022-07-16 10:43:05 +08:00
|
|
|
static Menu* create(MenuItem* item, ...) AX_REQUIRES_NULL_TERMINATION;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
/**
|
2019-11-23 20:27:39 +08:00
|
|
|
* Creates a Menu with a Array of MenuItem objects.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
static Menu* createWithArray(const Vector<MenuItem*>& arrayOfItems);
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Creates a Menu with it's item, then use addChild() to add
|
2019-11-23 20:27:39 +08:00
|
|
|
* other items. It is used for script, it can't be initialized with undetermined
|
|
|
|
* number of variables.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
static Menu* createWithItem(MenuItem* item);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** Creates a Menu with MenuItem objects.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
static Menu* createWithItems(MenuItem* firstItem, va_list args);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/** Align items vertically. */
|
|
|
|
void alignItemsVertically();
|
|
|
|
|
|
|
|
/** Align items vertically with padding.
|
|
|
|
@since v0.7.2
|
|
|
|
*/
|
|
|
|
void alignItemsVerticallyWithPadding(float padding);
|
|
|
|
|
|
|
|
/** Align items horizontally. */
|
|
|
|
void alignItemsHorizontally();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** Align items horizontally with padding.
|
|
|
|
@since v0.7.2
|
|
|
|
*/
|
|
|
|
void alignItemsHorizontallyWithPadding(float padding);
|
|
|
|
|
|
|
|
/** Align items in rows of columns. */
|
2022-07-16 10:43:05 +08:00
|
|
|
void alignItemsInColumns(int columns, ...) AX_REQUIRES_NULL_TERMINATION;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** Align items in rows of columns. */
|
|
|
|
void alignItemsInColumns(int columns, va_list args);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** Align items in array of columns.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
void alignItemsInColumnsWithArray(const ValueVector& rows);
|
|
|
|
|
|
|
|
/** Align items in columns of rows. */
|
2022-07-16 10:43:05 +08:00
|
|
|
void alignItemsInRows(int rows, ...) AX_REQUIRES_NULL_TERMINATION;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** Align items in columns of rows. */
|
|
|
|
void alignItemsInRows(int rows, va_list args);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/** Align items in array of rows.
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
void alignItemsInRowsWithArray(const ValueVector& columns);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the menu is enabled.
|
|
|
|
* @see `setEnabled(bool)`.
|
|
|
|
* @return whether the menu is enabled or not.
|
|
|
|
*/
|
|
|
|
virtual bool isEnabled() const { return _enabled; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether the menu is enabled. If set to false, interacting with the menu
|
|
|
|
* will have no effect.
|
|
|
|
* The default value is true, a menu is enabled by default.
|
|
|
|
* @param value true if menu is to be enabled, false if menu is to be disabled.
|
|
|
|
*/
|
|
|
|
virtual void setEnabled(bool value) { _enabled = value; };
|
|
|
|
|
2021-09-01 10:10:46 +08:00
|
|
|
virtual bool onTouchBegan(Touch* touch, Event* event);
|
|
|
|
virtual void onTouchEnded(Touch* touch, Event* event);
|
|
|
|
virtual void onTouchCancelled(Touch* touch, Event* event);
|
|
|
|
virtual void onTouchMoved(Touch* touch, Event* event);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// overrides
|
|
|
|
virtual void removeChild(Node* child, bool cleanup) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
virtual void addChild(Node* child) override;
|
|
|
|
virtual void addChild(Node* child, int zOrder) override;
|
|
|
|
virtual void addChild(Node* child, int zOrder, int tag) override;
|
2021-12-31 12:12:40 +08:00
|
|
|
virtual void addChild(Node* child, int zOrder, std::string_view name) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void onEnter() override;
|
|
|
|
virtual void onExit() override;
|
|
|
|
virtual void setOpacityModifyRGB(bool value) override;
|
|
|
|
virtual bool isOpacityModifyRGB() const override;
|
|
|
|
|
|
|
|
virtual std::string getDescription() const override;
|
|
|
|
|
2022-03-18 21:46:07 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
|
|
|
Menu() : _selectedItem(nullptr), _selectedWithCamera(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual ~Menu();
|
|
|
|
|
|
|
|
/** initializes an empty Menu */
|
|
|
|
bool init() override;
|
|
|
|
|
|
|
|
/** initializes a Menu with a NSArray of MenuItem objects */
|
|
|
|
bool initWithArray(const Vector<MenuItem*>& arrayOfItems);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** whether or not the menu will receive events */
|
|
|
|
bool _enabled;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual MenuItem* getItemForTouch(Touch* touch, const Camera* camera);
|
2019-11-23 20:27:39 +08:00
|
|
|
State _state;
|
2021-12-25 10:04:45 +08:00
|
|
|
MenuItem* _selectedItem;
|
|
|
|
const Camera* _selectedWithCamera;
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
private:
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_DISALLOW_COPY_AND_ASSIGN(Menu);
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// end of _2d group
|
|
|
|
/// @}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif //__CCMENU_H_
|