Label.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. Label* label = new Label();
  14. label->_id = id ? id : "";
  15. label->initialize("Label", style, NULL);
  16. return label;
  17. }
  18. Control* Label::create(Theme::Style* style, Properties* properties)
  19. {
  20. Label* label = new Label();
  21. label->initialize("Label", style, properties);
  22. return label;
  23. }
  24. void Label::initialize(const char* typeName, Theme::Style* style, Properties* properties)
  25. {
  26. Control::initialize(typeName, style, properties);
  27. if (properties)
  28. {
  29. const char* text = properties->getString("text");
  30. if (text)
  31. {
  32. _text = text;
  33. }
  34. }
  35. }
  36. void Label::addListener(Control::Listener* listener, int eventFlags)
  37. {
  38. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  39. {
  40. GP_ERROR("TEXT_CHANGED event is not applicable to this control.");
  41. }
  42. if ((eventFlags & Control::Listener::VALUE_CHANGED) == Control::Listener::VALUE_CHANGED)
  43. {
  44. GP_ERROR("VALUE_CHANGED event is not applicable to this control.");
  45. }
  46. Control::addListener(listener, eventFlags);
  47. }
  48. void Label::setText(const char* text)
  49. {
  50. assert(text);
  51. if (strcmp(text, _text.c_str()) != 0)
  52. {
  53. _text = text;
  54. _dirty = true;
  55. }
  56. }
  57. const char* Label::getText()
  58. {
  59. return _text.c_str();
  60. }
  61. void Label::update(const Control* container, const Vector2& offset)
  62. {
  63. Control::update(container, offset);
  64. _textBounds.set((int)_viewportBounds.x, (int)_viewportBounds.y, _viewportBounds.width, _viewportBounds.height);
  65. Control::State state = getState();
  66. _font = getFont(state);
  67. _textColor = getTextColor(state);
  68. _textColor.w *= _opacity;
  69. Font* font = getFont(state);
  70. if ((_autoWidth == Control::AUTO_SIZE_FIT || _autoHeight == Control::AUTO_SIZE_FIT) && font)
  71. {
  72. unsigned int w, h;
  73. font->measureText(_text.c_str(), getFontSize(state), &w, &h);
  74. if (_autoWidth == Control::AUTO_SIZE_FIT)
  75. setWidth(w + getBorder(state).left + getBorder(state).right + getPadding().left + getPadding().right);
  76. if (_autoHeight == Control::AUTO_SIZE_FIT)
  77. setHeight(h + getBorder(state).top + getBorder(state).bottom + getPadding().top + getPadding().bottom);
  78. }
  79. }
  80. unsigned int Label::drawText(Form* form, const Rectangle& clip)
  81. {
  82. // Draw the text.
  83. if (_text.size() > 0 && _font)
  84. {
  85. Control::State state = getState();
  86. unsigned int fontSize = getFontSize(state);
  87. SpriteBatch* batch = _font->getSpriteBatch(fontSize);
  88. startBatch(form, batch);
  89. _font->drawText(_text.c_str(), _textBounds, _textColor, fontSize, getTextAlignment(state), true, getTextRightToLeft(state), &_viewportClipBounds);
  90. finishBatch(form, batch);
  91. return 1;
  92. }
  93. return 0;
  94. }
  95. const char* Label::getType() const
  96. {
  97. return "label";
  98. }
  99. }