Treballant en scoreboard::fillPanelTextures
This commit is contained in:
@@ -1,37 +1,42 @@
|
||||
#include "enter_name.h"
|
||||
#include "utils.h"
|
||||
#include <stddef.h> // Para size_t
|
||||
#include <algorithm> // Para max, min
|
||||
|
||||
// Constructor
|
||||
EnterName::EnterName()
|
||||
{
|
||||
init();
|
||||
}
|
||||
: character_list_(" ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-+-*/=?¿<>!\"#$%&/()") {}
|
||||
|
||||
// Inicializa el objeto
|
||||
void EnterName::init()
|
||||
void EnterName::init(const std::string &name)
|
||||
{
|
||||
// Obtiene el puntero al nombre
|
||||
name_ = "A";
|
||||
// No se pasa ningún nombre
|
||||
if (name == "")
|
||||
{
|
||||
name_ = "A";
|
||||
position_ = 0;
|
||||
}
|
||||
// Se pasa un nombre
|
||||
else
|
||||
{
|
||||
name_ = name;
|
||||
position_ = name_.length();
|
||||
}
|
||||
|
||||
// Inicia la lista de caracteres permitidos
|
||||
character_list_ = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-+-*/=?¿<>!\"#$%&/()";
|
||||
position_ = 0;
|
||||
num_characters_ = static_cast<int>(character_list_.size());
|
||||
|
||||
// Pone la lista de indices para que refleje el nombre
|
||||
updateCharacterIndex();
|
||||
|
||||
// Actualiza el nombre para que ocupe todos los espacios
|
||||
updateName();
|
||||
// Inicializa el vector de indices con el nombre y espacios
|
||||
initCharacterIndex(name_);
|
||||
}
|
||||
|
||||
// Incrementa la posición
|
||||
void EnterName::incPosition()
|
||||
{
|
||||
position_++;
|
||||
position_ = std::min(position_, NAME_LENGHT);
|
||||
checkIfPositionHasBeenUsed();
|
||||
++position_;
|
||||
position_ = std::min(position_, NAME_LENGHT + 1);
|
||||
if (position_ <= NAME_LENGHT)
|
||||
{
|
||||
character_index_[position_] = character_index_[position_ - 1];
|
||||
}
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Decrementa la posición
|
||||
@@ -39,17 +44,19 @@ void EnterName::decPosition()
|
||||
{
|
||||
--position_;
|
||||
position_ = std::max(position_, 0);
|
||||
character_index_[position_ + 1] = 0;
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Incrementa el índice
|
||||
void EnterName::incIndex()
|
||||
{
|
||||
++character_index_[position_];
|
||||
if (character_index_[position_] >= num_characters_)
|
||||
if (character_index_[position_] >= character_list_.size())
|
||||
{
|
||||
character_index_[position_] = 0;
|
||||
}
|
||||
updateName();
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Decrementa el índice
|
||||
@@ -58,36 +65,35 @@ void EnterName::decIndex()
|
||||
--character_index_[position_];
|
||||
if (character_index_[position_] < 0)
|
||||
{
|
||||
character_index_[position_] = num_characters_ - 1;
|
||||
character_index_[position_] = character_list_.size() - 1;
|
||||
}
|
||||
updateName();
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Actualiza el nombre a partir de la lista de índices
|
||||
void EnterName::updateName()
|
||||
void EnterName::updateNameFromCharacterIndex()
|
||||
{
|
||||
name_.clear();
|
||||
for (int i = 0; i < NAME_LENGHT; ++i)
|
||||
{
|
||||
name_.push_back(character_list_[character_index_[i]]);
|
||||
}
|
||||
name_ = trim(name_);
|
||||
}
|
||||
|
||||
// Actualiza la variable
|
||||
void EnterName::updateCharacterIndex()
|
||||
void EnterName::initCharacterIndex(const std::string &name)
|
||||
{
|
||||
// Rellena de espacios y marca como no usados
|
||||
// Rellena de espacios
|
||||
for (size_t i = 0; i < NAME_LENGHT; ++i)
|
||||
{
|
||||
character_index_[i] = 0;
|
||||
position_has_been_used_[i] = false;
|
||||
}
|
||||
|
||||
// Coloca los índices en función de los caracteres que forman el nombre
|
||||
for (size_t i = 0; i < name_.size(); ++i)
|
||||
for (size_t i = 0; i < name.substr(0, NAME_LENGHT).size(); ++i)
|
||||
{
|
||||
character_index_[i] = findIndex(name_.at(i));
|
||||
position_has_been_used_[i] = true;
|
||||
character_index_[i] = findIndex(name.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,30 +104,4 @@ int EnterName::findIndex(char character) const
|
||||
if (character == character_list_.at(i))
|
||||
return i;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Obtiene el nombre
|
||||
std::string EnterName::getName() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
// Obtiene la posición que se está editando
|
||||
int EnterName::getPosition() const
|
||||
{
|
||||
return position_;
|
||||
}
|
||||
|
||||
// Comprueba la posición y copia el caracter si es necesario
|
||||
void EnterName::checkIfPositionHasBeenUsed()
|
||||
{
|
||||
auto used = position_has_been_used_[position_];
|
||||
|
||||
if (!used && position_ > 0)
|
||||
{
|
||||
character_index_[position_] = character_index_[position_ - 1];
|
||||
}
|
||||
|
||||
position_has_been_used_[position_] = true;
|
||||
updateName();
|
||||
}
|
||||
Reference in New Issue
Block a user