CheckBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  72. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  73. {
  74. _checked = !_checked;
  75. notifyListeners(Control::Listener::VALUE_CHANGED);
  76. }
  77. }
  78. break;
  79. }
  80. return Button::touchEvent(evt, x, y, contactIndex);
  81. }
  82. void CheckBox::update(const Control* container, const Vector2& offset)
  83. {
  84. Label::update(container, offset);
  85. Vector2 size;
  86. if (_imageSize.isZero())
  87. {
  88. if (_checked)
  89. {
  90. const Rectangle& selectedRegion = getImageRegion("checked", _state);
  91. size.set(selectedRegion.width, selectedRegion.height);
  92. }
  93. else
  94. {
  95. const Rectangle& unselectedRegion = getImageRegion("unchecked", _state);
  96. size.set(unselectedRegion.width, unselectedRegion.height);
  97. }
  98. }
  99. else
  100. {
  101. size.set(_imageSize);
  102. }
  103. float iconWidth = size.x;
  104. _textBounds.x += iconWidth + 5;
  105. _textBounds.width -= iconWidth + 5;
  106. if (_checked)
  107. {
  108. _image = getImage("checked", _state);
  109. }
  110. else
  111. {
  112. _image = getImage("unchecked", _state);
  113. }
  114. }
  115. void CheckBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  116. {
  117. GP_ASSERT(spriteBatch);
  118. GP_ASSERT(_image);
  119. // Left, v-center.
  120. // TODO: Set an alignment for icons.
  121. const Rectangle& region = _image->getRegion();
  122. const Theme::UVs& uvs = _image->getUVs();
  123. Vector4 color = _image->getColor();
  124. color.w *= _opacity;
  125. Vector2 size;
  126. if (_imageSize.isZero())
  127. {
  128. size.set(region.width, region.height);
  129. }
  130. else
  131. {
  132. size.set(_imageSize);
  133. }
  134. Vector2 pos(_viewportBounds.x, _viewportBounds.y + _viewportBounds.height * 0.5f - size.y * 0.5f);
  135. spriteBatch->draw(pos.x, pos.y, size.x, size.y, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  136. }
  137. const char* CheckBox::getType() const
  138. {
  139. return "checkBox";
  140. }
  141. }