CheckBox.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 > 0 && x <= _clipBounds.width &&
  66. y > 0 && y <= _clipBounds.height)
  67. {
  68. _checked = !_checked;
  69. notifyListeners(Control::Listener::VALUE_CHANGED);
  70. // Animate between icons. Old fades out, then the new fades in.
  71. /*
  72. AnimationController* animationController = Game::getInstance()->getAnimationController();
  73. float from[1] = { 1.0f };
  74. float to[1] = { 0.0f };
  75. animationController->createAnimationFromTo("CheckBox::toggle", this, CheckBox::ANIMATE_SPRITE_ALPHA, from, to, Curve::QUADRATIC_IN_OUT, 200L);
  76. */
  77. }
  78. }
  79. }
  80. break;
  81. }
  82. return Button::touchEvent(evt, x, y, contactIndex);
  83. }
  84. void CheckBox::update(const Rectangle& clip)
  85. {
  86. Label::update(clip);
  87. Vector2 size;
  88. if (_imageSize.isZero())
  89. {
  90. if (_checked)
  91. {
  92. const Rectangle& selectedRegion = getImageRegion("checked", _state);
  93. size.set(selectedRegion.width, selectedRegion.height);
  94. }
  95. else
  96. {
  97. const Rectangle& unselectedRegion = getImageRegion("unchecked", _state);
  98. size.set(unselectedRegion.width, unselectedRegion.height);
  99. }
  100. }
  101. else
  102. {
  103. size.set(_imageSize);
  104. }
  105. float iconWidth = size.x;
  106. _textBounds.x += iconWidth + 5;
  107. _textBounds.width -= iconWidth + 5;
  108. if (_checked)
  109. {
  110. _image = getImage("checked", _state);
  111. }
  112. else
  113. {
  114. _image = getImage("unchecked", _state);
  115. }
  116. }
  117. void CheckBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  118. {
  119. // Left, v-center.
  120. // TODO: Set an alignment for icons.
  121. const Theme::Border& border = getBorder(_state);
  122. const Theme::Padding padding = getPadding();
  123. const Rectangle& region = _image->getRegion();
  124. const Theme::UVs& uvs = _image->getUVs();
  125. Vector4 color = _image->getColor();
  126. color.w *= _opacity;
  127. Vector2 size;
  128. if (_imageSize.isZero())
  129. {
  130. size.set(region.width, region.height);
  131. }
  132. else
  133. {
  134. size.set(_imageSize);
  135. }
  136. Vector2 pos(clip.x + _bounds.x + border.left + padding.left,
  137. clip.y + _bounds.y + (_clipBounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  138. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _clip);
  139. }
  140. }