From cd14ae22c5196ba5d13353593dc165c217998e5a Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 30 Mar 2026 19:35:43 +0200 Subject: [PATCH] separacio de linies automatica en console --- source/game/ui/console.cpp | 65 ++++++++++++++++++++++++-------------- source/game/ui/console.hpp | 5 +-- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/source/game/ui/console.cpp b/source/game/ui/console.cpp index d028965..0c48f92 100644 --- a/source/game/ui/console.cpp +++ b/source/game/ui/console.cpp @@ -792,11 +792,9 @@ static const std::vector COMMANDS = { {.keyword = "HELP", .execute = [](const std::vector&) -> std::string { printHelp(); std::string result = - "Commands (see terminal):\n" - "fullscreen, zoom, intscale, vsync, driver\n" - "palette, audio, music, sound, set\n" - "restart, kiosk, exit, quit, show\n" - "hide, size, help\n"; + "Commands:\n" + "fullscreen, zoom, intscale, vsync, driver, palette, audio, music, sound, set, restart, kiosk, exit, quit, show, hide, size, help\n" + "-- more info on the terminal"; #ifdef _DEBUG result += "\n[debug] debug room scene"; @@ -807,14 +805,9 @@ static const std::vector COMMANDS = { {.keyword = "?", .execute = [](const std::vector&) -> std::string { printHelp(); std::string result = - "Commands (see terminal):\n" - "[video] fullscreen zoom\n" - "intscale vsync driver\n" - "palette\n" - "[audio] audio music sound\n" - "[game] set restart kiosk\n" - "exit quit\n" - "[info] show hide size help"; + "Commands:\n" + "fullscreen, zoom, intscale, vsync, driver, palette, audio, music, sound, set, restart, kiosk, exit, quit, show, hide, size, help\n" + "-- more info on the terminal"; #ifdef _DEBUG result += "\n[debug] debug room scene"; result += "\ncheat"; @@ -832,6 +825,39 @@ static auto calcTargetHeight(int num_msg_lines) -> float { return static_cast((TEXT_SIZE * (num_msg_lines + 1)) + (PADDING_IN_V * 2)); } +// Divide text en líneas respetando los \n existentes y haciendo word-wrap por ancho en píxeles +auto Console::wrapText(const std::string& text) const -> std::vector { + constexpr int PADDING_IN_H = 6; // TEXT_SIZE; simétrico a ambos lados + const int MAX_PX = static_cast(Options::game.width) - (2 * PADDING_IN_H); + + std::vector result; + std::istringstream segment_stream(text); + std::string segment; + + while (std::getline(segment_stream, segment)) { + if (segment.empty()) { + result.emplace_back(); + continue; + } + std::string current_line; + std::istringstream word_stream(segment); + std::string word; + while (word_stream >> word) { + const std::string TEST = current_line.empty() ? word : (current_line + ' ' + word); + if (text_->length(TEST) <= MAX_PX) { + current_line = TEST; + } else { + if (!current_line.empty()) { result.push_back(current_line); } + current_line = word; + } + } + if (!current_line.empty()) { result.push_back(current_line); } + } + + if (result.empty()) { result.emplace_back(); } + return result; +} + // ── Singleton ───────────────────────────────────────────────────────────────── // [SINGLETON] @@ -1115,17 +1141,8 @@ void Console::processCommand() { } if (!found) { result = "Unknown: " + cmd; } - // Split en '\n' y truncar cada línea a MAX_LINE_CHARS - msg_lines_.clear(); - std::istringstream stream(result); - std::string line; - while (std::getline(stream, line)) { - if (static_cast(line.size()) > MAX_LINE_CHARS) { - line.resize(MAX_LINE_CHARS); - } - msg_lines_.push_back(std::move(line)); - } - if (msg_lines_.empty()) { msg_lines_.push_back({}); } + // Word-wrap automático según el ancho disponible en píxeles + msg_lines_ = wrapText(result); // Actualizar la altura objetivo para animar el resize target_height_ = calcTargetHeight(static_cast(msg_lines_.size())); diff --git a/source/game/ui/console.hpp b/source/game/ui/console.hpp index 6d84e8f..684366a 100644 --- a/source/game/ui/console.hpp +++ b/source/game/ui/console.hpp @@ -49,12 +49,12 @@ class Console { // Constantes de consola static constexpr std::string_view CONSOLE_NAME = "JDD Console"; - static constexpr std::string_view CONSOLE_VERSION = "v1.0"; + static constexpr std::string_view CONSOLE_VERSION = "v2.0"; static constexpr int MAX_LINE_CHARS = 32; static constexpr int MAX_HISTORY_SIZE = 20; static constexpr float CURSOR_ON_TIME = 0.5F; static constexpr float CURSOR_OFF_TIME = 0.3F; - static constexpr float TYPEWRITER_CHAR_DELAY = 0.03F; // segundos entre letra y letra + static constexpr float TYPEWRITER_CHAR_DELAY = 0.01F; // segundos entre letra y letra // [SINGLETON] static Console* console; @@ -67,6 +67,7 @@ class Console { void buildSurface(); // Crea la Surface con el aspecto visual void redrawText(); // Redibuja el texto dinámico (msg + input + cursor) void processCommand(); // Procesa el comando introducido por el usuario + [[nodiscard]] auto wrapText(const std::string& text) const -> std::vector; // Word-wrap por ancho en píxeles // Objetos de renderizado std::shared_ptr text_;