Label.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "Base.h"
  2. #include "Label.h"
  3. namespace gameplay
  4. {
  5. Label::Label() : _text("")
  6. {
  7. }
  8. Label::Label(const Label& copy)
  9. {
  10. }
  11. Label::~Label()
  12. {
  13. }
  14. Label* Label::create(Theme::Style* style, Properties* properties)
  15. {
  16. Label* label = new Label();
  17. label->initialize(style, properties);
  18. return label;
  19. }
  20. void Label::initialize(Theme::Style* style, Properties* properties)
  21. {
  22. Control::initialize(style, properties);
  23. const char* text = properties->getString("text");
  24. if (text)
  25. {
  26. _text = text;
  27. }
  28. }
  29. void Label::addListener(Control::Listener* listener, int eventFlags)
  30. {
  31. if ((eventFlags & Listener::TEXT_CHANGED) == Listener::TEXT_CHANGED)
  32. {
  33. assert("TEXT_CHANGED event is not applicable to this control.");
  34. eventFlags &= ~Listener::TEXT_CHANGED;
  35. }
  36. if ((eventFlags & Listener::VALUE_CHANGED) == Listener::VALUE_CHANGED)
  37. {
  38. assert("VALUE_CHANGED event is not applicable to this control.");
  39. eventFlags &= ~Listener::VALUE_CHANGED;
  40. }
  41. Control::addListener(listener, eventFlags);
  42. }
  43. void Label::setText(const char* text)
  44. {
  45. if (text)
  46. {
  47. _text = text;
  48. }
  49. }
  50. const char* Label::getText()
  51. {
  52. return _text.c_str();
  53. }
  54. void Label::drawText(const Rectangle& clip)
  55. {
  56. if (_text.size() <= 0)
  57. return;
  58. Font* font = getFont(_state);
  59. Vector4 textColor = getTextColor(_state);
  60. textColor.w *= getOpacity(_state);
  61. // Draw the text.
  62. font->drawText(_text.c_str(), _textBounds, textColor, getFontSize(_state), getTextAlignment(_state), true, getTextRightToLeft(_state), &_clip);
  63. _dirty = false;
  64. }
  65. }