separacio de linies automatica en console

This commit is contained in:
2026-03-30 19:35:43 +02:00
parent 1fdc29e9d2
commit cd14ae22c5
2 changed files with 44 additions and 26 deletions

View File

@@ -792,11 +792,9 @@ static const std::vector<ConsoleCommand> COMMANDS = {
{.keyword = "HELP", .execute = [](const std::vector<std::string>&) -> 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<ConsoleCommand> COMMANDS = {
{.keyword = "?", .execute = [](const std::vector<std::string>&) -> 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<float>((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<std::string> {
constexpr int PADDING_IN_H = 6; // TEXT_SIZE; simétrico a ambos lados
const int MAX_PX = static_cast<int>(Options::game.width) - (2 * PADDING_IN_H);
std::vector<std::string> 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<int>(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<int>(msg_lines_.size()));

View File

@@ -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<std::string>; // Word-wrap por ancho en píxeles
// Objetos de renderizado
std::shared_ptr<Text> text_;