PageRenderTime 32ms CodeModel.GetById 11ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/InputSystem/Tests/TouchTests.cs

#
C# | 340 lines | 249 code | 24 blank | 67 comment | 15 complexity | 8255d49a6a017da78105e7d199f5efa3 MD5 | raw file
  1using System.Collections.Generic;
  2using Delta.Engine;
  3using Delta.InputSystem.Devices;
  4using Delta.Rendering.Basics.Drawing;
  5using Delta.Rendering.Basics.Fonts;
  6using Delta.Utilities.Datatypes;
  7using Delta.Utilities.Helpers;
  8using NUnit.Framework;
  9
 10namespace Delta.InputSystem.Tests
 11{
 12	/// <summary>
 13	/// Touch tests
 14	/// </summary>
 15	internal class TouchTests
 16	{
 17		#region Helpers
 18		/// <summary>
 19		/// Touch line helper to draw lines with touches.
 20		/// </summary>
 21		private class TouchLine
 22		{
 23			#region id (Public)
 24			/// <summary>
 25			/// Touch id
 26			/// </summary>
 27			public readonly int id;
 28			#endregion
 29
 30			#region state (Public)
 31			/// <summary>
 32			/// Input state to simplify some checking.
 33			/// </summary>
 34			public InputState state;
 35			#endregion
 36
 37			#region start (Public)
 38			/// <summary>
 39			/// Start and end point of the line (end point is current position,
 40			/// start is the drag start position).
 41			/// </summary>
 42			public readonly Point start;
 43			#endregion
 44
 45			#region end (Public)
 46			/// <summary>
 47			/// Start and end point of the line (end point is current position,
 48			/// start is the drag start position).
 49			/// </summary>
 50			public Point end;
 51			#endregion
 52
 53			#region color (Public)
 54			/// <summary>
 55			/// Color of this line, randomly set.
 56			/// </summary>
 57			public Color color;
 58			#endregion
 59
 60			#region time (Public)
 61			/// <summary>
 62			/// Time, will always be 1 if touch is pressed, will go down to 0
 63			/// in one second when touch is released (then we remove this line).
 64			/// </summary>
 65			public float time;
 66			#endregion
 67
 68			#region Constructors
 69			/// <summary>
 70			/// Create touch line
 71			/// </summary>
 72			public TouchLine(TouchData touchData)
 73			{
 74				id = touchData.Id;
 75				start = touchData.Position;
 76				end = touchData.Position;
 77				//not colorful enough: color = Color.Random;
 78				color = Color.SolidColors[
 79					RandomHelper.RandomInt(Color.SolidColors.Length)];
 80				time = 1;
 81			}
 82			#endregion
 83		}
 84		#endregion
 85
 86		#region CheckTouchData (Visual)
 87		/// <summary>
 88		/// Check touch states
 89		/// </summary>
 90		[Test, Category("Visual")]
 91		public static void CheckTouchData()
 92		{
 93			// Testing is easier with 10 fps
 94			Settings.Extra.LimitFramerateNumber = 10;
 95
 96			Application.Start(delegate
 97			{
 98				Font testFont = Font.Default;
 99
100				BaseTouch touch = Input.Touch;
101				testFont.Draw("Touch IsConnected " + touch.IsConnected,
102					Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
103				testFont.Draw("Touch Pressed " + touch.TouchIsPressed,
104					Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
105				testFont.Draw("Touch Position " + touch.Position,
106					Rectangle.FromCenter(new Point(0.5f, 0.6f), Size.Half));
107				testFont.Draw("Active Touches " + touch.ActiveTouches,
108					Rectangle.FromCenter(new Point(0.5f, 0.65f), Size.Half));
109
110				testFont.Draw("Touches[0]=" + touch.Touches[0].State,
111					Rectangle.FromCenter(new Point(0.5f, 0.7f), Size.Half));
112				testFont.Draw("Touches[1]=" + touch.Touches[1].State,
113					Rectangle.FromCenter(new Point(0.5f, 0.75f), Size.Half));
114			});
115		}
116		#endregion
117
118		#region AllTouchInputButtons (Visual)
119		/// <summary>
120		/// Test all touch input buttons, will display all the input button states
121		/// we got for touch input (including normal touches and gestures).
122		/// </summary>
123		[Test, Category("Visual")]
124		public static void AllTouchInputButtons()
125		{
126			// Testing is easier with 10 fps
127			Settings.Extra.LimitFramerateNumber = 10;
128			const float lineDistance = 0.03f;
129			Application.Start(delegate
130			{
131				float yPos = 0.3f;
132				for (int buttonIndex = (int)InputButton.TouchPress;
133					buttonIndex <= (int)InputButton.TouchDeviceShake;
134					buttonIndex++)
135				{
136					Font.Default.Draw(
137						(InputButton)buttonIndex + ": " +
138						Input.GetState((InputButton)buttonIndex),
139						Rectangle.FromCenter(new Point(0.5f, yPos), Size.Half));
140					yPos += lineDistance;
141				}
142
143				// Some additional info
144				BaseTouch touch = Input.Touch;
145				yPos += lineDistance / 2.0f;
146				Font.Default.Draw("Touch Pressed " + touch.TouchIsPressed,
147					Rectangle.FromCenter(new Point(0.5f, yPos), Size.Half));
148				yPos += lineDistance;
149				Font.Default.Draw("Touch Position " + touch.Position,
150					Rectangle.FromCenter(new Point(0.5f, yPos), Size.Half));
151				yPos += lineDistance;
152				Font.Default.Draw("Active Touches " + touch.ActiveTouches,
153					Rectangle.FromCenter(new Point(0.5f, yPos), Size.Half));
154			});
155		}
156		#endregion
157
158		#region TouchLineDrawing (Visual)
159		/// <summary>
160		/// Test touch line drawing
161		/// </summary>
162		[Test, Category("Visual")]
163		public static void TouchLineDrawing()
164		{
165			List<TouchLine> lines = new List<TouchLine>();
166			Application.Start(delegate
167			{
168				// When a touch is pressed, add a new line
169				for (int index = 0; index < Input.Touch.ActiveTouches; index++)
170				{
171					if (Input.Touch.Touches[index].State == InputState.Pressed)
172					{
173						lines.Add(new TouchLine(Input.Touch.Touches[index]));
174					}
175					else if (lines.Count > 0)
176					{
177						// Otherwise update position of end, search for the touchline by id
178						for (int i = lines.Count - 1; i >= 0; i--)
179						{
180							if (lines[i].id == Input.Touch.Touches[index].Id) // &&
181								//lines[i].state == InputState.Pressed)
182							{
183								lines[i].state = Input.Touch.Touches[index].State;
184								lines[i].end = Input.Touch.Touches[index].Position;
185								lines[i].time = 1;
186								break;
187							}
188						}
189					} // else
190				} // for
191
192				// Draw box for fun
193				float pixelSize = ScreenSpace.ToQuadraticSpace(Point.One).X;
194				Point oneDown = new Point(0, pixelSize);
195				Point oneRight = new Point(pixelSize, 0);
196				Rect.DrawOutline(
197					ScreenSpace.DrawArea.Shrink(pixelSize), Color.Red);
198				//	"lines=" + lines.Count);
199
200				// Draw all touch lines
201				List<TouchLine> remToKillLines = new List<TouchLine>();
202				foreach (TouchLine line in lines)
203				{
204					// Fake bold trick ^^
205					Line.Draw(line.start, line.end, line.color);
206					Line.Draw(line.start + oneDown,
207						line.end + oneDown, line.color);
208					Line.Draw(line.start + oneRight,
209						line.end + oneRight, line.color);
210					//	"line=" + line.start + ", " + line.end + ", " + line.color);
211
212					// Decrease time of each touch line and kill faded out lines
213					if (line.state < InputState.Pressed)
214					{
215						line.time -= Time.Delta / 4.0f;
216						line.color.A = line.time;
217						if (line.time <= 0)
218						{
219							remToKillLines.Add(line);
220						}
221					} // if
222				} // foreach
223
224				// Do the delayed killing (we could not modify the lines list above)
225				foreach (TouchLine line in remToKillLines)
226				{
227					lines.Remove(line);
228				}
229			});
230		}
231		#endregion
232
233		#region MultiTouch (Visual)
234		/// <summary>
235		/// Testing multi touch
236		/// </summary>
237		[Test, Category("Visual")]
238		public static void MultiTouch()
239		{
240			// Init two fixed arrays which are corresponding for each touch-start
241			// and touch-end
242			int maxTouchCount = 10;
243			Point[] touchStartPoints = new Point[maxTouchCount];
244			Point[] touchEndPoints = new Point[maxTouchCount];
245			// and use a (random) color fixed to each touch (id)
246			Color[] drawColor = new Color[maxTouchCount];
247			for (int i = 0; i < maxTouchCount; i++)
248			{
249				drawColor[i] = Color.Random;
250			}
251
252			Application.BackgroundColor = Color.DarkBlue;
253			Application.Start(delegate
254			{
255				Rect.DrawOutline(ScreenSpace.DrawArea, Color.Red);
256				int index = 0;
257				for (; index < Input.Touch.ActiveTouches; index++)
258				{
259					TouchData touch = Input.Touch.Touches[index];
260					switch (touch.State)
261					{
262						case InputState.IsPressed:
263							touchEndPoints[index] = touch.Position;
264							break;
265						case InputState.Pressed:
266							touchStartPoints[index] = touch.Position;
267							touchEndPoints[index] = touch.Position;
268							break;
269						case InputState.Released:
270							touchEndPoints[index] = touch.Position;
271							break;
272					}
273
274					// Draw the graphical line from (remembered) start-point until the
275					// current touch-point
276					Line.Draw(touchStartPoints[index], touchEndPoints[index],
277						drawColor[index]);
278
279					// and show the name of the current action at the touch position
280					Font.Default.Draw("'" + touch.Id + "' does '" + touch.State + "'",
281						Rectangle.FromCenter(touch.Position, Size.Half));
282				} // for
283
284				for (; index < maxTouchCount; index++)
285				{
286					touchStartPoints[index] = Point.Zero;
287					touchEndPoints[index] = Point.Zero;
288				}
289			});
290		}
291		#endregion
292
293		#region TouchGestures (Visual)
294		/// <summary>
295		/// Test all Touch gestures, TouchTap, TouchDoubleTap, TouchHold,
296		/// TouchDrag, TouchFlick, and TouchPinchs
297		/// </summary>
298		[Test, Category("Visual")]
299		public static void TouchGestures()
300		{
301			Font infoFont = Font.Default;
302			Application.Start(delegate
303			{
304				infoFont.Draw(
305					"TouchTap " + Input.Touch.GetState(InputButton.GestureTap),
306					Rectangle.FromCenter(new Point(0.5f, 0.2f), Size.Half));
307				infoFont.Draw(
308					"TouchDoubleTap " +
309					Input.Touch.GetState(InputButton.GestureDoubleTap),
310					Rectangle.FromCenter(new Point(0.5f, 0.24f), Size.Half));
311				infoFont.Draw(
312					"TouchHold " + Input.Touch.GetState(InputButton.GestureHold),
313					Rectangle.FromCenter(new Point(0.5f, 0.28f), Size.Half));
314				infoFont.Draw(
315					"TouchDrag " + Input.Touch.GetState(InputButton.GestureDrag),
316					Rectangle.FromCenter(new Point(0.5f, 0.32f), Size.Half));
317				infoFont.Draw(
318					"Drag " + Input.Touch.GetMovement(InputButton.GestureDrag),
319					Rectangle.FromCenter(new Point(0.5f, 0.36f), Size.Half));
320				infoFont.Draw(
321					"TouchFlick " + Input.Touch.GetState(InputButton.GestureFlick),
322					Rectangle.FromCenter(new Point(0.5f, 0.4f), Size.Half));
323				infoFont.Draw(
324					"Flick " + Input.Touch.GetMovement(InputButton.GestureFlick),
325					Rectangle.FromCenter(new Point(0.5f, 0.44f), Size.Half));
326				infoFont.Draw(
327					"TouchDoubleDrag " +
328					Input.Touch.GetState(InputButton.GestureDualDrag),
329					Rectangle.FromCenter(new Point(0.5f, 0.48f), Size.Half));
330				infoFont.Draw(
331					"DoubleDrag " + Input.Touch.GetMovement(InputButton.GestureDualDrag),
332					Rectangle.FromCenter(new Point(0.5f, 0.52f), Size.Half));
333				infoFont.Draw(
334					"Pinch " + Input.Touch.GetMovement(InputButton.GesturePinch),
335					Rectangle.FromCenter(new Point(0.5f, 0.56f), Size.Half));
336			});
337		}
338		#endregion
339	}
340}