| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "GUI/BsGUIHelper.h" |
| 4 | #include "Image/BsSpriteTexture.h" |
| 5 | #include "GUI/BsGUIElementStyle.h" |
| 6 | #include "GUI/BsGUIDimensions.h" |
| 7 | #include "Image/BsTexture.h" |
| 8 | #include "String/BsUnicode.h" |
| 9 | |
| 10 | namespace bs |
| 11 | { |
| 12 | Vector2I GUIHelper::calcOptimalContentsSize(const Vector2I& contentSize, const GUIElementStyle& style, |
| 13 | const GUIDimensions& dimensions) |
| 14 | { |
| 15 | UINT32 contentWidth = style.margins.left + style.margins.right + style.contentOffset.left + style.contentOffset.right; |
| 16 | UINT32 contentHeight = style.margins.top + style.margins.bottom + style.contentOffset.top + style.contentOffset.bottom; |
| 17 | |
| 18 | return Vector2I(std::max((UINT32)contentSize.x, contentWidth), std::max((UINT32)contentSize.y, contentHeight)); |
| 19 | } |
| 20 | |
| 21 | Vector2I GUIHelper::calcOptimalContentsSize(const GUIContent& content, const GUIElementStyle& style, |
| 22 | const GUIDimensions& dimensions, GUIElementState state) |
| 23 | { |
| 24 | Vector2I contentBounds = calcOptimalContentsSize((const String&)content.text, style, dimensions); |
| 25 | |
| 26 | const HSpriteTexture& image = content.getImage(state); |
| 27 | if (SpriteTexture::checkIsLoaded(image)) |
| 28 | { |
| 29 | contentBounds.x += image->getWidth() + GUIContent::IMAGE_TEXT_SPACING; |
| 30 | contentBounds.y = std::max(image->getHeight(), (UINT32)contentBounds.y); |
| 31 | } |
| 32 | |
| 33 | return contentBounds; |
| 34 | } |
| 35 | |
| 36 | Vector2I GUIHelper::calcOptimalContentsSize(const String& text, const GUIElementStyle& style, const |
| 37 | GUIDimensions& dimensions) |
| 38 | { |
| 39 | UINT32 wordWrapWidth = 0; |
| 40 | |
| 41 | if(style.wordWrap) |
| 42 | wordWrapWidth = dimensions.maxWidth; |
| 43 | |
| 44 | UINT32 contentWidth = style.margins.left + style.margins.right + style.contentOffset.left + style.contentOffset.right; |
| 45 | UINT32 contentHeight = style.margins.top + style.margins.bottom + style.contentOffset.top + style.contentOffset.bottom; |
| 46 | |
| 47 | if(style.font != nullptr && !text.empty()) |
| 48 | { |
| 49 | bs_frame_mark(); |
| 50 | |
| 51 | const U32String utf32text = UTF8::toUTF32(text); |
| 52 | TextData<FrameAlloc> textData(utf32text, style.font, style.fontSize, wordWrapWidth, 0, style.wordWrap); |
| 53 | |
| 54 | contentWidth += textData.getWidth(); |
| 55 | contentHeight += textData.getNumLines() * textData.getLineHeight(); |
| 56 | |
| 57 | bs_frame_clear(); |
| 58 | } |
| 59 | |
| 60 | return Vector2I(contentWidth, contentHeight); |
| 61 | } |
| 62 | |
| 63 | Vector2I GUIHelper::calcTextSize(const String& text, const HFont& font, UINT32 fontSize) |
| 64 | { |
| 65 | Vector2I size; |
| 66 | if (font != nullptr) |
| 67 | { |
| 68 | bs_frame_mark(); |
| 69 | |
| 70 | const U32String utf32text = UTF8::toUTF32(text); |
| 71 | TextData<FrameAlloc> textData(utf32text, font, fontSize, 0, 0, false); |
| 72 | |
| 73 | size.x = textData.getWidth(); |
| 74 | size.y = textData.getNumLines() * textData.getLineHeight(); |
| 75 | |
| 76 | bs_frame_clear(); |
| 77 | } |
| 78 | |
| 79 | return size; |
| 80 | } |
| 81 | } |
| 82 | |