PageRenderTime 283ms CodeModel.GetById 114ms app.highlight 6ms RepoModel.GetById 155ms app.codeStats 0ms

/InputSystem/Tests/AccelerometerTests.cs

#
C# | 95 lines | 75 code | 6 blank | 14 comment | 4 complexity | d2a4a4d818ac40d1b1c153caa39c8df2 MD5 | raw file
 1using Delta.Engine;
 2using Delta.Rendering.Basics.Drawing;
 3using Delta.Rendering.Basics.Fonts;
 4using Delta.Rendering.Basics.Materials;
 5using Delta.Utilities.Datatypes;
 6using NUnit.Framework;
 7
 8namespace Delta.InputSystem.Tests
 9{
10	/// <summary>
11	/// Accelerometer tests
12	/// </summary>
13	internal class AccelerometerTests
14	{
15		#region CheckAccelerometerStates
16		/// <summary>
17		/// Check accelerometer states
18		/// </summary>
19		[Test, Category("Visual")]
20		public static void CheckAccelerometerStates()
21		{
22			Input.IgnoreDeviceOrientation = true;
23			Application.Start(delegate
24			{
25				Font.Default.Draw(
26					"Accelerometer IsConnected " + Input.Accelerometer.IsConnected,
27					Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
28				Font.Default.Draw(
29					"Accelerometer Rotation " + Input.Accelerometer.Rotation,
30					Rectangle.FromCenter(new Point(0.5f, 0.6f), Size.Half));
31				Font.Default.Draw(
32					"Accelerometer X " + Input.Accelerometer.Acceleration.X,
33					Rectangle.FromCenter(new Point(0.5f, 0.65f), Size.Half));
34				Font.Default.Draw(
35					"Accelerometer Y " + Input.Accelerometer.Acceleration.Y,
36					Rectangle.FromCenter(new Point(0.5f, 0.7f), Size.Half));
37				Font.Default.Draw(
38					"Accelerometer Z " + Input.Accelerometer.Acceleration.Z,
39					Rectangle.FromCenter(new Point(0.5f, 0.75f), Size.Half));
40
41				// Draw some lines for the accelerometer
42				Line.Draw(new Point(0.3f, 0.4f), new Point(
43					0.2f + Input.Accelerometer.Acceleration.X * 0.6f, 0.4f),
44					Color.Red);
45				Line.Draw(new Point(0.3f, 0.4f), new Point(0.3f,
46					0.2f + Input.Accelerometer.Acceleration.Y * 0.6f),
47					Color.Yellow);
48
49				// Also draw a line for the rotation
50				Point rotatedLine =
51					new Point(0.2f, 0).Rotate(Input.Accelerometer.Rotation);
52				Line.Draw(Point.Half - rotatedLine, Point.Half + rotatedLine,
53					Color.Green);
54			});
55		}
56		#endregion
57
58		#region AccelerometerMiniGame
59		/// <summary>
60		/// Accelerometer mini game
61		/// </summary>
62		[Test, Category("Visual")]
63		public static void AccelerometerMiniGame()
64		{
65			Input.IgnoreDeviceOrientation = true;
66			Material2D playerMaterial = Material2D.Default;
67			Point position = Point.Half;
68			Point velocity;
69			Size playerSize = new Size(0.05f);
70			Application.Start(delegate
71			{
72				// input
73				Vector accInput = Input.Accelerometer.Acceleration;
74				velocity = new Point(-accInput.Y, -accInput.X);
75
76				// movement
77				position += velocity * Time.Delta;
78				if (position.X < ScreenSpace.DrawArea.X)
79					position.X = ScreenSpace.DrawArea.X;
80				if (position.X > ScreenSpace.DrawArea.Right)
81					position.X = ScreenSpace.DrawArea.Right;
82				if (position.Y < ScreenSpace.DrawArea.Top)
83					position.Y = ScreenSpace.DrawArea.Top;
84				if (position.Y > ScreenSpace.DrawArea.Bottom)
85					position.Y = ScreenSpace.DrawArea.Bottom;
86
87				// draw
88				playerMaterial.Draw(new Rectangle(position.X - playerSize.WidthHalf,
89					position.Y - playerSize.HeightHalf, playerSize.Width,
90					playerSize.HeightHalf));
91			});
92		}
93		#endregion
94	}
95}