1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

Improvements for CMessage::breakText.

[ http://vcmi.antypika.aplus.pl/forum/viewtopic.php?p=2260 ]
This commit is contained in:
Michał W. Urbańczyk 2009-04-16 13:32:45 +00:00
parent cc631b1362
commit 06bb6dcce9
2 changed files with 26 additions and 14 deletions

View File

@ -112,25 +112,30 @@ SDL_Surface * CMessage::drawBox1(int w, int h, int playerColor) //draws box for
/* The map file contains long texts, with or without line breaks. This
* method takes such a text and breaks it into into several lines. */
std::vector<std::string> * CMessage::breakText(std::string text, size_t max_line_size,
std::vector<std::string> * CMessage::breakText(std::string text, size_t maxLineSize,
bool userBreak, bool ifor)
{
std::vector<std::string> * ret = new std::vector<std::string>();
boost::algorithm::trim(text);
boost::algorithm::trim_if(text,boost::algorithm::is_any_of(" "));
while (text.length()) {
while (text.length())
{
unsigned int z = 0;
unsigned int braces = 0;
bool opened = false;
while((text[z] != 0) && (text[z] != 0x0a) && (z < max_line_size+braces)) {
while((text[z] != 0) && (text[z] != 0x0a) && (z < maxLineSize+braces))
{
/* We don't count braces in string length. */
if (text[z] == '{') {
if (text[z] == '{')
{
opened=true;
braces++;
} else if (text[z]=='}') {
}
else if (text[z]=='}')
{
opened=false;
braces++;
}
@ -138,7 +143,8 @@ std::vector<std::string> * CMessage::breakText(std::string text, size_t max_line
z++;
}
if ((text[z] != 0) && (text[z] != 0x0a)) {
if ((text[z] != 0) && (text[z] != 0x0a))
{
/* We have a long line. Try to do a nice line break, if
* possible. We backtrack on the line until we find a
* suitable character. */
@ -157,18 +163,23 @@ std::vector<std::string> * CMessage::breakText(std::string text, size_t max_line
z = pos+1;
}
/* Note: empty lines will be skipped. Is that different than H3? */
if (z) {
if(z) //non-blank line
{
ret->push_back(text.substr(0, z));
if (opened)
/* Close the brace for the current line. */
(*(ret->end()-1))+='}';
ret->back() += '}';
text.erase(0, z);
}
else if(text[z] == 0x0a) //blank line
{
ret->push_back(""); //add empty string, no extra actions needed
}
if (text[0] == 0x0a) {
if (text[0] == 0x0a)
{
/* Braces do not carry over lines. The map author forgot
* to close it. */
opened = false;
@ -177,9 +188,10 @@ std::vector<std::string> * CMessage::breakText(std::string text, size_t max_line
text.erase(0, 1);
}
boost::algorithm::trim(text);
boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(" "));
if (opened) {
if (opened)
{
/* Add an opening brace for the next line. */
if (text.length())
text.insert(0, "{");

View File

@ -68,7 +68,7 @@ public:
static SDL_Surface * drawBox1(int w, int h, int playerColor=1);
static void drawBorder(int playerColor, SDL_Surface * ret, int w, int h, int x=0, int y=0);
static SDL_Surface * drawBoxTextBitmapSub(int player, std::string text, SDL_Surface* bitmap, std::string sub, int charperline=30, int imgToBmp=55);
static std::vector<std::string> * breakText(std::string text, size_t max_line_size=30, bool userBreak=true, bool ifor=true);
static std::vector<std::string> * breakText(std::string text, size_t maxLineSize=30, bool userBreak=true, bool ifor=true);
static void init();
static void dispose();
};