CheckBox.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  47. const Vector2& CheckBox::getImageSize() const
  48. {
  49. return _imageSize;
  50. }
  51. void CheckBox::addListener(Control::Listener* listener, int eventFlags)
  52. {
  53. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  54. {
  55. GP_ERROR("TEXT_CHANGED event is not applicable to CheckBox.");
  56. eventFlags &= ~Control::Listener::TEXT_CHANGED;
  57. }
  58. Control::addListener(listener, eventFlags);
  59. }
  60. bool CheckBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  61. {
  62. if (!isEnabled())
  63. {
  64. return false;
  65. }
  66. switch (evt)
  67. {
  68. case Touch::TOUCH_RELEASE:
  69. if (_contactIndex == (int) contactIndex && _state == Control::ACTIVE)
  70. {
  71. if (!_parent->isScrolling() &&
  72. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  73. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  74. {
  75. _checked = !_checked;
  76. notifyListeners(Control::Listener::VALUE_CHANGED);
  77. }
  78. }
  79. break;
  80. }
  81. return Button::touchEvent(evt, x, y, contactIndex);
  82. }
  83. void CheckBox::update(const Control* container, const Vector2& offset)
  84. {
  85. Label::update(container, offset);
  86. Vector2 size;
  87. if (_imageSize.isZero())
  88. {
  89. if (_checked)
  90. {
  91. const Rectangle& selectedRegion = getImageRegion("checked", _state);
  92. size.set(selectedRegion.width, selectedRegion.height);
  93. }
  94. else
  95. {
  96. const Rectangle& unselectedRegion = getImageRegion("unchecked", _state);
  97. size.set(unselectedRegion.width, unselectedRegion.height);
  98. }
  99. }
  100. else
  101. {
  102. size.set(_imageSize);
  103. }
  104. float iconWidth = size.x;
  105. _textBounds.x += iconWidth + 5;
  106. _textBounds.width -= iconWidth + 5;
  107. if (_checked)
  108. {
  109. _image = getImage("checked", _state);
  110. }
  111. else
  112. {
  113. _image = getImage("unchecked", _state);
  114. }
  115. }
  116. void CheckBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  117. {
  118. GP_ASSERT(spriteBatch);
  119. GP_ASSERT(_image);
  120. // Left, v-center.
  121. // TODO: Set an alignment for icons.
  122. const Rectangle& region = _image->getRegion();
  123. const Theme::UVs& uvs = _image->getUVs();
  124. Vector4 color = _image->getColor();
  125. color.w *= _opacity;
  126. Vector2 size;
  127. if (_imageSize.isZero())
  128. {
  129. size.set(region.width, region.height);
  130. }
  131. else
  132. {
  133. size.set(_imageSize);
  134. }
  135. Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
  136. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  137. }
  138. const char* CheckBox::getType() const
  139. {
  140. return "checkBox";
  141. }
  142. }