113 lines
2.2 KiB
C++
113 lines
2.2 KiB
C++
#include "enter_name.h"
|
|
#include <algorithm> // for max, min
|
|
|
|
// Constructor
|
|
EnterName::EnterName()
|
|
{
|
|
init();
|
|
}
|
|
|
|
// Inicializa el objeto
|
|
void EnterName::init()
|
|
{
|
|
// Obtiene el puntero al nombre
|
|
name_ = "A";
|
|
|
|
// Inicia la lista de caracteres permitidos
|
|
character_list_ = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-+-*/=?¿<>!\"#$%&/()";
|
|
pos_ = 0;
|
|
num_characters_ = (int)character_list_.size();
|
|
|
|
// Pone la lista de indices para que refleje el nombre
|
|
updateCharacterIndex();
|
|
|
|
// Actualiza el nombre para que ocupe 8 espacios
|
|
updateName();
|
|
}
|
|
|
|
// Incrementa la posición
|
|
void EnterName::incPos()
|
|
{
|
|
pos_++;
|
|
pos_ = std::min(pos_, NAME_LENGHT - 1);
|
|
}
|
|
|
|
// Decrementa la posición
|
|
void EnterName::decPos()
|
|
{
|
|
pos_--;
|
|
pos_ = std::max(pos_, 0);
|
|
}
|
|
|
|
// Incrementa el índice
|
|
void EnterName::incIndex()
|
|
{
|
|
++character_index_[pos_];
|
|
if (character_index_[pos_] >= num_characters_)
|
|
{
|
|
character_index_[pos_] = 0;
|
|
}
|
|
updateName();
|
|
}
|
|
|
|
// Decrementa el índice
|
|
void EnterName::decIndex()
|
|
{
|
|
--character_index_[pos_];
|
|
if (character_index_[pos_] < 0)
|
|
{
|
|
character_index_[pos_] = num_characters_ - 1;
|
|
}
|
|
updateName();
|
|
}
|
|
|
|
// Actualiza la variable
|
|
void EnterName::updateName()
|
|
{
|
|
name_.clear();
|
|
for (int i = 0; i < NAME_LENGHT; ++i)
|
|
{
|
|
name_.push_back(character_list_[character_index_[i]]);
|
|
}
|
|
}
|
|
|
|
// Actualiza la variable
|
|
void EnterName::updateCharacterIndex()
|
|
{
|
|
// Rellena de espacios
|
|
for (int i = 0; i < NAME_LENGHT; ++i)
|
|
{
|
|
character_index_[i] = 0;
|
|
}
|
|
|
|
// Coloca los índices en funcion de los caracteres que forman el nombre
|
|
for (int i = 0; i < (int)name_.size(); ++i)
|
|
{
|
|
character_index_[i] = findIndex(name_.at(i));
|
|
}
|
|
}
|
|
|
|
// Encuentra el indice de un caracter en "character_list_"
|
|
int EnterName::findIndex(char character)
|
|
{
|
|
for (int i = 0; i < (int)character_list_.size(); ++i)
|
|
{
|
|
if (character == character_list_[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::getPos() const
|
|
{
|
|
return pos_;
|
|
} |