Label.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "Base.h"
  2. #include "Label.h"
  3. namespace gameplay
  4. {
  5. Label::Label() : _text(""), _font(NULL)
  6. {
  7. }
  8. Label::~Label()
  9. {
  10. }
  11. Label* Label::create(const char* id, Theme::Style* style)
  12. {
  13. GP_ASSERT(style);
  14. Label* label = new Label();
  15. if (id)
  16. label->_id = id;
  17. label->setStyle(style);
  18. return label;
  19. }
  20. Label* Label::create(Theme::Style* style, Properties* properties)
  21. {
  22. Label* label = new Label();
  23. label->initialize(style, properties);
  24. label->_consumeInputEvents = false;
  25. // Ensure that labels cannot receive focus.
  26. label->_focusIndex = -2;
  27. return label;
  28. }
  29. void Label::initialize(Theme::Style* style, Properties* properties)
  30. {
  31. GP_ASSERT(properties);
  32. Control::initialize(style, properties);
  33. const char* text = properties->getString("text");
  34. if (text)
  35. {
  36. _text = text;
  37. }
  38. }
  39. void Label::addListener(Control::Listener* listener, int eventFlags)
  40. {
  41. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  42. {
  43. GP_ERROR("TEXT_CHANGED event is not applicable to this control.");
  44. }
  45. if ((eventFlags & Listener::VALUE_CHANGED) == Listener::VALUE_CHANGED)
  46. {
  47. GP_ERROR("VALUE_CHANGED event is not applicable to this control.");
  48. }
  49. Control::addListener(listener, eventFlags);
  50. }
  51. void Label::setText(const char* text)
  52. {
  53. assert(text);
  54. if (strcmp(text, _text.c_str()) != 0)
  55. {
  56. _text = text;
  57. _dirty = true;
  58. }
  59. }
  60. const char* Label::getText()
  61. {
  62. return _text.c_str();
  63. }
  64. void Label::update(const Control* container, const Vector2& offset)
  65. {
  66. Control::update(container, offset);
  67. _textBounds.set(_viewportBounds);
  68. _font = getFont(_state);
  69. _textColor = getTextColor(_state);
  70. _textColor.w *= getOpacity(_state);
  71. }
  72. void Label::drawText(const Rectangle& clip)
  73. {
  74. if (_text.size() <= 0)
  75. return;
  76. // Draw the text.
  77. if (_font)
  78. {
  79. _font->start();
  80. _font->drawText(_text.c_str(), _textBounds, _textColor, getFontSize(_state), getTextAlignment(_state), true, getTextRightToLeft(_state), &_viewportClipBounds);
  81. _font->finish();
  82. }
  83. _dirty = false;
  84. }
  85. const char* Label::getType() const
  86. {
  87. return "label";
  88. }
  89. }