CheckBox.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "Base.h"
  2. #include "CheckBox.h"
  3. namespace gameplay
  4. {
  5. static std::vector<CheckBox*> __checkBoxes;
  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->init(style, properties);
  20. properties->getVector2("iconSize", &checkBox->_iconSize);
  21. checkBox->_checked = properties->getBool("checked");
  22. __checkBoxes.push_back(checkBox);
  23. return checkBox;
  24. }
  25. CheckBox* CheckBox::getCheckBox(const char* id)
  26. {
  27. std::vector<CheckBox*>::const_iterator it;
  28. for (it = __checkBoxes.begin(); it < __checkBoxes.end(); it++)
  29. {
  30. CheckBox* checkBox = *it;
  31. if (strcmp(id, checkBox->getID()) == 0)
  32. {
  33. return checkBox;
  34. }
  35. }
  36. return NULL;
  37. }
  38. bool CheckBox::isChecked()
  39. {
  40. return _checked;
  41. }
  42. bool CheckBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  43. {
  44. if (!isEnabled())
  45. {
  46. return false;
  47. }
  48. switch (evt)
  49. {
  50. case Touch::TOUCH_RELEASE:
  51. {
  52. if (_state == Control::STATE_ACTIVE)
  53. {
  54. if (x > 0 && x <= _size.x &&
  55. y > 0 && y <= _size.y)
  56. {
  57. _checked = !_checked;
  58. }
  59. }
  60. }
  61. break;
  62. }
  63. return Button::touchEvent(evt, x, y, contactIndex);
  64. }
  65. void CheckBox::update(const Vector2& position)
  66. {
  67. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  68. Theme::Icon* icon = overlay->getCheckBoxIcon();
  69. Theme::Border border;
  70. Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  71. if (containerRegion)
  72. {
  73. border = overlay->getContainerRegion()->getBorder();
  74. }
  75. Theme::Padding padding = _style->getPadding();
  76. // Set up the text viewport.
  77. float iconWidth = 0.0f;
  78. if (icon)
  79. {
  80. iconWidth = icon->getSize().x;
  81. }
  82. Font* font = overlay->getFont();
  83. Vector2 pos(position.x + _position.x + border.left + padding.left + iconWidth,
  84. position.y + _position.y + border.top + padding.top);
  85. _viewport.set(pos.x, pos.y,
  86. _size.x - border.left - padding.left - border.right - padding.right - iconWidth,
  87. _size.y - border.top - padding.top - border.bottom - padding.bottom - overlay->getFontSize());
  88. }
  89. void CheckBox::drawSprites(SpriteBatch* spriteBatch, const Vector2& position)
  90. {
  91. // Left, v-center.
  92. // TODO: Set an alignment for icons.
  93. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  94. Theme::Icon* icon = overlay->getCheckBoxIcon();
  95. if (icon)
  96. {
  97. const Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  98. Theme::Border border;
  99. if (containerRegion)
  100. {
  101. border = containerRegion->getBorder();
  102. }
  103. const Theme::Padding padding = _style->getPadding();
  104. Vector2& size = _iconSize;
  105. if (_iconSize.isZero())
  106. {
  107. size = icon->getSize();
  108. }
  109. const Vector4 color = icon->getColor();
  110. Vector2 pos(position.x + _position.x + border.left + padding.left,
  111. position.y + _position.y + (_size.y - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  112. if (_checked)
  113. {
  114. const Theme::UVs on = icon->getOnUVs();
  115. spriteBatch->draw(pos.x, pos.y, size.x, size.y, on.u1, on.v1, on.u2, on.v2, color);
  116. }
  117. else
  118. {
  119. const Theme::UVs off = icon->getOffUVs();
  120. spriteBatch->draw(pos.x, pos.y, size.x, size.y, off.u1, off.v1, off.u2, off.v2, color);
  121. }
  122. }
  123. }
  124. void CheckBox::drawText(const Vector2& position)
  125. {
  126. // TODO: Batch all labels that use the same font.
  127. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  128. Font* font = overlay->getFont();
  129. // Draw the text.
  130. font->begin();
  131. font->drawText(_text.c_str(), _viewport, overlay->getTextColor(), overlay->getFontSize(), overlay->getTextAlignment(), true, overlay->getTextRightToLeft());
  132. font->end();
  133. _dirty = false;
  134. }
  135. }