PageRenderTime 156ms CodeModel.GetById 70ms app.highlight 48ms RepoModel.GetById 33ms app.codeStats 0ms

/InputSystem/Tests/CommandTests.cs

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