BsGUILabel.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "BsGUILabel.h"
  2. #include "BsGUIElementStyle.h"
  3. #include "BsTextSprite.h"
  4. #include "BsGUISkin.h"
  5. #include "BsGUIWidget.h"
  6. #include "BsGUILayoutOptions.h"
  7. #include "CmTextUtility.h"
  8. using namespace CamelotFramework;
  9. namespace BansheeEngine
  10. {
  11. GUILabel::GUILabel(GUIWidget& parent, const GUIElementStyle* style, const WString& text, const GUILayoutOptions& layoutOptions)
  12. :GUIElement(parent, style, layoutOptions), mText(text)
  13. {
  14. mTextSprite = cm_new<TextSprite, PoolAlloc>();
  15. mDesc.text = text;
  16. mDesc.font = mStyle->font;
  17. mDesc.fontSize = mStyle->fontSize;
  18. mDesc.wordWrap = mStyle->wordWrap;
  19. mDesc.horzAlign = mStyle->textHorzAlign;
  20. mDesc.vertAlign = mStyle->textVertAlign;
  21. }
  22. GUILabel::~GUILabel()
  23. {
  24. cm_delete<PoolAlloc>(mTextSprite);
  25. }
  26. UINT32 GUILabel::getNumRenderElements() const
  27. {
  28. return mTextSprite->getNumRenderElements();
  29. }
  30. const HMaterial& GUILabel::getMaterial(UINT32 renderElementIdx) const
  31. {
  32. return mTextSprite->getMaterial(renderElementIdx);
  33. }
  34. UINT32 GUILabel::getNumQuads(UINT32 renderElementIdx) const
  35. {
  36. return mTextSprite->getNumQuads(renderElementIdx);
  37. }
  38. void GUILabel::updateRenderElementsInternal()
  39. {
  40. mDesc.width = mWidth;
  41. mDesc.height = mHeight;
  42. mTextSprite->update(mDesc);
  43. GUIElement::updateRenderElementsInternal();
  44. }
  45. void GUILabel::updateBounds()
  46. {
  47. mBounds = mTextSprite->getBounds(mOffset, mClipRect);
  48. }
  49. UINT32 GUILabel::_getOptimalWidth() const
  50. {
  51. UINT32 wordWrapWidth = 0;
  52. if(mDesc.wordWrap)
  53. {
  54. if(_getLayoutOptions().fixedWidth)
  55. wordWrapWidth = _getLayoutOptions().width;
  56. else
  57. wordWrapWidth = _getLayoutOptions().maxWidth;
  58. }
  59. std::shared_ptr<TextUtility::TextData> textData = TextUtility::getTextData(mDesc.text, mDesc.font, mDesc.fontSize, wordWrapWidth, 0, mDesc.wordWrap);
  60. if(textData == nullptr)
  61. return 0;
  62. return textData->getWidth();
  63. }
  64. UINT32 GUILabel::_getOptimalHeight() const
  65. {
  66. UINT32 wordWrapWidth = 0;
  67. if(mDesc.wordWrap)
  68. {
  69. if(_getLayoutOptions().fixedWidth)
  70. wordWrapWidth = _getLayoutOptions().width;
  71. else
  72. wordWrapWidth = _getLayoutOptions().maxWidth;
  73. }
  74. std::shared_ptr<TextUtility::TextData> textData = TextUtility::getTextData(mDesc.text, mDesc.font, mDesc.fontSize, wordWrapWidth, 0, mDesc.wordWrap);
  75. if(textData == nullptr)
  76. return 0;
  77. return textData->getHeight();
  78. }
  79. void GUILabel::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  80. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  81. {
  82. mTextSprite->fillBuffer(vertices, uv, indices, startingQuad, maxNumQuads, vertexStride, indexStride, renderElementIdx, mOffset, mClipRect);
  83. }
  84. void GUILabel::setText(const CM::WString& text)
  85. {
  86. mDesc.text = text;
  87. markContentAsDirty();
  88. }
  89. GUILabel* GUILabel::create(GUIWidget& parent, const WString& text, const GUIElementStyle* style)
  90. {
  91. if(style == nullptr)
  92. {
  93. const GUISkin* skin = parent.getSkin();
  94. style = skin->getStyle(getGUITypeName());
  95. }
  96. return new (cm_alloc<GUILabel, PoolAlloc>()) GUILabel(parent, style, text, getDefaultLayoutOptions(style));
  97. }
  98. GUILabel* GUILabel::create(GUIWidget& parent, const WString& text, const GUILayoutOptions& layoutOptions, const GUIElementStyle* style)
  99. {
  100. if(style == nullptr)
  101. {
  102. const GUISkin* skin = parent.getSkin();
  103. style = skin->getStyle(getGUITypeName());
  104. }
  105. return new (cm_alloc<GUILabel, PoolAlloc>()) GUILabel(parent, style, text, layoutOptions);
  106. }
  107. const String& GUILabel::getGUITypeName()
  108. {
  109. static String typeName = "Label";
  110. return typeName;
  111. }
  112. }