BsGUIHelper.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "BsGUIHelper.h"
  2. #include "BsSpriteTexture.h"
  3. #include "BsGUIElementStyle.h"
  4. #include "BsGUIDimensions.h"
  5. #include "BsTexture.h"
  6. namespace BansheeEngine
  7. {
  8. Vector2I GUIHelper::calcOptimalContentsSize(const Vector2I& contentSize, const GUIElementStyle& style, const GUIDimensions& dimensions)
  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 GUIDimensions& dimensions)
  15. {
  16. Vector2I textContentBounds = calcOptimalContentsSize(content.getText(), style, dimensions);
  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()->getWidth();
  22. contentHeight += content.getImage()->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 GUIDimensions& dimensions)
  27. {
  28. UINT32 wordWrapWidth = 0;
  29. if(style.wordWrap)
  30. wordWrapWidth = dimensions.maxWidth;
  31. UINT32 contentWidth = style.margins.left + style.margins.right + style.contentOffset.left + style.contentOffset.right;
  32. UINT32 contentHeight = style.margins.top + style.margins.bottom + style.contentOffset.top + style.contentOffset.bottom;
  33. if(style.font != nullptr)
  34. {
  35. TextData textData(text, style.font, style.fontSize, wordWrapWidth, 0, style.wordWrap);
  36. contentWidth += textData.getWidth();
  37. contentHeight += textData.getNumLines() * textData.getLineHeight();
  38. }
  39. return Vector2I(contentWidth, contentHeight);
  40. }
  41. }