Label.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. const char* Label::getTypeName() const
  37. {
  38. return "Label";
  39. }
  40. void Label::addListener(Control::Listener* listener, int eventFlags)
  41. {
  42. if ((eventFlags & Control::Listener::TEXT_CHANGED) == Control::Listener::TEXT_CHANGED)
  43. {
  44. GP_ERROR("TEXT_CHANGED event is not applicable to this control.");
  45. }
  46. if ((eventFlags & Control::Listener::VALUE_CHANGED) == Control::Listener::VALUE_CHANGED)
  47. {
  48. GP_ERROR("VALUE_CHANGED event is not applicable to this control.");
  49. }
  50. Control::addListener(listener, eventFlags);
  51. }
  52. void Label::setText(const char* text)
  53. {
  54. if ((text == NULL && _text.length() > 0) || strcmp(text, _text.c_str()) != 0)
  55. {
  56. _text = text ? text : "";
  57. if (_autoSize != AUTO_SIZE_NONE)
  58. setDirty(DIRTY_BOUNDS);
  59. }
  60. }
  61. const char* Label::getText()
  62. {
  63. return _text.c_str();
  64. }
  65. void Label::update(float elapsedTime)
  66. {
  67. Control::update(elapsedTime);
  68. // Update text opacity each frame since opacity is updated in Control::update.
  69. _textColor = getTextColor(getState());
  70. _textColor.w *= _opacity;
  71. }
  72. void Label::updateState(State state)
  73. {
  74. Control::updateState(state);
  75. _font = getFont(state);
  76. }
  77. void Label::updateBounds()
  78. {
  79. Control::updateBounds();
  80. if (_autoSize != AUTO_SIZE_NONE && _font)
  81. {
  82. // Measure bounds based only on normal state so that bounds updates are not always required on state changes.
  83. // This is a trade-off for functionality vs performance, but changing the size of UI controls on hover/focus/etc
  84. // is a pretty bad practice so we'll prioritize performance here.
  85. unsigned int w, h;
  86. _font->measureText(_text.c_str(), getFontSize(NORMAL), &w, &h);
  87. if (_autoSize & AUTO_SIZE_WIDTH)
  88. {
  89. setWidthInternal(w + getBorder(NORMAL).left + getBorder(NORMAL).right + getPadding().left + getPadding().right);
  90. }
  91. if (_autoSize & AUTO_SIZE_HEIGHT)
  92. {
  93. setHeightInternal(h + getBorder(NORMAL).top + getBorder(NORMAL).bottom + getPadding().top + getPadding().bottom);
  94. }
  95. }
  96. }
  97. void Label::updateAbsoluteBounds(const Vector2& offset)
  98. {
  99. Control::updateAbsoluteBounds(offset);
  100. _textBounds.set((int)_viewportBounds.x, (int)_viewportBounds.y, _viewportBounds.width, _viewportBounds.height);
  101. }
  102. unsigned int Label::drawText(Form* form, const Rectangle& clip)
  103. {
  104. // Draw the text.
  105. if (_text.size() > 0 && _font)
  106. {
  107. Control::State state = getState();
  108. unsigned int fontSize = getFontSize(state);
  109. SpriteBatch* batch = _font->getSpriteBatch(fontSize);
  110. startBatch(form, batch);
  111. _font->drawText(_text.c_str(), _textBounds, _textColor, fontSize, getTextAlignment(state), true, getTextRightToLeft(state), &_viewportClipBounds);
  112. finishBatch(form, batch);
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. }