Xna Input is rather simple to use. First, you'll need to reference the XnaInput.dll file from the Debug folder in your project. Next you'll need to add this using statement:

using XnaInput;

In your Game object, you'll need to add:

public Input Input;

and in the constructor or initialize method, initialize the object:

Input = new Input(this);

Now your ready to use your new input object.

To add a control, call Input.AssignControl().

For example, to make controls for an arcade game that needs analog motion and a fire button, you can call:

Input.AssignControl(PlayerIndex.One, "X", Pad.ThumbSticks.Left.X);
Input.AssignControl(PlayerIndex.One, "Y", Pad.ThumbSticks.Left.Y);
Input.AssignControl(PlayerIndex.One, "Fire", Buttons.RightTrigger);

When you need to poll for input, call:

float x = Input.ControlState(PlayerIndex.One, "X");
float y = Input.ControlState(PlayerIndex.One, "X");
bool fire = (Input.ControlState(PlayerIndex.One, "X") == 1.0f);

Later, if you need to change your input to use the mouse, just call AssignControl again with mouse parts as parameters:

Input.AssignControl(PlayerIndex.One, "X", Input.Mouse.X);
Input.AssignControl(PlayerIndex.One, "Y", Input.Mouse.Y);
Input.AssignControl(PlayerIndex.One, "Fire", Input.Mouse.Click(Input.Mouse.Button.Left));

The only difference in how the ControlState call will respond, is that now the x and y values will be the position of the mouse pointer, as opposed to relative values from the gamepad. Maybe in the next version I'll add Input.Mouse.RelativeX and Y. Alternatively, you can implement it yourself:

Input.AssignControl(PlayerIndex.One, "X", new delegate { return Input.Mouse.PreviousState.X - Input.Mouse.State.X; } );

This shows that you can create your own input control methods, by just creating a delegate that returns a float. Because this system is based on delegates, it's very flexible and simple. The possibilities are endless. For example, a control that returns random numbers between 0 and 1:

Random rand = new Random();
Input.AssignControl(PlayerIndex.One, "Random", new delegate { return (float)rand.NextDouble(); });

This can then be replaced by other possible sources of randomness, such as mouse position, etc.

If you would prefer keyboard over mouse, that's possible too, even though the keyboard isn't analog. This is because we can assign one key as the "high" key, and the other as the "low" key. Continuing our example:

Input.AssignControl(PlayerIndex.One, "X", Keys.Right, Keys.Left);
Input.AssignControl(PlayerIndex.One, "Y", Keys.Up, Keys.Down);
Input.AssignControl(PlayerIndex.One, "Fire", Keys.Space);

The "X" control will now return 1.0f if the left arrow is pressed, -1.0f if the right arrow is pressed, and 0.0f if both or none are pressed. The "Y" control works in the same way. The "Fire" control, however, returns 1.0f if the space bar is pressed, and 0.0f otherwise. Pretty simple, eh? Xna Input makes it really easy to swap analog and non-analog controls in this manner. Game pad buttons are supported in a similar way. And of course, you can always make your own controls ;)

That's it for basic usage. You now know how to use Xna Input.

Last edited Apr 6, 2008 at 1:21 AM by Shader, version 1


No comments yet.