treballan en el carrusel per a posar el nom
This commit is contained in:
@@ -1,147 +1,96 @@
|
||||
#include "enter_name.h"
|
||||
|
||||
#include <cstddef> // Para size_t
|
||||
#include <array> // Para array
|
||||
#include <cstdlib> // Para rand
|
||||
#include <string_view> // Para basic_string_view, string_view
|
||||
|
||||
#include "utils.h" // Para trim
|
||||
|
||||
// Constructor
|
||||
EnterName::EnterName()
|
||||
: character_list_(" ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789."),
|
||||
character_index_{0} {}
|
||||
: character_list_("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789. "),
|
||||
selected_index_(0) {}
|
||||
|
||||
// Inicializa el objeto
|
||||
void EnterName::init(const std::string &name) {
|
||||
// No se pasa ningún nombre
|
||||
if (name.empty()) {
|
||||
name_ = "A";
|
||||
position_ = 0;
|
||||
position_overflow_ = false;
|
||||
}
|
||||
// Se pasa un nombre
|
||||
else {
|
||||
name_ = name;
|
||||
position_ = name_.length();
|
||||
position_overflow_ = position_ >= NAME_SIZE;
|
||||
}
|
||||
|
||||
// Inicializa el vector de indices con el nombre y espacios
|
||||
initCharacterIndex(name_);
|
||||
name_ = sanitizeName(name);
|
||||
selected_index_ = 0;
|
||||
}
|
||||
|
||||
// Incrementa la posición
|
||||
void EnterName::incPosition() {
|
||||
if (position_overflow_) {
|
||||
// Si ya estamos en overflow, no incrementamos más.
|
||||
return;
|
||||
}
|
||||
|
||||
++position_;
|
||||
|
||||
if (position_ >= NAME_SIZE) {
|
||||
position_ = NAME_SIZE; // Mantenemos en el índice máximo válido.
|
||||
position_overflow_ = true; // Activamos el flag de overflow.
|
||||
} else if (position_ > 0) // No es necesario verificar position_ < MAX_NAME_LENGTH
|
||||
{
|
||||
// Copiamos el índice del carácter anterior si es posible.
|
||||
// character_index_[position_] = character_index_[position_ - 1];
|
||||
|
||||
// Ponemos el caracter "espacio"
|
||||
character_index_[position_] = 0;
|
||||
} else {
|
||||
// Si position_ es 0, inicializamos el carácter actual.
|
||||
character_index_[position_] = 0;
|
||||
}
|
||||
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Decrementa la posición
|
||||
void EnterName::decPosition() {
|
||||
if (position_overflow_) {
|
||||
// Si estaba en overflow, lo desactivamos y mantenemos position_ en el máximo.
|
||||
position_overflow_ = false;
|
||||
position_ = NAME_SIZE - 1;
|
||||
} else {
|
||||
if (position_ > 0) {
|
||||
--position_;
|
||||
|
||||
// Limpiamos el carácter siguiente si el índice es válido.
|
||||
if (position_ + 1 < NAME_SIZE) {
|
||||
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_LENGTH, aseguramos que el overflow esté desactivado.
|
||||
if (position_ < NAME_SIZE) {
|
||||
position_overflow_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Incrementa el índice
|
||||
// Incrementa el índice del carácter seleccionado
|
||||
void EnterName::incIndex() {
|
||||
if (position_overflow_) {
|
||||
return;
|
||||
++selected_index_;
|
||||
if (selected_index_ >= static_cast<int>(character_list_.size())) {
|
||||
selected_index_ = 0;
|
||||
}
|
||||
|
||||
++character_index_[position_];
|
||||
if (character_index_[position_] >= static_cast<int>(character_list_.size())) {
|
||||
character_index_[position_] = 0;
|
||||
}
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Decrementa el índice
|
||||
// Decrementa el índice del carácter seleccionado
|
||||
void EnterName::decIndex() {
|
||||
if (position_overflow_) {
|
||||
return;
|
||||
}
|
||||
|
||||
--character_index_[position_];
|
||||
if (character_index_[position_] < 0) {
|
||||
character_index_[position_] = character_list_.size() - 1;
|
||||
}
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Actualiza el nombre a partir de la lista de índices
|
||||
void EnterName::updateNameFromCharacterIndex() {
|
||||
name_.clear();
|
||||
for (size_t i = 0; i < NAME_SIZE; ++i) {
|
||||
name_.push_back(character_list_[character_index_[i]]);
|
||||
}
|
||||
name_ = trim(name_);
|
||||
}
|
||||
|
||||
// Actualiza la variable
|
||||
void EnterName::initCharacterIndex(const std::string &name) {
|
||||
// Rellena de espacios
|
||||
for (size_t i = 0; i < NAME_SIZE; ++i) {
|
||||
character_index_[i] = 0;
|
||||
}
|
||||
|
||||
// Coloca los índices en función de los caracteres que forman el nombre
|
||||
for (size_t i = 0; i < name.substr(0, NAME_SIZE).size(); ++i) {
|
||||
character_index_[i] = findIndex(name.at(i));
|
||||
--selected_index_;
|
||||
if (selected_index_ < 0) {
|
||||
selected_index_ = character_list_.size() - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Encuentra el indice de un caracter en "character_list_"
|
||||
auto EnterName::findIndex(char character) const -> int {
|
||||
for (size_t i = 0; i < character_list_.size(); ++i) {
|
||||
if (character == character_list_.at(i)) {
|
||||
return i;
|
||||
// Añade el carácter seleccionado al nombre
|
||||
void EnterName::addCharacter() {
|
||||
if (name_.length() < MAX_NAME_SIZE) {
|
||||
name_.push_back(character_list_[selected_index_]);
|
||||
}
|
||||
}
|
||||
|
||||
// Elimina el último carácter del nombre
|
||||
void EnterName::removeLastCharacter() {
|
||||
if (!name_.empty()) {
|
||||
name_.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
// Devuelve el carácter seleccionado con offset relativo como string
|
||||
auto EnterName::getSelectedCharacter(int offset) const -> std::string {
|
||||
// Calcular el índice con offset, con wrap-around circular
|
||||
int size = static_cast<int>(character_list_.size());
|
||||
int index = (selected_index_ + offset) % size;
|
||||
|
||||
// Manejar índices negativos (hacer wrap-around hacia atrás)
|
||||
if (index < 0) {
|
||||
index += size;
|
||||
}
|
||||
|
||||
return std::string(1, character_list_[index]);
|
||||
}
|
||||
|
||||
// Devuelve el carrusel completo de caracteres centrado en el seleccionado
|
||||
auto EnterName::getCarousel(int size) const -> std::string {
|
||||
// Asegurar que el tamaño sea impar para tener un centro claro
|
||||
if (size % 2 == 0) {
|
||||
++size;
|
||||
}
|
||||
|
||||
std::string carousel;
|
||||
carousel.reserve(size); // Optimización: reservar memoria de antemano
|
||||
|
||||
int half = size / 2;
|
||||
|
||||
// Construir desde -half hasta +half (inclusive)
|
||||
for (int offset = -half; offset <= half; ++offset) {
|
||||
carousel += getSelectedCharacter(offset);
|
||||
}
|
||||
|
||||
return carousel;
|
||||
}
|
||||
|
||||
// Valida y limpia el nombre: solo caracteres legales y longitud máxima
|
||||
auto EnterName::sanitizeName(const std::string &name) const -> std::string {
|
||||
std::string sanitized;
|
||||
|
||||
for (size_t i = 0; i < name.length() && sanitized.length() < MAX_NAME_SIZE; ++i) {
|
||||
// Verifica si el carácter está en la lista permitida
|
||||
if (character_list_.find(name[i]) != std::string::npos) {
|
||||
sanitized.push_back(name[i]);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
return sanitized;
|
||||
}
|
||||
|
||||
// Devuelve un nombre al azar
|
||||
@@ -157,12 +106,11 @@ auto EnterName::getRandomName() -> std::string {
|
||||
"PEPE"};
|
||||
return std::string(NAMES[rand() % NAMES.size()]);
|
||||
}
|
||||
|
||||
// Obtiene el nombre final introducido
|
||||
auto EnterName::getFinalName() -> std::string {
|
||||
auto name = trim(name_.substr(0, position_ + 1)); // Devuelve el texto intruducido incluyendo el del selector
|
||||
if (name.empty()) {
|
||||
name = getRandomName();
|
||||
if (name_.empty()) {
|
||||
name_ = getRandomName();
|
||||
}
|
||||
name_ = name;
|
||||
return name_;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user