BsGUILabel.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "BsGUILabel.h"
  2. #include "BsGUIElementStyle.h"
  3. #include "BsTextSprite.h"
  4. #include "BsGUISkin.h"
  5. #include "BsGUIWidget.h"
  6. #include "BsGUIDimensions.h"
  7. #include "BsGUIHelper.h"
  8. #include "BsTextData.h"
  9. namespace BansheeEngine
  10. {
  11. GUILabel::GUILabel(const String& styleName, const GUIContent& content, const GUIDimensions& dimensions)
  12. :GUIElement(styleName, dimensions), mContent(content)
  13. {
  14. mTextSprite = bs_new<TextSprite, PoolAlloc>();
  15. mLocStringUpdatedConn = mContent.getText().addOnStringModifiedCallback(std::bind(&GUILabel::_markContentAsDirty, this));
  16. }
  17. GUILabel::~GUILabel()
  18. {
  19. mLocStringUpdatedConn.disconnect();
  20. bs_delete<PoolAlloc>(mTextSprite);
  21. }
  22. UINT32 GUILabel::_getNumRenderElements() const
  23. {
  24. return mTextSprite->getNumRenderElements();
  25. }
  26. const GUIMaterialInfo& GUILabel::_getMaterial(UINT32 renderElementIdx) const
  27. {
  28. return mTextSprite->getMaterial(renderElementIdx);
  29. }
  30. UINT32 GUILabel::_getNumQuads(UINT32 renderElementIdx) const
  31. {
  32. return mTextSprite->getNumQuads(renderElementIdx);
  33. }
  34. void GUILabel::updateRenderElementsInternal()
  35. {
  36. mDesc.font = _getStyle()->font;
  37. mDesc.fontSize = _getStyle()->fontSize;
  38. mDesc.wordWrap = _getStyle()->wordWrap;
  39. mDesc.horzAlign = _getStyle()->textHorzAlign;
  40. mDesc.vertAlign = _getStyle()->textVertAlign;
  41. mDesc.width = mLayoutData.area.width;
  42. mDesc.height = mLayoutData.area.height;
  43. mDesc.text = mContent.getText();
  44. mDesc.color = mColor * _getStyle()->normal.textColor;;
  45. mTextSprite->update(mDesc, (UINT64)_getParentWidget());
  46. GUIElement::updateRenderElementsInternal();
  47. }
  48. void GUILabel::updateClippedBounds()
  49. {
  50. mClippedBounds = mLayoutData.area;
  51. mClippedBounds.clip(mLayoutData.clipRect);
  52. }
  53. Vector2I GUILabel::_getOptimalSize() const
  54. {
  55. return GUIHelper::calcOptimalContentsSize(mContent, *_getStyle(), _getDimensions());
  56. }
  57. void GUILabel::_fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  58. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  59. {
  60. Vector2I offset(mLayoutData.area.x, mLayoutData.area.y);
  61. mTextSprite->fillBuffer(vertices, uv, indices, startingQuad, maxNumQuads, vertexStride,
  62. indexStride, renderElementIdx, offset, mLayoutData.clipRect);
  63. }
  64. void GUILabel::setContent(const GUIContent& content)
  65. {
  66. mLocStringUpdatedConn.disconnect();
  67. mLocStringUpdatedConn = content.getText().addOnStringModifiedCallback(std::bind(&GUILabel::_markContentAsDirty, this));
  68. mContent = content;
  69. _markContentAsDirty();
  70. }
  71. void GUILabel::setTint(const Color& color)
  72. {
  73. mColor = color;
  74. _markContentAsDirty();
  75. }
  76. GUILabel* GUILabel::create(const HString& text, const String& styleName)
  77. {
  78. return create(GUIContent(text), styleName);
  79. }
  80. GUILabel* GUILabel::create(const HString& text, const GUIOptions& options, const String& styleName)
  81. {
  82. return create(GUIContent(text), options, styleName);
  83. }
  84. GUILabel* GUILabel::create(const GUIContent& content, const String& styleName)
  85. {
  86. return new (bs_alloc<GUILabel, PoolAlloc>()) GUILabel(getStyleName<GUILabel>(styleName), content, GUIDimensions::create());
  87. }
  88. GUILabel* GUILabel::create(const GUIContent& content, const GUIOptions& options, const String& styleName)
  89. {
  90. return new (bs_alloc<GUILabel, PoolAlloc>()) GUILabel(getStyleName<GUILabel>(styleName), content, GUIDimensions::create(options));
  91. }
  92. const String& GUILabel::getGUITypeName()
  93. {
  94. static String typeName = "Label";
  95. return typeName;
  96. }
  97. }