diff --git a/source/enter_name.cpp b/source/enter_name.cpp index c267aae..54a91c1 100644 --- a/source/enter_name.cpp +++ b/source/enter_name.cpp @@ -2,6 +2,8 @@ #include "utils.h" #include // Para size_t #include // Para max, min +#include // Para assert +#include // Constructor EnterName::EnterName() @@ -15,12 +17,14 @@ void EnterName::init(const std::string &name) { name_ = "A"; position_ = 0; + position_overflow_ = false; } // Se pasa un nombre else { name_ = name; position_ = name_.length(); + position_overflow_ = position_ >= NAME_LENGHT ? true : false; } // Inicializa el vector de indices con el nombre y espacios @@ -30,29 +34,85 @@ void EnterName::init(const std::string &name) // Incrementa la posición void EnterName::incPosition() { - ++position_; - position_ = std::min(position_, NAME_LENGHT + 1); - if (position_ <= NAME_LENGHT) + if (position_overflow_) { - character_index_[position_] = character_index_[position_ - 1]; + // Si ya estamos en overflow, no incrementamos más. + return; } + + ++position_; + + if (position_ >= NAME_LENGHT) + { + position_ = NAME_LENGHT; // Mantenemos en el índice máximo válido. + position_overflow_ = true; // Activamos el flag de overflow. + } + else + { + // Copiamos el índice del carácter anterior si es posible. + if (position_ > 0 && position_ < NAME_LENGHT) + { + character_index_[position_] = character_index_[position_ - 1]; + } + else + { + // Si position_ es 0, inicializamos el carácter actual. + character_index_[position_] = 0; + } + } + updateNameFromCharacterIndex(); } // Decrementa la posición void EnterName::decPosition() { - --position_; - position_ = std::max(position_, 0); - character_index_[position_ + 1] = 0; + if (position_overflow_) + { + // Si estaba en overflow, lo desactivamos y mantenemos position_ en el máximo. + position_overflow_ = false; + position_ = NAME_LENGHT - 1; + } + else + { + if (position_ > 0) + { + --position_; + + // Limpiamos el carácter siguiente si el índice es válido. + if (position_ + 1 < NAME_LENGHT) + { + character_index_[position_ + 1] = 0; + } + } + else + { + // Si position_ es 0, aseguramos que no vaya a ser negativo y limpiamos el carácter actual. + position_ = 0; + character_index_[position_] = 0; + } + + // Si position_ es menor que NAME_LENGHT, aseguramos que el overflow esté desactivado. + if (position_ < NAME_LENGHT) + { + position_overflow_ = false; + } + } + updateNameFromCharacterIndex(); } + // Incrementa el índice void EnterName::incIndex() { + if (position_overflow_) + { + return; + } + ++character_index_[position_]; - if (character_index_[position_] >= character_list_.size()) + if (character_index_[position_] >= static_cast(character_list_.size())) { character_index_[position_] = 0; } @@ -62,6 +122,11 @@ void EnterName::incIndex() // Decrementa el índice void EnterName::decIndex() { + if (position_overflow_) + { + return; + } + --character_index_[position_]; if (character_index_[position_] < 0) { diff --git a/source/enter_name.h b/source/enter_name.h index 84ba9bc..dc600a4 100644 --- a/source/enter_name.h +++ b/source/enter_name.h @@ -17,10 +17,11 @@ constexpr int NAME_LENGHT = 6; class EnterName { private: - std::string character_list_; // Lista de todos los caracteres permitidos - std::string name_; // Nombre introducido - int position_; // Posición a editar del nombre - size_t character_index_[NAME_LENGHT]; // Indice de la lista para cada uno de los caracteres que forman el nombre + std::string character_list_; // Lista de todos los caracteres permitidos + std::string name_; // Nombre introducido + int position_ = 0; // Posición a editar del nombre + bool position_overflow_ = false; // Indica si hemos incrementado la posición más allá del límite + int character_index_[NAME_LENGHT]; // Indice de la lista para cada uno de los caracteres que forman el nombre // Actualiza el nombre a partir de la lista de índices void updateNameFromCharacterIndex(); diff --git a/source/scoreboard.cpp b/source/scoreboard.cpp index 2b7f53c..274fe65 100644 --- a/source/scoreboard.cpp +++ b/source/scoreboard.cpp @@ -382,8 +382,8 @@ void Scoreboard::recalculateAnchors() slot4_4_ = {col, row4}; // Primer cuadrado para poner el nombre de record - // const int enter_name_lenght = NAME_LENGHT * 7; - const int enter_name_lenght = text_scoreboard_->lenght(std::string(NAME_LENGHT, ' ')); + //const int enter_name_lenght = NAME_LENGHT * 7; + const int enter_name_lenght = text_scoreboard_->lenght(std::string(NAME_LENGHT, 'A')); enter_name_pos_.x = (panel_width - enter_name_lenght) / 2; enter_name_pos_.y = row4;