PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/InputSystem/Tests/Tutorials.cs

#
C# | 332 lines | 215 code | 30 blank | 87 comment | 9 complexity | 769a4b17a4fb6816d882930532ebb8dd MD5 | raw file
Possible License(s): Apache-2.0
  1. using Delta.Engine;
  2. using Delta.InputSystem.Devices;
  3. using Delta.Rendering.Basics.Drawing;
  4. using Delta.Rendering.Basics.Fonts;
  5. using Delta.Utilities.Datatypes;
  6. using NUnit.Framework;
  7. namespace Delta.InputSystem.Tests
  8. {
  9. /// <summary>
  10. /// Contains the tutorials for the InputSystem each with the description,
  11. /// level, Image and file Paths.
  12. /// </summary>
  13. internal class Tutorials
  14. {
  15. #region KeyboardStatus (Static)
  16. /// <summary>
  17. /// Tutorial: Input System Tutorial 1: Check Keyboard States
  18. /// Difficulty: Easy
  19. /// Description: Check the keyboard status (i.e. is it connected or not)
  20. /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial1.png
  21. /// </summary>
  22. [Test, Category("Visual")]
  23. public static void KeyboardStatus()
  24. {
  25. // Start our application here.
  26. Application.Start(delegate
  27. {
  28. // Display messages in the screen
  29. Font infoFont = Font.Default;
  30. infoFont.Draw("Green point is 'Active', red is 'Inactive'",
  31. Rectangle.FromCenter(new Point(0.45f, 0.35f), Size.Half));
  32. infoFont.Draw("Keyboard is 'Connected'",
  33. Rectangle.FromCenter(new Point(0.45f, 0.45f), Size.Half));
  34. infoFont.Draw("'A' is 'Pressed'",
  35. Rectangle.FromCenter(new Point(0.45f, 0.5f), Size.Half));
  36. infoFont.Draw("'Shift' key is 'Pressed'",
  37. Rectangle.FromCenter(new Point(0.45f, 0.55f), Size.Half));
  38. infoFont.Draw("Right 'Alt' key is 'Pressed'",
  39. Rectangle.FromCenter(new Point(0.45f, 0.6f), Size.Half));
  40. // Now we can draw Green points in the same spot of the red points
  41. // if status is changes to active.
  42. BaseKeyboard keyboard = Input.Keyboard;
  43. // Check if Keyboard is Connected
  44. Circle.DrawFilled(new Point(0.6f, 0.45f), 0.015f,
  45. (keyboard.IsConnected)
  46. ? Color.Green
  47. : Color.Red);
  48. // Is the 'A' key is pressed
  49. Circle.DrawFilled(new Point(0.6f, 0.5f), 0.015f,
  50. keyboard.IsPressed(InputButton.A)
  51. ? Color.Green
  52. : Color.Red);
  53. // Is the 'Shift' key pressed
  54. Circle.DrawFilled(new Point(0.6f, 0.55f), 0.015f,
  55. keyboard.IsPressed(InputButton.Shift)
  56. ? Color.Green
  57. : Color.Red);
  58. // Is The right Alt Key Pressed
  59. Circle.DrawFilled(new Point(0.6f, 0.6f), 0.015f,
  60. keyboard.IsPressed(InputButton.RightAlt)
  61. ? Color.Green
  62. : Color.Red);
  63. });
  64. }
  65. #endregion
  66. #region MouseStatus (Static)
  67. /// <summary>
  68. /// Tutorial: Input System Tutorial 2: Check Mouse States
  69. /// Difficulty: Easy
  70. /// Description: Check mouse status (i.e. Connected or not, any button
  71. /// pressed, etc)
  72. /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial2.png
  73. /// </summary>
  74. [Test, Category("Visual")]
  75. public static void MouseStatus()
  76. {
  77. // Flag to check if double click or a mouse move happened.
  78. bool isDoubleClick = false;
  79. bool isMoving = false;
  80. // We need a time stamp to allow viewing the green color when the double
  81. // click occurs.
  82. long clickTimeStamp = 0;
  83. long moveTimeStamp = 0;
  84. // Start our application here.
  85. Application.Start(delegate
  86. {
  87. // Display messages in the screen
  88. Font infoFont = Font.Default;
  89. infoFont.Draw("Green point is 'Active', red is 'Inactive'",
  90. Rectangle.FromCenter(new Point(0.45f, 0.35f), Size.Half));
  91. infoFont.Draw("Mouse Connected",
  92. Rectangle.FromCenter(new Point(0.45f, 0.45f), Size.Half));
  93. infoFont.Draw("Mouse Click",
  94. Rectangle.FromCenter(new Point(0.45f, 0.5f), Size.Half));
  95. infoFont.Draw("Mouse Move",
  96. Rectangle.FromCenter(new Point(0.45f, 0.55f), Size.Half));
  97. infoFont.Draw("Mouse DoubleClick",
  98. Rectangle.FromCenter(new Point(0.45f, 0.6f), Size.Half));
  99. // Now we can draw green points in the same spot of the red points
  100. // if status is changes to active.
  101. BaseMouse mouse = Input.Mouse;
  102. // Check if Mouse is Connected
  103. Circle.DrawFilled(new Point(0.6f, 0.45f), 0.015f,
  104. (mouse.IsConnected)
  105. ? Color.Green
  106. : Color.Red);
  107. // Is the left mouse button pressed?
  108. if (Input.Mouse.IsPressed(InputButton.MouseLeft))
  109. {
  110. Circle.DrawFilled(new Point(0.6f, 0.5f), 0.015f, Color.Green);
  111. isDoubleClick = false;
  112. }
  113. else
  114. {
  115. Circle.DrawFilled(new Point(0.6f, 0.5f), 0.015f, Color.Red);
  116. }
  117. // Mouse is moving?
  118. if (Input.Mouse.GetState(InputButton.MouseMove) == InputState.Pressed)
  119. {
  120. isMoving = true;
  121. moveTimeStamp = Time.Milliseconds;
  122. }
  123. Circle.DrawFilled(new Point(0.6f, 0.55f), 0.015f,
  124. isMoving
  125. ? Color.Green
  126. : Color.Red);
  127. // Check if a double click happened.
  128. if (Input.Gestures.IsDoubleTap)
  129. {
  130. isDoubleClick = true;
  131. clickTimeStamp = Time.Milliseconds;
  132. }
  133. Circle.DrawFilled(new Point(0.6f, 0.6f), 0.015f,
  134. isDoubleClick
  135. ? Color.Green
  136. : Color.Red);
  137. // We need this to reset the green flag when the double click or a move
  138. // occurs after few milliseconds.
  139. if (isDoubleClick &&
  140. Time.Milliseconds - clickTimeStamp > 300)
  141. {
  142. isDoubleClick = false;
  143. }
  144. if (isMoving &&
  145. (Time.Milliseconds - moveTimeStamp) > 100)
  146. {
  147. isMoving = false;
  148. }
  149. });
  150. }
  151. #endregion
  152. #region ClickCommand (Static)
  153. /// <summary>
  154. /// Tutorial: Input System Tutorial 3: Command Click
  155. /// Difficulty: Medium
  156. /// Description: Make an action when a mouse click event is performed.
  157. /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial3.png
  158. /// </summary>
  159. [Test, Category("Visual")]
  160. public static void ClickCommand()
  161. {
  162. // First: Define our Command trigger. The UIClick command will fire if a
  163. // mouse click or a touch event happened. A click represents a press and
  164. // release for a mouse or a tap with a touch device.
  165. Input.Commands[Command.UIClick].Add(delegate
  166. {
  167. // Whenever a click happens, change a back ground color by choosing a
  168. // random color.
  169. Application.BackgroundColor = Color.Random;
  170. });
  171. // Start the application
  172. Application.Start(delegate
  173. {
  174. Font.Default.Draw(
  175. "Click with a mouse, or perform a touch (In case of a touch " +
  176. "screen) to test the 'Click' command",
  177. Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
  178. });
  179. }
  180. #endregion
  181. #region DragCommand (Static)
  182. /// <summary>
  183. /// Tutorial: Input System Tutorial 4: Command Dragging
  184. /// Difficulty: Medium
  185. /// Description: Draw a line by using a mouse drag between two points
  186. /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial4.png
  187. /// </summary>
  188. [Test, Category("Visual")]
  189. public static void DragCommand()
  190. {
  191. // Flags we need for out tutorial.
  192. bool showDragCircle = false;
  193. // Start and End points of our drag
  194. Point startPoint = Point.Zero;
  195. Point endPoint = Point.Zero;
  196. // Define a command to store our first point of dragging, this point will
  197. // be the first point of drawing our circle.
  198. Input.Commands[Command.UIDragBegin].Add(delegate(CommandTrigger input)
  199. {
  200. // Define the drag starting point.
  201. startPoint = input.DragStart;
  202. showDragCircle = false;
  203. });
  204. // As we drag move, we need to keep drawing our circle as a feedback of
  205. // our 'DragMove' action
  206. Input.Commands[Command.UIDragMove].Add(delegate(CommandTrigger input)
  207. {
  208. // Get the end point where the circle extends and draw it on the screen
  209. endPoint = input.Position;
  210. showDragCircle = true;
  211. });
  212. // Start the application
  213. Application.Start(delegate
  214. {
  215. // Change the background color for a better presentation.
  216. Application.BackgroundColor = Color.Black;
  217. // Display a message on a screen
  218. Font.Default.Draw(
  219. "In this tutorial we will use the 'Drag' command to draw a " +
  220. "circle as far as we are dragging.",
  221. Rectangle.FromCenter(new Point(0.5f, 0.15f), Size.Half));
  222. // If we are dragging, draw the circle.
  223. if (showDragCircle)
  224. {
  225. // Get the distance between the two points
  226. float distance = endPoint.Length - startPoint.Length;
  227. // Draw the circle.
  228. Circle.DrawFilled(startPoint, distance, Color.White);
  229. }
  230. });
  231. }
  232. #endregion
  233. #region TriggersIntervals (Static)
  234. /// <summary>
  235. /// Tutorial: Input System Tutorial 5: Command Click Intervals
  236. /// Difficulty: Advanced
  237. /// Description: Make an action when a mouse click event is performed.
  238. /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial5.png
  239. /// </summary>
  240. [Test, Category("Visual")]
  241. public static void TriggersIntervals()
  242. {
  243. // Define flags for status
  244. // for testing pressing a key
  245. bool pressed = false;
  246. // and for testing a key pressed for a time interval
  247. bool isPressed = false;
  248. // Define a command that doesn't exist yet.
  249. const string CommandName = "IntervalCheckCommand";
  250. // Define the Command triggers
  251. Input.Commands[CommandName].AddTrigger(new CommandTrigger
  252. {
  253. Button = InputButton.Enter,
  254. State = InputState.Released,
  255. });
  256. // Add another trigger only if the key you pressed previously has been
  257. // Pressed again for over 1 second.
  258. Input.Commands[CommandName].AddTrigger(new CommandTrigger
  259. {
  260. Button = InputButton.Enter,
  261. State = InputState.IsPressed,
  262. Interval = 1f,
  263. });
  264. // Define a delegate for the command. A reaction that will happen if
  265. // the command is triggered.
  266. Input.Commands[CommandName].Add(delegate(CommandTrigger trigger)
  267. {
  268. // Here we define te command which is triggered only when the time
  269. // interval is 1 second.
  270. if (trigger.Interval == 1.0f)
  271. {
  272. isPressed = true;
  273. pressed = false;
  274. }
  275. else
  276. {
  277. pressed = true;
  278. isPressed = false;
  279. }
  280. });
  281. Application.Start(delegate
  282. {
  283. Font infoFont = Font.Default;
  284. infoFont.Draw("Green point is 'Active', red is 'Inactive' ",
  285. Rectangle.FromCenter(new Point(0.45f, 0.35f), Size.Half));
  286. infoFont.Draw("Press 'Enter' key to Activate",
  287. Rectangle.FromCenter(new Point(0.45f, 0.45f), Size.Half));
  288. infoFont.Draw("Press 'Enter' for 1 second to Activate",
  289. Rectangle.FromCenter(new Point(0.45f, 0.5f), Size.Half));
  290. Circle.DrawFilled(new Point(0.6f, 0.45f), 0.015f,
  291. pressed
  292. ? Color.Green
  293. : Color.Red);
  294. Circle.DrawFilled(new Point(0.6f, 0.5f), 0.015f,
  295. isPressed
  296. ? Color.Green
  297. : Color.Red);
  298. });
  299. }
  300. #endregion
  301. }
  302. }