this could probably be optimized draws text and ensures it's never longer than xLen
| 341 | //this could probably be optimized |
| 342 | //draws text and ensures it's never longer than xLen |
| 343 | void Font::drawWrappedText(std::string text, const Eigen::Vector2f& offset, float xLen, unsigned int color) |
| 344 | { |
| 345 | float y = offset.y(); |
| 346 | |
| 347 | std::string line, word, temp; |
| 348 | Eigen::Vector2f textSize; |
| 349 | size_t space, newline; |
| 350 | |
| 351 | while(text.length() > 0 || !line.empty()) //while there's text or we still have text to render |
| 352 | { |
| 353 | space = text.find(' ', 0); |
| 354 | if(space == std::string::npos) |
| 355 | space = text.length() - 1; |
| 356 | |
| 357 | |
| 358 | word = text.substr(0, space + 1); |
| 359 | |
| 360 | //check if the next word contains a newline |
| 361 | newline = word.find('\n', 0); |
| 362 | if(newline != std::string::npos) |
| 363 | { |
| 364 | word = word.substr(0, newline); |
| 365 | text.erase(0, newline + 1); |
| 366 | }else{ |
| 367 | text.erase(0, space + 1); |
| 368 | } |
| 369 | |
| 370 | temp = line + word; |
| 371 | |
| 372 | textSize = sizeText(temp); |
| 373 | |
| 374 | //if we're on the last word and it'll fit on the line, just add it to the line |
| 375 | if((textSize.x() <= xLen && text.length() == 0) || newline != std::string::npos) |
| 376 | { |
| 377 | line = temp; |
| 378 | word = ""; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | //if the next line will be too long or we're on the last of the text, render it |
| 383 | if(textSize.x() > xLen || text.length() == 0 || newline != std::string::npos) |
| 384 | { |
| 385 | //render line now |
| 386 | if(textSize.x() > 0) //make sure it's not blank |
| 387 | drawText(line, Eigen::Vector2f(offset.x(), y), color); |
| 388 | |
| 389 | //increment y by height and some extra padding for the next line |
| 390 | y += textSize.y() + 4; |
| 391 | |
| 392 | //move the word we skipped to the next line |
| 393 | line = word; |
| 394 | }else{ |
| 395 | //there's still space, continue building the line |
| 396 | line = temp; |
| 397 | } |
| 398 | |
| 399 | } |
| 400 | } |