RadioButton.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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->initialize(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. bool RadioButton::isSelected() const
  41. {
  42. return _selected;
  43. }
  44. void RadioButton::setImageSize(float width, float height)
  45. {
  46. _imageSize.set(width, height);
  47. }
  48. const Vector2& RadioButton::getImageSize() const
  49. {
  50. return _imageSize;
  51. }
  52. void RadioButton::addListener(Control::Listener* listener, int eventFlags)
  53. {
  54. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  55. {
  56. assert("TEXT_CHANGED event is not applicable to RadioButton.");
  57. eventFlags &= ~Listener::TEXT_CHANGED;
  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 > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  74. y > _clipBounds.y && y <= _clipBounds.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. if (groupId == radioButton->_groupId)
  96. {
  97. radioButton->_selected = false;
  98. radioButton->_dirty = true;
  99. radioButton->notifyListeners(Listener::VALUE_CHANGED);
  100. }
  101. }
  102. }
  103. void RadioButton::update(const Rectangle& clip, const Vector2& offset)
  104. {
  105. Label::update(clip, offset);
  106. Vector2 size;
  107. if (_imageSize.isZero())
  108. {
  109. if (_selected)
  110. {
  111. const Rectangle& selectedRegion = getImageRegion("selected", _state);
  112. size.set(selectedRegion.width, selectedRegion.height);
  113. }
  114. else
  115. {
  116. const Rectangle& unselectedRegion = getImageRegion("unselected", _state);
  117. size.set(unselectedRegion.width, unselectedRegion.height);
  118. }
  119. }
  120. else
  121. {
  122. size.set(_imageSize);
  123. }
  124. float iconWidth = size.x;
  125. _textBounds.x += iconWidth + 5;
  126. _textBounds.width -= iconWidth + 5;
  127. if (_selected)
  128. {
  129. _image = getImage("selected", _state);
  130. }
  131. else
  132. {
  133. _image = getImage("unselected", _state);
  134. }
  135. }
  136. void RadioButton::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  137. {
  138. // Left, v-center.
  139. // TODO: Set an alignment for radio button images.
  140. const Rectangle& region = _image->getRegion();
  141. const Theme::UVs& uvs = _image->getUVs();
  142. Vector4 color = _image->getColor();
  143. color.w *= _opacity;
  144. Vector2 size;
  145. if (_imageSize.isZero())
  146. {
  147. size.set(region.width, region.height);
  148. }
  149. else
  150. {
  151. size.set(_imageSize);
  152. }
  153. Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
  154. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  155. }
  156. }