UIRadioButton.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /****************************************************************************
  2. Copyright (c) 2015 Neo Kim (neo.kim@neofect.com)
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "ui/UIRadioButton.h"
  21. NS_CC_BEGIN
  22. namespace ui {
  23. IMPLEMENT_CLASS_GUI_INFO(RadioButton)
  24. RadioButton::RadioButton() :
  25. _radioButtonEventCallback(nullptr),
  26. _group(nullptr)
  27. {
  28. }
  29. RadioButton::~RadioButton()
  30. {
  31. _radioButtonEventCallback = nullptr;
  32. _group = nullptr;
  33. }
  34. RadioButton* RadioButton::create()
  35. {
  36. RadioButton* widget = new (std::nothrow) RadioButton();
  37. if (widget && widget->init())
  38. {
  39. widget->autorelease();
  40. return widget;
  41. }
  42. CC_SAFE_DELETE(widget);
  43. return nullptr;
  44. }
  45. RadioButton* RadioButton::create(const std::string& backGround,
  46. const std::string& backGroundSelected,
  47. const std::string& cross,
  48. const std::string& backGroundDisabled,
  49. const std::string& frontCrossDisabled,
  50. TextureResType texType)
  51. {
  52. RadioButton *pWidget = new (std::nothrow) RadioButton;
  53. if (pWidget && pWidget->init(backGround,
  54. backGroundSelected,
  55. cross,
  56. backGroundDisabled,
  57. frontCrossDisabled,
  58. texType))
  59. {
  60. pWidget->autorelease();
  61. return pWidget;
  62. }
  63. CC_SAFE_DELETE(pWidget);
  64. return nullptr;
  65. }
  66. RadioButton* RadioButton::create(const std::string& backGround,
  67. const std::string& cross,
  68. TextureResType texType)
  69. {
  70. RadioButton *pWidget = new (std::nothrow) RadioButton;
  71. if (pWidget && pWidget->init(backGround,
  72. "",
  73. cross,
  74. "",
  75. "",
  76. texType))
  77. {
  78. pWidget->autorelease();
  79. return pWidget;
  80. }
  81. CC_SAFE_DELETE(pWidget);
  82. return nullptr;
  83. }
  84. void RadioButton::dispatchSelectChangedEvent(bool selected)
  85. {
  86. EventType eventType = (selected ? EventType::SELECTED : EventType::UNSELECTED);
  87. this->retain();
  88. if (_radioButtonEventCallback)
  89. {
  90. _radioButtonEventCallback(this, eventType);
  91. }
  92. if (_ccEventCallback)
  93. {
  94. _ccEventCallback(this, static_cast<int>(eventType));
  95. }
  96. if(selected && _group != nullptr)
  97. {
  98. _group->onChangedRadioButtonSelect(this);
  99. }
  100. this->release();
  101. }
  102. void RadioButton::addEventListener(const ccRadioButtonCallback& callback)
  103. {
  104. _radioButtonEventCallback = callback;
  105. }
  106. void RadioButton::releaseUpEvent()
  107. {
  108. Widget::releaseUpEvent();
  109. if (!_isSelected)
  110. {
  111. setSelected(true);
  112. dispatchSelectChangedEvent(true);
  113. }
  114. }
  115. std::string RadioButton::getDescription() const
  116. {
  117. return "RadioButton";
  118. }
  119. Widget* RadioButton::createCloneInstance()
  120. {
  121. return RadioButton::create();
  122. }
  123. void RadioButton::copySpecialProperties(Widget *widget)
  124. {
  125. RadioButton* radioButton = dynamic_cast<RadioButton*>(widget);
  126. if (radioButton)
  127. {
  128. AbstractCheckButton::copySpecialProperties(widget);
  129. _radioButtonEventCallback = radioButton->_radioButtonEventCallback;
  130. _ccEventCallback = radioButton->_ccEventCallback;
  131. _group = radioButton->_group;
  132. }
  133. }
  134. RadioButtonGroup::RadioButtonGroup() :
  135. _radioButtonGroupEventCallback(nullptr),
  136. _selectedRadioButton(nullptr),
  137. _allowedNoSelection(false)
  138. {
  139. }
  140. RadioButtonGroup::~RadioButtonGroup()
  141. {
  142. _radioButtonGroupEventCallback = nullptr;
  143. _selectedRadioButton = nullptr;
  144. _radioButtons.clear();
  145. }
  146. RadioButtonGroup* RadioButtonGroup::create()
  147. {
  148. RadioButtonGroup* widget = new (std::nothrow) RadioButtonGroup();
  149. if (widget && widget->init())
  150. {
  151. widget->autorelease();
  152. return widget;
  153. }
  154. CC_SAFE_DELETE(widget);
  155. return nullptr;
  156. }
  157. void RadioButtonGroup::addEventListener(const ccRadioButtonGroupCallback& callback)
  158. {
  159. _radioButtonGroupEventCallback = callback;
  160. }
  161. void RadioButtonGroup::addRadioButton(RadioButton* radioButton)
  162. {
  163. if(radioButton != nullptr)
  164. {
  165. CCASSERT(!radioButton->_group, "It already belongs to a group!");
  166. radioButton->_group = this;
  167. _radioButtons.pushBack(radioButton);
  168. if(!_allowedNoSelection && _selectedRadioButton == nullptr)
  169. {
  170. setSelectedButtonWithoutEvent(radioButton);
  171. }
  172. }
  173. }
  174. void RadioButtonGroup::removeRadioButton(RadioButton* radioButton)
  175. {
  176. ssize_t index = _radioButtons.getIndex(radioButton);
  177. if( index == CC_INVALID_INDEX )
  178. {
  179. CCLOGERROR("The radio button does not belong to this group!");
  180. return;
  181. }
  182. if(radioButton != nullptr)
  183. {
  184. radioButton->_group = nullptr;
  185. if(radioButton == _selectedRadioButton)
  186. {
  187. deselect();
  188. }
  189. _radioButtons.erase(index);
  190. if(!_allowedNoSelection && _selectedRadioButton == nullptr && !_radioButtons.empty())
  191. {
  192. setSelectedButtonWithoutEvent(0);
  193. }
  194. }
  195. }
  196. void RadioButtonGroup::removeAllRadioButtons()
  197. {
  198. while(!_radioButtons.empty())
  199. {
  200. removeRadioButton(_radioButtons.at(0));
  201. }
  202. }
  203. ssize_t RadioButtonGroup::getNumberOfRadioButtons() const
  204. {
  205. return _radioButtons.size();
  206. }
  207. RadioButton* RadioButtonGroup::getRadioButtonByIndex(int index) const
  208. {
  209. if(index >= _radioButtons.size())
  210. {
  211. CCLOGERROR("Out of array index! length=%d, requestedIndex=%d", (int)_radioButtons.size(), index);
  212. return nullptr;
  213. }
  214. return _radioButtons.at(index);
  215. }
  216. void RadioButtonGroup::deselect()
  217. {
  218. if(_selectedRadioButton != nullptr)
  219. {
  220. _selectedRadioButton->setSelected(false);
  221. _selectedRadioButton->dispatchSelectChangedEvent(false);
  222. }
  223. _selectedRadioButton = nullptr;
  224. }
  225. int RadioButtonGroup::getSelectedButtonIndex() const
  226. {
  227. return (int) _radioButtons.getIndex(_selectedRadioButton);
  228. }
  229. void RadioButtonGroup::setSelectedButton(int index)
  230. {
  231. CCASSERT(index < _radioButtons.size(), "Out of array index!");
  232. setSelectedButton(_radioButtons.at(index));
  233. }
  234. void RadioButtonGroup::setSelectedButton(RadioButton* radioButton)
  235. {
  236. setSelectedButtonWithoutEvent(radioButton);
  237. onChangedRadioButtonSelect(_selectedRadioButton);
  238. }
  239. void RadioButtonGroup::setSelectedButtonWithoutEvent(int index)
  240. {
  241. setSelectedButtonWithoutEvent(_radioButtons.at(index));
  242. }
  243. void RadioButtonGroup::setSelectedButtonWithoutEvent(RadioButton* radioButton)
  244. {
  245. if(!_allowedNoSelection && radioButton == nullptr)
  246. {
  247. return;
  248. }
  249. if(_selectedRadioButton == radioButton)
  250. {
  251. return;
  252. }
  253. if(radioButton != nullptr && !_radioButtons.contains(radioButton))
  254. {
  255. CCLOGERROR("The radio button does not belong to this group!");
  256. return;
  257. }
  258. deselect();
  259. _selectedRadioButton = radioButton;
  260. if(_selectedRadioButton != nullptr)
  261. {
  262. _selectedRadioButton->setSelected(true);
  263. }
  264. }
  265. std::string RadioButtonGroup::getDescription() const
  266. {
  267. return "RadioButtonGroup";
  268. }
  269. void RadioButtonGroup::setAllowedNoSelection(bool allowedNoSelection)
  270. {
  271. _allowedNoSelection = allowedNoSelection;
  272. if(!_allowedNoSelection && _selectedRadioButton == nullptr)
  273. {
  274. if (_radioButtons.size() > 0)
  275. {
  276. setSelectedButton(_radioButtons.at(0));
  277. }
  278. }
  279. }
  280. bool RadioButtonGroup::isAllowedNoSelection() const
  281. {
  282. return _allowedNoSelection;
  283. }
  284. Widget* RadioButtonGroup::createCloneInstance()
  285. {
  286. return RadioButtonGroup::create();
  287. }
  288. void RadioButtonGroup::copySpecialProperties(Widget *widget)
  289. {
  290. RadioButtonGroup* radioButtonGroup = dynamic_cast<RadioButtonGroup*>(widget);
  291. if (radioButtonGroup)
  292. {
  293. _radioButtonGroupEventCallback = radioButtonGroup->_radioButtonGroupEventCallback;
  294. _ccEventCallback = radioButtonGroup->_ccEventCallback;
  295. _selectedRadioButton = radioButtonGroup->_selectedRadioButton;
  296. _allowedNoSelection = radioButtonGroup->_allowedNoSelection;
  297. _radioButtons.clear();
  298. for(const auto& radioButton : radioButtonGroup->_radioButtons)
  299. {
  300. _radioButtons.pushBack(radioButton);
  301. }
  302. }
  303. }
  304. void RadioButtonGroup::onChangedRadioButtonSelect(RadioButton* radioButton)
  305. {
  306. if(_selectedRadioButton != radioButton)
  307. {
  308. deselect();
  309. _selectedRadioButton = radioButton;
  310. }
  311. this->retain();
  312. if (_radioButtonGroupEventCallback)
  313. {
  314. int index = (int) _radioButtons.getIndex(radioButton);
  315. _radioButtonGroupEventCallback(_selectedRadioButton, index, EventType::SELECT_CHANGED);
  316. }
  317. if (_ccEventCallback)
  318. {
  319. _ccEventCallback(this, static_cast<int>(EventType::SELECT_CHANGED));
  320. }
  321. this->release();
  322. }
  323. }
  324. NS_CC_END