RadioButton.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  84. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  85. {
  86. if (!_selected)
  87. {
  88. RadioButton::clearSelected(_groupId);
  89. _selected = true;
  90. notifyListeners(Listener::VALUE_CHANGED);
  91. }
  92. }
  93. }
  94. }
  95. break;
  96. }
  97. return Button::touchEvent(evt, x, y, contactIndex);
  98. }
  99. void RadioButton::clearSelected(const std::string& groupId)
  100. {
  101. std::vector<RadioButton*>::const_iterator it;
  102. for (it = __radioButtons.begin(); it < __radioButtons.end(); ++it)
  103. {
  104. RadioButton* radioButton = *it;
  105. GP_ASSERT(radioButton);
  106. if (groupId == radioButton->_groupId)
  107. {
  108. radioButton->_selected = false;
  109. radioButton->_dirty = true;
  110. radioButton->notifyListeners(Listener::VALUE_CHANGED);
  111. }
  112. }
  113. }
  114. void RadioButton::update(const Control* container, const Vector2& offset)
  115. {
  116. Label::update(container, offset);
  117. Vector2 size;
  118. if (_imageSize.isZero())
  119. {
  120. if (_selected)
  121. {
  122. const Rectangle& selectedRegion = getImageRegion("selected", _state);
  123. size.set(selectedRegion.width, selectedRegion.height);
  124. }
  125. else
  126. {
  127. const Rectangle& unselectedRegion = getImageRegion("unselected", _state);
  128. size.set(unselectedRegion.width, unselectedRegion.height);
  129. }
  130. }
  131. else
  132. {
  133. size.set(_imageSize);
  134. }
  135. float iconWidth = size.x;
  136. _textBounds.x += iconWidth + 5;
  137. _textBounds.width -= iconWidth + 5;
  138. if (_selected)
  139. {
  140. _image = getImage("selected", _state);
  141. }
  142. else
  143. {
  144. _image = getImage("unselected", _state);
  145. }
  146. }
  147. void RadioButton::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  148. {
  149. GP_ASSERT(spriteBatch);
  150. GP_ASSERT(_image);
  151. // Left, v-center.
  152. // TODO: Set an alignment for radio button images.
  153. const Rectangle& region = _image->getRegion();
  154. const Theme::UVs& uvs = _image->getUVs();
  155. Vector4 color = _image->getColor();
  156. color.w *= _opacity;
  157. Vector2 size;
  158. if (_imageSize.isZero())
  159. {
  160. size.set(region.width, region.height);
  161. }
  162. else
  163. {
  164. size.set(_imageSize);
  165. }
  166. Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
  167. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  168. }
  169. const char* RadioButton::getType() const
  170. {
  171. return "radioButton";
  172. }
  173. void RadioButton::setGroupId(const char* groupId)
  174. {
  175. _groupId = groupId;
  176. }
  177. const char* RadioButton::getGroupId() const
  178. {
  179. return _groupId.c_str();
  180. }
  181. }