RadioButton.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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->init(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. void RadioButton::setImageSize(float width, float height)
  41. {
  42. _imageSize.set(width, height);
  43. }
  44. const Vector2& RadioButton::getImageSize() const
  45. {
  46. return _imageSize;
  47. }
  48. void RadioButton::addListener(Control::Listener* listener, int eventFlags)
  49. {
  50. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  51. {
  52. assert("TEXT_CHANGED event is not applicable to RadioButton.");
  53. eventFlags &= ~Listener::TEXT_CHANGED;
  54. }
  55. Control::addListener(listener, eventFlags);
  56. }
  57. bool RadioButton::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  58. {
  59. if (!isEnabled())
  60. {
  61. return false;
  62. }
  63. switch (evt)
  64. {
  65. case Touch::TOUCH_RELEASE:
  66. {
  67. if (_state == Control::ACTIVE)
  68. {
  69. if (x > 0 && x <= _bounds.width &&
  70. y > 0 && y <= _bounds.height)
  71. {
  72. if (!_selected)
  73. {
  74. RadioButton::clearSelected(_groupId);
  75. _selected = true;
  76. notifyListeners(Listener::VALUE_CHANGED);
  77. }
  78. }
  79. }
  80. }
  81. break;
  82. }
  83. return Button::touchEvent(evt, x, y, contactIndex);
  84. }
  85. void RadioButton::clearSelected(const std::string& groupId)
  86. {
  87. std::vector<RadioButton*>::const_iterator it;
  88. for (it = __radioButtons.begin(); it < __radioButtons.end(); it++)
  89. {
  90. RadioButton* radioButton = *it;
  91. if (groupId == radioButton->_groupId)
  92. {
  93. radioButton->_selected = false;
  94. radioButton->_dirty = true;
  95. }
  96. }
  97. }
  98. void RadioButton::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  99. {
  100. // Left, v-center.
  101. // TODO: Set an alignment for radio button images.
  102. const Theme::Border border = getBorder(_state);
  103. const Theme::Padding padding = _style->getPadding();
  104. float opacity = getOpacity(_state);
  105. if (_selected)
  106. {
  107. const Rectangle& selectedRegion = getImageRegion("selected", _state);
  108. const Theme::UVs& selected = getImageUVs("selected", _state);
  109. Vector4 selectedColor = getImageColor("selected", _state);
  110. selectedColor.w *= opacity;
  111. Vector2 size;
  112. if (_imageSize.isZero())
  113. {
  114. size.set(selectedRegion.width, selectedRegion.height);
  115. }
  116. else
  117. {
  118. size.set(_imageSize);
  119. }
  120. Vector2 pos(clip.x + _position.x + border.left + padding.left,
  121. clip.y + _position.y + (_bounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  122. spriteBatch->draw(pos.x, pos.y, size.x, size.y, selected.u1, selected.v1, selected.u2, selected.v2, selectedColor, _clip);
  123. }
  124. else
  125. {
  126. const Rectangle& unselectedRegion = getImageRegion("unselected", _state);
  127. const Theme::UVs& unselected = getImageUVs("unselected", _state);
  128. Vector4 unselectedColor = getImageColor("unselected", _state);
  129. unselectedColor.w *= opacity;
  130. Vector2 size;
  131. if (_imageSize.isZero())
  132. {
  133. size.set(unselectedRegion.width, unselectedRegion.height);
  134. }
  135. else
  136. {
  137. size.set(_imageSize);
  138. }
  139. Vector2 pos(clip.x + _position.x + border.left + padding.left,
  140. clip.y + _position.y + (_bounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  141. spriteBatch->draw(pos.x, pos.y, size.x, size.y, unselected.u1, unselected.v1, unselected.u2, unselected.v2, unselectedColor, _clip);
  142. }
  143. }
  144. void RadioButton::update(const Rectangle& clip)
  145. {
  146. Control::update(clip);
  147. Vector2 size;
  148. if (_imageSize.isZero())
  149. {
  150. if (_selected)
  151. {
  152. const Rectangle& selectedRegion = getImageRegion("selected", _state);
  153. size.set(selectedRegion.width, selectedRegion.height);
  154. }
  155. else
  156. {
  157. const Rectangle& unselectedRegion = getImageRegion("unselected", _state);
  158. size.set(unselectedRegion.width, unselectedRegion.height);
  159. }
  160. }
  161. else
  162. {
  163. size.set(_imageSize);
  164. }
  165. float iconWidth = size.x;
  166. _textBounds.x += iconWidth;
  167. _textBounds.width -= iconWidth;
  168. }
  169. }