Tetris/stringinput.cpp

100 lines
2.1 KiB
C++
Raw Normal View History

2014-11-07 20:47:50 +01:00
#include "stringinput.hpp"
StringInput::StringInput()
{
str = "";
text = NULL;
textColor = make_color ( 255, 255, 255 );
}
StringInput::~StringInput()
{
SDL_FreeSurface ( text );
}
void StringInput::handle_input ( game * Game, SDL_Event *event)
{
//If a key was pressed
if ( event->type == SDL_KEYDOWN )
{
//Keep a copy of the current version of the string
string temp = str;
//If the string less than maximum size
if ( str.length() <= 11 )
{
//If the key is a number
if ( ( event->key.keysym.unicode >= ( Uint16 ) '0' ) && ( event->key.keysym.unicode <= ( Uint16 ) '9' ) )
{
//Append the character
str += ( char ) event->key.keysym.unicode;
}
else if ( ( event->key.keysym.unicode == ( Uint16 ) '-' ) )
{
//Append the character
str += ( char ) event->key.keysym.unicode;
}
//If the key is a uppercase letter
else if ( ( event->key.keysym.unicode >= ( Uint16 ) 'A' ) && ( event->key.keysym.unicode <= ( Uint16 ) 'Z' ) )
{
//Append the character
str += ( char ) event->key.keysym.unicode;
}
//If the key is a lowercase letter
else if ( ( event->key.keysym.unicode >= ( Uint16 ) 'a' ) && ( event->key.keysym.unicode <= ( Uint16 ) 'z' ) )
{
//Append the character
str += ( char ) event->key.keysym.unicode;
}
}
#ifdef DEBUG
cout << str << endl;
#endif
//If backspace was pressed and the string isn't blank
if ( ( event->key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
{
//Remove a character from the end
str.erase ( str.length() - 1 );
}
//If the string was changed
if ( str != temp )
{
//Free the old surface
SDL_FreeSurface ( text );
//Render a new text surface
text = TTF_RenderText_Solid ( Game->GetfontGame(), str.c_str(), textColor );
}
}
}
void StringInput::show_centered ( game * Game )
{
//If the surface isn't blank
if ( text != NULL )
{
//Show the name
apply_surface ( 50, 85, text, Game->GetScreen() );
}
}
string StringInput::GetStr()
{
return str;
}
void StringInput::Clear()
{
str = "";
SDL_FreeSurface ( text );
text = NULL;
}