BsGUILabel.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "BsGUIHelper.h"
  8. #include "CmTextUtility.h"
  9. using namespace CamelotFramework;
  10. namespace BansheeEngine
  11. {
  12. GUILabel::GUILabel(GUIWidget& parent, const GUIElementStyle* style, const GUIContent& content, const GUILayoutOptions& layoutOptions)
  13. :GUIElement(parent, style, layoutOptions), mContent(content)
  14. {
  15. mTextSprite = cm_new<TextSprite, PoolAlloc>();
  16. mDesc.text = content.getText();
  17. mDesc.font = mStyle->font;
  18. mDesc.fontSize = mStyle->fontSize;
  19. mDesc.wordWrap = mStyle->wordWrap;
  20. mDesc.horzAlign = mStyle->textHorzAlign;
  21. mDesc.vertAlign = mStyle->textVertAlign;
  22. }
  23. GUILabel::~GUILabel()
  24. {
  25. cm_delete<PoolAlloc>(mTextSprite);
  26. }
  27. UINT32 GUILabel::getNumRenderElements() const
  28. {
  29. return mTextSprite->getNumRenderElements();
  30. }
  31. const HMaterial& GUILabel::getMaterial(UINT32 renderElementIdx) const
  32. {
  33. return mTextSprite->getMaterial(renderElementIdx);
  34. }
  35. UINT32 GUILabel::getNumQuads(UINT32 renderElementIdx) const
  36. {
  37. return mTextSprite->getNumQuads(renderElementIdx);
  38. }
  39. void GUILabel::updateRenderElementsInternal()
  40. {
  41. mDesc.width = mWidth;
  42. mDesc.height = mHeight;
  43. mTextSprite->update(mDesc);
  44. GUIElement::updateRenderElementsInternal();
  45. }
  46. void GUILabel::updateClippedBounds()
  47. {
  48. mClippedBounds = mTextSprite->getBounds(mOffset, mClipRect);
  49. }
  50. Int2 GUILabel::_getOptimalSize() const
  51. {
  52. return GUIHelper::calcOptimalContentsSize(mContent, *mStyle, _getLayoutOptions());
  53. }
  54. void GUILabel::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads,
  55. UINT32 vertexStride, UINT32 indexStride, UINT32 renderElementIdx) const
  56. {
  57. mTextSprite->fillBuffer(vertices, uv, indices, startingQuad, maxNumQuads, vertexStride, indexStride, renderElementIdx, mOffset, mClipRect);
  58. }
  59. void GUILabel::setContent(const GUIContent& content)
  60. {
  61. mContent = content;
  62. mDesc.text = content.getText();
  63. markContentAsDirty();
  64. }
  65. GUILabel* GUILabel::create(GUIWidget& parent, const WString& text, const GUIElementStyle* style)
  66. {
  67. return create(parent, GUIContent(text), style);
  68. }
  69. GUILabel* GUILabel::create(GUIWidget& parent, const WString& text, const GUILayoutOptions& layoutOptions, const GUIElementStyle* style)
  70. {
  71. return create(parent, GUIContent(text), layoutOptions, style);
  72. }
  73. GUILabel* GUILabel::create(GUIWidget& parent, const GUIContent& content, const GUIElementStyle* style)
  74. {
  75. if(style == nullptr)
  76. {
  77. const GUISkin& skin = parent.getSkin();
  78. style = skin.getStyle(getGUITypeName());
  79. }
  80. return new (cm_alloc<GUILabel, PoolAlloc>()) GUILabel(parent, style, content, getDefaultLayoutOptions(style));
  81. }
  82. GUILabel* GUILabel::create(GUIWidget& parent, const GUIContent& content, const GUILayoutOptions& layoutOptions, const GUIElementStyle* style)
  83. {
  84. if(style == nullptr)
  85. {
  86. const GUISkin& skin = parent.getSkin();
  87. style = skin.getStyle(getGUITypeName());
  88. }
  89. return new (cm_alloc<GUILabel, PoolAlloc>()) GUILabel(parent, style, content, layoutOptions);
  90. }
  91. const String& GUILabel::getGUITypeName()
  92. {
  93. static String typeName = "Label";
  94. return typeName;
  95. }
  96. }