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

/InputSystem/Tests/CommandTests.cs

#
C# | 624 lines | 512 code | 47 blank | 65 comment | 24 complexity | 68fa39f0ab991f224f0a40a7d53bf308 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using Delta.Engine;
  3. using Delta.Utilities;
  4. using Delta.Utilities.Datatypes;
  5. using Delta.Rendering.Basics.Fonts;
  6. using Delta.Rendering.Basics.Drawing;
  7. using Delta.Utilities.Helpers;
  8. using NUnit.Framework;
  9. namespace Delta.InputSystem.Tests
  10. {
  11. /// <summary>
  12. /// Command Tests
  13. /// </summary>
  14. internal class CommandTests
  15. {
  16. #region EnableCommand
  17. /// <summary>
  18. /// Enable command
  19. /// </summary>
  20. [Test, Category("Visual")]
  21. public static void EnableCommand()
  22. {
  23. const string commandName = "Help";
  24. InputButton pressedKey = InputButton.None;
  25. int pressedKeyTimeStamp = 0;
  26. // Define now the conditions which will trigger the command
  27. Input.Commands[commandName].AddTrigger(new CommandTrigger
  28. {
  29. Button = InputButton.F1,
  30. State = InputState.Released
  31. });
  32. Input.Commands[commandName].AddTrigger(new CommandTrigger
  33. {
  34. Button = InputButton.Tab,
  35. State = InputState.Released
  36. });
  37. // Define the new command and the delegate which should be executed if
  38. // the command gets triggered by the input devices
  39. // Note: The command will be automatically created by a new name
  40. Input.Commands[commandName].Add(delegate(CommandTrigger trigger)
  41. {
  42. pressedKey = trigger.Button;
  43. pressedKeyTimeStamp = Time.Seconds;
  44. });
  45. // Last start the application
  46. Application.Start(delegate
  47. {
  48. // and show which key has triggered the command
  49. if (pressedKey != InputButton.None)
  50. {
  51. Font.Default.Draw(
  52. commandName + " command triggered by '" + pressedKey + "'",
  53. ScreenSpace.DrawArea);
  54. }
  55. else
  56. {
  57. Font.Default.Draw("Press the 'F1' key or the 'Tab' key.",
  58. ScreenSpace.DrawArea);
  59. }
  60. // but make a time out for this message of 1 second
  61. if ((Time.Seconds - pressedKeyTimeStamp) > 1)
  62. {
  63. pressedKey = InputButton.None;
  64. }
  65. });
  66. }
  67. #endregion
  68. #region DisableCommand
  69. /// <summary>
  70. /// Disable command
  71. /// </summary>
  72. [Test, Category("Visual")]
  73. public static void DisableCommand()
  74. {
  75. // Create the 'Action' and the 'Remove' command and define their triggers
  76. Input.Commands["Action"].AddTrigger(new CommandTrigger
  77. {
  78. Button = InputButton.MouseLeft,
  79. State = InputState.Released
  80. });
  81. Input.Commands["Remove"].AddTrigger(new CommandTrigger
  82. {
  83. Button = InputButton.Space,
  84. State = InputState.Released
  85. });
  86. // Our fictive owner of the command delegates we will attach by pressing
  87. // the 'Enter' key and on the application start
  88. string owner = "DelegateOwner";
  89. bool isFirstRun = true;
  90. Application.Start(delegate
  91. {
  92. if (isFirstRun ||
  93. Input.Keyboard.EnterReleased)
  94. {
  95. Input.Commands["Action"].Add(owner, delegate
  96. {
  97. Application.BackgroundColor = Color.Random;
  98. });
  99. Input.Commands["Remove"].Add(owner, delegate
  100. {
  101. Input.Commands.DetachAllDelegates(owner);
  102. });
  103. isFirstRun = false;
  104. } // if
  105. Font.Default.Draw(
  106. "Click the left mouse button to change the background color." +
  107. Environment.NewLine + "By pressing the 'Space' key the command " +
  108. "can be disabled and by pressing the 'Enter' it can be enabled " +
  109. "again.", ScreenSpace.DrawArea);
  110. });
  111. }
  112. #endregion
  113. #region MouseMoveCommand
  114. /// <summary>
  115. /// Testing UIPositionChange command, which is triggered for every mouse
  116. /// move (but also for touch position changes or keyboard or game pad
  117. /// virtual cursor changes).
  118. /// </summary>
  119. [Test, Category("Visual")]
  120. public static void MouseMoveCommand()
  121. {
  122. Input.Commands[Command.UIPositionChange].Add(
  123. delegate(CommandTrigger input)
  124. {
  125. Application.BackgroundColor = Color.Random;
  126. });
  127. Application.Start(delegate
  128. {
  129. Font.Default.Draw("Move mouse to test!", ScreenSpace.DrawArea);
  130. });
  131. }
  132. #endregion
  133. #region MouseWheelCommand
  134. /// <summary>
  135. /// Test if the mouse wheel works with the commands.
  136. /// </summary>
  137. [Test, Category("Visual")]
  138. public static void MouseWheelCommand()
  139. {
  140. const string MouseWheel = "MouseWheel";
  141. Input.Commands[MouseWheel].AddTrigger(new CommandTrigger
  142. {
  143. Button = InputButton.MouseScrollWheel,
  144. State = InputState.Released,
  145. });
  146. Point position = Point.Half;
  147. Application.Start(delegate
  148. {
  149. Application.BackgroundColor = Color.Black;
  150. Font.Default.Draw("Move mouse wheel to test!", ScreenSpace.DrawArea);
  151. // Add the delegate to the mouse wheel move.
  152. Input.Commands[MouseWheel].Add(delegate
  153. {
  154. position.Y = Input.Mouse.TotalScrollWheel / 20;
  155. // Clamp it to the screen otherwise it will run out of scope.
  156. position.Y = MathHelper.Clamp(position.Y, 0.35f,0.65f);
  157. });
  158. // Draw the cross line
  159. Line.DrawCross(position, 0.05f, Color.Red);
  160. });
  161. }
  162. #endregion
  163. #region CommandClickTests
  164. /// <summary>
  165. /// Command UIClick test
  166. /// </summary>
  167. [Test, Category("Visual")]
  168. public static void CommandClickTest()
  169. {
  170. CommandTrigger lastTrigger = null;
  171. Input.Commands[Command.UIClick].Add(delegate(CommandTrigger trigger)
  172. {
  173. Application.BackgroundColor = Color.Random;
  174. lastTrigger = trigger;
  175. });
  176. Application.Start(delegate
  177. {
  178. Font.Default.Draw(
  179. "Test UIClick Command (click or touch)!\nLast Trigger: " +
  180. (lastTrigger == null
  181. ? "none"
  182. : lastTrigger.ToString()),
  183. ScreenSpace.DrawArea);
  184. });
  185. }
  186. #endregion
  187. #region DraggingCommand
  188. /// <summary>
  189. /// Dragging command. Note: This only does one drag operation, use the
  190. /// TouchTests.TouchLineDrawing test for multiple lines and dragging.
  191. /// </summary>
  192. [Test, Category("Visual")]
  193. public static void DraggingCommand()
  194. {
  195. // Define our data
  196. Point startPoint = Point.Zero;
  197. Point endPoint = Point.Zero;
  198. // states we use to know what we have to draw in the test
  199. bool showStartPoint = false;
  200. bool showDragLine = false;
  201. bool showEndPoint = false;
  202. // Define now drag commands with the delegates
  203. Input.Commands[Command.UIDragBegin].Add(delegate(CommandTrigger input)
  204. {
  205. startPoint = input.DragStart;
  206. showStartPoint = true;
  207. showDragLine = false;
  208. showEndPoint = false;
  209. });
  210. Input.Commands[Command.UIDragMove].Add(delegate(CommandTrigger input)
  211. {
  212. endPoint = input.Position;
  213. showDragLine = true;
  214. });
  215. Input.Commands[Command.UIDragEnd].Add(delegate(CommandTrigger input)
  216. {
  217. endPoint = input.Position;
  218. showEndPoint = true;
  219. });
  220. // and finally start the application
  221. Application.Start(delegate
  222. {
  223. if (showStartPoint)
  224. {
  225. Line.DrawCross(startPoint, 0.01f, Color.Yellow);
  226. } // if
  227. if (showDragLine)
  228. {
  229. Line.Draw(startPoint, endPoint, Color.Red);
  230. } // if
  231. if (showEndPoint)
  232. {
  233. Line.DrawCross(endPoint, 0.01f, Color.Green);
  234. } // if
  235. if (showStartPoint == false &&
  236. showDragLine == false &&
  237. showEndPoint == false)
  238. {
  239. Font.Default.Draw(
  240. "Just click and drag the cursor on the screen (using Mouse, " +
  241. "Touch, GamePad or Keyboard).", ScreenSpace.DrawArea);
  242. } // if
  243. });
  244. }
  245. #endregion
  246. #region KeyboardCommandTests
  247. /// <summary>
  248. /// Keyboard Commands test
  249. /// </summary>
  250. [Test, Category("Visual")]
  251. public static void KeyboardCommandTests()
  252. {
  253. bool enterPressed = false;
  254. bool spacePressed = false;
  255. Font font = Font.Default;
  256. // Create new TestCommand with 2 triggers and one action.
  257. Input.Commands["TestCommand"].AddTrigger(new CommandTrigger
  258. {
  259. Button = InputButton.Space,
  260. State = InputState.IsPressed,
  261. Interval = 1f,
  262. });
  263. Input.Commands["TestCommand"].AddTrigger(new CommandTrigger
  264. {
  265. Button = InputButton.Enter,
  266. State = InputState.Pressed,
  267. });
  268. Input.Commands["TestCommand"].Add(delegate(CommandTrigger trigger)
  269. {
  270. if (trigger.Button == InputButton.Enter)
  271. {
  272. enterPressed = true;
  273. }
  274. else
  275. {
  276. spacePressed = true;
  277. }
  278. });
  279. Application.Start(delegate
  280. {
  281. font.Draw(
  282. "Press Enter to test instant command reaction.",
  283. Rectangle.FromCenter(new Point(0.5f, 0.45f), Size.Half));
  284. font.Draw(
  285. "Keep Space pressed for 1 Second to test delayed command reaction.",
  286. Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
  287. font.Draw("Enter",
  288. Rectangle.FromCenter(new Point(0.4f, 0.55f), Size.Half));
  289. font.Draw("Space",
  290. Rectangle.FromCenter(new Point(0.6f, 0.55f), Size.Half));
  291. Circle.DrawFilled(new Point(0.4f, 0.6f), 0.04f, Color.Red);
  292. Circle.DrawFilled(new Point(0.6f, 0.6f), 0.04f, Color.Red);
  293. if (enterPressed)
  294. {
  295. Circle.DrawFilled(new Point(0.4f, 0.6f), 0.04f, Color.Green);
  296. }
  297. if (spacePressed)
  298. {
  299. Circle.DrawFilled(new Point(0.6f, 0.6f), 0.04f, Color.Green);
  300. }
  301. });
  302. }
  303. #endregion
  304. #region GamePadStateCheck
  305. /// <summary>
  306. /// GamePad state check
  307. /// </summary>
  308. [Test, Category("Visual")]
  309. public static void GamePadStateCheck()
  310. {
  311. // This test currently only works with the Xna InputModule
  312. Assert.Equal(Settings.Modules.InputModule, "Xna");
  313. // Command names
  314. string MoveLeft = "MoveLeft";
  315. string MoveRight = "MoveRight";
  316. string MoveUp = "MoveUp";
  317. string MoveDown = "MoveDown";
  318. string ButtonA = "ButtonA";
  319. string ButtonB = "ButtonB";
  320. string ButtonX = "ButtonX";
  321. string ButtonY = "ButtonY";
  322. CommandManager commands = Input.Commands;
  323. // GamePad movement directions
  324. bool left = false;
  325. #region Move left command
  326. commands[MoveLeft].AddTrigger(new CommandTrigger
  327. {
  328. Button = InputButton.GamePadLeft,
  329. State = InputState.Pressed,
  330. });
  331. commands[MoveLeft].AddTrigger(new CommandTrigger
  332. {
  333. Button = InputButton.GamePadLeft,
  334. State = InputState.Released,
  335. });
  336. Input.Commands[MoveLeft].Add(delegate(CommandTrigger trigger)
  337. {
  338. left = (trigger.State == InputState.Pressed);
  339. });
  340. #endregion
  341. bool right = false;
  342. #region Move right command
  343. commands[MoveRight].AddTrigger(new CommandTrigger
  344. {
  345. Button = InputButton.GamePadRight,
  346. State = InputState.Pressed,
  347. });
  348. commands[MoveRight].AddTrigger(new CommandTrigger
  349. {
  350. Button = InputButton.GamePadRight,
  351. State = InputState.Released,
  352. });
  353. Input.Commands[MoveRight].Add(delegate(CommandTrigger trigger)
  354. {
  355. right = (trigger.State == InputState.Pressed);
  356. });
  357. #endregion
  358. bool up = false;
  359. #region Move up command
  360. commands[MoveUp].AddTrigger(new CommandTrigger
  361. {
  362. Button = InputButton.GamePadUp,
  363. State = InputState.Pressed,
  364. });
  365. commands[MoveUp].AddTrigger(new CommandTrigger
  366. {
  367. Button = InputButton.GamePadUp,
  368. State = InputState.Released,
  369. });
  370. Input.Commands[MoveUp].Add(delegate(CommandTrigger trigger)
  371. {
  372. up = (trigger.State == InputState.Pressed);
  373. });
  374. #endregion
  375. bool down = false;
  376. #region Move down command
  377. commands[MoveDown].AddTrigger(new CommandTrigger
  378. {
  379. Button = InputButton.GamePadDown,
  380. State = InputState.Pressed,
  381. });
  382. commands[MoveDown].AddTrigger(new CommandTrigger
  383. {
  384. Button = InputButton.GamePadDown,
  385. State = InputState.Released,
  386. });
  387. Input.Commands[MoveDown].Add(delegate(CommandTrigger trigger)
  388. {
  389. down = (trigger.State == InputState.Pressed);
  390. });
  391. #endregion
  392. // GamePad buttons
  393. bool a = false;
  394. #region Button A command
  395. commands[ButtonA].AddTrigger(new CommandTrigger
  396. {
  397. Button = InputButton.GamePadA,
  398. State = InputState.Pressed,
  399. });
  400. commands[ButtonA].AddTrigger(new CommandTrigger
  401. {
  402. Button = InputButton.GamePadA,
  403. State = InputState.Released,
  404. });
  405. commands[ButtonA].Add(delegate(CommandTrigger trigger)
  406. {
  407. a = (trigger.State == InputState.Pressed);
  408. });
  409. #endregion
  410. bool b = false;
  411. #region Button B command
  412. commands[ButtonB].AddTrigger(new CommandTrigger
  413. {
  414. Button = InputButton.GamePadB,
  415. State = InputState.Pressed,
  416. });
  417. commands[ButtonB].AddTrigger(new CommandTrigger
  418. {
  419. Button = InputButton.GamePadB,
  420. State = InputState.Released,
  421. });
  422. Input.Commands[ButtonB].Add(delegate(CommandTrigger trigger)
  423. {
  424. b = (trigger.State == InputState.Pressed);
  425. });
  426. #endregion
  427. bool x = false;
  428. #region Button X command
  429. commands[ButtonX].AddTrigger(new CommandTrigger
  430. {
  431. Button = InputButton.GamePadX,
  432. State = InputState.Pressed,
  433. });
  434. commands[ButtonX].AddTrigger(new CommandTrigger
  435. {
  436. Button = InputButton.GamePadX,
  437. State = InputState.Released,
  438. });
  439. Input.Commands[ButtonX].Add(delegate(CommandTrigger trigger)
  440. {
  441. x = (trigger.State == InputState.Pressed);
  442. });
  443. #endregion
  444. bool y = false;
  445. #region Button Y command
  446. commands[ButtonY].AddTrigger(new CommandTrigger
  447. {
  448. Button = InputButton.GamePadY,
  449. State = InputState.Pressed,
  450. });
  451. commands[ButtonY].AddTrigger(new CommandTrigger
  452. {
  453. Button = InputButton.GamePadY,
  454. State = InputState.Released,
  455. });
  456. Input.Commands[ButtonY].Add(delegate(CommandTrigger trigger)
  457. {
  458. y = (trigger.State == InputState.Pressed);
  459. });
  460. #endregion
  461. string newLine = Environment.NewLine;
  462. Font font = Font.Default;
  463. #region CommandDelegates
  464. commands[Command.CameraMoveDown].Add(delegate
  465. {
  466. down = true;
  467. });
  468. commands[Command.CameraMoveUp].Add(delegate
  469. {
  470. up = true;
  471. });
  472. commands[Command.CameraMoveLeft].Add(delegate
  473. {
  474. left = true;
  475. });
  476. commands[Command.CameraMoveRight].Add(delegate
  477. {
  478. right = true;
  479. });
  480. #endregion
  481. Application.Start(delegate
  482. {
  483. // Digital pad
  484. font.Draw("Digital Pad",
  485. Rectangle.FromCenter(new Point(0.2f, 0.4f), Size.Half));
  486. font.Draw("Up" + newLine + up,
  487. Rectangle.FromCenter(new Point(0.2f, 0.45f), Size.Half));
  488. font.Draw("Left" + newLine + left,
  489. Rectangle.FromCenter(new Point(0.15f, 0.5f), Size.Half));
  490. font.Draw("Right" + newLine + right,
  491. Rectangle.FromCenter(new Point(0.25f, 0.5f), Size.Half));
  492. font.Draw("Down" + newLine + down,
  493. Rectangle.FromCenter(new Point(0.2f, 0.55f), Size.Half));
  494. // Basic Buttons
  495. font.Draw("Buttons",
  496. Rectangle.FromCenter(new Point(0.8f, 0.4f), Size.Half));
  497. font.Draw("Y" + newLine + y,
  498. Rectangle.FromCenter(new Point(0.8f, 0.45f), Size.Half));
  499. font.Draw("X" + newLine + x,
  500. Rectangle.FromCenter(new Point(0.75f, 0.5f), Size.Half));
  501. font.Draw("B" + newLine + b,
  502. Rectangle.FromCenter(new Point(0.85f, 0.5f), Size.Half));
  503. font.Draw("A" + newLine + a,
  504. Rectangle.FromCenter(new Point(0.8f, 0.55f), Size.Half));
  505. });
  506. }
  507. #endregion
  508. #region TestUIClickCommand
  509. /// <summary>
  510. /// Test UIClick command
  511. /// </summary>
  512. [Test, Category("Visual")]
  513. public static void TestUIClickCommand()
  514. {
  515. Input.Commands[Command.UIClick].Add(delegate(CommandTrigger trigger)
  516. {
  517. Application.BackgroundColor = Color.Random;
  518. });
  519. Application.Start(delegate
  520. {
  521. Font.Default.Draw("Click, Touch or press Space key",
  522. ScreenSpace.DrawArea);
  523. });
  524. }
  525. #endregion
  526. #region TestCustomCommandTriggerWithInterval
  527. /// <summary>
  528. /// Test Custom Command Trigger with Interval instead of loading from
  529. /// InputSettings.xml file.
  530. /// </summary>
  531. [Test, Category("Visual")]
  532. public static void TestCustomCommandTriggerWithInterval()
  533. {
  534. string CommandName = "TestCommand";
  535. Input.Commands[CommandName].AddTrigger(new CommandTrigger
  536. {
  537. Button = InputButton.Space,
  538. State = InputState.IsPressed,
  539. StartInterval = 0f,
  540. Interval = 0.15f,
  541. });
  542. Input.Commands[CommandName].Add(delegate(CommandTrigger trigger)
  543. {
  544. Application.BackgroundColor = Color.Random;
  545. });
  546. Application.Start(delegate
  547. {
  548. Font.Default.Draw("Hold Space key", ScreenSpace.DrawArea);
  549. });
  550. }
  551. #endregion
  552. #region TestCustomPinchCommand
  553. /// <summary>
  554. /// Test Custom Pinch Command Trigger (we don't have pinch commands in
  555. /// InputSettings.xml yet).
  556. /// </summary>
  557. [Test, Category("Visual")]
  558. public static void TestCustomPinchCommand()
  559. {
  560. const string PinchCommandName = "TestCustomPinchCommand";
  561. Input.Commands[PinchCommandName].AddTrigger(new CommandTrigger
  562. {
  563. Button = InputButton.GesturePinch,
  564. State = InputState.Released,
  565. });
  566. Input.Commands[PinchCommandName].Add(delegate(CommandTrigger trigger)
  567. {
  568. Application.BackgroundColor = Color.Random;
  569. });
  570. Application.Start(delegate
  571. {
  572. Font.Default.Draw("Pinch to change color", ScreenSpace.DrawArea);
  573. });
  574. }
  575. #endregion
  576. }
  577. }