RadioButton.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. }
  16. RadioButton* RadioButton::create(Theme::Style* style, Properties* properties)
  17. {
  18. RadioButton* radioButton = new RadioButton();
  19. radioButton->init(style, properties);
  20. properties->getVector2("iconSize", &radioButton->_iconSize);
  21. if (properties->getBool("selected"))
  22. {
  23. RadioButton::clearSelected(radioButton->_groupId);
  24. radioButton->_selected = true;
  25. }
  26. const char* groupId = properties->getString("group");
  27. if (groupId)
  28. {
  29. radioButton->_groupId = groupId;
  30. }
  31. __radioButtons.push_back(radioButton);
  32. return radioButton;
  33. }
  34. RadioButton* RadioButton::getRadioButton(const char* id)
  35. {
  36. std::vector<RadioButton*>::const_iterator it;
  37. for (it = __radioButtons.begin(); it < __radioButtons.end(); it++)
  38. {
  39. RadioButton* radioButton = *it;
  40. if (strcmp(id, radioButton->getID()) == 0)
  41. {
  42. return radioButton;
  43. }
  44. }
  45. return NULL;
  46. }
  47. bool RadioButton::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  48. {
  49. if (isEnabled())
  50. {
  51. switch (evt)
  52. {
  53. case Touch::TOUCH_RELEASE:
  54. {
  55. if (_state == Control::STATE_ACTIVE)
  56. {
  57. if (x > 0 && x <= _size.x &&
  58. y > 0 && y <= _size.y)
  59. {
  60. if (_callback)
  61. {
  62. _callback->trigger(this);
  63. }
  64. RadioButton::clearSelected(_groupId);
  65. _selected = true;
  66. }
  67. }
  68. }
  69. break;
  70. }
  71. return Button::touchEvent(evt, x, y, contactIndex);
  72. }
  73. return false;
  74. }
  75. void RadioButton::clearSelected(const std::string& groupId)
  76. {
  77. std::vector<RadioButton*>::const_iterator it;
  78. for (it = __radioButtons.begin(); it < __radioButtons.end(); it++)
  79. {
  80. RadioButton* radioButton = *it;
  81. if (groupId == radioButton->_groupId)
  82. {
  83. radioButton->_selected = false;
  84. }
  85. }
  86. }
  87. void RadioButton::drawSprites(SpriteBatch* spriteBatch, const Vector2& position)
  88. {
  89. // Left, v-center.
  90. // TODO: Set an alignment for icons.
  91. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  92. Theme::Icon* icon = overlay->getRadioButtonIcon();
  93. if (icon)
  94. {
  95. Theme::Border border;
  96. Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  97. if (containerRegion)
  98. {
  99. border = containerRegion->getBorder();
  100. }
  101. const Theme::Padding padding = _style->getPadding();
  102. Vector2& size = _iconSize;
  103. if (_iconSize.isZero())
  104. {
  105. size = icon->getSize();
  106. }
  107. const Vector4 color = icon->getColor();
  108. Vector2 pos(position.x + _position.x + border.left + padding.left,
  109. position.y + _position.y + (_size.y - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  110. if (_selected)
  111. {
  112. const Theme::UVs on = icon->getOnUVs();
  113. spriteBatch->draw(pos.x, pos.y, size.x, size.y, on.u1, on.v1, on.u2, on.v2, color);
  114. }
  115. else
  116. {
  117. const Theme::UVs off = icon->getOffUVs();
  118. spriteBatch->draw(pos.x, pos.y, size.x, size.y, off.u1, off.v1, off.u2, off.v2, color);
  119. }
  120. }
  121. }
  122. void RadioButton::update(const Vector2& position)
  123. {
  124. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  125. Theme::Icon* icon = overlay->getCheckBoxIcon();
  126. Theme::Border border;
  127. Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  128. if (containerRegion)
  129. {
  130. border = overlay->getContainerRegion()->getBorder();
  131. }
  132. Theme::Padding padding = _style->getPadding();
  133. // Set up the text viewport.
  134. float iconWidth = 0.0f;
  135. if (icon)
  136. {
  137. iconWidth = icon->getSize().x;
  138. }
  139. Font* font = overlay->getFont();
  140. Vector2 pos(position.x + _position.x + border.left + padding.left + iconWidth,
  141. position.y + _position.y + border.top + padding.top);
  142. _viewport.set(pos.x, pos.y,
  143. _size.x - border.left - padding.left - border.right - padding.right - iconWidth,
  144. _size.y - border.top - padding.top - border.bottom - padding.bottom - overlay->getFontSize());
  145. }
  146. void RadioButton::drawText(const Vector2& position)
  147. {
  148. // TODO: Batch all labels that use the same font.
  149. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  150. Font* font = overlay->getFont();
  151. // Draw the text.
  152. font->begin();
  153. font->drawText(_text.c_str(), _viewport, overlay->getTextColor(), overlay->getFontSize(), overlay->getTextAlignment(), true, overlay->getTextRightToLeft());
  154. font->end();
  155. _dirty = false;
  156. }
  157. }