RadioButton.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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->init(style, properties);
  26. properties->getVector2("iconSize", &radioButton->_iconSize);
  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. void RadioButton::setIconSize(float width, float height)
  41. {
  42. _iconSize.set(width, height);
  43. }
  44. const Vector2& RadioButton::getIconSize() const
  45. {
  46. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  47. Theme::Icon* icon = overlay->getCheckBoxIcon();
  48. if (_iconSize.isZero() && icon)
  49. {
  50. return icon->getSize();
  51. }
  52. return _iconSize;
  53. }
  54. void RadioButton::addListener(Control::Listener* listener, int eventFlags)
  55. {
  56. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  57. {
  58. assert("TEXT_CHANGED event is not applicable to RadioButton.");
  59. eventFlags &= ~Listener::TEXT_CHANGED;
  60. }
  61. Control::addListener(listener, eventFlags);
  62. }
  63. bool RadioButton::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  64. {
  65. if (!isEnabled())
  66. {
  67. return false;
  68. }
  69. switch (evt)
  70. {
  71. case Touch::TOUCH_RELEASE:
  72. {
  73. if (_state == Control::ACTIVE)
  74. {
  75. if (x > 0 && x <= _bounds.width &&
  76. y > 0 && y <= _bounds.height)
  77. {
  78. if (!_selected)
  79. {
  80. RadioButton::clearSelected(_groupId);
  81. _selected = true;
  82. notifyListeners(Listener::VALUE_CHANGED);
  83. }
  84. }
  85. }
  86. }
  87. break;
  88. }
  89. return Button::touchEvent(evt, x, y, contactIndex);
  90. }
  91. void RadioButton::clearSelected(const std::string& groupId)
  92. {
  93. std::vector<RadioButton*>::const_iterator it;
  94. for (it = __radioButtons.begin(); it < __radioButtons.end(); it++)
  95. {
  96. RadioButton* radioButton = *it;
  97. if (groupId == radioButton->_groupId)
  98. {
  99. radioButton->_selected = false;
  100. radioButton->_dirty = true;
  101. }
  102. }
  103. }
  104. void RadioButton::drawSprites(SpriteBatch* spriteBatch, const Rectangle& clip)
  105. {
  106. // Left, v-center.
  107. // TODO: Set an alignment for icons.
  108. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  109. Theme::Icon* icon = overlay->getRadioButtonIcon();
  110. if (icon)
  111. {
  112. Theme::Border border;
  113. Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  114. if (containerRegion)
  115. {
  116. border = containerRegion->getBorder();
  117. }
  118. const Theme::Padding padding = _style->getPadding();
  119. Vector2& size = _iconSize;
  120. if (_iconSize.isZero())
  121. {
  122. size = icon->getSize();
  123. }
  124. const Vector4 color = icon->getColor();
  125. Vector2 pos(clip.x + _position.x + border.left + padding.left,
  126. clip.y + _position.y + (_bounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  127. if (_selected)
  128. {
  129. const Theme::UVs on = icon->getOnUVs();
  130. spriteBatch->draw(pos.x, pos.y, size.x, size.y, on.u1, on.v1, on.u2, on.v2, color);
  131. }
  132. else
  133. {
  134. const Theme::UVs off = icon->getOffUVs();
  135. spriteBatch->draw(pos.x, pos.y, size.x, size.y, off.u1, off.v1, off.u2, off.v2, color);
  136. }
  137. }
  138. }
  139. void RadioButton::update(const Rectangle& clip)
  140. {
  141. Control::update(clip);
  142. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  143. Theme::Icon* icon = overlay->getCheckBoxIcon();
  144. Vector2& size = _iconSize;
  145. if (_iconSize.isZero() && icon)
  146. {
  147. size = icon->getSize();
  148. }
  149. float iconWidth = size.x;
  150. _textBounds.x += iconWidth;
  151. _textBounds.width -= iconWidth;
  152. }
  153. }