RadioButton.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "Base.h"
  2. #include "RadioButton.h"
  3. namespace gameplay
  4. {
  5. static std::vector<RadioButton*> __radioButtons;
  6. RadioButton::RadioButton() : _selected(false)
  7. {
  8. }
  9. RadioButton::RadioButton(const RadioButton& copy)
  10. {
  11. // Hidden.
  12. }
  13. RadioButton::~RadioButton()
  14. {
  15. // Remove this RadioButton from the global list.
  16. std::vector<RadioButton*>::iterator it = std::find(__radioButtons.begin(), __radioButtons.end(), this);
  17. if (it != __radioButtons.end())
  18. {
  19. __radioButtons.erase(it);
  20. }
  21. }
  22. RadioButton* RadioButton::create(Theme::Style* style, Properties* properties)
  23. {
  24. RadioButton* radioButton = new RadioButton();
  25. radioButton->initialize(style, properties);
  26. properties->getVector2("imageSize", &radioButton->_imageSize);
  27. if (properties->getBool("selected"))
  28. {
  29. RadioButton::clearSelected(radioButton->_groupId);
  30. radioButton->_selected = true;
  31. }
  32. const char* groupId = properties->getString("group");
  33. if (groupId)
  34. {
  35. radioButton->_groupId = groupId;
  36. }
  37. __radioButtons.push_back(radioButton);
  38. return radioButton;
  39. }
  40. bool RadioButton::isSelected() const
  41. {
  42. return _selected;
  43. }
  44. void RadioButton::setImageSize(float width, float height)
  45. {
  46. _imageSize.set(width, height);
  47. }
  48. const Vector2& RadioButton::getImageSize() const
  49. {
  50. return _imageSize;
  51. }
  52. void RadioButton::addListener(Control::Listener* listener, int eventFlags)
  53. {
  54. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  55. {
  56. assert("TEXT_CHANGED event is not applicable to RadioButton.");
  57. eventFlags &= ~Listener::TEXT_CHANGED;
  58. }
  59. Control::addListener(listener, eventFlags);
  60. }
  61. bool RadioButton::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  62. {
  63. if (!isEnabled())
  64. {
  65. return false;
  66. }
  67. switch (evt)
  68. {
  69. case Touch::TOUCH_RELEASE:
  70. {
  71. if (_state == Control::ACTIVE)
  72. {
  73. if (x > 0 && x <= _clipBounds.width &&
  74. y > 0 && y <= _clipBounds.height)
  75. {
  76. if (!_selected)
  77. {
  78. RadioButton::clearSelected(_groupId);
  79. _selected = true;
  80. notifyListeners(Listener::VALUE_CHANGED);
  81. }
  82. }
  83. }
  84. }
  85. break;
  86. }
  87. return Button::touchEvent(evt, x, y, contactIndex);
  88. }
  89. void RadioButton::clearSelected(const std::string& groupId)
  90. {
  91. std::vector<RadioButton*>::const_iterator it;
  92. for (it = __radioButtons.begin(); it < __radioButtons.end(); it++)
  93. {
  94. RadioButton* radioButton = *it;
  95. if (groupId == radioButton->_groupId)
  96. {
  97. radioButton->_selected = false;
  98. radioButton->_dirty = true;
  99. radioButton->notifyListeners(Listener::VALUE_CHANGED);
  100. }
  101. }
  102. }
  103. void RadioButton::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  104. {
  105. // Left, v-center.
  106. // TODO: Set an alignment for radio button images.
  107. const Theme::Border border = getBorder(_state);
  108. const Theme::Padding padding = getPadding();
  109. float opacity = getOpacity(_state);
  110. if (_selected)
  111. {
  112. const Rectangle& selectedRegion = getImageRegion("selected", _state);
  113. const Theme::UVs& selected = getImageUVs("selected", _state);
  114. Vector4 selectedColor = getImageColor("selected", _state);
  115. selectedColor.w *= opacity;
  116. Vector2 size;
  117. if (_imageSize.isZero())
  118. {
  119. size.set(selectedRegion.width, selectedRegion.height);
  120. }
  121. else
  122. {
  123. size.set(_imageSize);
  124. }
  125. Vector2 pos(clip.x + _bounds.x + border.left + padding.left,
  126. clip.y + _bounds.y + (_clipBounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  127. spriteBatch->draw(pos.x, pos.y, size.x, size.y, selected.u1, selected.v1, selected.u2, selected.v2, selectedColor, _clip);
  128. }
  129. else
  130. {
  131. const Rectangle& unselectedRegion = getImageRegion("unselected", _state);
  132. const Theme::UVs& unselected = getImageUVs("unselected", _state);
  133. Vector4 unselectedColor = getImageColor("unselected", _state);
  134. unselectedColor.w *= opacity;
  135. Vector2 size;
  136. if (_imageSize.isZero())
  137. {
  138. size.set(unselectedRegion.width, unselectedRegion.height);
  139. }
  140. else
  141. {
  142. size.set(_imageSize);
  143. }
  144. Vector2 pos(clip.x + _bounds.x + border.left + padding.left,
  145. clip.y + _bounds.y + (_clipBounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  146. spriteBatch->draw(pos.x, pos.y, size.x, size.y, unselected.u1, unselected.v1, unselected.u2, unselected.v2, unselectedColor, _clip);
  147. }
  148. }
  149. void RadioButton::update(const Rectangle& clip)
  150. {
  151. Control::update(clip);
  152. Vector2 size;
  153. if (_imageSize.isZero())
  154. {
  155. if (_selected)
  156. {
  157. const Rectangle& selectedRegion = getImageRegion("selected", _state);
  158. size.set(selectedRegion.width, selectedRegion.height);
  159. }
  160. else
  161. {
  162. const Rectangle& unselectedRegion = getImageRegion("unselected", _state);
  163. size.set(unselectedRegion.width, unselectedRegion.height);
  164. }
  165. }
  166. else
  167. {
  168. size.set(_imageSize);
  169. }
  170. float iconWidth = size.x;
  171. _textBounds.x += iconWidth;
  172. _textBounds.width -= iconWidth;
  173. }
  174. }