RadioButton.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "Base.h"
  2. #include "RadioButton.h"
  3. namespace gameplay
  4. {
  5. static std::vector<RadioButton*> __radioButtons;
  6. RadioButton::RadioButton() : _selected(false), _image(NULL)
  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. RadioButton* rb = new RadioButton();
  21. rb->_id = id ? id : "";
  22. rb->initialize("RadioButton", style, NULL);
  23. __radioButtons.push_back(rb);
  24. return rb;
  25. }
  26. Control* RadioButton::create(Theme::Style* style, Properties* properties)
  27. {
  28. RadioButton* rb = new RadioButton();
  29. rb->initialize("RadioButton", style, properties);
  30. __radioButtons.push_back(rb);
  31. return rb;
  32. }
  33. void RadioButton::initialize(const char* typeName, Theme::Style* style, Properties* properties)
  34. {
  35. Button::initialize(typeName, style, properties);
  36. if (properties)
  37. {
  38. if (properties->getBool("selected"))
  39. {
  40. RadioButton::clearSelected(_groupId);
  41. _selected = true;
  42. }
  43. const char* groupId = properties->getString("group");
  44. if (groupId)
  45. {
  46. _groupId = groupId;
  47. }
  48. }
  49. }
  50. const char* RadioButton::getTypeName() const
  51. {
  52. return "RadioButton";
  53. }
  54. bool RadioButton::isSelected() const
  55. {
  56. return _selected;
  57. }
  58. void RadioButton::setSelected(bool selected)
  59. {
  60. if (selected)
  61. RadioButton::clearSelected(_groupId);
  62. if (selected != _selected)
  63. {
  64. _selected = selected;
  65. setDirty(DIRTY_STATE);
  66. notifyListeners(Control::Listener::VALUE_CHANGED);
  67. }
  68. }
  69. void RadioButton::addListener(Control::Listener* listener, int eventFlags)
  70. {
  71. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  72. {
  73. GP_ERROR("TEXT_CHANGED event is not applicable to RadioButton.");
  74. }
  75. Control::addListener(listener, eventFlags);
  76. }
  77. void RadioButton::clearSelected(const std::string& groupId)
  78. {
  79. std::vector<RadioButton*>::const_iterator it;
  80. for (it = __radioButtons.begin(); it < __radioButtons.end(); ++it)
  81. {
  82. RadioButton* radioButton = *it;
  83. GP_ASSERT(radioButton);
  84. if (groupId == radioButton->_groupId)
  85. {
  86. radioButton->setSelected(false);
  87. }
  88. }
  89. }
  90. bool RadioButton::keyEvent(Keyboard::KeyEvent evt, int key)
  91. {
  92. if (getState() == ACTIVE && evt == Keyboard::KEY_RELEASE && key == Keyboard::KEY_RETURN && !_selected)
  93. {
  94. RadioButton::clearSelected(_groupId);
  95. _selected = true;
  96. notifyListeners(Control::Listener::VALUE_CHANGED);
  97. }
  98. return Button::keyEvent(evt, key);
  99. }
  100. void RadioButton::controlEvent(Control::Listener::EventType evt)
  101. {
  102. Button::controlEvent(evt);
  103. switch (evt)
  104. {
  105. case Control::Listener::CLICK:
  106. if (!_selected)
  107. {
  108. RadioButton::clearSelected(_groupId);
  109. _selected = true;
  110. notifyListeners(Control::Listener::VALUE_CHANGED);
  111. }
  112. break;
  113. }
  114. }
  115. void RadioButton::updateState(State state)
  116. {
  117. Label::updateState(state);
  118. _image = getImage(_selected ? "selected" : "unselected", state);
  119. }
  120. void RadioButton::updateBounds()
  121. {
  122. Label::updateBounds();
  123. Vector2 size;
  124. if (_selected)
  125. {
  126. const Rectangle& selectedRegion = getImageRegion("selected", NORMAL);
  127. size.set(selectedRegion.width, selectedRegion.height);
  128. }
  129. else
  130. {
  131. const Rectangle& unselectedRegion = getImageRegion("unselected", NORMAL);
  132. size.set(unselectedRegion.width, unselectedRegion.height);
  133. }
  134. if (_autoSize & AUTO_SIZE_HEIGHT)
  135. {
  136. // Text-only width was already measured in Label::update - append image
  137. const Theme::Border& border = getBorder(NORMAL);
  138. const Theme::Border& padding = getPadding();
  139. setHeightInternal(std::max(_bounds.height, size.y + border.top + border.bottom + padding.top + padding.bottom));
  140. }
  141. if (_autoSize & AUTO_SIZE_WIDTH)
  142. {
  143. // Text-only width was already measured in Label::update - append image
  144. setWidthInternal(_bounds.height + 5 + _bounds.width);
  145. }
  146. }
  147. void RadioButton::updateAbsoluteBounds(const Vector2& offset)
  148. {
  149. Label::updateAbsoluteBounds(offset);
  150. _textBounds.x += _bounds.height + 5;
  151. }
  152. unsigned int RadioButton::drawImages(Form* form, const Rectangle& clip)
  153. {
  154. if (!_image)
  155. return 0;
  156. // Left, v-center.
  157. // TODO: Set an alignment for radio button images.
  158. const Rectangle& region = _image->getRegion();
  159. const Theme::UVs& uvs = _image->getUVs();
  160. Vector4 color = _image->getColor();
  161. color.w *= _opacity;
  162. Vector2 pos(_viewportBounds.x, _viewportBounds.y);
  163. SpriteBatch* batch = _style->getTheme()->getSpriteBatch();
  164. startBatch(form, batch);
  165. batch->draw(pos.x, pos.y, _viewportBounds.height, _viewportBounds.height, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  166. finishBatch(form, batch);
  167. return 1;
  168. }
  169. void RadioButton::setGroupId(const char* groupId)
  170. {
  171. _groupId = groupId;
  172. }
  173. const char* RadioButton::getGroupId() const
  174. {
  175. return _groupId.c_str();
  176. }
  177. }