UIRadioButton.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #include "ui/UIRadioButton.h"
  22. NS_CC_BEGIN
  23. namespace ui {
  24. IMPLEMENT_CLASS_GUI_INFO(RadioButton)
  25. RadioButton::RadioButton() :
  26. _radioButtonEventCallback(nullptr),
  27. _group(nullptr)
  28. {
  29. }
  30. RadioButton::~RadioButton()
  31. {
  32. _radioButtonEventCallback = nullptr;
  33. _group = nullptr;
  34. }
  35. RadioButton* RadioButton::create()
  36. {
  37. RadioButton* widget = new (std::nothrow) RadioButton();
  38. if (widget && widget->init())
  39. {
  40. widget->autorelease();
  41. return widget;
  42. }
  43. CC_SAFE_DELETE(widget);
  44. return nullptr;
  45. }
  46. RadioButton* RadioButton::create(const std::string& backGround,
  47. const std::string& backGroundSelected,
  48. const std::string& cross,
  49. const std::string& backGroundDisabled,
  50. const std::string& frontCrossDisabled,
  51. TextureResType texType)
  52. {
  53. RadioButton *pWidget = new (std::nothrow) RadioButton;
  54. if (pWidget && pWidget->init(backGround,
  55. backGroundSelected,
  56. cross,
  57. backGroundDisabled,
  58. frontCrossDisabled,
  59. texType))
  60. {
  61. pWidget->autorelease();
  62. return pWidget;
  63. }
  64. CC_SAFE_DELETE(pWidget);
  65. return nullptr;
  66. }
  67. RadioButton* RadioButton::create(const std::string& backGround,
  68. const std::string& cross,
  69. TextureResType texType)
  70. {
  71. RadioButton *pWidget = new (std::nothrow) RadioButton;
  72. if (pWidget && pWidget->init(backGround,
  73. "",
  74. cross,
  75. "",
  76. "",
  77. texType))
  78. {
  79. pWidget->autorelease();
  80. return pWidget;
  81. }
  82. CC_SAFE_DELETE(pWidget);
  83. return nullptr;
  84. }
  85. void RadioButton::dispatchSelectChangedEvent(bool selected)
  86. {
  87. EventType eventType = (selected ? EventType::SELECTED : EventType::UNSELECTED);
  88. this->retain();
  89. if (_radioButtonEventCallback)
  90. {
  91. _radioButtonEventCallback(this, eventType);
  92. }
  93. if (_ccEventCallback)
  94. {
  95. _ccEventCallback(this, static_cast<int>(eventType));
  96. }
  97. if(selected && _group != nullptr)
  98. {
  99. _group->onChangedRadioButtonSelect(this);
  100. }
  101. this->release();
  102. }
  103. void RadioButton::addEventListener(const ccRadioButtonCallback& callback)
  104. {
  105. _radioButtonEventCallback = callback;
  106. }
  107. void RadioButton::releaseUpEvent(Touch* touch)
  108. {
  109. Widget::releaseUpEvent(touch);
  110. if (!_isSelected)
  111. {
  112. setSelected(true);
  113. dispatchSelectChangedEvent(true);
  114. }
  115. }
  116. std::string RadioButton::getDescription() const
  117. {
  118. return "RadioButton";
  119. }
  120. Widget* RadioButton::createCloneInstance()
  121. {
  122. return RadioButton::create();
  123. }
  124. void RadioButton::copySpecialProperties(Widget *widget)
  125. {
  126. RadioButton* radioButton = dynamic_cast<RadioButton*>(widget);
  127. if (radioButton)
  128. {
  129. AbstractCheckButton::copySpecialProperties(widget);
  130. _radioButtonEventCallback = radioButton->_radioButtonEventCallback;
  131. _ccEventCallback = radioButton->_ccEventCallback;
  132. _group = radioButton->_group;
  133. }
  134. }
  135. RadioButtonGroup::RadioButtonGroup() :
  136. _radioButtonGroupEventCallback(nullptr),
  137. _selectedRadioButton(nullptr),
  138. _allowedNoSelection(false)
  139. {
  140. }
  141. RadioButtonGroup::~RadioButtonGroup()
  142. {
  143. _radioButtonGroupEventCallback = nullptr;
  144. _selectedRadioButton = nullptr;
  145. _radioButtons.clear();
  146. }
  147. RadioButtonGroup* RadioButtonGroup::create()
  148. {
  149. RadioButtonGroup* widget = new (std::nothrow) RadioButtonGroup();
  150. if (widget && widget->init())
  151. {
  152. widget->autorelease();
  153. return widget;
  154. }
  155. CC_SAFE_DELETE(widget);
  156. return nullptr;
  157. }
  158. void RadioButtonGroup::addEventListener(const ccRadioButtonGroupCallback& callback)
  159. {
  160. _radioButtonGroupEventCallback = callback;
  161. }
  162. void RadioButtonGroup::addRadioButton(RadioButton* radioButton)
  163. {
  164. if(radioButton != nullptr)
  165. {
  166. CCASSERT(!radioButton->_group, "It already belongs to a group!");
  167. radioButton->_group = this;
  168. _radioButtons.pushBack(radioButton);
  169. if(!_allowedNoSelection && _selectedRadioButton == nullptr)
  170. {
  171. setSelectedButtonWithoutEvent(radioButton);
  172. }
  173. }
  174. }
  175. void RadioButtonGroup::removeRadioButton(RadioButton* radioButton)
  176. {
  177. ssize_t index = _radioButtons.getIndex(radioButton);
  178. if( index == CC_INVALID_INDEX )
  179. {
  180. CCLOGERROR("The radio button does not belong to this group!");
  181. return;
  182. }
  183. if(radioButton != nullptr)
  184. {
  185. radioButton->_group = nullptr;
  186. if(radioButton == _selectedRadioButton)
  187. {
  188. deselect();
  189. }
  190. _radioButtons.erase(index);
  191. if(!_allowedNoSelection && _selectedRadioButton == nullptr && !_radioButtons.empty())
  192. {
  193. setSelectedButtonWithoutEvent(0);
  194. }
  195. }
  196. }
  197. void RadioButtonGroup::removeAllRadioButtons()
  198. {
  199. while(!_radioButtons.empty())
  200. {
  201. removeRadioButton(_radioButtons.at(0));
  202. }
  203. }
  204. ssize_t RadioButtonGroup::getNumberOfRadioButtons() const
  205. {
  206. return _radioButtons.size();
  207. }
  208. RadioButton* RadioButtonGroup::getRadioButtonByIndex(int index) const
  209. {
  210. if(index >= _radioButtons.size())
  211. {
  212. CCLOGERROR("Out of array index! length=%d, requestedIndex=%d", (int)_radioButtons.size(), index);
  213. return nullptr;
  214. }
  215. return _radioButtons.at(index);
  216. }
  217. void RadioButtonGroup::deselect()
  218. {
  219. if(_selectedRadioButton != nullptr)
  220. {
  221. _selectedRadioButton->setSelected(false);
  222. _selectedRadioButton->dispatchSelectChangedEvent(false);
  223. }
  224. _selectedRadioButton = nullptr;
  225. }
  226. int RadioButtonGroup::getSelectedButtonIndex() const
  227. {
  228. return (int) _radioButtons.getIndex(_selectedRadioButton);
  229. }
  230. void RadioButtonGroup::setSelectedButton(int index)
  231. {
  232. CCASSERT(index < _radioButtons.size(), "Out of array index!");
  233. setSelectedButton(_radioButtons.at(index));
  234. }
  235. void RadioButtonGroup::setSelectedButton(RadioButton* radioButton)
  236. {
  237. setSelectedButtonWithoutEvent(radioButton);
  238. onChangedRadioButtonSelect(_selectedRadioButton);
  239. }
  240. void RadioButtonGroup::setSelectedButtonWithoutEvent(int index)
  241. {
  242. setSelectedButtonWithoutEvent(_radioButtons.at(index));
  243. }
  244. void RadioButtonGroup::setSelectedButtonWithoutEvent(RadioButton* radioButton)
  245. {
  246. if(!_allowedNoSelection && radioButton == nullptr)
  247. {
  248. return;
  249. }
  250. if(_selectedRadioButton == radioButton)
  251. {
  252. return;
  253. }
  254. if(radioButton != nullptr && !_radioButtons.contains(radioButton))
  255. {
  256. CCLOGERROR("The radio button does not belong to this group!");
  257. return;
  258. }
  259. deselect();
  260. _selectedRadioButton = radioButton;
  261. if(_selectedRadioButton != nullptr)
  262. {
  263. _selectedRadioButton->setSelected(true);
  264. }
  265. }
  266. std::string RadioButtonGroup::getDescription() const
  267. {
  268. return "RadioButtonGroup";
  269. }
  270. void RadioButtonGroup::setAllowedNoSelection(bool allowedNoSelection)
  271. {
  272. _allowedNoSelection = allowedNoSelection;
  273. if(!_allowedNoSelection && _selectedRadioButton == nullptr)
  274. {
  275. if (_radioButtons.size() > 0)
  276. {
  277. setSelectedButton(_radioButtons.at(0));
  278. }
  279. }
  280. }
  281. bool RadioButtonGroup::isAllowedNoSelection() const
  282. {
  283. return _allowedNoSelection;
  284. }
  285. Widget* RadioButtonGroup::createCloneInstance()
  286. {
  287. return RadioButtonGroup::create();
  288. }
  289. void RadioButtonGroup::copySpecialProperties(Widget *widget)
  290. {
  291. RadioButtonGroup* radioButtonGroup = dynamic_cast<RadioButtonGroup*>(widget);
  292. if (radioButtonGroup)
  293. {
  294. _radioButtonGroupEventCallback = radioButtonGroup->_radioButtonGroupEventCallback;
  295. _ccEventCallback = radioButtonGroup->_ccEventCallback;
  296. _selectedRadioButton = radioButtonGroup->_selectedRadioButton;
  297. _allowedNoSelection = radioButtonGroup->_allowedNoSelection;
  298. _radioButtons.clear();
  299. for(const auto& radioButton : radioButtonGroup->_radioButtons)
  300. {
  301. _radioButtons.pushBack(radioButton);
  302. }
  303. }
  304. }
  305. void RadioButtonGroup::onChangedRadioButtonSelect(RadioButton* radioButton)
  306. {
  307. if(_selectedRadioButton != radioButton)
  308. {
  309. deselect();
  310. _selectedRadioButton = radioButton;
  311. }
  312. this->retain();
  313. if (_radioButtonGroupEventCallback)
  314. {
  315. int index = (int) _radioButtons.getIndex(radioButton);
  316. _radioButtonGroupEventCallback(_selectedRadioButton, index, EventType::SELECT_CHANGED);
  317. }
  318. if (_ccEventCallback)
  319. {
  320. _ccEventCallback(this, static_cast<int>(EventType::SELECT_CHANGED));
  321. }
  322. this->release();
  323. }
  324. }
  325. NS_CC_END