Files
coffee_crisis_arcade_edition/source/enter_name.cpp

108 lines
2.0 KiB
C++

#include "enter_name.h"
#include <algorithm> // for max, min
// Constructor
EnterName::EnterName()
{
// Obtiene el puntero al nombre
name = "";
// Inicia la lista de caracteres permitidos
// characterList = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
characterList = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-+-*/=?¿<>!\"#$%&/()";
pos = 0;
numCharacters = (int)characterList.size();
// Pone la lista de indices para que refleje el nombre
updateCharacterIndex();
}
// Destructor
EnterName::~EnterName()
{
}
// 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()
{
++characterIndex[pos];
if (characterIndex[pos] >= numCharacters)
{
characterIndex[pos] = 0;
}
updateName();
}
// Decrementa el índice
void EnterName::decIndex()
{
--characterIndex[pos];
if (characterIndex[pos] < 0)
{
characterIndex[pos] = numCharacters - 1;
}
updateName();
}
// Actualiza la variable
void EnterName::updateName()
{
name.clear();
for (int i = 0; i < NAME_LENGHT; ++i)
{
name.push_back(characterList[characterIndex[i]]);
}
}
// Actualiza la variable
void EnterName::updateCharacterIndex()
{
for (int i = 0; i < NAME_LENGHT; ++i)
{
characterIndex[i] = 0;
}
for (int i = 0; i < (int)name.size(); ++i)
{
characterIndex[i] = findIndex(name.at(i));
}
}
// Encuentra el indice de un caracter en "characterList"
int EnterName::findIndex(char character)
{
for (int i = 0; i < (int)characterList.size(); ++i)
{
if (character == characterList[i])
{
return i;
}
}
return 0;
}
// Obtiene el nombre
std::string EnterName::getName()
{
return name;
}
// Obtiene la posición que se está editando
int EnterName::getPos()
{
return pos;
}