CheckBox.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "Base.h"
  2. #include "CheckBox.h"
  3. namespace gameplay
  4. {
  5. CheckBox::CheckBox() : _checked(false)
  6. {
  7. }
  8. CheckBox::CheckBox(const CheckBox& copy)
  9. {
  10. // Hidden.
  11. }
  12. CheckBox::~CheckBox()
  13. {
  14. }
  15. CheckBox* CheckBox::create(Theme::Style* style, Properties* properties)
  16. {
  17. CheckBox* checkBox = new CheckBox();
  18. checkBox->init(style, properties);
  19. properties->getVector2("iconSize", &checkBox->_iconSize);
  20. checkBox->_checked = properties->getBool("checked");
  21. return checkBox;
  22. }
  23. bool CheckBox::isChecked()
  24. {
  25. return _checked;
  26. }
  27. void CheckBox::setIconSize(float width, float height)
  28. {
  29. _iconSize.set(width, height);
  30. }
  31. const Vector2& CheckBox::getIconSize() const
  32. {
  33. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  34. Theme::Icon* icon = overlay->getCheckBoxIcon();
  35. if (_iconSize.isZero() && icon)
  36. {
  37. return icon->getSize();
  38. }
  39. return _iconSize;
  40. }
  41. bool CheckBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  42. {
  43. if (!isEnabled())
  44. {
  45. return false;
  46. }
  47. switch (evt)
  48. {
  49. case Touch::TOUCH_RELEASE:
  50. {
  51. if (_state == Control::ACTIVE)
  52. {
  53. if (x > 0 && x <= _bounds.width &&
  54. y > 0 && y <= _bounds.height)
  55. {
  56. _checked = !_checked;
  57. }
  58. }
  59. }
  60. break;
  61. }
  62. return Button::touchEvent(evt, x, y, contactIndex);
  63. }
  64. void CheckBox::update(const Rectangle& clip)
  65. {
  66. Control::update(clip);
  67. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  68. Theme::Icon* icon = overlay->getCheckBoxIcon();
  69. Vector2& size = _iconSize;
  70. if (_iconSize.isZero() && icon)
  71. {
  72. size = icon->getSize();
  73. }
  74. float iconWidth = size.x;
  75. _clip.x += iconWidth;
  76. _clip.width -= iconWidth;
  77. }
  78. void CheckBox::drawSprites(SpriteBatch* spriteBatch, const Rectangle& clip)
  79. {
  80. // Left, v-center.
  81. // TODO: Set an alignment for icons.
  82. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  83. Theme::Icon* icon = overlay->getCheckBoxIcon();
  84. if (icon)
  85. {
  86. const Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  87. Theme::Border border;
  88. if (containerRegion)
  89. {
  90. border = containerRegion->getBorder();
  91. }
  92. const Theme::Padding padding = _style->getPadding();
  93. Vector2& size = _iconSize;
  94. if (_iconSize.isZero())
  95. {
  96. size = icon->getSize();
  97. }
  98. const Vector4 color = icon->getColor();
  99. Vector2 pos(clip.x + _position.x + border.left + padding.left,
  100. clip.y + _position.y + (_bounds.height - border.bottom - padding.bottom) / 2.0f - size.y / 2.0f);
  101. if (_checked)
  102. {
  103. const Theme::UVs on = icon->getOnUVs();
  104. spriteBatch->draw(pos.x, pos.y, size.x, size.y, on.u1, on.v1, on.u2, on.v2, color);
  105. }
  106. else
  107. {
  108. const Theme::UVs off = icon->getOffUVs();
  109. spriteBatch->draw(pos.x, pos.y, size.x, size.y, off.u1, off.v1, off.u2, off.v2, color);
  110. }
  111. }
  112. }
  113. void CheckBox::drawText(const Rectangle& clip)
  114. {
  115. // TODO: Batch all labels that use the same font.
  116. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  117. Font* font = overlay->getFont();
  118. // Draw the text.
  119. font->begin();
  120. font->drawText(_text.c_str(), _clip, overlay->getTextColor(), overlay->getFontSize(), overlay->getTextAlignment(), true, overlay->getTextRightToLeft());
  121. font->end();
  122. _dirty = false;
  123. }
  124. }