Label.cpp 3.5 KB

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