BsGUIHelper.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGUIHelper.h"
  4. #include "BsSpriteTexture.h"
  5. #include "BsGUIElementStyle.h"
  6. #include "BsGUIDimensions.h"
  7. #include "BsTexture.h"
  8. namespace BansheeEngine
  9. {
  10. Vector2I GUIHelper::calcOptimalContentsSize(const Vector2I& contentSize, const GUIElementStyle& style,
  11. const GUIDimensions& dimensions)
  12. {
  13. UINT32 contentWidth = style.margins.left + style.margins.right + style.contentOffset.left + style.contentOffset.right;
  14. UINT32 contentHeight = style.margins.top + style.margins.bottom + style.contentOffset.top + style.contentOffset.bottom;
  15. return Vector2I(std::max((UINT32)contentSize.x, contentWidth), std::max((UINT32)contentSize.y, contentHeight));
  16. }
  17. Vector2I GUIHelper::calcOptimalContentsSize(const GUIContent& content, const GUIElementStyle& style,
  18. const GUIDimensions& dimensions, GUIElementState state)
  19. {
  20. Vector2I contentBounds = calcOptimalContentsSize((const WString&)content.getText(), style, dimensions);
  21. HSpriteTexture image = content.getImage(state);
  22. if (image.isLoaded())
  23. {
  24. contentBounds.x += image->getWidth() + GUIContent::IMAGE_TEXT_SPACING;
  25. contentBounds.y = std::max(image->getHeight(), (UINT32)contentBounds.y);
  26. }
  27. return contentBounds;
  28. }
  29. Vector2I GUIHelper::calcOptimalContentsSize(const WString& text, const GUIElementStyle& style, const
  30. GUIDimensions& dimensions)
  31. {
  32. UINT32 wordWrapWidth = 0;
  33. if(style.wordWrap)
  34. wordWrapWidth = dimensions.maxWidth;
  35. UINT32 contentWidth = style.margins.left + style.margins.right + style.contentOffset.left + style.contentOffset.right;
  36. UINT32 contentHeight = style.margins.top + style.margins.bottom + style.contentOffset.top + style.contentOffset.bottom;
  37. if(style.font != nullptr)
  38. {
  39. bs_frame_mark();
  40. TextData<FrameAlloc> textData(text, style.font, style.fontSize, wordWrapWidth, 0, style.wordWrap);
  41. contentWidth += textData.getWidth();
  42. contentHeight += textData.getNumLines() * textData.getLineHeight();
  43. bs_frame_clear();
  44. }
  45. return Vector2I(contentWidth, contentHeight);
  46. }
  47. }