2011-03-19 10:59:01 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies Inc.
|
2011-03-19 10:59:01 +08:00
|
|
|
|
|
|
|
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_H_
|
|
|
|
#define __CCMENU_H_
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCMenuItem.h"
|
|
|
|
#include "2d/CCLayer.h"
|
2014-04-27 01:35:57 +08:00
|
|
|
#include "base/CCValue.h"
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
NS_CC_BEGIN
|
2014-08-27 13:39:50 +08:00
|
|
|
class Touch;
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* @addtogroup _2d
|
2012-06-20 18:09:11 +08:00
|
|
|
* @{
|
|
|
|
*/
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2013-07-25 20:04:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/** @brief A Menu for touch handling.
|
2012-03-22 14:22:06 +08:00
|
|
|
*
|
|
|
|
* Features and Limitation:
|
2015-03-25 18:17:58 +08:00
|
|
|
* - You can add MenuItem objects in runtime using addChild.
|
|
|
|
* - But the only accepted children are MenuItem objects.
|
2012-03-22 14:22:06 +08:00
|
|
|
*/
|
2013-12-06 18:07:16 +08:00
|
|
|
class CC_DLL Menu : public Layer
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Menu state, it's used internally.
|
|
|
|
*/
|
2013-07-26 08:47:42 +08:00
|
|
|
enum class State
|
2013-07-25 20:04:24 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
WAITING,
|
|
|
|
TRACKING_TOUCH,
|
2013-07-25 20:04:24 +08:00
|
|
|
};
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*@brief Creates an empty Menu.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static Menu* create();
|
2014-03-25 06:09:24 +08:00
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
|
|
|
// VS2013 does not support nullptr in variable args lists and variadic templates are also not supported.
|
2014-03-25 06:09:24 +08:00
|
|
|
typedef MenuItem* M;
|
|
|
|
static Menu* create(M m1, std::nullptr_t listEnd) { return variadicCreate(m1, NULL); }
|
|
|
|
static Menu* create(M m1, M m2, std::nullptr_t listEnd) { return variadicCreate(m1, m2, NULL); }
|
|
|
|
static Menu* create(M m1, M m2, M m3, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, NULL); }
|
|
|
|
static Menu* create(M m1, M m2, M m3, M m4, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, m4, NULL); }
|
|
|
|
static Menu* create(M m1, M m2, M m3, M m4, M m5, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, m4, m5, NULL); }
|
|
|
|
static Menu* create(M m1, M m2, M m3, M m4, M m5, M m6, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, m4, m5, m6, NULL); }
|
|
|
|
static Menu* create(M m1, M m2, M m3, M m4, M m5, M m6, M m7, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, m4, m5, m6, m7, NULL); }
|
|
|
|
static Menu* create(M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, m4, m5, m6, m7, m8, NULL); }
|
|
|
|
static Menu* create(M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, M m9, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, m4, m5, m6, m7, m8, m9, NULL); }
|
|
|
|
static Menu* create(M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, M m9, M m10, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, NULL); }
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
// On WP8 for lists longer than 10 items, use createWithArray or variadicCreate with NULL as the last argument.
|
2014-03-25 06:09:24 +08:00
|
|
|
static Menu* variadicCreate(MenuItem* item, ...);
|
|
|
|
#else
|
2015-03-25 18:17:58 +08:00
|
|
|
/** Creates a Menu with MenuItem objects. */
|
2013-07-24 06:20:22 +08:00
|
|
|
static Menu* create(MenuItem* item, ...) CC_REQUIRES_NULL_TERMINATION;
|
2014-03-25 06:09:24 +08:00
|
|
|
#endif
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2015-03-28 14:28:16 +08:00
|
|
|
/**
|
|
|
|
* Creates a Menu with a Array of MenuItem objects.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-11-28 16:02:03 +08:00
|
|
|
static Menu* createWithArray(const Vector<MenuItem*>& arrayOfItems);
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Creates a Menu with it's item, then use addChild() to add
|
2016-01-27 09:42:25 +08:00
|
|
|
* other items. It is used for script, it can't be initialized with undetermined
|
2015-03-25 18:17:58 +08:00
|
|
|
* number of variables.
|
2015-03-28 14:28:16 +08:00
|
|
|
* @js NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static Menu* createWithItem(MenuItem* item);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2015-03-28 14:28:16 +08:00
|
|
|
/** Creates a Menu with MenuItem objects.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
static Menu* createWithItems(MenuItem *firstItem, va_list args);
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/** Align items vertically. */
|
2012-04-19 14:35:52 +08:00
|
|
|
void alignItemsVertically();
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/** Align items vertically with padding.
|
2012-04-19 14:35:52 +08:00
|
|
|
@since v0.7.2
|
|
|
|
*/
|
|
|
|
void alignItemsVerticallyWithPadding(float padding);
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/** Align items horizontally. */
|
2012-04-19 14:35:52 +08:00
|
|
|
void alignItemsHorizontally();
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/** Align items horizontally with padding.
|
2012-04-19 14:35:52 +08:00
|
|
|
@since v0.7.2
|
|
|
|
*/
|
|
|
|
void alignItemsHorizontallyWithPadding(float padding);
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/** Align items in rows of columns. */
|
2013-07-24 06:20:22 +08:00
|
|
|
void alignItemsInColumns(int columns, ...) CC_REQUIRES_NULL_TERMINATION;
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/** Align items in rows of columns. */
|
2013-07-20 13:01:27 +08:00
|
|
|
void alignItemsInColumns(int columns, va_list args);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
2015-03-28 14:28:16 +08:00
|
|
|
/** Align items in array of columns.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
void alignItemsInColumnsWithArray(const ValueVector& rows);
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/** Align items in columns of rows. */
|
2013-07-24 06:20:22 +08:00
|
|
|
void alignItemsInRows(int rows, ...) CC_REQUIRES_NULL_TERMINATION;
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/** Align items in columns of rows. */
|
2013-07-20 13:01:27 +08:00
|
|
|
void alignItemsInRows(int rows, va_list args);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
2015-03-28 14:28:16 +08:00
|
|
|
/** Align items in array of rows.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2013-12-04 17:46:57 +08:00
|
|
|
void alignItemsInRowsWithArray(const ValueVector& columns);
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
2016-01-27 09:42:25 +08:00
|
|
|
* Determines if the menu is enabled.
|
2015-03-25 18:17:58 +08:00
|
|
|
* @see `setEnabled(bool)`.
|
|
|
|
* @return whether the menu is enabled or not.
|
|
|
|
*/
|
2013-07-07 13:01:21 +08:00
|
|
|
virtual bool isEnabled() const { return _enabled; }
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
2016-01-27 09:42:25 +08:00
|
|
|
* Set whether the menu is visible. If set false, interacting with the menu
|
|
|
|
* will have no effect.
|
2015-03-25 18:17:58 +08:00
|
|
|
* The default value is true, a menu is default to visible.
|
2016-01-27 09:42:25 +08:00
|
|
|
*@param value true if menu is to be enabled, false if menu is to be disabled.
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
virtual void setEnabled(bool value) { _enabled = value; };
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2015-03-15 02:33:15 +08:00
|
|
|
virtual bool onTouchBegan(Touch* touch, Event* event) override;
|
|
|
|
virtual void onTouchEnded(Touch* touch, Event* event) override;
|
|
|
|
virtual void onTouchCancelled(Touch* touch, Event* event) override;
|
|
|
|
virtual void onTouchMoved(Touch* touch, Event* event) override;
|
2013-10-23 16:14:03 +08:00
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
// overrides
|
2013-09-16 22:20:01 +08:00
|
|
|
virtual void removeChild(Node* child, bool cleanup) override;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-07-18 07:56:19 +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;
|
2014-06-25 11:27:48 +08:00
|
|
|
virtual void addChild(Node * child, int zOrder, const std::string &name) override;
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
virtual void onEnter() override;
|
2013-07-18 07:56:19 +08:00
|
|
|
virtual void onExit() override;
|
2016-11-16 09:48:37 +08:00
|
|
|
virtual void setOpacityModifyRGB(bool value) override;
|
|
|
|
virtual bool isOpacityModifyRGB(void) const override;
|
2013-07-18 07:56:19 +08:00
|
|
|
|
2013-12-13 06:30:22 +08:00
|
|
|
virtual std::string getDescription() const override;
|
|
|
|
|
2014-03-21 13:44:29 +08:00
|
|
|
CC_CONSTRUCTOR_ACCESS:
|
2013-11-16 05:14:12 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2015-08-07 16:27:25 +08:00
|
|
|
Menu() : _selectedItem(nullptr), _selectedWithCamera(nullptr) {}
|
2013-11-16 05:14:12 +08:00
|
|
|
virtual ~Menu();
|
|
|
|
|
|
|
|
/** initializes an empty Menu */
|
2015-03-15 02:33:15 +08:00
|
|
|
bool init() override;
|
2013-11-16 05:14:12 +08:00
|
|
|
|
|
|
|
/** initializes a Menu with a NSArray of MenuItem objects */
|
2013-11-28 16:02:03 +08:00
|
|
|
bool initWithArray(const Vector<MenuItem*>& arrayOfItems);
|
2013-11-16 05:14:12 +08:00
|
|
|
|
2014-03-21 13:44:29 +08:00
|
|
|
protected:
|
2014-03-25 06:09:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
/** whether or not the menu will receive events */
|
|
|
|
bool _enabled;
|
|
|
|
|
2015-08-07 16:27:25 +08:00
|
|
|
virtual MenuItem* getItemForTouch(Touch * touch, const Camera *camera);
|
2013-07-25 20:04:24 +08:00
|
|
|
State _state;
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem *_selectedItem;
|
2015-08-07 16:27:25 +08:00
|
|
|
const Camera *_selectedWithCamera;
|
2013-11-16 05:14:12 +08:00
|
|
|
private:
|
|
|
|
CC_DISALLOW_COPY_AND_ASSIGN(Menu);
|
2012-03-22 14:22:06 +08:00
|
|
|
};
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
// end of _2d group
|
2012-06-20 18:09:11 +08:00
|
|
|
/// @}
|
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
NS_CC_END
|
2011-03-19 10:59:01 +08:00
|
|
|
|
2011-07-28 10:45:32 +08:00
|
|
|
#endif//__CCMENU_H_
|