RadioButton.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. _selected = selected;
  54. }
  55. void RadioButton::setImageSize(float width, float height)
  56. {
  57. _imageSize.set(width, height);
  58. }
  59. const Vector2& RadioButton::getImageSize() const
  60. {
  61. return _imageSize;
  62. }
  63. void RadioButton::addListener(Control::Listener* listener, int eventFlags)
  64. {
  65. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  66. {
  67. GP_ERROR("TEXT_CHANGED event is not applicable to RadioButton.");
  68. }
  69. Control::addListener(listener, eventFlags);
  70. }
  71. bool RadioButton::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  72. {
  73. if (!isEnabled())
  74. {
  75. return false;
  76. }
  77. switch (evt)
  78. {
  79. case Touch::TOUCH_RELEASE:
  80. {
  81. if (_contactIndex == (int) _contactIndex && _state == Control::ACTIVE)
  82. {
  83. if (!_parent->isScrolling() &&
  84. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  85. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  86. {
  87. if (!_selected)
  88. {
  89. RadioButton::clearSelected(_groupId);
  90. _selected = true;
  91. notifyListeners(Listener::VALUE_CHANGED);
  92. }
  93. }
  94. }
  95. }
  96. break;
  97. }
  98. return Button::touchEvent(evt, x, y, contactIndex);
  99. }
  100. void RadioButton::clearSelected(const std::string& groupId)
  101. {
  102. std::vector<RadioButton*>::const_iterator it;
  103. for (it = __radioButtons.begin(); it < __radioButtons.end(); ++it)
  104. {
  105. RadioButton* radioButton = *it;
  106. GP_ASSERT(radioButton);
  107. if (groupId == radioButton->_groupId)
  108. {
  109. radioButton->_selected = false;
  110. radioButton->_dirty = true;
  111. radioButton->notifyListeners(Listener::VALUE_CHANGED);
  112. }
  113. }
  114. }
  115. void RadioButton::update(const Control* container, const Vector2& offset)
  116. {
  117. Label::update(container, offset);
  118. Vector2 size;
  119. if (_imageSize.isZero())
  120. {
  121. if (_selected)
  122. {
  123. const Rectangle& selectedRegion = getImageRegion("selected", _state);
  124. size.set(selectedRegion.width, selectedRegion.height);
  125. }
  126. else
  127. {
  128. const Rectangle& unselectedRegion = getImageRegion("unselected", _state);
  129. size.set(unselectedRegion.width, unselectedRegion.height);
  130. }
  131. }
  132. else
  133. {
  134. size.set(_imageSize);
  135. }
  136. float iconWidth = size.x;
  137. _textBounds.x += iconWidth + 5;
  138. _textBounds.width -= iconWidth + 5;
  139. if (_selected)
  140. {
  141. _image = getImage("selected", _state);
  142. }
  143. else
  144. {
  145. _image = getImage("unselected", _state);
  146. }
  147. }
  148. void RadioButton::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  149. {
  150. GP_ASSERT(spriteBatch);
  151. GP_ASSERT(_image);
  152. // Left, v-center.
  153. // TODO: Set an alignment for radio button images.
  154. const Rectangle& region = _image->getRegion();
  155. const Theme::UVs& uvs = _image->getUVs();
  156. Vector4 color = _image->getColor();
  157. color.w *= _opacity;
  158. Vector2 size;
  159. if (_imageSize.isZero())
  160. {
  161. size.set(region.width, region.height);
  162. }
  163. else
  164. {
  165. size.set(_imageSize);
  166. }
  167. Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
  168. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  169. }
  170. const char* RadioButton::getType() const
  171. {
  172. return "radioButton";
  173. }
  174. void RadioButton::setGroupId(const char* groupId)
  175. {
  176. _groupId = groupId;
  177. }
  178. const char* RadioButton::getGroupId() const
  179. {
  180. return _groupId.c_str();
  181. }
  182. }