From 1fdc29e9d2b84893bdbed19ed3cd6d72c014bdb0 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Mon, 30 Mar 2026 19:16:57 +0200 Subject: [PATCH] afegit typewriter effect a console --- source/game/ui/console.cpp | 28 ++++++++++++++++++++++++++-- source/game/ui/console.hpp | 5 +++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/source/game/ui/console.cpp b/source/game/ui/console.cpp index 22cd62366..d0289658c 100644 --- a/source/game/ui/console.cpp +++ b/source/game/ui/console.cpp @@ -893,10 +893,14 @@ void Console::redrawText() { SDL_FRect rect = {.x = 0, .y = 0, .w = WIDTH, .h = height_}; surface_->drawRectBorder(&rect, BORDER_COLOR); - // Líneas de mensaje (una o más); las que no caben en la Surface no se dibujan + // Líneas de mensaje con efecto typewriter (solo muestra los primeros typewriter_chars_) int y_pos = PADDING_IN_V; + int remaining = typewriter_chars_; for (const auto& line : msg_lines_) { - text_->writeColored(PADDING_IN_H, y_pos, line, MSG_COLOR); + if (remaining <= 0) { break; } + const int VISIBLE = std::min(remaining, static_cast(line.size())); + text_->writeColored(PADDING_IN_H, y_pos, line.substr(0, VISIBLE), MSG_COLOR); + remaining -= VISIBLE; y_pos += TEXT_SIZE; } @@ -924,6 +928,19 @@ void Console::update(float delta_time) { } } + // Efecto typewriter: revelar letras una a una (solo cuando ACTIVE) + if (status_ == Status::ACTIVE) { + int total_chars = 0; + for (const auto& line : msg_lines_) { total_chars += static_cast(line.size()); } + if (typewriter_chars_ < total_chars) { + typewriter_timer_ += delta_time; + while (typewriter_timer_ >= TYPEWRITER_CHAR_DELAY && typewriter_chars_ < total_chars) { + typewriter_timer_ -= TYPEWRITER_CHAR_DELAY; + ++typewriter_chars_; + } + } + } + // Animación de altura (resize cuando msg_lines_ cambia); solo en ACTIVE if (status_ == Status::ACTIVE && height_ != target_height_) { const float PREV_HEIGHT = height_; @@ -998,6 +1015,9 @@ void Console::toggle() { input_line_.clear(); cursor_timer_ = 0.0F; cursor_visible_ = true; + // El mensaje inicial ("JDD Console v1.0") aparece completo, sin typewriter + typewriter_chars_ = static_cast(msg_lines_[0].size()); + typewriter_timer_ = 0.0F; SDL_StartTextInput(SDL_GetKeyboardFocus()); if (Notifier::get() != nullptr) { const int OFFSET = static_cast(height_); @@ -1109,6 +1129,10 @@ void Console::processCommand() { // Actualizar la altura objetivo para animar el resize target_height_ = calcTargetHeight(static_cast(msg_lines_.size())); + + // Reiniciar el typewriter para revelar la respuesta letra a letra + typewriter_chars_ = 0; + typewriter_timer_ = 0.0F; } } input_line_.clear(); diff --git a/source/game/ui/console.hpp b/source/game/ui/console.hpp index 64866b115..6d84e8f3b 100644 --- a/source/game/ui/console.hpp +++ b/source/game/ui/console.hpp @@ -54,6 +54,7 @@ class Console { 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 // [SINGLETON] static Console* console; @@ -83,6 +84,10 @@ class Console { float cursor_timer_{0.0F}; bool cursor_visible_{true}; + // Efecto typewriter + int typewriter_chars_{0}; // Caracteres de msg_lines_ actualmente visibles + float typewriter_timer_{0.0F}; + // Animación de altura dinámica float target_height_{0.0F}; // Altura objetivo (según número de líneas de mensaje) int notifier_offset_applied_{0}; // Acumulador del offset enviado al Notifier