RadioButton.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. GP_ASSERT(properties);
  25. RadioButton* radioButton = new RadioButton();
  26. radioButton->initialize(style, properties);
  27. properties->getVector2("imageSize", &radioButton->_imageSize);
  28. if (properties->getBool("selected"))
  29. {
  30. RadioButton::clearSelected(radioButton->_groupId);
  31. radioButton->_selected = true;
  32. }
  33. const char* groupId = properties->getString("group");
  34. if (groupId)
  35. {
  36. radioButton->_groupId = groupId;
  37. }
  38. __radioButtons.push_back(radioButton);
  39. return radioButton;
  40. }
  41. bool RadioButton::isSelected() const
  42. {
  43. return _selected;
  44. }
  45. void RadioButton::setImageSize(float width, float height)
  46. {
  47. _imageSize.set(width, height);
  48. }
  49. const Vector2& RadioButton::getImageSize() const
  50. {
  51. return _imageSize;
  52. }
  53. void RadioButton::addListener(Control::Listener* listener, int eventFlags)
  54. {
  55. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  56. {
  57. GP_ERROR("TEXT_CHANGED event is not applicable to RadioButton.");
  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. GP_ASSERT(radioButton);
  96. if (groupId == radioButton->_groupId)
  97. {
  98. radioButton->_selected = false;
  99. radioButton->_dirty = true;
  100. radioButton->notifyListeners(Listener::VALUE_CHANGED);
  101. }
  102. }
  103. }
  104. void RadioButton::update(const Rectangle& clip)
  105. {
  106. Label::update(clip);
  107. Vector2 size;
  108. if (_imageSize.isZero())
  109. {
  110. if (_selected)
  111. {
  112. const Rectangle& selectedRegion = getImageRegion("selected", _state);
  113. size.set(selectedRegion.width, selectedRegion.height);
  114. }
  115. else
  116. {
  117. const Rectangle& unselectedRegion = getImageRegion("unselected", _state);
  118. size.set(unselectedRegion.width, unselectedRegion.height);
  119. }
  120. }
  121. else
  122. {
  123. size.set(_imageSize);
  124. }
  125. float iconWidth = size.x;
  126. _textBounds.x += iconWidth + 5;
  127. _textBounds.width -= iconWidth + 5;
  128. if (_selected)
  129. {
  130. _image = getImage("selected", _state);
  131. }
  132. else
  133. {
  134. _image = getImage("unselected", _state);
  135. }
  136. }
  137. void RadioButton::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  138. {
  139. GP_ASSERT(spriteBatch);
  140. GP_ASSERT(_image);
  141. // Left, v-center.
  142. // TODO: Set an alignment for radio button images.
  143. const Theme::Border& border = getBorder(_state);
  144. const Theme::Padding padding = getPadding();
  145. const Rectangle& region = _image->getRegion();
  146. const Theme::UVs& uvs = _image->getUVs();
  147. Vector4 color = _image->getColor();
  148. color.w *= _opacity;
  149. Vector2 size;
  150. if (_imageSize.isZero())
  151. {
  152. size.set(region.width, region.height);
  153. }
  154. else
  155. {
  156. size.set(_imageSize);
  157. }
  158. Vector2 pos(clip.x + _bounds.x + border.left + padding.left,
  159. clip.y + _bounds.y + (_clipBounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  160. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _clip);
  161. }
  162. }