CCMenu.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CCMENU_H_
  23. #define __CCMENU_H_
  24. #include "2d/CCMenuItem.h"
  25. #include "2d/CCLayer.h"
  26. #include "base/CCValue.h"
  27. NS_CC_BEGIN
  28. class Touch;
  29. /**
  30. * @addtogroup _2d
  31. * @{
  32. */
  33. /** @brief A Menu for touch handling.
  34. *
  35. * Features and Limitation:
  36. * - You can add MenuItem objects in runtime using addChild.
  37. * - But the only accepted children are MenuItem objects.
  38. */
  39. class CC_DLL Menu : public Layer
  40. {
  41. public:
  42. /**
  43. * Menu state, it's used internally.
  44. */
  45. enum class State
  46. {
  47. WAITING,
  48. TRACKING_TOUCH,
  49. };
  50. /**
  51. *@brief Creates an empty Menu.
  52. */
  53. static Menu* create();
  54. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  55. // VS2013 does not support nullptr in variable args lists and variadic templates are also not supported.
  56. typedef MenuItem* M;
  57. static Menu* create(M m1, std::nullptr_t listEnd) { return variadicCreate(m1, NULL); }
  58. static Menu* create(M m1, M m2, std::nullptr_t listEnd) { return variadicCreate(m1, m2, NULL); }
  59. static Menu* create(M m1, M m2, M m3, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, NULL); }
  60. static Menu* create(M m1, M m2, M m3, M m4, std::nullptr_t listEnd) { return variadicCreate(m1, m2, m3, m4, NULL); }
  61. 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); }
  62. 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); }
  63. 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); }
  64. 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); }
  65. 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); }
  66. 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); }
  67. // On WP8 for lists longer than 10 items, use createWithArray or variadicCreate with NULL as the last argument.
  68. static Menu* variadicCreate(MenuItem* item, ...);
  69. #else
  70. /** Creates a Menu with MenuItem objects. */
  71. static Menu* create(MenuItem* item, ...) CC_REQUIRES_NULL_TERMINATION;
  72. #endif
  73. /**
  74. * Creates a Menu with a Array of MenuItem objects.
  75. * @js NA
  76. */
  77. static Menu* createWithArray(const Vector<MenuItem*>& arrayOfItems);
  78. /**
  79. * Creates a Menu with it's item, then use addChild() to add
  80. * other items. It is used for script, it can't be initialized with undetermined
  81. * number of variables.
  82. * @js NA
  83. */
  84. static Menu* createWithItem(MenuItem* item);
  85. /** Creates a Menu with MenuItem objects.
  86. * @js NA
  87. */
  88. static Menu* createWithItems(MenuItem *firstItem, va_list args);
  89. /** Align items vertically. */
  90. void alignItemsVertically();
  91. /** Align items vertically with padding.
  92. @since v0.7.2
  93. */
  94. void alignItemsVerticallyWithPadding(float padding);
  95. /** Align items horizontally. */
  96. void alignItemsHorizontally();
  97. /** Align items horizontally with padding.
  98. @since v0.7.2
  99. */
  100. void alignItemsHorizontallyWithPadding(float padding);
  101. /** Align items in rows of columns. */
  102. void alignItemsInColumns(int columns, ...) CC_REQUIRES_NULL_TERMINATION;
  103. /** Align items in rows of columns. */
  104. void alignItemsInColumns(int columns, va_list args);
  105. /** Align items in array of columns.
  106. * @js NA
  107. */
  108. void alignItemsInColumnsWithArray(const ValueVector& rows);
  109. /** Align items in columns of rows. */
  110. void alignItemsInRows(int rows, ...) CC_REQUIRES_NULL_TERMINATION;
  111. /** Align items in columns of rows. */
  112. void alignItemsInRows(int rows, va_list args);
  113. /** Align items in array of rows.
  114. * @js NA
  115. */
  116. void alignItemsInRowsWithArray(const ValueVector& columns);
  117. /**
  118. * Determines if the menu is enabled.
  119. * @see `setEnabled(bool)`.
  120. * @return whether the menu is enabled or not.
  121. */
  122. virtual bool isEnabled() const { return _enabled; }
  123. /**
  124. * Set whether the menu is visible. If set false, interacting with the menu
  125. * will have no effect.
  126. * The default value is true, a menu is default to visible.
  127. *@param value true if menu is to be enabled, false if menu is to be disabled.
  128. */
  129. virtual void setEnabled(bool value) { _enabled = value; };
  130. virtual bool onTouchBegan(Touch* touch, Event* event) override;
  131. virtual void onTouchEnded(Touch* touch, Event* event) override;
  132. virtual void onTouchCancelled(Touch* touch, Event* event) override;
  133. virtual void onTouchMoved(Touch* touch, Event* event) override;
  134. // overrides
  135. virtual void removeChild(Node* child, bool cleanup) override;
  136. virtual void addChild(Node * child) override;
  137. virtual void addChild(Node * child, int zOrder) override;
  138. virtual void addChild(Node * child, int zOrder, int tag) override;
  139. virtual void addChild(Node * child, int zOrder, const std::string &name) override;
  140. virtual void onEnter() override;
  141. virtual void onExit() override;
  142. virtual void setOpacityModifyRGB(bool value) override;
  143. virtual bool isOpacityModifyRGB(void) const override;
  144. virtual std::string getDescription() const override;
  145. CC_CONSTRUCTOR_ACCESS:
  146. /**
  147. * @js ctor
  148. */
  149. Menu() : _selectedItem(nullptr), _selectedWithCamera(nullptr) {}
  150. virtual ~Menu();
  151. /** initializes an empty Menu */
  152. bool init() override;
  153. /** initializes a Menu with a NSArray of MenuItem objects */
  154. bool initWithArray(const Vector<MenuItem*>& arrayOfItems);
  155. protected:
  156. /** whether or not the menu will receive events */
  157. bool _enabled;
  158. virtual MenuItem* getItemForTouch(Touch * touch, const Camera *camera);
  159. State _state;
  160. MenuItem *_selectedItem;
  161. const Camera *_selectedWithCamera;
  162. private:
  163. CC_DISALLOW_COPY_AND_ASSIGN(Menu);
  164. };
  165. // end of _2d group
  166. /// @}
  167. NS_CC_END
  168. #endif//__CCMENU_H_