CheckBox.cpp 3.7 KB

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