Documentation for V1
For an intro on how to setup a basic project in either V1 or V1.1 see... BasicSetupV1V1.1
Namespaces
using AndrewsXnaHelpers

This namespace contains the Variables class - currently this is all it contains but I will most likely be adding more to this
Variables
public GraphicsDeviceManager - use: Variables.GraphicsDeviceManager
public GraphicsDevice - use: Variables.GraphicsDevice
public ContentManager - use: Variables.Content
public SpriteBatch - use: Variables.SpriteBatch
public GameTime - use: Variables.GameTime
public Texture2D -use: Variables.WhitePixel this is used for drawing rectangles

Methods
public static void SetVariables arguments (GraphicsDeviceManager, ContentManager, SpriteBatch)
This is used to set all the static variables that will be used by the rest of the library - should be called once in load content
public static void Updatearguments (GameTime)
This is used to update the Variables.GameTime value
using AndrewsXnaHelpers.Helpers

This namespace currently contains 2 classes. The Helpers class and the AnimatedSprite class.
Helpers class

Variables
this class contains no variables

Methods
public static void SetResolution arguments(GraphicsDeviceManager, Resolution, Boolean)
This sets the resolution of the passed graphics device to the specified value from the Resolution enum, this also gives you the option to turn on multi-sampling by making the boolean value true.
public static void SetResolution arguments(GrpahicsDeviceManager, Vector2, Boolean)
this sets the resolution of the passed graphics device to the values specified in the passed Vector2 and also gives you the option to turn on multi-sampling by making the boolean value true.
AnimatedSprite class

Variables
public Texture2D SpriteSheet returns the Spritesheet - readonly
public Vector2 Position returns a Vector2 representing the position - readonly
public float Rotation returns a float representing the rotation - readonly
public int NumberOfFrames returns a int representing the number of frames - readonly

Methods
public AnimatedSprite arguments(Texture2D spriteSheet, int numberOfFramesArg, int FPS)
this creates a new instance of the AnimatedSprite class, most of the classes values are generated automatically
public Move arguments(int x, int y)
this moves the position of the sprite by the specified values in the respective directions
public Play arguments vary
There are many overloads for play but I will not go into all of them as I have set ///summary to them so they are fairly self explanatory
public Stop no arguments
This sets the frame to the first frame and resets all the counters as if a new AnimatedSprite has been created
public Pause no arguments
This stops the animation from updating but does not reset any values
public Resume no arguments
This makes the animation play after being paused
public Update no arguments
Updates the animation, call it even if its not playing that's all built into it so it won't update unless its playing
public Draw arguments vary
Technically there are 2 overloads but the overload that has a boolean as a argument doesn't do anything I just put that there so they appear under the same method name.
If you want it to draw the animation at the default origin of the upper left hand corner of the sprite use the one with no arguments. If you want it to draw the animation with the origin at the center just pass true to the one with the boolean argument. NOTE: you can pass true or false it has no affect on how it draws.
using AndrewsXnaHelpers.Input
This namespace contains 4 classes but only 1 is meant to be used at runtime
public static void InputHelper static class that has an instance of each type of input method and an accessor to each.

Updating Input States
For the InputHelper class to function properly you have to call the update for the specific type of input you want to update.
InputHelper.UpdateKeyboard, InputHelper.UpdateMouse or InputHelper.UpdateGamePad or all of these depending on what you want to use.
Keyboard Input

You would access this from InputHelper.Keyboard
There are 2 accessor methods you can use to get the previous and current KeyboardStates.
public KeyboardState CurrentKeyboardState and public KeyboardState PreviousKeyboardState
note that they are read only and nothing should modify them other than the Update() method.

Methods
The first one is the Update method, you can call this yourself but its best to use the one provided for you in the InputHelper class.

public bool IsKeyDown(Keys key)
This method returns true if the passed key is pressed - note that this will constantly return true while the key is pressed, so this method would be ideal for things such as movement but not idea for things such as moving through menus.
public bool IsKeyUp(Keys key)
This method returns true if the passed key is released - I have never found a use for this but someone might
public bool IsKeyPressed(Keys key)
This method returns true if the passed key is pressed once - To me this is the most useful, it will only return true once and the user will have to release the key until it will return true again, ideal for selecting items in a menu or a ingame weapon selector.

That is all I have for the keyboard so far, I plan to add a way of giving the option to log keys to an array so that button combos can be detected and possibly a way of controlling how often a key can be pressed through a timer.
Mouse Input

You would access this from InputHelper.Mouse
There are 2 accessor methods that you can use to get the current and previous mouse state
public MouseState CurrentMouseState and public MouseState PreviousMouseState
note that they are read only and nothing should modify them other than their Update method

Methods
The first one is the Update method, like in the Keyboard class you should use the UpdateMouse method in InputHelper.
There are more methods in this class than there were in the Keyboard class mainly because the MouseState class isn't as flexible in seeing what is being pressed.

public void SetMousePosition(Vector2 positionArg)
This sets the mouse position to the passed coords.
public Vector2 MousePosition()
this returns a Vector2 representing the current location of the mouse.
public int MousePositionX()
this returns an int representation of the mouse's X position
public int MousePositionY()
this returns an int representation of the mouse's Y position
public bool IsLeftMouseButtonDown()
this returns true if the left mouse button is down - will constantly return true if it is held.
public bool IsLeftMouseButtonClicked()
this returns true if the left mouse button is pressed and wasn't pressed last frame - works like the Keyboard.IsKeyPressed() method.
public bool IsLeftMouseButtonUp()
this returns true if the left mouse button is released
public bool IsRightMouseButtonDown()
this returns true if the right mouse button is down - will constantly return true if it is held.
public bool IsRightMouseButtonUp()
this returns true if the right mouse button is released

I plan to be adding more methods to this to make access of the mouse easier.
GamePad Input

You would access this from InputHelper.GamePad
Again like the Mouse and Keyboard classes there are 2 accessor methods but these are a bit different.
public GamePadState[] CurrentStates and public GamePadState[] PreviousStates
Since there can be 4 controllers connected at once the gamepad class stores an array of gamepad states.
They are setup in such a way that to get the state of say Controller 2 you would take the PlayerIndex and subtract 1 from it and you would have the place in the array. So controller 1 would be at index 0 and so forth.

Methods
public void Update()
You can call this yourself but as in Keyboard and Mouse you should use the one supplied in the InputHelper class.
public float LeftStickDirection(PlayerIndex index) - not tested completely
this will return a float representing the current direction of the LeftStick on the specific controller
public float RightStickDirection(PlayerIndex index) - not tested completely
this will return a float representing the current direction of the RightStick on the specific controller
public bool IsButtonDown(PlayerIndex index, Buttons button)
this will return true if the button is down for the specified controller index - will constantly return true if it is held
public bool IsButtonPressed(PlayerIndex index, Buttons button)
this will return true if the button is pressed for the specific controller index - will return true once and then will return false until the player released the button and presses it again.
public bool IsButtonUp(PlayerIndex index, Buttons button)
this will return true if the button for the specified PlayerIndex is released

Again like the Keyboard and Mouse class I will be adding more functionality.
using AndrewsXnaHelpers.Extenders
The Extenders namespace contains 2 classes currently.
RectangleExtenders and Texture2DExtenders
You will never be accessing the class, For the classes in RectangleExtenders they will appear under the list of methods in the actual Rectangle object you create. the same goes for the Texture2DExtenders.

RectangleExtenders
Right now the only methods I have in it are for drawing. It is fairly limited since it does not pass values by ref but for drawing it works just fine.
public static void Draw(this Rectangle rectArg, Texture2D textureArg)
this will draw the rectangle that it was called from filled with the passed texture.
public static void Draw(this Rectangle rectArg, Texture2D textureArg, Color tint)
this will draw the rectangle that it was called from filled with the passed texture tinted with the passed Color value.
public static void Draw(this Rectangle reectArg)
this will draw the rectangle that it was called from filled with solid white.
public static void Draw(this Rectangle rectArg, Color tintArg)
this will draw the rectangle that it was called from filled with the passed color.

One thing to remember, it draws by the values in the actual rectangle, so if you have another variable that controls where they are being drawn then if you want to use these methods you have to set the position in the actual rectangle, obviously this is far from complete and I will add many more overloads to be useful in more situations.

Texture2DExtenders
Right now most of my methods are for Drawing but there is one for calculating the origin.
public static Vector2 CalculateOrigin(this Texture2D sprite)
returns a vector2 representing the center of the texture2D it was called from.
public static void Draw(this Texture2D spriteArg, Vector2 positionArg)
this will draw the texture it was called from at the passed position.
public static void Draw(this Texture2D spriteArg, Vector2 positionArg, Color tint)
this will draw the texture it was called from at the passed position tinted with the value passed
public static void Draw(this Texture2D spriteArg, Vector2 positionArg, float rotationArg, float depthArg)
this will draw the texture it was called from at the passed position, with the passed rotation, at the specified depth

Again, this is far from complete, I will be adding more methods to make this more useful and accomodate more situations.
Using the library

Setting up the Library

Now that we see how everything is setup lets start learning how to implement it.
Implementing the library is the easiest part.

Include the namespace using AndrewsXnaHelpers;
Once the namespace is included in your LoadContent method call Variables.SetVariables()

Pass your GraphicsDeviceManager, by default this is named graphics.
Pass your ContentManager, by default this is named Content.
Pass your SpriteBatch, by default this is named spriteBatch.

Once Variables.SetVariables() has been called you are now able to use all of the classes in the library, but you still have to add a call to Variables.Update in your Update loop.
In your update loop just add this to the top.
Variables.Update() and pass gameTime to it.

Once you have those done, there's no more setup to do now you can dive right in and start using all the classes.

Using the library for input
At the top of your update loop you have to update the state of whatever input method your using. So for this example we'll set it up as if we were getting input from the Keyboard.
You would have this at the top of the Update loop.
InputHelper.UpdateKeyboard();

Now that the keyboard is being updated you can start using the input methods.
Say we are creating a menu system and want the Up key and Down key to change the current menu option. We would have this setup.

if(InputHelper.Keyboard.IsKeyPressed(Keys.Up))
MenuOptionUp();
if(InputHelper.Keyboard.IsKeyPressed(Keys.Down))
MenuOptionDown();

If we wanted to move our player we would use a setup like this

if(InputHelper.Keyboard.IsKeyDown(Keys.W))
MovePlayerUp();
if(InputHelper.Keyboard.IsKeyDown(Keys.S))
MovePlayerDown();
if(InputHelper.Keyboard.IsKeyDown(Keys.A))
MovePlayerLeft();
if(InputHelper.Keyboard.IsKeyDown(Keys.D))
MovePlayerRight();

The reason we use IsKeyDown() in the 2nd example instead of IsKeyPressed() as in the first example is that IsKeyPressed waits for the key to be released which makes it perfect for menus but ill suited for continuous movement, which is why we used IsKeyDown, it tells you if its down every frame so it will be continuous.

The example above carries over to all the other types of input methods, it would only take a few modifications to the methods you use to get the input but the way they work and how you update them is exactly the same.

Last edited Jun 7, 2010 at 1:05 AM by Destroyer229, version 5

Comments

No comments yet.