PageRenderTime 61ms CodeModel.GetById 39ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/InputSystem/Tests/Tutorials.cs

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