CheckBox.cpp 4.1 KB

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