BsGUIHelper.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "BsGUIHelper.h"
  2. #include "BsSpriteTexture.h"
  3. #include "BsGUIElementStyle.h"
  4. #include "BsGUILayoutOptions.h"
  5. #include "BsTexture.h"
  6. namespace BansheeEngine
  7. {
  8. Vector2I GUIHelper::calcOptimalContentsSize(const Vector2I& contentSize, const GUIElementStyle& style, const GUILayoutOptions& layoutOptions)
  9. {
  10. UINT32 contentWidth = style.margins.left + style.margins.right + style.contentOffset.left + style.contentOffset.right;
  11. UINT32 contentHeight = style.margins.top + style.margins.bottom + style.contentOffset.top + style.contentOffset.bottom;
  12. return Vector2I(std::max((UINT32)contentSize.x, contentWidth), std::max((UINT32)contentSize.y, contentHeight));
  13. }
  14. Vector2I GUIHelper::calcOptimalContentsSize(const GUIContent& content, const GUIElementStyle& style, const GUILayoutOptions& layoutOptions)
  15. {
  16. Vector2I textContentBounds = calcOptimalContentsSize(content.getText(), style, layoutOptions);
  17. UINT32 contentWidth = style.margins.left + style.margins.right + style.contentOffset.left + style.contentOffset.right;
  18. UINT32 contentHeight = style.margins.top + style.margins.bottom + style.contentOffset.top + style.contentOffset.bottom;
  19. if(content.getImage() != nullptr)
  20. {
  21. contentWidth += content.getImage()->getTexture()->getWidth();
  22. contentHeight += content.getImage()->getTexture()->getHeight();
  23. }
  24. return Vector2I(std::max((UINT32)textContentBounds.x, contentWidth), std::max((UINT32)textContentBounds.y, contentHeight));
  25. }
  26. Vector2I GUIHelper::calcOptimalContentsSize(const WString& text, const GUIElementStyle& style, const GUILayoutOptions& layoutOptions)
  27. {
  28. UINT32 wordWrapWidth = 0;
  29. if(style.wordWrap)
  30. {
  31. if(layoutOptions.fixedWidth)
  32. wordWrapWidth = layoutOptions.width;
  33. else
  34. wordWrapWidth = layoutOptions.maxWidth;
  35. }
  36. UINT32 contentWidth = style.margins.left + style.margins.right + style.contentOffset.left + style.contentOffset.right;
  37. UINT32 contentHeight = style.margins.top + style.margins.bottom + style.contentOffset.top + style.contentOffset.bottom;
  38. if(style.font != nullptr)
  39. {
  40. TextData textData(text, style.font, style.fontSize, wordWrapWidth, 0, style.wordWrap);
  41. contentWidth += textData.getWidth();
  42. contentHeight += textData.getNumLines() * textData.getLineHeight();
  43. }
  44. return Vector2I(contentWidth, contentHeight);
  45. }
  46. }