PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/InputSystem/Tests/TouchTests.cs

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