CheckBox.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "Base.h"
  2. #include "CheckBox.h"
  3. #include "Game.h"
  4. namespace gameplay
  5. {
  6. CheckBox::CheckBox() : _checked(false), _image(NULL)
  7. {
  8. }
  9. CheckBox::~CheckBox()
  10. {
  11. }
  12. CheckBox* CheckBox::create(const char* id, Theme::Style* style)
  13. {
  14. GP_ASSERT(style);
  15. CheckBox* checkBox = new CheckBox();
  16. if (id)
  17. checkBox->_id = id;
  18. checkBox->setStyle(style);
  19. return checkBox;
  20. }
  21. CheckBox* CheckBox::create(Theme::Style* style, Properties* properties)
  22. {
  23. GP_ASSERT(properties);
  24. CheckBox* checkBox = new CheckBox();
  25. checkBox->initialize(style, properties);
  26. properties->getVector2("imageSize", &checkBox->_imageSize);
  27. checkBox->_checked = properties->getBool("checked");
  28. return checkBox;
  29. }
  30. bool CheckBox::isChecked()
  31. {
  32. return _checked;
  33. }
  34. void CheckBox::setChecked(bool checked)
  35. {
  36. if (_checked != checked)
  37. {
  38. _checked = checked;
  39. _dirty = true;
  40. notifyListeners(Control::Listener::VALUE_CHANGED);
  41. }
  42. }
  43. void CheckBox::setImageSize(float width, float height)
  44. {
  45. _imageSize.set(width, height);
  46. _dirty = true;
  47. }
  48. const Vector2& CheckBox::getImageSize() const
  49. {
  50. return _imageSize;
  51. }
  52. void CheckBox::addListener(Control::Listener* listener, int eventFlags)
  53. {
  54. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  55. {
  56. GP_ERROR("TEXT_CHANGED event is not applicable to CheckBox.");
  57. eventFlags &= ~Control::Listener::TEXT_CHANGED;
  58. }
  59. Control::addListener(listener, eventFlags);
  60. }
  61. bool CheckBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  62. {
  63. switch (evt)
  64. {
  65. case Touch::TOUCH_RELEASE:
  66. if (_contactIndex == (int) contactIndex && _state == Control::ACTIVE)
  67. {
  68. if (!_parent->isScrolling() &&
  69. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  70. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  71. {
  72. setChecked( !_checked );
  73. }
  74. }
  75. break;
  76. }
  77. return Button::touchEvent(evt, x, y, contactIndex);
  78. }
  79. bool CheckBox::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  80. {
  81. switch (evt)
  82. {
  83. case Gamepad::BUTTON_EVENT:
  84. if (_state == Control::ACTIVE)
  85. {
  86. if (!gamepad->isButtonDown(Gamepad::BUTTON_A) &&
  87. !gamepad->isButtonDown(Gamepad::BUTTON_X))
  88. {
  89. setChecked( !_checked );
  90. }
  91. }
  92. break;
  93. }
  94. return Button::gamepadEvent(evt, gamepad, analogIndex);
  95. }
  96. bool CheckBox::keyEvent(Keyboard::KeyEvent evt, int key)
  97. {
  98. if (_state == ACTIVE && evt == Keyboard::KEY_RELEASE && key == Keyboard::KEY_RETURN)
  99. {
  100. setChecked( !_checked );
  101. }
  102. return Button::keyEvent(evt, key);
  103. }
  104. void CheckBox::update(const Control* container, const Vector2& offset)
  105. {
  106. Label::update(container, offset);
  107. Vector2 size;
  108. if (_imageSize.isZero())
  109. {
  110. if (_checked)
  111. {
  112. const Rectangle& selectedRegion = getImageRegion("checked", _state);
  113. size.set(selectedRegion.width, selectedRegion.height);
  114. }
  115. else
  116. {
  117. const Rectangle& unselectedRegion = getImageRegion("unchecked", _state);
  118. size.set(unselectedRegion.width, unselectedRegion.height);
  119. }
  120. }
  121. else
  122. {
  123. size.set(_imageSize);
  124. }
  125. if (_autoWidth == Control::AUTO_SIZE_FIT)
  126. {
  127. // Text-only width was already measured in Label::update - append image
  128. setWidth(size.x + _bounds.width + 5);
  129. }
  130. if (_autoHeight == Control::AUTO_SIZE_FIT)
  131. {
  132. // Text-only width was already measured in Label::update - append image
  133. setHeight(std::max(getHeight(), size.y));
  134. }
  135. _textBounds.x += size.x + 5;
  136. if (_checked)
  137. {
  138. _image = getImage("checked", _state);
  139. }
  140. else
  141. {
  142. _image = getImage("unchecked", _state);
  143. }
  144. }
  145. void CheckBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  146. {
  147. GP_ASSERT(spriteBatch);
  148. GP_ASSERT(_image);
  149. // Left, v-center.
  150. // TODO: Set an alignment for icons.
  151. const Rectangle& region = _image->getRegion();
  152. const Theme::UVs& uvs = _image->getUVs();
  153. Vector4 color = _image->getColor();
  154. color.w *= _opacity;
  155. Vector2 size;
  156. if (_imageSize.isZero())
  157. {
  158. size.set(region.width, region.height);
  159. }
  160. else
  161. {
  162. size.set(_imageSize);
  163. }
  164. Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
  165. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  166. }
  167. const char* CheckBox::getType() const
  168. {
  169. return "checkBox";
  170. }
  171. }