Sign in to follow this  
BaneTrapper

Using sfml2.0 to input a name

Recommended Posts

BaneTrapper    1531
Hello.
Currently i am trying to find a easy way to make a input name in SFML 2.0

What i am trying to do is something like this
[CODE]
std::string Name;
case sf::Event::KeyPressed: //When key is pressed
Name.push_back(EventList.key.code); //Push back the key code
break;
[/CODE]

The problem in this is that the EventList.key.code is SFML enum that is not "ascii" i think.
So what do i do?

This is the code i am using to add to namestring when a key is pressed, and it works flawlessly
But i want something more efficient!
[CODE]
//objApp.objHeroCreator.NameString is a std::string
case sf::Event::KeyPressed:
switch(EventList.key.code)
{
case sf::Keyboard::Num1:
if(Shift)
objApp.objHeroCreator.NameString.push_back('!');
else
objApp.objHeroCreator.NameString.push_back('1');
break;
case sf::Keyboard::Num2:
if(Shift)
objApp.objHeroCreator.NameString.push_back('@');
else
objApp.objHeroCreator.NameString.push_back('2');
break;
case sf::Keyboard::Num3:
if(Shift)
objApp.objHeroCreator.NameString.push_back('#');
else
objApp.objHeroCreator.NameString.push_back('3');
break;
case sf::Keyboard::Num4:
if(Shift)
objApp.objHeroCreator.NameString.push_back('$');
else
objApp.objHeroCreator.NameString.push_back('4');
break;
case sf::Keyboard::Num5:
if(Shift)
objApp.objHeroCreator.NameString.push_back('%');
else
objApp.objHeroCreator.NameString.push_back('5');
break;
case sf::Keyboard::Num6:
if(Shift)
objApp.objHeroCreator.NameString.push_back('^');
else
objApp.objHeroCreator.NameString.push_back('6');
break;
case sf::Keyboard::Num7:
if(Shift)
objApp.objHeroCreator.NameString.push_back('&');
else
objApp.objHeroCreator.NameString.push_back('7');
break;
case sf::Keyboard::Num8:
if(Shift)
objApp.objHeroCreator.NameString.push_back('*');
else
objApp.objHeroCreator.NameString.push_back('8');
break;
case sf::Keyboard::Num9:
if(Shift)
objApp.objHeroCreator.NameString.push_back('(');
else
objApp.objHeroCreator.NameString.push_back('9');
break;
case sf::Keyboard::Num0:
if(Shift)
objApp.objHeroCreator.NameString.push_back(')');
else
objApp.objHeroCreator.NameString.push_back('0');
break;
case sf::Keyboard::Return:
sf::Mouse::setPosition(sf::Vector2i(objApp.Screen.getPosition().x + SCREEN_WIDTH /2,
objApp.Screen.getPosition().y + SCREEN_HEIGHT /2));
break;
case sf::Keyboard::Delete:
if(objApp.objHeroCreator.NameString.size() > 0)
{
objApp.objHeroCreator.NameString.erase(objApp.objHeroCreator.NameString.size() - 1);
}
break;
case sf::Keyboard::Back:
if(objApp.objHeroCreator.NameString.size() > 0)
{
objApp.objHeroCreator.NameString.erase(objApp.objHeroCreator.NameString.size() - 1);
}
break;
case sf::Keyboard::Space:
if(objApp.objHeroCreator.NameString.size() > 0)
objApp.objHeroCreator.NameString.push_back(' ');
break;
case sf::Keyboard::Q:
if(Shift)
objApp.objHeroCreator.NameString.push_back('Q');
else
objApp.objHeroCreator.NameString.push_back('q');
break;
case sf::Keyboard::W:
if(Shift)
objApp.objHeroCreator.NameString.push_back('W');
else
objApp.objHeroCreator.NameString.push_back('w');
break;
case sf::Keyboard::E:
if(Shift)
objApp.objHeroCreator.NameString.push_back('E');
else
objApp.objHeroCreator.NameString.push_back('e');
break;
case sf::Keyboard::R:
if(Shift)
objApp.objHeroCreator.NameString.push_back('R');
else
objApp.objHeroCreator.NameString.push_back('r');
break;
case sf::Keyboard::T:
if(Shift)
objApp.objHeroCreator.NameString.push_back('T');
else
objApp.objHeroCreator.NameString.push_back('t');
break;
case sf::Keyboard::Y:
if(Shift)
objApp.objHeroCreator.NameString.push_back('Y');
else
objApp.objHeroCreator.NameString.push_back('y');
break;
case sf::Keyboard::U:
if(Shift)
objApp.objHeroCreator.NameString.push_back('U');
else
objApp.objHeroCreator.NameString.push_back('u');
break;
case sf::Keyboard::I:
if(Shift)
objApp.objHeroCreator.NameString.push_back('I');
else
objApp.objHeroCreator.NameString.push_back('i');
break;
case sf::Keyboard::o:
if(Shift)
objApp.objHeroCreator.NameString.push_back('O');
else
objApp.objHeroCreator.NameString.push_back('o');
break;
case sf::Keyboard::P:
if(Shift)
objApp.objHeroCreator.NameString.push_back('P');
else
objApp.objHeroCreator.NameString.push_back('p');
break;
case sf::Keyboard::A:
if(Shift)
objApp.objHeroCreator.NameString.push_back('A');
else
objApp.objHeroCreator.NameString.push_back('a');
break;
case sf::Keyboard::S:
if(Shift)
objApp.objHeroCreator.NameString.push_back('S');
else
objApp.objHeroCreator.NameString.push_back('s');
break;
case sf::Keyboard::D:
if(Shift)
objApp.objHeroCreator.NameString.push_back('D');
else
objApp.objHeroCreator.NameString.push_back('d');
break;
case sf::Keyboard::F:
if(Shift)
objApp.objHeroCreator.NameString.push_back('F');
else
objApp.objHeroCreator.NameString.push_back('f');
break;
case sf::Keyboard::G:
if(Shift)
objApp.objHeroCreator.NameString.push_back('G');
else
objApp.objHeroCreator.NameString.push_back('g');
break;
case sf::Keyboard::H:
if(Shift)
objApp.objHeroCreator.NameString.push_back('H');
else
objApp.objHeroCreator.NameString.push_back('h');
break;
case sf::Keyboard::J:
if(Shift)
objApp.objHeroCreator.NameString.push_back('J');
else
objApp.objHeroCreator.NameString.push_back('j');
break;
case sf::Keyboard::K:
if(Shift)
objApp.objHeroCreator.NameString.push_back('K');
else
objApp.objHeroCreator.NameString.push_back('k');
break;
case sf::Keyboard::L:
if(Shift)
objApp.objHeroCreator.NameString.push_back('L');
else
objApp.objHeroCreator.NameString.push_back('l');
break;
case sf::Keyboard::Z:
if(Shift)
objApp.objHeroCreator.NameString.push_back('Z');
else
objApp.objHeroCreator.NameString.push_back('z');
break;
case sf::Keyboard::X:
if(Shift)
objApp.objHeroCreator.NameString.push_back('X');
else
objApp.objHeroCreator.NameString.push_back('x');
break;
case sf::Keyboard::C:
if(Shift)
objApp.objHeroCreator.NameString.push_back('C');
else
objApp.objHeroCreator.NameString.push_back('c');
break;
case sf::Keyboard::V:
if(Shift)
objApp.objHeroCreator.NameString.push_back('V');
else
objApp.objHeroCreator.NameString.push_back('V');
break;
case sf::Keyboard::B:
if(Shift)
objApp.objHeroCreator.NameString.push_back('B');
else
objApp.objHeroCreator.NameString.push_back('B');
break;
case sf::Keyboard::N:
if(Shift)
objApp.objHeroCreator.NameString.push_back('N');
else
objApp.objHeroCreator.NameString.push_back('n');
break;
case sf::Keyboard::M:
if(Shift)
objApp.objHeroCreator.NameString.push_back('M');
else
objApp.objHeroCreator.NameString.push_back('m');
break;
default:
break;
}
[/CODE] Edited by BaneTrapper

Share this post


Link to post
Share on other sites
For text input, you're supposed to use [url="http://www.sfml-dev.org/documentation/2.0/structsf_1_1Event_1_1TextEvent.php"]sf::Event::TextEvent[/url] instead of keypresses - it wraps this for you.

TextEvent gives each character, when entered, as a UTF-32 character in a Uint32. I haven't messed around with Unicode before, but I think SFML also provides a way to convert it - check out [url="http://en.sfml-dev.org/forums/index.php?topic=9562.0"]this thread[/url] on the SFML forums.

Share this post


Link to post
Share on other sites
kidman171    498
I was actually just working on getting a user's name today. Here is a snippet of the code I used:

[code]
while( window.pollEvent(event) )
{
switch( event.type )
{
case sf::Event::TextEntered:
if( event.text.unicode < 128 )
{
std::string name = playerName.getString();
if( event.text.unicode == 13 ) // return key
{
// finished entering name
} else if( event.text.unicode == 8 ) { // backspace
if( name.size() > 0 ) name.resize( name.size() - 1 );
} else {
name += static_cast<char>(event.text.unicode);
}
playerName.setString( name );
}
break;
// ... more cases
}
}
[/code] Edited by kidman171

Share this post


Link to post
Share on other sites
BaneTrapper    1531
[quote name='Servant of the Lord' timestamp='1352143615' post='4997691']
For text input, you're supposed to use [url="http://www.sfml-dev.org/documentation/2.0/structsf_1_1Event_1_1TextEvent.php"]sf::Event::TextEvent[/url] instead of keypresses - it wraps this for you.

TextEvent gives each character, when entered, as a UTF-32 character in a Uint32. I haven't messed around with Unicode before, but I think SFML also provides a way to convert it - check out [url="http://en.sfml-dev.org/forums/index.php?topic=9562.0"]this thread[/url] on the SFML forums.
[/quote]

Thanks on info

[quote name='kidman171' timestamp='1352146623' post='4997707']
I was actually just working on getting a user's name today. Here is a snippet of the code I used:
[code]
while( window.pollEvent(event) )
{
switch( event.type )
{
case sf::Event::TextEntered:
if( event.text.unicode < 128 )
{
std::string name = playerName.getString();
if( event.text.unicode == 13 ) // return key
{
// finished entering name
} else if( event.text.unicode == 8 ) { // backspace
if( name.size() > 0 ) name.resize( name.size() - 1 );
} else {
name += static_cast<char>(event.text.unicode);
}
playerName.setString( name );
}
break;
// ... more cases
}
}
[/code]
[/quote]

Thanks for code, good to have example shown.
That be all i need help with. Edited by BaneTrapper

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this