Label.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "Base.h"
  2. #include "Label.h"
  3. namespace gameplay
  4. {
  5. static std::vector<Label*> __labels;
  6. Label::Label() : _text("")
  7. {
  8. }
  9. Label::Label(const Label& copy)
  10. {
  11. }
  12. Label::~Label()
  13. {
  14. }
  15. Label* Label::create(Theme::Style* style, Properties* properties)
  16. {
  17. Label* label = new Label();
  18. label->_style = style;
  19. properties->getVector2("position", &label->_position);
  20. properties->getVector2("size", &label->_size);
  21. const char* id = properties->getId();
  22. if (id)
  23. {
  24. label->_id = id;
  25. }
  26. const char* text = properties->getString("text");
  27. if (text)
  28. {
  29. label->_text = text;
  30. }
  31. __labels.push_back(label);
  32. return label;
  33. }
  34. void Label::init(Theme::Style* style, Properties* properties)
  35. {
  36. Control::init(style, properties);
  37. const char* text = properties->getString("text");
  38. if (text)
  39. {
  40. _text = text;
  41. }
  42. }
  43. Label* Label::getLabel(const char* id)
  44. {
  45. std::vector<Label*>::const_iterator it;
  46. for (it = __labels.begin(); it < __labels.end(); it++)
  47. {
  48. Label* l = *it;
  49. if (strcmp(id, l->getID()) == 0)
  50. {
  51. return l;
  52. }
  53. }
  54. return NULL;
  55. }
  56. void Label::setText(const char* text)
  57. {
  58. if (text)
  59. {
  60. _text = text;
  61. }
  62. }
  63. const char* Label::getText()
  64. {
  65. return _text.c_str();
  66. }
  67. void Label::update(const Vector2& position)
  68. {
  69. Vector2 pos(position.x + _position.x, position.y + _position.y);
  70. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  71. Theme::Border border;
  72. Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  73. if (containerRegion)
  74. {
  75. border = overlay->getContainerRegion()->getBorder();
  76. }
  77. Theme::Padding padding = _style->getPadding();
  78. // Set up the text viewport.
  79. Font* font = overlay->getFont();
  80. _viewport.set(pos.x + border.left + padding.left,
  81. pos.y + border.top + padding.top,
  82. _size.x - border.left - padding.left - border.right - padding.right,
  83. _size.y - border.top - padding.top - border.bottom - padding.bottom - overlay->getFontSize());
  84. }
  85. void Label::drawText(const Vector2& position)
  86. {
  87. if (_text.size() <= 0)
  88. return;
  89. // TODO: Batch all labels that use the same font.
  90. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  91. Font* font = overlay->getFont();
  92. // Draw the text.
  93. font->begin();
  94. font->drawText(_text.c_str(), _viewport, overlay->getTextColor(), overlay->getFontSize(), overlay->getTextAlignment(), true, overlay->getTextRightToLeft());
  95. font->end();
  96. _dirty = false;
  97. }
  98. }