Browse Source

Tested text word wrap and alignment

Marko Pintera 12 years ago
parent
commit
973145efa6

+ 1 - 1
CamelotClient/CmTestTextSprite.cpp

@@ -36,7 +36,7 @@ namespace CamelotEngine
 		mSkin->setStyle(GUILabel::getGUITypeName(), labelStyle);
 		mSkin->setStyle(GUILabel::getGUITypeName(), labelStyle);
 
 
 		setSkin(mSkin);
 		setSkin(mSkin);
-		addLabel(text);
+		addLabel(text, 400, 400, true, THA_Right, TVA_Bottom);
 	}
 	}
 
 
 	void TestTextSprite::update()
 	void TestTextSprite::update()

+ 7 - 1
CamelotCore/Include/CmGUILabel.h

@@ -2,6 +2,7 @@
 
 
 #include "CmPrerequisites.h"
 #include "CmPrerequisites.h"
 #include "CmGUIElement.h"
 #include "CmGUIElement.h"
+#include "CmTextSprite.h"
 
 
 namespace CamelotEngine
 namespace CamelotEngine
 {
 {
@@ -35,8 +36,13 @@ namespace CamelotEngine
 	private:
 	private:
 		TextSprite* mTextSprite;
 		TextSprite* mTextSprite;
 		String mText;
 		String mText;
+		UINT32 mFixedWidth, mFixedHeight;
+		bool mWordWrap;
+		TextHorzAlign mHorzAlign;
+		TextVertAlign mVertAlign;
 
 
 		friend class GUIWidget;
 		friend class GUIWidget;
-		GUILabel(GUIWidget* parent, const String& text, const GUISkin* skin);
+		GUILabel(GUIWidget* parent, const String& text, const GUISkin* skin, UINT32 fixedWidth, UINT32 fixedHeight, 
+			bool wordWrap, TextHorzAlign horzAlign, TextVertAlign vertAlign);
 	};
 	};
 }
 }

+ 3 - 1
CamelotCore/Include/CmGUIWidget.h

@@ -2,6 +2,7 @@
 
 
 #include "CmPrerequisites.h"
 #include "CmPrerequisites.h"
 #include "CmOverlay.h"
 #include "CmOverlay.h"
+#include "CmTextSprite.h"
 
 
 namespace CamelotEngine
 namespace CamelotEngine
 {
 {
@@ -16,7 +17,8 @@ namespace CamelotEngine
 
 
 		GUIWidget(const HSceneObject& parent);
 		GUIWidget(const HSceneObject& parent);
 
 
-		GUILabel* addLabel(const String& text);
+		GUILabel* addLabel(const String& text, UINT32 fixedWidth = 0, UINT32 fixedHeight = 0, bool wordWrap = false, TextHorzAlign horzAlign = THA_Left, TextVertAlign vertAlign = TVA_Top);
+		GUILabel* addLabel(const String& text, TextHorzAlign horzAlign = THA_Left, TextVertAlign vertAlign = TVA_Top);
 
 
 		void setSkin(const GUISkin* skin);
 		void setSkin(const GUISkin* skin);
 		const GUISkin* getGUISkin() const;
 		const GUISkin* getGUISkin() const;

+ 8 - 2
CamelotCore/Source/CmGUILabel.cpp

@@ -5,13 +5,19 @@
 
 
 namespace CamelotEngine
 namespace CamelotEngine
 {
 {
-	GUILabel::GUILabel(GUIWidget* parent, const String& text, const GUISkin* skin)
-		:GUIElement(parent, skin), mText(text)
+	GUILabel::GUILabel(GUIWidget* parent, const String& text, const GUISkin* skin, UINT32 fixedWidth, UINT32 fixedHeight, 
+		bool wordWrap, TextHorzAlign horzAlign, TextVertAlign vertAlign)
+		:GUIElement(parent, skin), mText(text), mFixedWidth(fixedWidth), mFixedHeight(fixedHeight), mWordWrap(wordWrap),
+		mHorzAlign(horzAlign), mVertAlign(vertAlign)
 	{
 	{
 		// This is calling a virtual method but it's okay because we always want the one
 		// This is calling a virtual method but it's okay because we always want the one
 		// existing on this class.
 		// existing on this class.
 		mStyle = skin->getStyle(getGUITypeName());
 		mStyle = skin->getStyle(getGUITypeName());
 		mTextSprite = CM_NEW(TextSprite, PoolAlloc) TextSprite(text, mStyle->font, mStyle->fontSize);
 		mTextSprite = CM_NEW(TextSprite, PoolAlloc) TextSprite(text, mStyle->font, mStyle->fontSize);
+		mTextSprite->setSize(fixedWidth, fixedHeight);
+		mTextSprite->setWordWrap(wordWrap);
+		mTextSprite->setClipRect(Rect(0, 0, fixedWidth, fixedHeight));
+		mTextSprite->setAlignment(horzAlign, vertAlign);
 	}
 	}
 
 
 	GUILabel::~GUILabel()
 	GUILabel::~GUILabel()

+ 7 - 2
CamelotCore/Source/CmGUIWidget.cpp

@@ -29,14 +29,19 @@ namespace CamelotEngine
 		mElements.clear();
 		mElements.clear();
 	}
 	}
 
 
-	GUILabel* GUIWidget::addLabel(const String& text)
+	GUILabel* GUIWidget::addLabel(const String& text, UINT32 fixedWidth, UINT32 fixedHeight, bool wordWrap, TextHorzAlign horzAlign, TextVertAlign vertAlign)
 	{
 	{
-		GUILabel* label = CM_NEW(GUILabel, GUIAlloc) GUILabel(this, text, getGUISkin());
+		GUILabel* label = CM_NEW(GUILabel, GUIAlloc) GUILabel(this, text, getGUISkin(), fixedWidth, fixedHeight, wordWrap, horzAlign, vertAlign);
 		mElements.push_back(label);
 		mElements.push_back(label);
 
 
 		return label;
 		return label;
 	}
 	}
 
 
+	GUILabel* GUIWidget::addLabel(const String& text, TextHorzAlign horzAlign, TextVertAlign vertAlign)
+	{
+		return addLabel(text, 0, 0, false, horzAlign, vertAlign);
+	}
+
 	void GUIWidget::setSkin(const GUISkin* skin)
 	void GUIWidget::setSkin(const GUISkin* skin)
 	{
 	{
 		mSkin = skin;
 		mSkin = skin;

+ 3 - 1
CamelotCore/Source/CmTextSprite.cpp

@@ -27,6 +27,8 @@ namespace CamelotEngine
 		void addSpace(UINT32 spaceWidth)
 		void addSpace(UINT32 spaceWidth)
 		{
 		{
 			mSpaceWidth += spaceWidth;
 			mSpaceWidth += spaceWidth;
+
+			calculateWidth();
 		}
 		}
 
 
 		void removeLastChar()
 		void removeLastChar()
@@ -337,7 +339,7 @@ namespace CamelotEngine
 						curLine->addWord(lastWord); // Spaces can stay on previous line even if they don't technically fit
 						curLine->addWord(lastWord); // Spaces can stay on previous line even if they don't technically fit
 
 
 					// No more lines fit vertically so we're done
 					// No more lines fit vertically so we're done
-					if(heightIsLimited && (curHeight + fontData->fontDesc.lineHeight * 2) > mHeight)
+					if(heightIsLimited && curHeight > mHeight)
 						break;
 						break;
 
 
 					curLine = CM_NEW(TextLine, ScratchAlloc) TextLine();
 					curLine = CM_NEW(TextLine, ScratchAlloc) TextLine();