Difference between revisions of "Mouse or touch control Input"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Adding GamePad Control)
(Reading the GamePad and Move)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
This page is to document how to you can handle mouse and touch input. You could essentially program all inputs within your player movement code. The page about the keyboard input is: [[Adding and Processing Player Input]].
 
This page is to document how to you can handle mouse and touch input. You could essentially program all inputs within your player movement code. The page about the keyboard input is: [[Adding and Processing Player Input]].
 +
 +
=Adding Mouse Control=
 +
The code below shows how you can get the state of the Mouse, and then check or get its position. It also shows you how to detect clicks of the mouse buttons. this code will obviously need to go into your update method of your Game1.cs, you could have this in its own method but you will still need to call it from within the update:
 +
 +
<syntaxhighlight lang=csharp>
 +
MouseState state = Mouse.GetState();
 +
 +
// Update our sprites position to the current cursor location
 +
position.X = state.X;
 +
position.Y = state.Y;
 +
 +
// Check if Right Mouse Button pressed, if so, exit
 +
if (state.RightButton == ButtonState.Pressed)
 +
      Exit();
 +
 +
if (state.LeftButton == ButtonState.Pressed)
 +
{
 +
    position.X = state.X;
 +
    position.Y = state.Y;
 +
}
 +
</syntaxhighlight>
  
 
=Adding GamePad Control=
 
=Adding GamePad Control=
With in the Game1.cs file, find the the Game1 class and find the line Player player; . After this line enter the following, this will be used to save the current and previous gamepad state and to also control the speed of movement (you may already have this if you have already coded the keyboard input):
+
With in the Game1.cs file, we need to declare a couple of variables at the top of your code this will be used to save the current and previous gamepad state and to also control the speed of movement (you may already have this if you have already coded the keyboard input):
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
Line 28: Line 49:
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
player.Position.X += currentGamePadState.ThumbSticks.Left.X * playerMoveSpeed;
+
Position.X += currentGamePadState.ThumbSticks.Left.X * playerMoveSpeed;
player.Position.Y -= currentGamePadState.ThumbSticks.Left.Y * playerMoveSpeed;
+
Position.Y -= currentGamePadState.ThumbSticks.Left.Y * playerMoveSpeed;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 43: Line 64:
  
 
=Adding Touch Control=
 
=Adding Touch Control=
With in the Game1.cs file, find the the Game1 class and find the line Player player; . After this line enter the following, this will be used to save the current and previous touch state and to also control the speed of movement (you may already have this if you have already coded the keyboard input):
+
With in the Game1.cs file, find the the Game1 class and find the Initialize method within the Game1.cs file. You need to add the line below to set the FreeDrag gesture:
 
 
<syntaxhighlight lang=csharp>
 
// Gamepad states used to determine button presses
 
GamePadState currentGamePadState;
 
GamePadState previousGamePadState;
 
 
 
// A movement speed for the player
 
float playerMoveSpeed;
 
</syntaxhighlight>
 
 
 
Now find the Initialize method within the Game1.cs file. You need to add the line below to set the movement speed:
 
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
// Set a constant player move speed
+
//Enable the FreeDrag gesture.
playerMoveSpeed = 8.0f;
+
TouchPanel.EnabledGestures = GestureType.FreeDrag;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 66: Line 76:
  
 
==Reading the GamePad and Move==
 
==Reading the GamePad and Move==
Now in the UpdatePlayer method add the code to detect left\right & up\down movement:
+
Now in the UpdatePlayer method add the code to move the player based upon the FreeDrag gesture:
  
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
player.Position.X += currentGamePadState.ThumbSticks.Left.X * playerMoveSpeed;
+
// Windows 8 Touch Gestures for MonoGame
player.Position.Y -= currentGamePadState.ThumbSticks.Left.Y * playerMoveSpeed;
+
while (TouchPanel.IsGestureAvailable)
 +
{
 +
    GestureSample gesture = TouchPanel.ReadGesture();
 +
    if (gesture.GestureType == GestureType.FreeDrag)
 +
    {
 +
        Position += gesture.Delta;
 +
    }
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Now, back in the Update method add the following lines to listen to the keyboard. Enter them just before we call UpdatePlayer:
+
==Other Touch Gestures==
 +
<syntaxhighlight lang=csharp>
 +
GestureType.Hold
  
 +
GestureType.Tap
 +
 +
GestureType.DoubleTap
 +
 +
GestureType.FreeDrag
 +
 +
GestureType.Flick
 +
 +
GestureType.Pinch
 +
</syntaxhighlight>
 +
 +
You can enable many gestures at the same time:
 
<syntaxhighlight lang=csharp>
 
<syntaxhighlight lang=csharp>
// Save the previous state of the  game pad so we can determine single movement
+
TouchPanel.EnabledGestures = gesture1 | gesture2 | gesture ... ;
previousGamePadState = currentGamePadState;;
+
</syntaxhighlight>
  
// Read the current state of the gamepad and store it
+
You would need to add checks for the other enabled gestures, eg:
currentGamePadState = GamePad.GetState(PlayerIndex.One);
+
<syntaxhighlight lang=csharp>
 +
if (gesture.GestureType == GestureType.Tap)
 +
{
 +
   
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 09:09, 18 June 2018

This page is to document how to you can handle mouse and touch input. You could essentially program all inputs within your player movement code. The page about the keyboard input is: Adding and Processing Player Input.

Adding Mouse Control

The code below shows how you can get the state of the Mouse, and then check or get its position. It also shows you how to detect clicks of the mouse buttons. this code will obviously need to go into your update method of your Game1.cs, you could have this in its own method but you will still need to call it from within the update:

MouseState state = Mouse.GetState();

// Update our sprites position to the current cursor location
position.X = state.X;
position.Y = state.Y;

// Check if Right Mouse Button pressed, if so, exit
if (state.RightButton == ButtonState.Pressed)
      Exit();

if (state.LeftButton == ButtonState.Pressed)
{
    position.X = state.X;
    position.Y = state.Y;
}

Adding GamePad Control

With in the Game1.cs file, we need to declare a couple of variables at the top of your code this will be used to save the current and previous gamepad state and to also control the speed of movement (you may already have this if you have already coded the keyboard input):

// Gamepad states used to determine button presses
GamePadState currentGamePadState;
GamePadState previousGamePadState;

// A movement speed for the player
float playerMoveSpeed;

Now find the Initialize method within the Game1.cs file. You need to add the line below to set the movement speed:

// Set a constant player move speed
playerMoveSpeed = 8.0f;

The Update method within the Game1.cs file is used while the game is running. In order to make this easy to understand and read, we will create a new method to update the player and call it from the Update method. If you haven't already created the UpdatePlayer method, create the UpdatePlayer method and call it from Update, see below:

Update player.png

Reading the GamePad and Move

Now in the UpdatePlayer method add the code to detect left\right & up\down movement:

Position.X += currentGamePadState.ThumbSticks.Left.X * playerMoveSpeed;
Position.Y -= currentGamePadState.ThumbSticks.Left.Y * playerMoveSpeed;

Now, back in the Update method add the following lines to listen to the gamepad. Enter them just before we call UpdatePlayer:

// Save the previous state of the  game pad so we can determine single movement
previousGamePadState = currentGamePadState;;

// Read the current state of the gamepad and store it
currentGamePadState = GamePad.GetState(PlayerIndex.One);

Adding Touch Control

With in the Game1.cs file, find the the Game1 class and find the Initialize method within the Game1.cs file. You need to add the line below to set the FreeDrag gesture:

//Enable the FreeDrag gesture.
TouchPanel.EnabledGestures = GestureType.FreeDrag;

The Update method within the Game1.cs file is used while the game is running. In order to make this easy to understand and read, we will create a new method to update the player and call it from the Update method. If you haven't already created the UpdatePlayer method, create the UpdatePlayer method and call it from Update, see below:

Update player.png

Reading the GamePad and Move

Now in the UpdatePlayer method add the code to move the player based upon the FreeDrag gesture:

// Windows 8 Touch Gestures for MonoGame
while (TouchPanel.IsGestureAvailable)
{
    GestureSample gesture = TouchPanel.ReadGesture();
    if (gesture.GestureType == GestureType.FreeDrag)
    {
        Position += gesture.Delta;
    }
}

Other Touch Gestures

GestureType.Hold

GestureType.Tap 

GestureType.DoubleTap

GestureType.FreeDrag

GestureType.Flick

GestureType.Pinch

You can enable many gestures at the same time:

TouchPanel.EnabledGestures = gesture1 | gesture2 | gesture ... ;

You would need to add checks for the other enabled gestures, eg:

if (gesture.GestureType == GestureType.Tap)
{
    
}