-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputHelper.cs
62 lines (56 loc) · 2.28 KB
/
InputHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
/// <summary>
/// A class for helping out with input-related tasks, such as checking if a key or mouse button has been pressed.
/// </summary>
class InputHelper
{
// The current and previous mouse/keyboard states.
MouseState currentMouseState, previousMouseState;
KeyboardState currentKeyboardState, previousKeyboardState;
/// <summary>
/// Updates the InputHelper object by retrieving the new mouse/keyboard state, and keeping the previous state as a back-up.
/// </summary>
/// <param name="gameTime">An object with information about the time that has passed in the game.</param>
public void Update(GameTime gameTime)
{
// update the mouse and keyboard states
previousMouseState = currentMouseState;
previousKeyboardState = currentKeyboardState;
currentMouseState = Mouse.GetState();
currentKeyboardState = Keyboard.GetState();
}
/// <summary>
/// Gets the current position of the mouse cursor.
/// </summary>
public Vector2 MousePosition
{
get { return new Vector2(currentMouseState.X, currentMouseState.Y); }
}
/// <summary>
/// Returns whether or not the left mouse button has just been pressed.
/// </summary>
/// <returns></returns>
public bool MouseLeftButtonPressed()
{
return currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released;
}
/// <summary>
/// Returns whether or not a given keyboard key has just been pressed.
/// </summary>
/// <param name="k">The key to check.</param>
/// <returns>true if the given key has just been pressed in this frame; false otherwise.</returns>
public bool KeyPressed(Keys k)
{
return currentKeyboardState.IsKeyDown(k) && previousKeyboardState.IsKeyUp(k);
}
/// <summary>
/// Returns whether or not a given keyboard key is currently being held down.
/// </summary>
/// <param name="k">The key to check.</param>
/// <returns>true if the given key is being held down; false otherwise.</returns>
public bool KeyDown(Keys k)
{
return currentKeyboardState.IsKeyDown(k);
}
}