Afegit position_overflow_ per a EnterName i poder plenar tots els slots de lletres

This commit is contained in:
2025-02-07 12:31:59 +01:00
parent 559210652f
commit 29bc4a64fd
3 changed files with 80 additions and 14 deletions

View File

@@ -2,6 +2,8 @@
#include "utils.h" #include "utils.h"
#include <stddef.h> // Para size_t #include <stddef.h> // Para size_t
#include <algorithm> // Para max, min #include <algorithm> // Para max, min
#include <cassert> // Para assert
#include <iostream>
// Constructor // Constructor
EnterName::EnterName() EnterName::EnterName()
@@ -15,12 +17,14 @@ void EnterName::init(const std::string &name)
{ {
name_ = "A"; name_ = "A";
position_ = 0; position_ = 0;
position_overflow_ = false;
} }
// Se pasa un nombre // Se pasa un nombre
else else
{ {
name_ = name; name_ = name;
position_ = name_.length(); position_ = name_.length();
position_overflow_ = position_ >= NAME_LENGHT ? true : false;
} }
// Inicializa el vector de indices con el nombre y espacios // 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 // Incrementa la posición
void EnterName::incPosition() void EnterName::incPosition()
{ {
++position_; if (position_overflow_)
position_ = std::min(position_, NAME_LENGHT + 1);
if (position_ <= NAME_LENGHT)
{ {
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(); updateNameFromCharacterIndex();
} }
// Decrementa la posición // Decrementa la posición
void EnterName::decPosition() void EnterName::decPosition()
{ {
--position_; if (position_overflow_)
position_ = std::max(position_, 0); {
character_index_[position_ + 1] = 0; // 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(); updateNameFromCharacterIndex();
} }
// Incrementa el índice // Incrementa el índice
void EnterName::incIndex() void EnterName::incIndex()
{ {
if (position_overflow_)
{
return;
}
++character_index_[position_]; ++character_index_[position_];
if (character_index_[position_] >= character_list_.size()) if (character_index_[position_] >= static_cast<int>(character_list_.size()))
{ {
character_index_[position_] = 0; character_index_[position_] = 0;
} }
@@ -62,6 +122,11 @@ void EnterName::incIndex()
// Decrementa el índice // Decrementa el índice
void EnterName::decIndex() void EnterName::decIndex()
{ {
if (position_overflow_)
{
return;
}
--character_index_[position_]; --character_index_[position_];
if (character_index_[position_] < 0) if (character_index_[position_] < 0)
{ {

View File

@@ -17,10 +17,11 @@ constexpr int NAME_LENGHT = 6;
class EnterName class EnterName
{ {
private: private:
std::string character_list_; // Lista de todos los caracteres permitidos std::string character_list_; // Lista de todos los caracteres permitidos
std::string name_; // Nombre introducido std::string name_; // Nombre introducido
int position_; // Posición a editar del nombre int position_ = 0; // 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 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 // Actualiza el nombre a partir de la lista de índices
void updateNameFromCharacterIndex(); void updateNameFromCharacterIndex();

View File

@@ -382,8 +382,8 @@ void Scoreboard::recalculateAnchors()
slot4_4_ = {col, row4}; slot4_4_ = {col, row4};
// Primer cuadrado para poner el nombre de record // Primer cuadrado para poner el nombre de record
// const int enter_name_lenght = NAME_LENGHT * 7; //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 = text_scoreboard_->lenght(std::string(NAME_LENGHT, 'A'));
enter_name_pos_.x = (panel_width - enter_name_lenght) / 2; enter_name_pos_.x = (panel_width - enter_name_lenght) / 2;
enter_name_pos_.y = row4; enter_name_pos_.y = row4;