From 16b85dbe798a11c980bcaa6ae6f667a31499d962 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Wed, 7 Nov 2012 21:24:44 +0000 Subject: [PATCH] - added another patch from cygal (he's on fire people!) to word wrap messagebox text - remove newlines from english text where appropriate --- source/glest_game/graphics/renderer.cpp | 13 +++++++++-- source/shared_lib/include/graphics/font.h | 3 +++ source/shared_lib/sources/graphics/font.cpp | 25 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 121dff01..02cff239 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -3529,6 +3529,15 @@ void Renderer::renderMessageBox(GraphicMessageBox *messageBox) { messageBox->setFont(CoreData::getInstance().getMenuFontNormal()); messageBox->setFont3D(CoreData::getInstance().getMenuFontNormal3D()); } + + string wrappedText = messageBox->getText(); + if(renderText3DEnabled == false) { + wrappedText = messageBox->getFont()->getMetrics()->wordWrapText(wrappedText,messageBox->getW() * 0.90); + } + else { + wrappedText = messageBox->getFont3D()->getMetrics()->wordWrapText(wrappedText,messageBox->getW() * 0.90); + } + //background glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); glEnable(GL_BLEND); @@ -3598,7 +3607,7 @@ void Renderer::renderMessageBox(GraphicMessageBox *messageBox) { if(renderText3DEnabled == true) { //text renderTextShadow3D( - messageBox->getText(), messageBox->getFont3D(), fontColor, + wrappedText, messageBox->getFont3D(), fontColor, messageBox->getX()+15, messageBox->getY()+7*messageBox->getH()/10, false ); @@ -3611,7 +3620,7 @@ void Renderer::renderMessageBox(GraphicMessageBox *messageBox) { else { //text renderTextShadow( - messageBox->getText(), messageBox->getFont(), fontColor, + wrappedText, messageBox->getFont(), fontColor, messageBox->getX()+15, messageBox->getY()+7*messageBox->getH()/10, false ); diff --git a/source/shared_lib/include/graphics/font.h b/source/shared_lib/include/graphics/font.h index d458430e..3ee7874a 100644 --- a/source/shared_lib/include/graphics/font.h +++ b/source/shared_lib/include/graphics/font.h @@ -52,6 +52,9 @@ public: float getTextWidth(const string &str); float getHeight(const string &str) const; + + string wordWrapText(string text, int maxWidth); + }; // ===================================================== diff --git a/source/shared_lib/sources/graphics/font.cpp b/source/shared_lib/sources/graphics/font.cpp index 7dc513ae..711ba670 100644 --- a/source/shared_lib/sources/graphics/font.cpp +++ b/source/shared_lib/sources/graphics/font.cpp @@ -176,6 +176,31 @@ float FontMetrics::getHeight(const string &str) const { } } +string FontMetrics::wordWrapText(string text, int maxWidth) { + // Strip newlines from source + //replaceAll(text, "\n", " "); + + // Get all words (space separated text) + vector words; + Tokenize(text,words," "); + + string wrappedText = ""; + float lineWidth = 0.0f; + + for(unsigned int i = 0; i < words.size(); ++i) { + string word = words[i]; + float wordWidth = this->getTextWidth(word); + if (lineWidth + wordWidth > maxWidth) { + wrappedText += "\n"; + lineWidth = 0; + } + lineWidth += wordWidth; + wrappedText += word + " "; + } + + return wrappedText; +} + // =============================================== // class Font // ===============================================