CheckBox.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. Control::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. }
  109. void CheckBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  110. {
  111. // Left, v-center.
  112. // TODO: Set an alignment for icons.
  113. const Theme::Border border = getBorder(_state);
  114. const Theme::Padding padding = getPadding();
  115. float opacity = getOpacity(_state);
  116. if (_checked)
  117. {
  118. const Rectangle& selectedRegion = getImageRegion("checked", _state);
  119. const Theme::UVs& selected = getImageUVs("checked", _state);
  120. Vector4 selectedColor = getImageColor("checked", _state);
  121. selectedColor.w *= opacity;
  122. Vector2 size;
  123. if (_imageSize.isZero())
  124. {
  125. size.set(selectedRegion.width, selectedRegion.height);
  126. }
  127. else
  128. {
  129. size.set(_imageSize);
  130. }
  131. Vector2 pos(clip.x + _bounds.x + border.left + padding.left,
  132. clip.y + _bounds.y + (_clipBounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  133. spriteBatch->draw(pos.x, pos.y, size.x, size.y, selected.u1, selected.v1, selected.u2, selected.v2, selectedColor, _clip);
  134. }
  135. else
  136. {
  137. const Rectangle& unselectedRegion = getImageRegion("unchecked", _state);
  138. const Theme::UVs& unselected = getImageUVs("unchecked", _state);
  139. Vector4 unselectedColor = getImageColor("unchecked", _state);
  140. unselectedColor.w *= opacity;
  141. Vector2 size;
  142. if (_imageSize.isZero())
  143. {
  144. size.set(unselectedRegion.width, unselectedRegion.height);
  145. }
  146. else
  147. {
  148. size.set(_imageSize);
  149. }
  150. Vector2 pos(clip.x + _bounds.x + border.left + padding.left,
  151. clip.y + _bounds.y + (_clipBounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  152. spriteBatch->draw(pos.x, pos.y, size.x, size.y, unselected.u1, unselected.v1, unselected.u2, unselected.v2, unselectedColor, _clip);
  153. }
  154. }
  155. }