UIRadioButton.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /****************************************************************************
  2. Copyright (c) 2015 Neo Kim (neo.kim@neofect.com)
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __UIRADIOBUTTON_H__
  22. #define __UIRADIOBUTTON_H__
  23. #include "ui/UIAbstractCheckButton.h"
  24. #include "ui/GUIExport.h"
  25. /**
  26. * @addtogroup ui
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. namespace ui {
  31. class RadioButtonGroup;
  32. /**
  33. * RadioButton is a specific type of two-states button that is similar to CheckBox.
  34. * Additionally, it can be used together with RadioButtonGroup to interact with other radio buttons.
  35. */
  36. class CC_GUI_DLL RadioButton : public AbstractCheckButton
  37. {
  38. DECLARE_CLASS_GUI_INFO
  39. friend class RadioButtonGroup;
  40. public:
  41. /**
  42. * Radio button event types.
  43. */
  44. enum class EventType
  45. {
  46. SELECTED,
  47. UNSELECTED
  48. };
  49. /**
  50. * A callback which will be called after certain RadioButton event issue.
  51. * @see `RadioButton::EventType`
  52. */
  53. typedef std::function<void(RadioButton* radioButton, EventType)> ccRadioButtonCallback;
  54. /**
  55. * Default constructor.
  56. *
  57. * @lua new
  58. */
  59. RadioButton();
  60. /**
  61. * Default destructor.
  62. *
  63. * @lua NA
  64. */
  65. virtual ~RadioButton();
  66. /**
  67. * Create and return a empty RadioButton instance pointer.
  68. */
  69. static RadioButton* create();
  70. /**
  71. * Create a radio button with various images.
  72. *
  73. * @param backGround backGround texture.
  74. * @param backGroundSelected backGround selected state texture.
  75. * @param cross cross texture.
  76. * @param backGroundDisabled backGround disabled state texture.
  77. * @param frontCrossDisabled cross dark state texture.
  78. * @param texType @see `Widget::TextureResType`
  79. *
  80. * @return A RadioButton instance pointer.
  81. */
  82. static RadioButton* create(const std::string& backGround,
  83. const std::string& backGroundSelected,
  84. const std::string& cross,
  85. const std::string& backGroundDisabled,
  86. const std::string& frontCrossDisabled,
  87. TextureResType texType = TextureResType::LOCAL);
  88. /**
  89. * Another factory method to create a RadioButton instance.
  90. * This method uses less resource to create a RadioButton.
  91. * @param backGround The background image name in `std::string`.
  92. * @param cross The cross image name in `std::string`.
  93. * @param texType The texture's resource type in `Widget::TextureResType`.
  94. * @return A RadioButton instance pointer
  95. */
  96. static RadioButton* create(const std::string& backGround,
  97. const std::string& cross,
  98. TextureResType texType = TextureResType::LOCAL);
  99. /**
  100. * Add a callback function which would be called when radio button is selected or unselected.
  101. *@param callback A std::function with type @see `ccRadioButtonCallback`
  102. */
  103. void addEventListener(const ccRadioButtonCallback& callback);
  104. virtual std::string getDescription() const override;
  105. protected:
  106. virtual void releaseUpEvent(Touch* touch=nullptr) override;
  107. virtual void dispatchSelectChangedEvent(bool selected) override;
  108. virtual Widget* createCloneInstance() override;
  109. virtual void copySpecialProperties(Widget* model) override;
  110. ccRadioButtonCallback _radioButtonEventCallback;
  111. RadioButtonGroup* _group;
  112. };
  113. /**
  114. * RadioButtonGroup groups designated radio buttons to make them interact to each other.
  115. * In one RadioButtonGroup, only one or no RadioButton can be checked.
  116. */
  117. class CC_GUI_DLL RadioButtonGroup : public Widget
  118. {
  119. friend class RadioButton;
  120. public:
  121. /**
  122. * Radio button group event types.
  123. */
  124. enum class EventType
  125. {
  126. SELECT_CHANGED,
  127. };
  128. /**
  129. * A callback which will be called after RadioButtonGroup event issue.
  130. * @see `RadioButtonGroup::EventType`
  131. */
  132. typedef std::function<void(RadioButton* radioButton, int index, EventType)> ccRadioButtonGroupCallback;
  133. /**
  134. * Default constructor.
  135. *
  136. * @lua new
  137. */
  138. RadioButtonGroup();
  139. /**
  140. * Default destructor.
  141. *
  142. * @lua NA
  143. */
  144. virtual ~RadioButtonGroup();
  145. /**
  146. * Create and return a empty RadioButtonGroup instance pointer.
  147. */
  148. static RadioButtonGroup* create();
  149. /**
  150. * Add a callback function which would be called when radio button is selected or unselected.
  151. *@param callback A std::function with type @see `ccRadioButtonGroupCallback`
  152. */
  153. void addEventListener(const ccRadioButtonGroupCallback& callback);
  154. /**
  155. * Get the index of selected radio button.
  156. *
  157. * @return the selected button's index. Returns -1 if no button is selected.
  158. */
  159. virtual int getSelectedButtonIndex() const;
  160. /**
  161. * Select a radio button by index.
  162. *
  163. * @param index of the radio button
  164. */
  165. virtual void setSelectedButton(int index);
  166. /**
  167. * Select a radio button by instance.
  168. *
  169. * @param radio button instance
  170. */
  171. virtual void setSelectedButton(RadioButton* radioButton);
  172. /**
  173. * Select a radio button by index without event dispatch.
  174. *
  175. * @param index of the radio button
  176. */
  177. virtual void setSelectedButtonWithoutEvent(int index);
  178. /**
  179. * Select a radio button by instance without event dispatch.
  180. *
  181. * @param radio button instance
  182. */
  183. virtual void setSelectedButtonWithoutEvent(RadioButton* radioButton);
  184. /**
  185. * Add a radio button into this group.
  186. *
  187. * @param radio button instance
  188. */
  189. virtual void addRadioButton(RadioButton* radioButton);
  190. /**
  191. * Remove a radio button from this group.
  192. *
  193. * @param radio button instance
  194. */
  195. virtual void removeRadioButton(RadioButton* radioButton);
  196. /**
  197. * Remove all radio button from this group.
  198. */
  199. virtual void removeAllRadioButtons();
  200. /**
  201. * Get the number of radio buttons in this group.
  202. *
  203. * @return the number of radio buttons in this group
  204. */
  205. ssize_t getNumberOfRadioButtons() const;
  206. /**
  207. * Get a radio button in this group by index.
  208. *
  209. * @param index of the radio button
  210. * @return radio button instance. Returns nullptr if out of index.
  211. */
  212. RadioButton* getRadioButtonByIndex(int index) const;
  213. /**
  214. * Set a flag for allowing no-selection feature.
  215. * If it is allowed, no radio button can be selected.
  216. * If it is not allowed, one radio button must be selected all time except it is empty.
  217. * Default is not allowed.
  218. *
  219. * @param true means allowing no-selection, false means disallowing no-selection.
  220. */
  221. void setAllowedNoSelection(bool allowedNoSelection);
  222. /**
  223. * Query whether no-selection is allowed or not.
  224. *
  225. * @param true means no-selection is allowed, false means no-selection is not allowed.
  226. */
  227. bool isAllowedNoSelection() const;
  228. virtual std::string getDescription() const override;
  229. protected:
  230. virtual Widget* createCloneInstance() override;
  231. virtual void copySpecialProperties(Widget* model) override;
  232. void onChangedRadioButtonSelect(RadioButton* radioButton);
  233. void deselect();
  234. Vector<RadioButton*> _radioButtons;
  235. ccRadioButtonGroupCallback _radioButtonGroupEventCallback;
  236. RadioButton* _selectedRadioButton;
  237. bool _allowedNoSelection;
  238. };
  239. }
  240. NS_CC_END
  241. // end of ui group
  242. /// @}
  243. #endif /* defined(__UIRADIOBUTTON_H__) */