|
8 | 8 | #include "Scripting.h" |
9 | 9 | #include <algorithm> |
10 | 10 | #include <iomanip> |
| 11 | +#include <sstream> |
| 12 | +#include <vector> |
11 | 13 |
|
12 | 14 | #ifdef WIN32 |
13 | 15 | #include <SDL_events.h> |
@@ -297,15 +299,91 @@ void Window::renderLoadingScreen(std::string text, float percent, unsigned char |
297 | 299 | splash.setPosition((Renderer::getScreenWidth() - splash.getSize().x()) / 2, (Renderer::getScreenHeight() - splash.getSize().y()) / 2 * 0.6f); |
298 | 300 | splash.render(trans); |
299 | 301 |
|
300 | | - auto& font = mDefaultFonts.at(1); |
301 | | - TextCache* cache = font->buildTextCache(text, 0, 0, 0x656565FF); |
| 302 | + std::vector<std::string> lines; |
| 303 | + if(text.find('\n') != std::string::npos) |
| 304 | + { |
| 305 | + std::stringstream ss(text); |
| 306 | + std::string line; |
| 307 | + while(std::getline(ss, line, '\n')) |
| 308 | + { |
| 309 | + if(!line.empty()) |
| 310 | + lines.push_back(line); |
| 311 | + } |
| 312 | + } |
| 313 | + else if(text.find(", ") != std::string::npos) |
| 314 | + { |
| 315 | + size_t start = 0; |
| 316 | + while(start < text.size()) |
| 317 | + { |
| 318 | + size_t end = text.find(", ", start); |
| 319 | + if(end == std::string::npos) |
| 320 | + end = text.size(); |
302 | 321 |
|
303 | | - float x = Math::round((Renderer::getScreenWidth() - cache->metrics.size.x()) / 2.0f); |
304 | | - float y = Math::round(Renderer::getScreenHeight() * 0.78f); |
305 | | - trans = trans.translate(Vector3f(x, y, 0.0f)); |
306 | | - Renderer::setMatrix(trans); |
307 | | - font->renderTextCache(cache); |
308 | | - delete cache; |
| 322 | + std::string line = text.substr(start, end - start); |
| 323 | + if(!line.empty()) |
| 324 | + lines.push_back(line); |
| 325 | + |
| 326 | + if(end == text.size()) |
| 327 | + break; |
| 328 | + start = end + 2; |
| 329 | + } |
| 330 | + } |
| 331 | + else |
| 332 | + { |
| 333 | + lines.push_back(text); |
| 334 | + } |
| 335 | + |
| 336 | + if(lines.empty()) |
| 337 | + lines.push_back("Loading..."); |
| 338 | + |
| 339 | + auto font = mDefaultFonts.at(1); |
| 340 | + if(lines.size() > 4) |
| 341 | + font = mDefaultFonts.at(0); |
| 342 | + |
| 343 | + float maxTextWidth = 0.0f; |
| 344 | + for(const auto& line : lines) |
| 345 | + { |
| 346 | + TextCache* cache = font->buildTextCache(line, 0, 0, 0x656565FF); |
| 347 | + maxTextWidth = std::max(maxTextWidth, cache->metrics.size.x()); |
| 348 | + delete cache; |
| 349 | + } |
| 350 | + |
| 351 | + if(maxTextWidth > Renderer::getScreenWidth() * 0.9f) |
| 352 | + font = mDefaultFonts.at(0); |
| 353 | + |
| 354 | + const float barTop = Renderer::getScreenHeight() - (Renderer::getScreenHeight() * 3 * 0.04f); |
| 355 | + const float textTopLimit = Renderer::getScreenHeight() * 0.58f; |
| 356 | + const float textBottomLimit = barTop - 8.0f; |
| 357 | + const float lineHeight = font->getHeight() * 1.15f; |
| 358 | + const float availableHeight = std::max(0.0f, textBottomLimit - textTopLimit); |
| 359 | + |
| 360 | + int maxLines = lineHeight <= 0.0f ? 1 : (int)(availableHeight / lineHeight); |
| 361 | + if(maxLines < 1) |
| 362 | + maxLines = 1; |
| 363 | + |
| 364 | + if((int)lines.size() > maxLines) |
| 365 | + { |
| 366 | + const int hidden = (int)lines.size() - maxLines + 1; |
| 367 | + lines.resize(maxLines); |
| 368 | + lines[maxLines - 1] = "+" + std::to_string(hidden) + " more"; |
| 369 | + } |
| 370 | + |
| 371 | + const float totalHeight = lineHeight * (float)lines.size(); |
| 372 | + float startY = textBottomLimit - totalHeight; |
| 373 | + if(startY < textTopLimit) |
| 374 | + startY = textTopLimit; |
| 375 | + |
| 376 | + for(size_t i = 0; i < lines.size(); i++) |
| 377 | + { |
| 378 | + TextCache* cache = font->buildTextCache(lines[i], 0, 0, 0x656565FF); |
| 379 | + float x = Math::round((Renderer::getScreenWidth() - cache->metrics.size.x()) / 2.0f); |
| 380 | + float y = Math::round(startY + (lineHeight * (float)i)); |
| 381 | + Transform4x4f lineTrans = Transform4x4f::Identity(); |
| 382 | + lineTrans.translate(Vector3f(x, y, 0.0f)); |
| 383 | + Renderer::setMatrix(lineTrans); |
| 384 | + font->renderTextCache(cache); |
| 385 | + delete cache; |
| 386 | + } |
309 | 387 |
|
310 | 388 | Renderer::swapBuffers(); |
311 | 389 |
|
|
0 commit comments