Label.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // Labels don't consume input events by default like other controls.
  19. label->_consumeInputEvents = false;
  20. // Ensure that labels cannot receive focus.
  21. label->_focusIndex = -2;
  22. return label;
  23. }
  24. Label* Label::create(Theme::Style* style, Properties* properties)
  25. {
  26. Label* label = new Label();
  27. label->initialize(style, properties);
  28. label->_consumeInputEvents = false;
  29. label->_focusIndex = -2;
  30. return label;
  31. }
  32. void Label::initialize(Theme::Style* style, Properties* properties)
  33. {
  34. GP_ASSERT(properties);
  35. Control::initialize(style, properties);
  36. const char* text = properties->getString("text");
  37. if (text)
  38. {
  39. _text = text;
  40. }
  41. }
  42. void Label::addListener(Control::Listener* listener, int eventFlags)
  43. {
  44. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  45. {
  46. GP_ERROR("TEXT_CHANGED event is not applicable to this control.");
  47. }
  48. if ((eventFlags & Control::Listener::VALUE_CHANGED) == Control::Listener::VALUE_CHANGED)
  49. {
  50. GP_ERROR("VALUE_CHANGED event is not applicable to this control.");
  51. }
  52. Control::addListener(listener, eventFlags);
  53. }
  54. void Label::setText(const char* text)
  55. {
  56. assert(text);
  57. if (strcmp(text, _text.c_str()) != 0)
  58. {
  59. _text = text;
  60. _dirty = true;
  61. }
  62. }
  63. const char* Label::getText()
  64. {
  65. return _text.c_str();
  66. }
  67. void Label::update(const Control* container, const Vector2& offset)
  68. {
  69. Control::update(container, offset);
  70. _textBounds.set(_viewportBounds);
  71. _font = getFont(_state);
  72. _textColor = getTextColor(_state);
  73. _textColor.w *= _opacity;
  74. }
  75. void Label::drawText(const Rectangle& clip)
  76. {
  77. if (_text.size() <= 0)
  78. return;
  79. // Draw the text.
  80. if (_font)
  81. {
  82. _font->start();
  83. _font->drawText(_text.c_str(), _textBounds, _textColor, getFontSize(_state), getTextAlignment(_state), true, getTextRightToLeft(_state), &_viewportClipBounds);
  84. _font->finish();
  85. }
  86. }
  87. const char* Label::getType() const
  88. {
  89. return "label";
  90. }
  91. }