Input boxes
Getting keyboard input (so you can get a player's name)
It is easy to wait for the player to press a particular key. For example, if we are waiting for the user to press the Escape key, all we have to do is this:
if (kbd.IsKeyDown(Keys.Escape)
prevKbd.IsKeyUp(Keys.Escape) )
However, if we don't know which key the user is going to press next, the problem becomes much more difficult. We have to check ALL keys to see if a key has been pressed.
Using XNA to get string input
Create a new XNA project called InputBoxes. We can use XNA to get string input from a keyboard. Add the following variable declaration at the top of your program:
IAsyncResult keyboardAsyncResult = null;
GamePadState gamePad, prevGamePad;
KeyboardState kbd, prevKbd;
string currentPlayerName = "";
bool nameRequested = false;
enumGameStates
{
SplashScreen,
GetName,
Paused
}
GameStates gameState;
And we will need this statement in our Game1() method:
Components.Add(newGamerServicesComponent(this));
We will use this variable to determine if an input box has been displayed on the screen. If it has a value of null, then no input box has been displayed. If it has a value that is notnull, then the input box is currently being displayed.
XNA allows us to display input boxes that look like this:
Add to Update:
gamePad = GamePad.GetState(PlayerIndex.One);
kbd = Keyboard.GetState();
if (gameState == GameStates.GetName)
{
if (!nameRequested)
{
nameRequested = true;
RequestName();
}
}
UpdateState();
prevGamePad = gamePad;
prevKbd = kbd;
We need a way to determine if we should display an input box. This can be triggered by a key press from the keyboard or a button press on the game pad. The following example uses the space bar or the start button to signal that the input box should be displayed.
Write UpdateState:
void UpdateState()
{
if (!(prevGamePad.Buttons.Start == ButtonState.Released &
gamePad.Buttons.Start == ButtonState.Pressed ||
prevKbd.IsKeyUp(Keys.Space) &
kbd.IsKeyDown(Keys.Space))
)
return;
// We only get this far if space or start has been pressed
if (gameState ==GameStates.SplashScreen)
gameState = GameStates.GetName;
}
To display an input box, use the following command. This is the generic format for the command:
keyboardAsyncResult = Guide.BeginShowKeyboardInput(
PlayerIndex, "Title", "Description", "Default Text",
Callback, State);
Where:
- PlayerIndex is the index of the player providing the input, e.g. PlayerIndex.One
- "Title" is the title (a string) of the dialog box (appears at the top).
- "Description" is the message that is displayed (a string).
- "Default Text" is the text (a string) that is displayed in the user input area at the beginning.
- Callback is the name of the method to be called once the asynchronous method is completed.
- State is a variable we won't worry about. We will always provide the value null.
In our program, we will call BeginShowKeyBoardInput like this:
keyboardAsyncResult = Guide.BeginShowKeyboardInput(
PlayerIndex.One, "Name", "Please enter your name: ",
"", newAsyncCallback(GetName), null);
Now we can write RequestName:
void RequestName()
{
if (keyboardAsyncResult == null)
{
keyboardAsyncResult = Guide.BeginShowKeyboardInput(
PlayerIndex.One, "Name", "Please enter your name: ",
"", newAsyncCallback(GetName), null);
}
}
Write GetName:
void GetName(IAsyncResult result)
{// input box HAS been displayed
if (keyboardAsyncResult != null
& keyboardAsyncResult.IsCompleted)// user is done with input box
{
// Get what the user typed
currentPlayerName = Guide.EndShowKeyboardInput(keyboardAsyncResult);
if (currentPlayerName != null)
{
// Display what the user typed
//Console.WriteLine(message);
currentPlayerName = currentPlayerName.Trim();
Console.WriteLine(currentPlayerName);
keyboardAsyncResult = null;
gameState = GameStates.Paused;
}
}
}
Arguments:
- The first argument is the player providing the input.
- The "Title" is the title of the text box and will appear in its title bar.
- The "Description" is additional text providing instructions to the user and appears below the title box and above the input box's text input area.
- The "Default Text" is what will appear in the input box's text input area.
- The last two arguments are not important for what we are doing and can be left as null values.
2/27/2012Day18--Input-Boxes.docx1