RadioButton.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "Base.h"
  2. #include "RadioButton.h"
  3. namespace gameplay
  4. {
  5. static std::vector<RadioButton*> __radioButtons;
  6. RadioButton::RadioButton() : _selected(false), _image(NULL)
  7. {
  8. }
  9. RadioButton::~RadioButton()
  10. {
  11. // Remove this RadioButton from the global list.
  12. std::vector<RadioButton*>::iterator it = std::find(__radioButtons.begin(), __radioButtons.end(), this);
  13. if (it != __radioButtons.end())
  14. {
  15. __radioButtons.erase(it);
  16. }
  17. }
  18. RadioButton* RadioButton::create(const char* id, Theme::Style* style)
  19. {
  20. GP_ASSERT(style);
  21. RadioButton* radioButton = new RadioButton();
  22. if (id)
  23. radioButton->_id = id;
  24. radioButton->setStyle(style);
  25. __radioButtons.push_back(radioButton);
  26. return radioButton;
  27. }
  28. RadioButton* RadioButton::create(Theme::Style* style, Properties* properties)
  29. {
  30. GP_ASSERT(properties);
  31. RadioButton* radioButton = new RadioButton();
  32. radioButton->initialize(style, properties);
  33. properties->getVector2("imageSize", &radioButton->_imageSize);
  34. if (properties->getBool("selected"))
  35. {
  36. RadioButton::clearSelected(radioButton->_groupId);
  37. radioButton->_selected = true;
  38. }
  39. const char* groupId = properties->getString("group");
  40. if (groupId)
  41. {
  42. radioButton->_groupId = groupId;
  43. }
  44. __radioButtons.push_back(radioButton);
  45. return radioButton;
  46. }
  47. bool RadioButton::isSelected() const
  48. {
  49. return _selected;
  50. }
  51. void RadioButton::setSelected(bool selected)
  52. {
  53. if (selected)
  54. RadioButton::clearSelected(_groupId);
  55. if (selected != _selected)
  56. {
  57. _selected = selected;
  58. _dirty = true;
  59. notifyListeners(Control::Listener::VALUE_CHANGED);
  60. }
  61. }
  62. void RadioButton::setImageSize(float width, float height)
  63. {
  64. _imageSize.set(width, height);
  65. _dirty = true;
  66. }
  67. const Vector2& RadioButton::getImageSize() const
  68. {
  69. return _imageSize;
  70. }
  71. void RadioButton::addListener(Control::Listener* listener, int eventFlags)
  72. {
  73. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  74. {
  75. GP_ERROR("TEXT_CHANGED event is not applicable to RadioButton.");
  76. }
  77. Control::addListener(listener, eventFlags);
  78. }
  79. bool RadioButton::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  80. {
  81. switch (evt)
  82. {
  83. case Touch::TOUCH_RELEASE:
  84. {
  85. if (_contactIndex == (int) _contactIndex && _state == Control::ACTIVE)
  86. {
  87. if (!_parent->isScrolling() &&
  88. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  89. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  90. {
  91. if (!_selected)
  92. {
  93. RadioButton::clearSelected(_groupId);
  94. _selected = true;
  95. notifyListeners(Control::Listener::VALUE_CHANGED);
  96. }
  97. }
  98. }
  99. }
  100. break;
  101. }
  102. return Button::touchEvent(evt, x, y, contactIndex);
  103. }
  104. bool RadioButton::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  105. {
  106. switch (evt)
  107. {
  108. case Gamepad::BUTTON_EVENT:
  109. if (_state == Control::ACTIVE)
  110. {
  111. if (!gamepad->isButtonDown(Gamepad::BUTTON_A) &&
  112. !gamepad->isButtonDown(Gamepad::BUTTON_X))
  113. {
  114. RadioButton::clearSelected(_groupId);
  115. _selected = true;
  116. notifyListeners(Control::Listener::VALUE_CHANGED);
  117. }
  118. }
  119. break;
  120. }
  121. return Button::gamepadEvent(evt, gamepad, analogIndex);
  122. }
  123. void RadioButton::clearSelected(const std::string& groupId)
  124. {
  125. std::vector<RadioButton*>::const_iterator it;
  126. for (it = __radioButtons.begin(); it < __radioButtons.end(); ++it)
  127. {
  128. RadioButton* radioButton = *it;
  129. GP_ASSERT(radioButton);
  130. if (groupId == radioButton->_groupId)
  131. {
  132. radioButton->_selected = false;
  133. radioButton->_dirty = true;
  134. radioButton->notifyListeners(Control::Listener::VALUE_CHANGED);
  135. }
  136. }
  137. }
  138. bool RadioButton::keyEvent(Keyboard::KeyEvent evt, int key)
  139. {
  140. if (_state == ACTIVE && evt == Keyboard::KEY_RELEASE && key == Keyboard::KEY_RETURN && !_selected)
  141. {
  142. RadioButton::clearSelected(_groupId);
  143. _selected = true;
  144. notifyListeners(Control::Listener::VALUE_CHANGED);
  145. }
  146. return Button::keyEvent(evt, key);
  147. }
  148. void RadioButton::update(const Control* container, const Vector2& offset)
  149. {
  150. Label::update(container, offset);
  151. Vector2 size;
  152. if (_imageSize.isZero())
  153. {
  154. if (_selected)
  155. {
  156. const Rectangle& selectedRegion = getImageRegion("selected", _state);
  157. size.set(selectedRegion.width, selectedRegion.height);
  158. }
  159. else
  160. {
  161. const Rectangle& unselectedRegion = getImageRegion("unselected", _state);
  162. size.set(unselectedRegion.width, unselectedRegion.height);
  163. }
  164. }
  165. else
  166. {
  167. size.set(_imageSize);
  168. }
  169. float iconWidth = size.x;
  170. _textBounds.x += iconWidth + 5;
  171. _textBounds.width -= iconWidth + 5;
  172. if (_selected)
  173. {
  174. _image = getImage("selected", _state);
  175. }
  176. else
  177. {
  178. _image = getImage("unselected", _state);
  179. }
  180. }
  181. void RadioButton::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  182. {
  183. GP_ASSERT(spriteBatch);
  184. GP_ASSERT(_image);
  185. // Left, v-center.
  186. // TODO: Set an alignment for radio button images.
  187. const Rectangle& region = _image->getRegion();
  188. const Theme::UVs& uvs = _image->getUVs();
  189. Vector4 color = _image->getColor();
  190. color.w *= _opacity;
  191. Vector2 size;
  192. if (_imageSize.isZero())
  193. {
  194. size.set(region.width, region.height);
  195. }
  196. else
  197. {
  198. size.set(_imageSize);
  199. }
  200. Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
  201. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  202. }
  203. const char* RadioButton::getType() const
  204. {
  205. return "radioButton";
  206. }
  207. void RadioButton::setGroupId(const char* groupId)
  208. {
  209. _groupId = groupId;
  210. }
  211. const char* RadioButton::getGroupId() const
  212. {
  213. return _groupId.c_str();
  214. }
  215. }