CCMenuItem.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #ifndef __CCMENU_ITEM_H__
  25. #define __CCMENU_ITEM_H__
  26. // C++ includes
  27. #include <functional>
  28. // cocos2d includes
  29. #include "2d/CCNode.h"
  30. #include "base/CCProtocols.h"
  31. NS_CC_BEGIN
  32. typedef std::function<void(Ref*)> ccMenuCallback;
  33. class Label;
  34. class LabelAtlas;
  35. class Sprite;
  36. class SpriteFrame;
  37. #define kItemSize 32
  38. /**
  39. * @addtogroup _2d
  40. * @{
  41. */
  42. /** @brief MenuItem base class.
  43. *
  44. * Subclass MenuItem (or any subclass) to create your custom MenuItem objects.
  45. */
  46. class CC_DLL MenuItem : public Node
  47. {
  48. public:
  49. /** Creates a MenuItem with no target/selector. */
  50. static MenuItem* create();
  51. /** Creates a MenuItem with a target/selector. */
  52. CC_DEPRECATED_ATTRIBUTE static MenuItem* create(Ref *rec, SEL_MenuHandler selector);
  53. /** Creates a MenuItem with a target/selector. */
  54. static MenuItem* create(const ccMenuCallback& callback);
  55. /** Returns the outside box. */
  56. Rect rect() const;
  57. /** Activate the item. */
  58. virtual void activate();
  59. /** The item was selected (not activated), similar to "mouse-over". */
  60. virtual void selected();
  61. /** The item was unselected. */
  62. virtual void unselected();
  63. /** Returns whether or not the item is enabled. */
  64. virtual bool isEnabled() const;
  65. /** Enables or disables the item. */
  66. virtual void setEnabled(bool value);
  67. /** Returns whether or not the item is selected. */
  68. virtual bool isSelected() const;
  69. /** Set the callback to the menu item.
  70. * @code
  71. * In js,can contain two params,the second param is jsptr.
  72. * @endcode
  73. * @lua NA
  74. */
  75. void setCallback(const ccMenuCallback& callback);
  76. /** Set the target/selector of the menu item.
  77. * @lua NA
  78. */
  79. CC_DEPRECATED_ATTRIBUTE void setTarget(Ref *rec, SEL_MenuHandler selector);
  80. /**
  81. * @js NA
  82. */
  83. virtual std::string getDescription() const override;
  84. CC_CONSTRUCTOR_ACCESS:
  85. /**
  86. * @js ctor
  87. */
  88. MenuItem()
  89. : _selected(false)
  90. , _enabled(false)
  91. , _callback(nullptr)
  92. {}
  93. /**
  94. * @js NA
  95. * @lua NA
  96. */
  97. virtual ~MenuItem();
  98. /** Initializes a MenuItem with a target/selector.
  99. * @lua NA
  100. */
  101. bool initWithCallback(const ccMenuCallback& callback);
  102. /** Initializes a MenuItem with a target/selector.
  103. * @js NA
  104. * @lua NA
  105. */
  106. CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Ref *rec, SEL_MenuHandler selector);
  107. protected:
  108. bool _selected;
  109. bool _enabled;
  110. // callback
  111. ccMenuCallback _callback;
  112. private:
  113. CC_DISALLOW_COPY_AND_ASSIGN(MenuItem);
  114. };
  115. /** @brief An abstract class for "label" MenuItemLabel items.
  116. Any Node that supports the LabelProtocol protocol can be added.
  117. Supported nodes:
  118. - BitmapFontAtlas
  119. - LabelAtlas
  120. - LabelTTF
  121. - Label
  122. */
  123. class CC_DLL MenuItemLabel : public MenuItem
  124. {
  125. public:
  126. /** Creates a MenuItemLabel with a Label, target and selector. */
  127. CC_DEPRECATED_ATTRIBUTE static MenuItemLabel * create(Node*label, Ref* target, SEL_MenuHandler selector);
  128. /** Creates a MenuItemLabel with a Label and a callback. */
  129. static MenuItemLabel * create(Node*label, const ccMenuCallback& callback);
  130. /** Creates a MenuItemLabel with a Label. Target and selector will be nil. */
  131. static MenuItemLabel* create(Node *label);
  132. /** Sets a new string to the inner label. */
  133. void setString(const std::string& label);
  134. /** Get the inner string of the inner label. */
  135. std::string getString() const;
  136. /** Gets the color that will be used when the item is disabled. */
  137. const Color3B& getDisabledColor() const { return _disabledColor; }
  138. /** Sets the color that will be used when the item is disabled. */
  139. void setDisabledColor(const Color3B& color) { _disabledColor = color; }
  140. /** Gets the label that is rendered. */
  141. Node* getLabel() const { return _label; }
  142. /** Sets the label that is rendered. */
  143. void setLabel(Node* node);
  144. // Overrides
  145. virtual void activate() override;
  146. virtual void selected() override;
  147. virtual void unselected() override;
  148. virtual void setEnabled(bool enabled) override;
  149. CC_CONSTRUCTOR_ACCESS:
  150. /**
  151. * @js ctor
  152. */
  153. MenuItemLabel()
  154. : _originalScale(0.0)
  155. , _label(nullptr)
  156. {}
  157. /**
  158. * @js NA
  159. * @lua NA
  160. */
  161. virtual ~MenuItemLabel();
  162. /** Initializes a MenuItemLabel with a Label, target and selector. */
  163. bool initWithLabel(Node* label, const ccMenuCallback& callback);
  164. /** Initializes a MenuItemLabel with a Label, target and selector. */
  165. CC_DEPRECATED_ATTRIBUTE bool initWithLabel(Node* label, Ref* target, SEL_MenuHandler selector);
  166. protected:
  167. Color3B _colorBackup;
  168. float _originalScale;
  169. /** The color that will be used to disable the item. */
  170. Color3B _disabledColor;
  171. /** Label that is rendered. It can be any Node that implements the LabelProtocol. */
  172. Node* _label;
  173. private:
  174. CC_DISALLOW_COPY_AND_ASSIGN(MenuItemLabel);
  175. };
  176. /** @brief A MenuItemAtlasFont.
  177. Helper class that creates a MenuItemLabel class with a LabelAtlas.
  178. */
  179. class CC_DLL MenuItemAtlasFont : public MenuItemLabel
  180. {
  181. public:
  182. /** Creates a menu item from a string and atlas with a target/selector. */
  183. static MenuItemAtlasFont* create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap);
  184. /** Creates a menu item from a string and atlas. Use it with MenuItemToggle. */
  185. 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);
  186. /** Creates a menu item from a string and atlas. Use it with MenuItemToggle. */
  187. static MenuItemAtlasFont* create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback);
  188. CC_CONSTRUCTOR_ACCESS:
  189. /**
  190. * @js ctor
  191. */
  192. MenuItemAtlasFont(){}
  193. /**
  194. * @js NA
  195. * @lua NA
  196. */
  197. virtual ~MenuItemAtlasFont(){}
  198. /** Initializes a menu item from a string and atlas with a target/selector. */
  199. CC_DEPRECATED_ATTRIBUTE bool initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, Ref* target, SEL_MenuHandler selector);
  200. /** Initializes a menu item from a string and atlas with a target/selector. */
  201. bool initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback);
  202. private:
  203. CC_DISALLOW_COPY_AND_ASSIGN(MenuItemAtlasFont);
  204. };
  205. /** @brief A MenuItemFont.
  206. Helper class that creates a MenuItemLabel class with a Label.
  207. */
  208. class CC_DLL MenuItemFont : public MenuItemLabel
  209. {
  210. public:
  211. /** Creates a menu item from a string without target/selector. To be used with MenuItemToggle. */
  212. static MenuItemFont * create(const std::string& value = "");
  213. /** Creates a menu item from a string with a target/selector. */
  214. CC_DEPRECATED_ATTRIBUTE static MenuItemFont * create(const std::string& value, Ref* target, SEL_MenuHandler selector);
  215. /** Creates a menu item from a string with a target/selector. */
  216. static MenuItemFont * create(const std::string& value, const ccMenuCallback& callback);
  217. /** Set default font size. */
  218. static void setFontSize(int size);
  219. /** Get default font size. */
  220. static int getFontSize();
  221. CC_DEPRECATED_ATTRIBUTE static int fontSize() { return MenuItemFont::getFontSize(); };
  222. /** Set the default font name. */
  223. static void setFontName(const std::string& name);
  224. /** Get the default font name. */
  225. static const std::string& getFontName();
  226. CC_DEPRECATED_ATTRIBUTE static const std::string& fontName() { return MenuItemFont::getFontName(); };
  227. /** Set font size.
  228. * c++ can not overload static and non-static member functions with the same parameter types.
  229. * so change the name to setFontSizeObj.
  230. * @js setFontSize
  231. * @js NA
  232. */
  233. void setFontSizeObj(int size);
  234. /** get font size .
  235. * @js getFontSize
  236. * @js NA
  237. */
  238. int getFontSizeObj() const;
  239. CC_DEPRECATED_ATTRIBUTE int fontSizeObj() const { return getFontSizeObj(); };
  240. /**
  241. * Set the font name .
  242. * c++ can not overload static and non-static member functions with the same parameter types.
  243. * so change the name to setFontNameObj.
  244. * @js setFontName
  245. * @js NA
  246. */
  247. void setFontNameObj(const std::string& name);
  248. /** Returns the name of the Font.
  249. * @js getFontNameObj
  250. * @js NA
  251. */
  252. const std::string& getFontNameObj() const;
  253. /** Deprecated Use getFontNameObj() instead.
  254. * @js NA
  255. */
  256. CC_DEPRECATED_ATTRIBUTE const std::string& fontNameObj() const { return getFontNameObj(); }
  257. CC_CONSTRUCTOR_ACCESS:
  258. /**
  259. * @js ctor
  260. */
  261. MenuItemFont();
  262. /**
  263. * @js NA
  264. * @lua NA
  265. */
  266. virtual ~MenuItemFont();
  267. /** Initializes a menu item from a string with a target/selector. */
  268. CC_DEPRECATED_ATTRIBUTE bool initWithString(const std::string& value, Ref* target, SEL_MenuHandler selector);
  269. /** Initializes a menu item from a string with a target/selector. */
  270. bool initWithString(const std::string& value, const ccMenuCallback& callback);
  271. protected:
  272. int _fontSize;
  273. std::string _fontName;
  274. private:
  275. CC_DISALLOW_COPY_AND_ASSIGN(MenuItemFont);
  276. };
  277. /** @brief MenuItemSprite accepts Node<RGBAProtocol> objects as items.
  278. The images has 3 different states:
  279. - unselected image
  280. - selected image
  281. - disabled image
  282. @since v0.8.0
  283. */
  284. class CC_DLL MenuItemSprite : public MenuItem
  285. {
  286. public:
  287. /** Creates a menu item with a normal, selected and disabled image.*/
  288. static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite = nullptr);
  289. /** Creates a menu item with a normal and selected image with target/selector. */
  290. CC_DEPRECATED_ATTRIBUTE static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Ref* target, SEL_MenuHandler selector);
  291. /** Creates a menu item with a normal, selected and disabled image with target/selector. */
  292. CC_DEPRECATED_ATTRIBUTE static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Ref* target, SEL_MenuHandler selector);
  293. /** Creates a menu item with a normal and selected image with a callable object. */
  294. static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, const ccMenuCallback& callback);
  295. /** Creates a menu item with a normal,selected and disabled image with target/selector. */
  296. static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, const ccMenuCallback& callback);
  297. /** Gets the image used when the item is not selected. */
  298. Node* getNormalImage() const { return _normalImage; }
  299. /** Sets the image used when the item is not selected. */
  300. void setNormalImage(Node* image);
  301. /** Gets the image used when the item is selected. */
  302. Node* getSelectedImage() const { return _selectedImage; }
  303. /** Sets the image used when the item is selected. */
  304. void setSelectedImage(Node* image);
  305. /** Gets the image used when the item is disabled. */
  306. Node* getDisabledImage() const { return _disabledImage; }
  307. /** Sets the image used when the item is disabled. */
  308. void setDisabledImage(Node* image);
  309. /**
  310. * The item was selected (not activated), similar to "mouse-over".
  311. @since v0.99.5
  312. */
  313. virtual void selected();
  314. /** The item was unselected. */
  315. virtual void unselected();
  316. /** Enables or disables the item. */
  317. virtual void setEnabled(bool bEnabled);
  318. CC_CONSTRUCTOR_ACCESS:
  319. MenuItemSprite()
  320. :_normalImage(nullptr)
  321. ,_selectedImage(nullptr)
  322. ,_disabledImage(nullptr)
  323. {}
  324. /** Initializes a menu item with a normal, selected and disabled image with target/selector. */
  325. CC_DEPRECATED_ATTRIBUTE bool initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Ref* target, SEL_MenuHandler selector);
  326. /** Initializes a menu item with a normal, selected and disabled image with a callable object. */
  327. bool initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, const ccMenuCallback& callback);
  328. protected:
  329. virtual void updateImagesVisibility();
  330. /** The image used when the item is not selected. */
  331. Node* _normalImage;
  332. /** The image used when the item is selected. */
  333. Node* _selectedImage;
  334. /** The image used when the item is disabled. */
  335. Node* _disabledImage;
  336. private:
  337. CC_DISALLOW_COPY_AND_ASSIGN(MenuItemSprite);
  338. };
  339. /** @brief MenuItemImage accepts images as items.
  340. The images has 3 different states:
  341. - unselected image
  342. - selected image
  343. - disabled image
  344. For best results try that all images are of the same size.
  345. */
  346. class CC_DLL MenuItemImage : public MenuItemSprite
  347. {
  348. public:
  349. /** Creates an MenuItemImage. */
  350. static MenuItemImage* create();
  351. /** Creates a menu item with a normal and selected image.*/
  352. static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage);
  353. /** Creates a menu item with a normal,selected and disabled image.*/
  354. static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage);
  355. /** Creates a menu item with a normal and selected image with target/selector. */
  356. CC_DEPRECATED_ATTRIBUTE static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage, Ref* target, SEL_MenuHandler selector);
  357. /** Creates a menu item with a normal and selected image with a callable object. */
  358. static MenuItemImage* create(const std::string&normalImage, const std::string&selectedImage, const ccMenuCallback& callback);
  359. /** Creates a menu item with a normal,selected and disabled image with target/selector. */
  360. CC_DEPRECATED_ATTRIBUTE static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector);
  361. /** Creates a menu item with a normal,selected and disabled image with a callable object. */
  362. static MenuItemImage* create(const std::string&normalImage, const std::string&selectedImage, const std::string&disabledImage, const ccMenuCallback& callback);
  363. /** Sets the sprite frame for the normal image. */
  364. void setNormalSpriteFrame(SpriteFrame* frame);
  365. /** Sets the sprite frame for the selected image. */
  366. void setSelectedSpriteFrame(SpriteFrame* frame);
  367. /** Sets the sprite frame for the disabled image. */
  368. void setDisabledSpriteFrame(SpriteFrame* frame);
  369. CC_CONSTRUCTOR_ACCESS:
  370. /**
  371. * @js ctor
  372. */
  373. MenuItemImage(){}
  374. /**
  375. * @js NA
  376. * @lua NA
  377. */
  378. virtual ~MenuItemImage(){}
  379. bool init();
  380. /** Initializes a menu item with a normal, selected and disabled image with target/selector. */
  381. CC_DEPRECATED_ATTRIBUTE bool initWithNormalImage(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector);
  382. /** Initializes a menu item with a normal, selected and disabled image with a callable object. */
  383. bool initWithNormalImage(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, const ccMenuCallback& callback);
  384. private:
  385. CC_DISALLOW_COPY_AND_ASSIGN(MenuItemImage);
  386. };
  387. /** @brief A MenuItemToggle.
  388. A simple container class that "toggles" it's inner items.
  389. The inner items can be any MenuItem.
  390. */
  391. class CC_DLL MenuItemToggle : public MenuItem
  392. {
  393. public:
  394. /** Creates a menu item from a Array with a target selector.
  395. * @js NA
  396. * @lua NA
  397. */
  398. CC_DEPRECATED_ATTRIBUTE static MenuItemToggle * createWithTarget(Ref* target, SEL_MenuHandler selector, const Vector<MenuItem*>& menuItems);
  399. /** Creates a menu item from a list of items with a target/selector.
  400. * @js NA
  401. * @lua NA
  402. */
  403. CC_DEPRECATED_ATTRIBUTE static MenuItemToggle* createWithTarget(Ref* target, SEL_MenuHandler selector, MenuItem* item, ...)CC_REQUIRES_NULL_TERMINATION;
  404. /**
  405. *@brief Creates a menu item from a Array with a callable object.
  406. */
  407. static MenuItemToggle * createWithCallback(const ccMenuCallback& callback, const Vector<MenuItem*>& menuItems);
  408. /** Creates a menu item from a list of items with a callable object. */
  409. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  410. // VS2013 does not support nullptr in variable args lists and variadic templates are also not supported.
  411. typedef MenuItem* M;
  412. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, NULL); }
  413. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, NULL); }
  414. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, NULL); }
  415. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, NULL); }
  416. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, NULL); }
  417. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, M m6, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, NULL); }
  418. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, M m6, M m7, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, m7, NULL); }
  419. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, m7, m8, NULL); }
  420. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, M m9, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, m7, m8, m9, NULL); }
  421. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, 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 createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, NULL); }
  422. // On WP8 for lists longer than 10 items, use createWithArray or variadicCreate with NULL as the last argument.
  423. static MenuItemToggle* createWithCallbackVA(const ccMenuCallback& callback, M item, ...);
  424. #else
  425. static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, MenuItem* item, ...) CC_REQUIRES_NULL_TERMINATION;
  426. #endif
  427. /** Creates a menu item with no target/selector and no items. */
  428. static MenuItemToggle* create();
  429. /** Creates a menu item with a item. */
  430. static MenuItemToggle* create(MenuItem *item);
  431. /** Add more menu item. */
  432. void addSubItem(MenuItem *item);
  433. /** Return the selected item. */
  434. MenuItem* getSelectedItem();
  435. /**
  436. *@deprecated Use `getSelectedItem` instead.
  437. */
  438. CC_DEPRECATED_ATTRIBUTE MenuItem* selectedItem() { return getSelectedItem(); }
  439. /** Gets the index of the selected item. */
  440. unsigned int getSelectedIndex() const { return _selectedIndex; }
  441. /** Sets the index of the selected item. */
  442. void setSelectedIndex(unsigned int index);
  443. /** Gets the array that contains the subitems.
  444. *You can add/remove items in runtime, and you can replace the array with a new one.
  445. * @since v0.7.2
  446. * @js NA
  447. * @lua NA
  448. */
  449. const Vector<MenuItem*>& getSubItems() const { return _subItems; }
  450. Vector<MenuItem*>& getSubItems() { return _subItems; }
  451. /** Sets the array that contains the subitems. */
  452. void setSubItems(const Vector<MenuItem*>& items) {
  453. _subItems = items;
  454. }
  455. // Overrides
  456. virtual void activate() override;
  457. virtual void selected() override;
  458. virtual void unselected() override;
  459. virtual void setEnabled(bool var) override;
  460. virtual void cleanup() override;
  461. CC_CONSTRUCTOR_ACCESS:
  462. /**
  463. * @js ctor
  464. */
  465. MenuItemToggle()
  466. : _selectedIndex(0)
  467. , _selectedItem(nullptr)
  468. {}
  469. /** Initializes a menu item from a list of items with a target selector.
  470. * @js NA
  471. * @lua NA
  472. */
  473. CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Ref* target, SEL_MenuHandler selector, MenuItem* item, va_list args);
  474. /** Initializes a menu item from a list of items with a callable object. */
  475. bool initWithCallback(const ccMenuCallback& callback, MenuItem* item, va_list args);
  476. /** Initializes a menu item with a item. */
  477. bool initWithItem(MenuItem *item);
  478. protected:
  479. /** Returns the selected item. */
  480. unsigned int _selectedIndex;
  481. MenuItem* _selectedItem;
  482. /** Array that contains the subitems. You can add/remove items in runtime, and you can replace the array with a new one.
  483. @since v0.7.2
  484. */
  485. Vector<MenuItem*> _subItems;
  486. private:
  487. CC_DISALLOW_COPY_AND_ASSIGN(MenuItemToggle);
  488. };
  489. // end of 2d group
  490. /// @}
  491. NS_CC_END
  492. #endif //__CCMENU_ITEM_H__