PageRenderTime 38ms CodeModel.GetById 22ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/InputSystem/Tests/GamePadTests.cs

#
C# | 181 lines | 144 code | 11 blank | 26 comment | 3 complexity | bf6dc3fbe473d68835633dbdd5c64098 MD5 | raw file
  1using System;
  2using Delta.Engine;
  3using Delta.InputSystem.Devices;
  4using Delta.Rendering.Basics.Fonts;
  5using Delta.Utilities;
  6using Delta.Utilities.Datatypes;
  7using NUnit.Framework;
  8
  9namespace Delta.InputSystem.Tests
 10{
 11	/// <summary>
 12	/// Simple GamePad tests to make sure GamePads work and is optimized.
 13	/// </summary>
 14	internal class GamePadTests
 15	{
 16		#region CheckGamePadStatesAndPosition (Visual)
 17		/// <summary>
 18		/// Check GamePad states
 19		/// </summary>
 20		[Test, Category("Visual")]
 21		public static void CheckGamePadStatesAndPosition()
 22		{
 23			// We have to change the input module to Xna here because it's currently
 24			// the only input module (for Windows) which supports a GamePad
 25
 26			// This test is indented to be tested with Xna Input.
 27			Assert.Equal(Settings.Modules.InputModule, "Xna");
 28			BaseGamePad pad = Input.GamePad;
 29
 30			Font font = Font.Default;
 31			string newLine = Environment.NewLine;
 32			Application.Start(delegate
 33			{
 34				// Connection test
 35				font.Draw("GamePad IsConnected" + newLine + pad.IsConnected,
 36					Rectangle.FromCenter(new Point(0.5f, 0.2f), Size.Half));
 37
 38				// Digital Pad
 39				font.Draw("Digital Pad",
 40					Rectangle.FromCenter(new Point(0.2f, 0.4f), Size.Half));
 41				font.Draw(
 42					"Up" + newLine + pad.GetState(InputButton.GamePadUp),
 43					Rectangle.FromCenter(new Point(0.2f, 0.45f), Size.Half));
 44				font.Draw(
 45					"Left" + newLine + pad.GetState(InputButton.GamePadLeft),
 46					Rectangle.FromCenter(new Point(0.15f, 0.5f), Size.Half));
 47				font.Draw(
 48					"Right" + newLine + pad.GetState(InputButton.GamePadRight),
 49					Rectangle.FromCenter(new Point(0.25f, 0.5f), Size.Half));
 50				font.Draw(
 51					"Down" + newLine + pad.GetState(InputButton.GamePadDown),
 52					Rectangle.FromCenter(new Point(0.2f, 0.55f), Size.Half));
 53
 54				// Basic Buttons
 55				font.Draw("Buttons",
 56					Rectangle.FromCenter(new Point(0.8f, 0.4f), Size.Half));
 57				font.Draw(
 58					"Y" + newLine + pad.GetState(InputButton.GamePadY),
 59					Rectangle.FromCenter(new Point(0.8f, 0.45f), Size.Half));
 60				font.Draw(
 61					"X" + newLine + pad.GetState(InputButton.GamePadX),
 62					Rectangle.FromCenter(new Point(0.75f, 0.5f), Size.Half));
 63				font.Draw(
 64					"B" + newLine + pad.GetState(InputButton.GamePadB),
 65					Rectangle.FromCenter(new Point(0.85f, 0.5f), Size.Half));
 66				font.Draw(
 67					"A" + newLine + pad.GetState(InputButton.GamePadA),
 68					Rectangle.FromCenter(new Point(0.8f, 0.55f), Size.Half));
 69
 70				// Shoulder buttons
 71				font.Draw(
 72					"Left Trigger" + newLine +
 73					pad.GetState(InputButton.GamePadLeftTrigger),
 74					Rectangle.FromCenter(new Point(0.3f, 0.275f), Size.Half));
 75				font.Draw(
 76					"Left Shoulder" + newLine +
 77					pad.GetState(InputButton.GamePadLeftShoulder),
 78					Rectangle.FromCenter(new Point(0.3f, 0.325f), Size.Half));
 79				font.Draw(
 80					"Right Trigger" + newLine +
 81					pad.GetState(InputButton.GamePadRightTrigger),
 82					Rectangle.FromCenter(new Point(0.7f, 0.275f), Size.Half));
 83				font.Draw(
 84					"Right Shoulder" + newLine +
 85					pad.GetState(InputButton.GamePadRightShoulder),
 86					Rectangle.FromCenter(new Point(0.7f, 0.325f), Size.Half));
 87
 88				// Player buttons
 89				font.Draw(
 90					"Back/Select" + newLine + pad.GetState(InputButton.GamePadBack),
 91					Rectangle.FromCenter(new Point(0.4f, 0.425f), Size.Half));
 92				//font.DrawCentered("Home" + newLine +
 93				//  pad.GetState(InputButton.GamePadHome),
 94				//  Rectangle.FromCenter(new Point(0.35f, 0.5f), Size.Half));
 95				font.Draw(
 96					"Start" + newLine + pad.GetState(InputButton.GamePadStart),
 97					Rectangle.FromCenter(new Point(0.6f, 0.425f), Size.Half));
 98
 99				// Thumb sticks
100				font.Draw(
101					"Left stick" + newLine + pad.LeftThumbStick + newLine +
102					pad.GetState(InputButton.GamePadLeftStick),
103					Rectangle.FromCenter(new Point(0.4f, 0.6f), Size.Half));
104				font.Draw(
105					"Right stick" + newLine + pad.RightThumbStick + newLine +
106					pad.GetState(InputButton.GamePadRightStick),
107					Rectangle.FromCenter(new Point(0.6f, 0.6f), Size.Half));
108			});
109
110			// After the test switch to the original set input module again
111		}
112		#endregion
113
114		#region CheckMultipleGamePads (Visual)
115		/// <summary>
116		/// Test multiple GamePads connected (up to 4)!
117		/// </summary>
118		[Test, Category("Visual")]
119		public static void CheckMultipleGamePads()
120		{
121			Application.Start(delegate
122			{
123				Font.Default.Draw(
124					"GamePad 1 IsConnected=" + Input.GamePads(0).IsConnected,
125					Rectangle.FromCenter(new Point(0.5f, 0.4f), Size.Half));
126				Font.Default.Draw(
127					"GamePad 1 A Button=" +
128					Input.GamePads(0).GetState(InputButton.GamePadA),
129					Rectangle.FromCenter(new Point(0.5f, 0.45f), Size.Half));
130				Font.Default.Draw(
131					"GamePad 2 IsConnected=" + Input.GamePads(1).IsConnected,
132					Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
133				Font.Default.Draw(
134					"GamePad 2 A Button=" +
135					Input.GamePads(1).GetState(InputButton.GamePadA),
136					Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
137				Font.Default.Draw(
138					"GamePad 3 IsConnected=" + Input.GamePads(2).IsConnected,
139					Rectangle.FromCenter(new Point(0.5f, 0.6f), Size.Half));
140				Font.Default.Draw(
141					"GamePad 3 A Button=" +
142					Input.GamePads(2).GetState(InputButton.GamePadA),
143					Rectangle.FromCenter(new Point(0.5f, 0.65f), Size.Half));
144			});
145		}
146		#endregion
147
148		#region CheckRummble (Visual)
149		/// <summary>
150		/// Check if the device rumbles and stops rumbling if certain keys are 
151		/// pressed
152		/// </summary>
153		[Test, Category("Visual")]
154		public static void CheckRummble()
155		{
156			Application.Start(delegate
157			{
158				if (Input.GamePad.IsConnected)
159				{
160					Font.Default.Draw(
161						"Press 'A' to Rumble and release and 'B' to stop rumbling",
162						Rectangle.FromCenter(new Point(0.5f, 0.45f), Size.Half));
163					if (Input.GamePad.IsPressed(InputButton.GamePadA))
164					{
165						Input.GamePad.Rumble(5.0f, 5.0f);
166					}
167					if (Input.GamePad.IsPressed(InputButton.GamePadB))
168					{
169						Input.GamePad.Rumble(false);
170					}
171				}
172				else
173				{
174					Font.Default.Draw("Pad is not connected!",
175						Rectangle.FromCenter(new Point(0.5f, 0.45f), Size.Half));
176				}
177			});
178		}
179		#endregion
180	}
181}