Label.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. Label* Label::getLabel(const char* id)
  35. {
  36. std::vector<Label*>::const_iterator it;
  37. for (it = __labels.begin(); it < __labels.end(); it++)
  38. {
  39. Label* l = *it;
  40. if (strcmp(id, l->getID()) == 0)
  41. {
  42. return l;
  43. }
  44. }
  45. return NULL;
  46. }
  47. void Label::setText(const char* text)
  48. {
  49. if (text)
  50. {
  51. _text = text;
  52. }
  53. }
  54. const char* Label::getText()
  55. {
  56. return _text.c_str();
  57. }
  58. void Label::update(const Vector2& position)
  59. {
  60. Vector2 pos(position.x + _position.x, position.y + _position.y);
  61. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  62. Theme::Border border;
  63. Theme::ContainerRegion* containerRegion = overlay->getContainerRegion();
  64. if (containerRegion)
  65. {
  66. border = overlay->getContainerRegion()->getBorder();
  67. }
  68. Theme::Padding padding = _style->getPadding();
  69. // Set up the text viewport.
  70. Font* font = overlay->getFont();
  71. _viewport.set(pos.x + border.left + padding.left,
  72. pos.y + border.top + padding.top,
  73. _size.x - border.left - padding.left - border.right - padding.right,
  74. _size.y - border.top - padding.top - border.bottom - padding.bottom - overlay->getFontSize());
  75. }
  76. void Label::drawText(const Vector2& position)
  77. {
  78. // TODO: Batch all labels that use the same font.
  79. Theme::Style::Overlay* overlay = _style->getOverlay(getOverlayType());
  80. Font* font = overlay->getFont();
  81. // Draw the text.
  82. font->begin();
  83. font->drawText(_text.c_str(), _viewport, overlay->getTextColor(), overlay->getFontSize(), overlay->getTextAlignment(), true, overlay->getTextRightToLeft());
  84. font->end();
  85. _dirty = false;
  86. }
  87. }