RadioButton.cpp 4.8 KB

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