Kaynağa Gözat

Added optimal size calculations for GUILabel

Marko Pintera 12 yıl önce
ebeveyn
işleme
a52471802f

+ 1 - 1
BansheeEngine/Include/BsGUIElement.h

@@ -106,7 +106,7 @@ namespace BansheeEngine
 		UINT32 getWidth() const { return mWidth; }
 		UINT32 getWidth() const { return mWidth; }
 		UINT32 getHeight() const { return mHeight; }
 		UINT32 getHeight() const { return mHeight; }
 
 
-		void setLayoutOptions(const GUI_LAYOUT_OPTIONS& layoutOptions) { mLayoutOptions = layoutOptions; }
+		void setLayoutOptions(const GUI_LAYOUT_OPTIONS& layoutOptions);
 		const GUI_LAYOUT_OPTIONS& getLayoutOptions() const { return mLayoutOptions; }
 		const GUI_LAYOUT_OPTIONS& getLayoutOptions() const { return mLayoutOptions; }
 
 
 		void markAsClean() { mIsDirty = false; }
 		void markAsClean() { mIsDirty = false; }

+ 18 - 0
BansheeEngine/Source/BsGUIElement.cpp

@@ -26,6 +26,24 @@ namespace BansheeEngine
 		markAsClean();
 		markAsClean();
 	}
 	}
 
 
+	void GUIElement::setLayoutOptions(const GUI_LAYOUT_OPTIONS& layoutOptions) 
+	{
+		if(layoutOptions.maxWidth < layoutOptions.minWidth)
+		{
+			CM_EXCEPT(InvalidParametersException, "Maximum width is less than minimum width! Max width: " + 
+			toString(layoutOptions.maxWidth) + ". Min width: " + toString(layoutOptions.minWidth));
+		}
+
+		if(layoutOptions.maxHeight < layoutOptions.minHeight)
+		{
+			CM_EXCEPT(InvalidParametersException, "Maximum height is less than minimum height! Max height: " + 
+			toString(layoutOptions.maxHeight) + ". Min height: " + toString(layoutOptions.minHeight));
+		}
+
+		mLayoutOptions = layoutOptions; 
+	}
+
+
 	bool GUIElement::mouseEvent(const GUIMouseEvent& ev)
 	bool GUIElement::mouseEvent(const GUIMouseEvent& ev)
 	{
 	{
 		return false;
 		return false;

+ 27 - 2
BansheeEngine/Source/BsGUILabel.cpp

@@ -4,6 +4,7 @@
 #include "BsGUISkin.h"
 #include "BsGUISkin.h"
 #include "BsGUIWidget.h"
 #include "BsGUIWidget.h"
 #include "BsGUILayoutOptions.h"
 #include "BsGUILayoutOptions.h"
+#include "CmTextUtility.h"
 
 
 using namespace CamelotFramework;
 using namespace CamelotFramework;
 
 
@@ -60,16 +61,40 @@ namespace BansheeEngine
 
 
 	UINT32 GUILabel::getOptimalWidth() const
 	UINT32 GUILabel::getOptimalWidth() const
 	{
 	{
+		UINT32 wordWrapWidth = 0;
 
 
+		if(mDesc.wordWrap)
+		{
+			if(getLayoutOptions().fixedWidth)
+				wordWrapWidth = getLayoutOptions().width;
+			else if(getLayoutOptions().maxWidth > 0)
+				wordWrapWidth = getLayoutOptions().maxWidth;
+		}
+
+		std::shared_ptr<TextUtility::TextData> textData = TextUtility::getTextData(mDesc.text, mDesc.font, mDesc.fontSize, wordWrapWidth, 0, mDesc.wordWrap);
+		if(textData == nullptr)
+			return 0;
 
 
-		return 0;
+		return textData->getWidth();
 	}
 	}
 
 
 	UINT32 GUILabel::getOptimalHeight() const
 	UINT32 GUILabel::getOptimalHeight() const
 	{
 	{
+		UINT32 wordWrapWidth = 0;
+
+		if(mDesc.wordWrap)
+		{
+			if(getLayoutOptions().fixedWidth)
+				wordWrapWidth = getLayoutOptions().width;
+			else if(getLayoutOptions().maxWidth > 0)
+				wordWrapWidth = getLayoutOptions().maxWidth;
+		}
 
 
+		std::shared_ptr<TextUtility::TextData> textData = TextUtility::getTextData(mDesc.text, mDesc.font, mDesc.fontSize, wordWrapWidth, 0, mDesc.wordWrap);
+		if(textData == nullptr)
+			return 0;
 
 
-		return 0;
+		return textData->getHeight();
 	}
 	}
 
 
 	void GUILabel::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads, 
 	void GUILabel::fillBuffer(UINT8* vertices, UINT8* uv, UINT32* indices, UINT32 startingQuad, UINT32 maxNumQuads, 

+ 3 - 0
CamelotCore/Include/CmTextUtility.h

@@ -103,6 +103,9 @@ namespace CamelotFramework
 			const std::vector<TextLine*>& getLines() const { return mLines; }
 			const std::vector<TextLine*>& getLines() const { return mLines; }
 			const std::vector<HTexture>& getTexturePages() const { return mTexturePages; }
 			const std::vector<HTexture>& getTexturePages() const { return mTexturePages; }
 			const std::vector<UINT32>& getNumQuadsPerPage() const  { return mQuadsPerPage; }
 			const std::vector<UINT32>& getNumQuadsPerPage() const  { return mQuadsPerPage; }
+			UINT32 getWidth() const;
+			UINT32 getHeight() const;
+
 		private:
 		private:
 			friend class TextUtility;
 			friend class TextUtility;
 
 

+ 20 - 0
CamelotCore/Source/CmTextUtility.cpp

@@ -367,4 +367,24 @@ namespace CamelotFramework
 
 
 		return textData;
 		return textData;
 	}
 	}
+
+	UINT32 TextUtility::TextData::getWidth() const
+	{
+		UINT32 width = 0;
+
+		for(auto& line : mLines)
+			width = std::max(width, line->getWidth());
+
+		return width;
+	}
+
+	UINT32 TextUtility::TextData::getHeight() const
+	{
+		UINT32 height = 0;
+
+		for(auto& line : mLines)
+			height += line->getHeight();
+
+		return height;
+	}
 }
 }