Prevent the multiline update for labels from going out of bounds

This commit is contained in:
Simon Robertshaw 2012-07-20 17:43:38 +01:00
parent 332fbfe590
commit 22990b680c

View File

@ -48,40 +48,51 @@ void Label::SetText(std::string text)
void Label::updateMultiline() void Label::updateMultiline()
{ {
char * rawText = new char[text.length()+1];
std::copy(text.begin(), text.end(), rawText);
rawText[text.length()] = 0;
int lines = 1; int lines = 1;
int currentWidth = 0; if(text.length()>0)
char * lastSpace = NULL;
char * currentWord = rawText;
char * nextSpace;
while(true)
{ {
nextSpace = strchr(currentWord+1, ' '); char * rawText = new char[text.length()+1];
if(nextSpace) std::copy(text.begin(), text.end(), rawText);
nextSpace[0] = 0; rawText[text.length()] = 0;
int width = Graphics::textwidth(currentWord);
if(width+currentWidth > Size.X-6) int currentWidth = 0;
char * lastSpace = NULL;
char * currentWord = rawText;
char * nextSpace;
while(true)
{ {
currentWidth = width; nextSpace = strchr(currentWord+1, ' ');
currentWord[0] = '\n'; if(nextSpace)
lines++; nextSpace[0] = 0;
int width = Graphics::textwidth(currentWord);
if(width+currentWidth > Size.X-6)
{
currentWidth = width;
currentWord[0] = '\n';
lines++;
}
else
currentWidth += width;
if(nextSpace)
nextSpace[0] = ' ';
if(!currentWord[0] || !currentWord[1] || !(currentWord = strchr(currentWord+1, ' ')))
break;
} }
else if(autoHeight)
currentWidth += width; {
if(nextSpace) Size.Y = lines*12;
nextSpace[0] = ' '; }
if(!(currentWord = strchr(currentWord+1, ' '))) textLines = std::string(rawText);
break; delete[] rawText;
} }
if(autoHeight) else
{ {
Size.Y = lines*12; if(autoHeight)
{
Size.Y = 12;
}
textLines = std::string("");
} }
textLines = std::string(rawText);
delete[] rawText;
} }
std::string Label::GetText() std::string Label::GetText()