2
0

RadioButton.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. Control* 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. void RadioButton::clearSelected(const std::string& groupId)
  80. {
  81. std::vector<RadioButton*>::const_iterator it;
  82. for (it = __radioButtons.begin(); it < __radioButtons.end(); ++it)
  83. {
  84. RadioButton* radioButton = *it;
  85. GP_ASSERT(radioButton);
  86. if (groupId == radioButton->_groupId)
  87. {
  88. radioButton->_selected = false;
  89. radioButton->_dirty = true;
  90. radioButton->notifyListeners(Control::Listener::VALUE_CHANGED);
  91. }
  92. }
  93. }
  94. bool RadioButton::keyEvent(Keyboard::KeyEvent evt, int key)
  95. {
  96. if (getState() == ACTIVE && evt == Keyboard::KEY_RELEASE && key == Keyboard::KEY_RETURN && !_selected)
  97. {
  98. RadioButton::clearSelected(_groupId);
  99. _selected = true;
  100. notifyListeners(Control::Listener::VALUE_CHANGED);
  101. }
  102. return Button::keyEvent(evt, key);
  103. }
  104. void RadioButton::controlEvent(Control::Listener::EventType evt)
  105. {
  106. Button::controlEvent(evt);
  107. switch (evt)
  108. {
  109. case Control::Listener::CLICK:
  110. if (!_selected)
  111. {
  112. RadioButton::clearSelected(_groupId);
  113. _selected = true;
  114. notifyListeners(Control::Listener::VALUE_CHANGED);
  115. }
  116. break;
  117. }
  118. }
  119. void RadioButton::update(const Control* container, const Vector2& offset)
  120. {
  121. Label::update(container, offset);
  122. Vector2 size;
  123. if (_imageSize.isZero())
  124. {
  125. if (_selected)
  126. {
  127. const Rectangle& selectedRegion = getImageRegion("selected", getState());
  128. size.set(selectedRegion.width, selectedRegion.height);
  129. }
  130. else
  131. {
  132. const Rectangle& unselectedRegion = getImageRegion("unselected", getState());
  133. size.set(unselectedRegion.width, unselectedRegion.height);
  134. }
  135. }
  136. else
  137. {
  138. size.set(_imageSize);
  139. }
  140. if (_autoWidth == Control::AUTO_SIZE_FIT)
  141. {
  142. // Text-only width was already measured in Label::update - append image
  143. setWidth(size.x + _bounds.width + 5);
  144. }
  145. if (_autoHeight == Control::AUTO_SIZE_FIT)
  146. {
  147. // Text-only width was already measured in Label::update - append image
  148. setHeight(std::max(getHeight(), size.y));
  149. }
  150. _textBounds.x += size.x + 5;
  151. if (_selected)
  152. {
  153. _image = getImage("selected", getState());
  154. }
  155. else
  156. {
  157. _image = getImage("unselected", getState());
  158. }
  159. }
  160. void RadioButton::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  161. {
  162. GP_ASSERT(spriteBatch);
  163. GP_ASSERT(_image);
  164. // Left, v-center.
  165. // TODO: Set an alignment for radio button images.
  166. const Rectangle& region = _image->getRegion();
  167. const Theme::UVs& uvs = _image->getUVs();
  168. Vector4 color = _image->getColor();
  169. color.w *= _opacity;
  170. Vector2 size;
  171. if (_imageSize.isZero())
  172. {
  173. size.set(region.width, region.height);
  174. }
  175. else
  176. {
  177. size.set(_imageSize);
  178. }
  179. Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
  180. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  181. }
  182. const char* RadioButton::getType() const
  183. {
  184. return "radioButton";
  185. }
  186. void RadioButton::setGroupId(const char* groupId)
  187. {
  188. _groupId = groupId;
  189. }
  190. const char* RadioButton::getGroupId() const
  191. {
  192. return _groupId.c_str();
  193. }
  194. }