refactor console: extreu updateCursorBlink/Typewriter/Resize/OpenClose i handleTextInput/HistoryUp/Down/Tab

This commit is contained in:
2026-05-17 21:40:05 +02:00
parent 030180443e
commit 4be51ea318
2 changed files with 174 additions and 149 deletions
+98 -81
View File
@@ -193,14 +193,8 @@ void Console::redrawText() {
Screen::get()->setRendererSurface(previous_renderer); Screen::get()->setRendererSurface(previous_renderer);
} }
// Actualiza la animación de la consola // Parpadeig del cursor (només quan ACTIVE)
void Console::update(float delta_time) { // NOLINT(readability-function-cognitive-complexity) void Console::updateCursorBlink(float delta_time) {
if (status_ == Status::HIDDEN) {
return;
}
// Parpadeo del cursor (solo cuando activa)
if (status_ == Status::ACTIVE) {
cursor_timer_ += delta_time; cursor_timer_ += delta_time;
const float THRESHOLD = cursor_visible_ ? CURSOR_ON_TIME : CURSOR_OFF_TIME; const float THRESHOLD = cursor_visible_ ? CURSOR_ON_TIME : CURSOR_OFF_TIME;
if (cursor_timer_ >= THRESHOLD) { if (cursor_timer_ >= THRESHOLD) {
@@ -209,22 +203,21 @@ void Console::update(float delta_time) { // NOLINT(readability-function-cogniti
} }
} }
// Efecto typewriter: revelar letras una a una (solo cuando ACTIVE) // Revelat lletra a lletra de msg_lines_ (només quan ACTIVE)
if (status_ == Status::ACTIVE) { void Console::updateTypewriter(float delta_time) {
const int TOTAL_CHARS = std::accumulate(msg_lines_.begin(), msg_lines_.end(), 0, [](int acc, const auto& line) { return acc + static_cast<int>(line.size()); }); const int TOTAL_CHARS = std::accumulate(msg_lines_.begin(), msg_lines_.end(), 0, [](int acc, const auto& line) { return acc + static_cast<int>(line.size()); });
if (typewriter_chars_ < TOTAL_CHARS) { if (typewriter_chars_ >= TOTAL_CHARS) { return; }
typewriter_timer_ += delta_time; typewriter_timer_ += delta_time;
while (typewriter_timer_ >= TYPEWRITER_CHAR_DELAY && typewriter_chars_ < TOTAL_CHARS) { while (typewriter_timer_ >= TYPEWRITER_CHAR_DELAY && typewriter_chars_ < TOTAL_CHARS) {
typewriter_timer_ -= TYPEWRITER_CHAR_DELAY; typewriter_timer_ -= TYPEWRITER_CHAR_DELAY;
++typewriter_chars_; ++typewriter_chars_;
} }
} }
}
// Animación de altura (resize cuando msg_lines_ cambia); solo en ACTIVE // Animació d'altura quan msg_lines_ canvia (només quan ACTIVE i height_ != target_height_)
if (status_ == Status::ACTIVE && height_ != target_height_) { void Console::updateResizeAnimation(float delta_time) {
if (anim_progress_ == 0.0F) { if (anim_progress_ == 0.0F) {
// Iniciar animación de resize // Iniciar animació de resize
anim_start_ = height_; anim_start_ = height_;
anim_end_ = target_height_; anim_end_ = target_height_;
} }
@@ -235,7 +228,7 @@ void Console::update(float delta_time) { // NOLINT(readability-function-cogniti
height_ = target_height_; height_ = target_height_;
anim_progress_ = 0.0F; anim_progress_ = 0.0F;
} }
// Actualizar el Notifier incrementalmente con el delta de altura // Actualitzar el Notifier incrementalment amb el delta d'altura
if (Notifier::get() != nullptr) { if (Notifier::get() != nullptr) {
const int DELTA_PX = static_cast<int>(height_) - static_cast<int>(PREV_HEIGHT); const int DELTA_PX = static_cast<int>(height_) - static_cast<int>(PREV_HEIGHT);
if (DELTA_PX > 0) { if (DELTA_PX > 0) {
@@ -246,33 +239,48 @@ void Console::update(float delta_time) { // NOLINT(readability-function-cogniti
notifier_offset_applied_ += DELTA_PX; notifier_offset_applied_ += DELTA_PX;
} }
} }
// Reconstruir la Surface al nuevo tamaño (pequeña: 256×~18-72px) // Reconstruir la Surface al nou tamany (xicoteta: 256×~18-72px)
const float WIDTH = Options::game.width; const float WIDTH = Options::game.width;
surface_ = std::make_shared<Surface>(WIDTH, height_); surface_ = std::make_shared<Surface>(WIDTH, height_);
sprite_->setSurface(surface_); sprite_->setSurface(surface_);
} }
// Redibujar texto cada frame // Animació RISING/VANISHING (basada en temps amb easing)
redrawText(); void Console::updateOpenCloseAnimation(float delta_time) {
// Animación de apertura/cierre (basada en tiempo)
if (status_ == Status::RISING || status_ == Status::VANISHING) {
anim_progress_ = std::min(anim_progress_ + (delta_time / ANIM_DURATION), 1.0F); anim_progress_ = std::min(anim_progress_ + (delta_time / ANIM_DURATION), 1.0F);
y_ = anim_start_ + ((anim_end_ - anim_start_) * Easing::cubicInOut(anim_progress_)); y_ = anim_start_ + ((anim_end_ - anim_start_) * Easing::cubicInOut(anim_progress_));
if (anim_progress_ >= 1.0F) { if (anim_progress_ < 1.0F) { return; }
y_ = anim_end_; y_ = anim_end_;
anim_progress_ = 0.0F; anim_progress_ = 0.0F;
if (status_ == Status::RISING) { if (status_ == Status::RISING) {
status_ = Status::ACTIVE; status_ = Status::ACTIVE;
} else { return;
}
status_ = Status::HIDDEN; status_ = Status::HIDDEN;
// Reset del missatge una vegada completament oculta
msg_lines_ = {std::string(CONSOLE_NAME) + " " + std::string(CONSOLE_VERSION)}; msg_lines_ = {std::string(CONSOLE_NAME) + " " + std::string(CONSOLE_VERSION)};
target_height_ = calcTargetHeight(static_cast<int>(msg_lines_.size())); target_height_ = calcTargetHeight(static_cast<int>(msg_lines_.size()));
} }
// Actualiza la animación de la consola
void Console::update(float delta_time) {
if (status_ == Status::HIDDEN) { return; }
if (status_ == Status::ACTIVE) {
updateCursorBlink(delta_time);
updateTypewriter(delta_time);
if (height_ != target_height_) {
updateResizeAnimation(delta_time);
} }
} }
redrawText();
if (status_ == Status::RISING || status_ == Status::VANISHING) {
updateOpenCloseAnimation(delta_time);
}
SDL_FRect rect = {.x = 0, .y = y_, .w = Options::game.width, .h = height_}; SDL_FRect rect = {.x = 0, .y = y_, .w = Options::game.width, .h = height_};
sprite_->setPosition(rect); sprite_->setPosition(rect);
sprite_->setClip({.x = 0.0F, .y = 0.0F, .w = Options::game.width, .h = height_}); sprite_->setClip({.x = 0.0F, .y = 0.0F, .w = Options::game.width, .h = height_});
@@ -334,14 +342,10 @@ void Console::toggle() {
} }
} }
// Procesa el evento SDL: entrada de texto, Backspace, Enter // Insereix caràcters imprimibles a input_line_ (filtra control i la toggle key activa)
void Console::handleEvent(const SDL_Event& event) { // NOLINT(readability-function-cognitive-complexity) void Console::handleTextInput(const SDL_Event& event) {
if (status_ != Status::ACTIVE) { return; }
if (event.type == SDL_EVENT_TEXT_INPUT) {
// Filtrar caracteres de control (tab, newline, etc.)
if (static_cast<unsigned char>(event.text.text[0]) < 32) { return; } if (static_cast<unsigned char>(event.text.text[0]) < 32) { return; }
// Ignorar texto si la tecla toggle está pulsada (evita escribir su carácter) // Ignorar text si la tecla toggle està pulsada (evita escriure el seu caràcter)
if (KeyConfig::get() != nullptr) { if (KeyConfig::get() != nullptr) {
SDL_Keycode toggle_key = KeyConfig::get()->key("GLOBAL", "console"); SDL_Keycode toggle_key = KeyConfig::get()->key("GLOBAL", "console");
SDL_Scancode toggle_sc = SDL_GetScancodeFromKey(toggle_key, nullptr); SDL_Scancode toggle_sc = SDL_GetScancodeFromKey(toggle_key, nullptr);
@@ -354,10 +358,67 @@ void Console::handleEvent(const SDL_Event& event) { // NOLINT(readability-funct
input_line_ += event.text.text; input_line_ += event.text.text;
} }
tab_matches_.clear(); tab_matches_.clear();
return;
} }
if (event.type == SDL_EVENT_KEY_DOWN) { // Navega enrere a l'historial (cap a comandes més antigues)
void Console::handleHistoryUp() {
tab_matches_.clear();
if (history_index_ >= static_cast<int>(history_.size()) - 1) { return; }
if (history_index_ == -1) { saved_input_ = input_line_; }
++history_index_;
input_line_ = history_[static_cast<size_t>(history_index_)];
}
// Navega cap al present a l'historial (cap a comandes més recents)
void Console::handleHistoryDown() {
tab_matches_.clear();
if (history_index_ < 0) { return; }
--history_index_;
input_line_ = (history_index_ == -1) ? saved_input_ : history_[static_cast<size_t>(history_index_)];
}
// Autocompletat per TAB: calcula candidats si cal i cicla
void Console::handleTab() {
if (tab_matches_.empty()) {
std::string upper;
for (const unsigned char C : input_line_) { upper += static_cast<char>(std::toupper(C)); }
const size_t SPACE_POS = upper.rfind(' ');
if (SPACE_POS == std::string::npos) {
// Mode comanda: cicla keywords visibles que comencen pel prefix
const auto KEYWORDS = registry_.getVisibleKeywords();
std::ranges::copy_if(KEYWORDS, std::back_inserter(tab_matches_), [&upper](const auto& kw) { return upper.empty() || kw.starts_with(upper); });
} else {
const std::string BASE_CMD = upper.substr(0, SPACE_POS);
const std::string SUB_PREFIX = upper.substr(SPACE_POS + 1);
const auto OPTS = registry_.getCompletions(BASE_CMD);
for (const auto& arg : OPTS) {
if (!SUB_PREFIX.empty() && !std::string_view{arg}.starts_with(SUB_PREFIX)) { continue; }
std::string match = BASE_CMD;
match += ' ';
match += arg;
tab_matches_.emplace_back(std::move(match));
}
}
tab_index_ = -1;
}
if (tab_matches_.empty()) { return; }
tab_index_ = (tab_index_ + 1) % static_cast<int>(tab_matches_.size());
std::string result = tab_matches_[static_cast<size_t>(tab_index_)];
std::ranges::transform(result, result.begin(), [](char c) { return static_cast<char>(std::tolower(static_cast<unsigned char>(c))); });
input_line_ = result;
}
// Procesa el evento SDL: entrada de texto, Backspace, Enter
void Console::handleEvent(const SDL_Event& event) {
if (status_ != Status::ACTIVE) { return; }
if (event.type == SDL_EVENT_TEXT_INPUT) {
handleTextInput(event);
return;
}
if (event.type != SDL_EVENT_KEY_DOWN) { return; }
switch (event.key.scancode) { switch (event.key.scancode) {
case SDL_SCANCODE_BACKSPACE: case SDL_SCANCODE_BACKSPACE:
tab_matches_.clear(); tab_matches_.clear();
@@ -368,62 +429,18 @@ void Console::handleEvent(const SDL_Event& event) { // NOLINT(readability-funct
processCommand(); processCommand();
break; break;
case SDL_SCANCODE_UP: case SDL_SCANCODE_UP:
// Navegar hacia atrás en el historial handleHistoryUp();
tab_matches_.clear();
if (history_index_ < static_cast<int>(history_.size()) - 1) {
if (history_index_ == -1) { saved_input_ = input_line_; }
++history_index_;
input_line_ = history_[static_cast<size_t>(history_index_)];
}
break; break;
case SDL_SCANCODE_DOWN: case SDL_SCANCODE_DOWN:
// Navegar hacia el presente en el historial handleHistoryDown();
tab_matches_.clear();
if (history_index_ >= 0) {
--history_index_;
input_line_ = (history_index_ == -1)
? saved_input_
: history_[static_cast<size_t>(history_index_)];
}
break; break;
case SDL_SCANCODE_TAB: { case SDL_SCANCODE_TAB:
if (tab_matches_.empty()) { handleTab();
// Calcular el input actual en mayúsculas
std::string upper;
for (unsigned char c : input_line_) { upper += static_cast<char>(std::toupper(c)); }
const size_t SPACE_POS = upper.rfind(' ');
if (SPACE_POS == std::string::npos) {
// Modo comando: ciclar keywords visibles que empiecen por el prefijo
const auto VISIBLE = registry_.getVisibleKeywords();
std::ranges::copy_if(VISIBLE, std::back_inserter(tab_matches_), [&](const auto& kw) { return upper.empty() || kw.starts_with(upper); });
} else {
const std::string BASE_CMD = upper.substr(0, SPACE_POS);
const std::string SUB_PREFIX = upper.substr(SPACE_POS + 1);
const auto OPTS = registry_.getCompletions(BASE_CMD);
for (const auto& arg : OPTS) {
if (SUB_PREFIX.empty() || std::string_view{arg}.starts_with(SUB_PREFIX)) {
std::string match = BASE_CMD;
match += ' ';
match += arg;
tab_matches_.emplace_back(std::move(match));
}
}
}
tab_index_ = -1;
}
if (tab_matches_.empty()) { break; }
tab_index_ = (tab_index_ + 1) % static_cast<int>(tab_matches_.size());
std::string result = tab_matches_[static_cast<size_t>(tab_index_)];
std::ranges::transform(result, result.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
input_line_ = result;
break; break;
}
default: default:
break; break;
} }
} }
}
// Ejecuta el comando introducido y reinicia la línea de input // Ejecuta el comando introducido y reinicia la línea de input
void Console::processCommand() { void Console::processCommand() {
+8
View File
@@ -79,6 +79,14 @@ class Console {
void redrawText(); // Redibuja el texto dinámico (msg + input + cursor) void redrawText(); // Redibuja el texto dinámico (msg + input + cursor)
void processCommand(); // Procesa el comando introducido por el usuario 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 [[nodiscard]] auto wrapText(const std::string& text) const -> std::vector<std::string>; // Word-wrap por ancho en píxeles
void updateCursorBlink(float delta_time); // Parpadeig del cursor (només quan ACTIVE)
void updateTypewriter(float delta_time); // Revelat lletra a lletra de msg_lines_
void updateResizeAnimation(float delta_time); // Animació d'altura quan msg_lines_ canvia
void updateOpenCloseAnimation(float delta_time); // Animació RISING/VANISHING
void handleTextInput(const SDL_Event& event); // Insereix caràcters imprimibles a input_line_
void handleHistoryUp(); // Navega enrere a l'historial
void handleHistoryDown(); // Navega cap al present a l'historial
void handleTab(); // Autocompletat per TAB: calcula candidats si cal i cicla
// Objetos de renderizado // Objetos de renderizado
std::shared_ptr<Text> text_; std::shared_ptr<Text> text_;