Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Getting Integer Input  (Read 7534 times)

0 Members and 1 Guest are viewing this topic.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Integer Input
« on: August 15, 2010, 11:21:55 am »
I have a program that displays a map and I need it to allow the user to input coordinates for that map. I'm trying to do the text input, but I've run into problems.
    1) The text doesn't display on the screen as you input
    2) It doesn't display on the screen after you input it either
    3) I doubt it's even working


Code: [Select]
           bool PressA = false;
            std::string XCoord;
            sf::String text;


            // Press A : Add coords
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::A))
            {
                PressA = true;

                // In event loop...
                if (Event.Type == sf::Event::TextEntered)
                {
                    // Handle ASCII characters only
                    if (Event.Text.Unicode < 128)
                    {
                        XCoord += static_cast<int>(Event.Text.Unicode);
                        text.SetText(XCoord);
                    }
                }
            }


            if (PressA == true)
               App.Draw(text);
-Wander

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Getting Integer Input
« Reply #1 on: August 15, 2010, 12:52:47 pm »
The second if will never be executed 'cause Event.Type will never be sf::Event::TextEntered as it is always sf::Event::KeyPressed in this block.

You have to put the 2nd if outside the first one.
SFML / OS X developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Integer Input
« Reply #2 on: August 15, 2010, 12:57:18 pm »
Wouldn't that make the first if useless?

EDIT: I put the 2nd one outside the first one and I'm still seeing no text display as I type and I still don't know if it's working.  :?
-Wander

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Getting Integer Input
« Reply #3 on: August 15, 2010, 01:00:11 pm »
well, if you want to know if A is down or not you can do as above. But you can also use sf::Input and check it's state.
SFML / OS X developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Integer Input
« Reply #4 on: August 15, 2010, 01:02:03 pm »
I'm not following. :?
Do you mind giving an example or something?
-Wander

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Getting Integer Input
« Reply #5 on: August 15, 2010, 01:09:48 pm »
you want to do what exactly ? get an int from the user while he's pushing A ?

Code: [Select]

main

create a window

create an empty sf::String/sf::Text for SFML 2.0

while (window is open) {

while (there is some event) {

if (text entred) {

if (A is down, checked with sf::Input) {

check if textentred is some int ('0' to '9')
if so, set up the text of you're String.
otherwise set up the text to "" (empty)

}

}

} // event loop

display all your stuff

} // main loop

end main
SFML / OS X developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Integer Input
« Reply #6 on: August 15, 2010, 01:11:06 pm »
I want the user to push A, which will activate the choice to Add a Coordinate to the list of coordinates. The I need him to enter the X coord and then the Y coord.
-Wander

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Getting Integer Input
« Reply #7 on: August 15, 2010, 01:17:19 pm »
ok, so you need to take some paper and pen and write down the state of your application. It will help you to conceptualize your app.

Think «state».
SFML / OS X developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Integer Input
« Reply #8 on: August 15, 2010, 01:19:33 pm »
This is soo confusing. Haha!

Code: [Select]
// Press A : Add coords
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::A))
            {
                PressA = true;
                if (Event.Type == sf::Event::TextEntered)
                {
                    // Handle ASCII characters only
                    if (Event.Text.Unicode < 128)
                    {
                        XCoord += static_cast<char>(Event.Text.Unicode);
                        text.SetText(XCoord);
                    }
                }
            }


I'm still not sure what to change it to....

what do you mean by state?

EDIT: I guess I'm trying to find the flashing cursor that displays the text AS I enter it number by number.
-Wander

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Getting Integer Input
« Reply #9 on: August 15, 2010, 01:35:48 pm »
Look at your code line by line. It can't work that way.

Code: [Select]
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::A)) If this is true, Event.Type will not be both KeyPressed and TextEntered thus the next if will not be executed. That why you have to put the second if outside the first.

But that not all, of course. If you do only that your app won't do what you want.

When I'm speaking about state I mean these ones : http://en.wikipedia.org/wiki/State_machine

Let's look at your aim :

-First, you're in «state 0» : you're waiting for the user to press A.
-When he press A you go into «state 1» : you're now waiting for an int (let's say to begin an int in range [0,9]).
-When the user gave you this number, you enter «state 2» : it's basically the same as «state 2» except you're waiting for the second coord.
-When you received this second number you're in «state 3» : you do what's appropriate with the two coords (move a piece of cheese on the board, launch a missile to (X;Y), set the character to run into direction (X;Y), … whatever).
-Then, you go back to «state 0» (if it's appropriate, sometimes you only want the user to give one time the coords but let's keep this for another day).

Now you have to create some code to do all that stuff. You can use 'enum' to manage the state.
SFML / OS X developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Integer Input
« Reply #10 on: August 15, 2010, 01:39:55 pm »
Wait! You just contradicted yourself....
At first you said that I need to put the 2nd if outside the 1st one.
After that you said that's not it and that I should leave the order.
:?
-Wander

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Getting Integer Input
« Reply #11 on: August 15, 2010, 01:45:48 pm »
hum, sorry. When I said «well, if you want to know if A is down or not you can do as above» I was speaking about sf::Event vs sf::Input, not about your «if» problem.
SFML / OS X developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Integer Input
« Reply #12 on: August 15, 2010, 01:47:10 pm »
Okay. so I should leave the order alone?

How would I use enum for this? I can't see any good use for it...
-Wander

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Getting Integer Input
« Reply #13 on: August 15, 2010, 01:50:52 pm »
You do have to make sure all your «if» have sense and can be true.

Enum can be use this way :
Code: [Select]
enum State { s0, s1, … };

State my_state = s0;

SFML / OS X developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Getting Integer Input
« Reply #14 on: August 15, 2010, 01:51:50 pm »
Well, I know how to write it, but I don't know how I would utilize it.
-Wander

 

anything