CheckBox.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "Base.h"
  2. #include "CheckBox.h"
  3. #include "Game.h"
  4. namespace gameplay
  5. {
  6. CheckBox::CheckBox() : _checked(false)
  7. {
  8. }
  9. CheckBox::CheckBox(const CheckBox& copy)
  10. {
  11. // Hidden.
  12. }
  13. CheckBox::~CheckBox()
  14. {
  15. }
  16. CheckBox* CheckBox::create(Theme::Style* style, Properties* properties)
  17. {
  18. CheckBox* checkBox = new CheckBox();
  19. checkBox->initialize(style, properties);
  20. properties->getVector2("imageSize", &checkBox->_imageSize);
  21. checkBox->_checked = properties->getBool("checked");
  22. return checkBox;
  23. }
  24. bool CheckBox::isChecked()
  25. {
  26. return _checked;
  27. }
  28. void CheckBox::setChecked(bool checked)
  29. {
  30. if (_checked != checked)
  31. {
  32. _checked = checked;
  33. notifyListeners(Control::Listener::VALUE_CHANGED);
  34. }
  35. }
  36. void CheckBox::setImageSize(float width, float height)
  37. {
  38. _imageSize.set(width, height);
  39. }
  40. const Vector2& CheckBox::getImageSize() const
  41. {
  42. return _imageSize;
  43. }
  44. void CheckBox::addListener(Control::Listener* listener, int eventFlags)
  45. {
  46. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  47. {
  48. assert("TEXT_CHANGED event is not applicable to CheckBox.");
  49. eventFlags &= ~Control::Listener::TEXT_CHANGED;
  50. }
  51. Control::addListener(listener, eventFlags);
  52. }
  53. bool CheckBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  54. {
  55. if (!isEnabled())
  56. {
  57. return false;
  58. }
  59. switch (evt)
  60. {
  61. case Touch::TOUCH_RELEASE:
  62. {
  63. if (_state == Control::ACTIVE)
  64. {
  65. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  66. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  67. {
  68. _checked = !_checked;
  69. notifyListeners(Control::Listener::VALUE_CHANGED);
  70. }
  71. }
  72. }
  73. break;
  74. }
  75. return Button::touchEvent(evt, x, y, contactIndex);
  76. }
  77. void CheckBox::update(const Rectangle& clip, const Vector2& offset)
  78. {
  79. Label::update(clip, offset);
  80. Vector2 size;
  81. if (_imageSize.isZero())
  82. {
  83. if (_checked)
  84. {
  85. const Rectangle& selectedRegion = getImageRegion("checked", _state);
  86. size.set(selectedRegion.width, selectedRegion.height);
  87. }
  88. else
  89. {
  90. const Rectangle& unselectedRegion = getImageRegion("unchecked", _state);
  91. size.set(unselectedRegion.width, unselectedRegion.height);
  92. }
  93. }
  94. else
  95. {
  96. size.set(_imageSize);
  97. }
  98. float iconWidth = size.x;
  99. _textBounds.x += iconWidth + 5;
  100. _textBounds.width -= iconWidth + 5;
  101. if (_checked)
  102. {
  103. _image = getImage("checked", _state);
  104. }
  105. else
  106. {
  107. _image = getImage("unchecked", _state);
  108. }
  109. }
  110. void CheckBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  111. {
  112. // Left, v-center.
  113. // TODO: Set an alignment for icons.
  114. const Rectangle& region = _image->getRegion();
  115. const Theme::UVs& uvs = _image->getUVs();
  116. Vector4 color = _image->getColor();
  117. color.w *= _opacity;
  118. Vector2 size;
  119. if (_imageSize.isZero())
  120. {
  121. size.set(region.width, region.height);
  122. }
  123. else
  124. {
  125. size.set(_imageSize);
  126. }
  127. Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
  128. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _clip);
  129. }
  130. }