/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
- using Delta.Engine;
- using Delta.InputSystem.Devices;
- using Delta.Rendering.Basics.Drawing;
- using Delta.Rendering.Basics.Fonts;
- using Delta.Utilities.Datatypes;
- using NUnit.Framework;
-
- namespace Delta.InputSystem.Tests
- {
- /// <summary>
- /// Contains the tutorials for the InputSystem each with the description,
- /// level, Image and file Paths.
- /// </summary>
- internal class Tutorials
- {
- #region KeyboardStatus (Static)
- /// <summary>
- /// Tutorial: Input System Tutorial 1: Check Keyboard States
- /// Difficulty: Easy
- /// Description: Check the keyboard status (i.e. is it connected or not)
- /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial1.png
- /// </summary>
- [Test, Category("Visual")]
- public static void KeyboardStatus()
- {
- // Start our application here.
- Application.Start(delegate
- {
- // Display messages in the screen
- Font infoFont = Font.Default;
- infoFont.Draw("Green point is 'Active', red is 'Inactive'",
- Rectangle.FromCenter(new Point(0.45f, 0.35f), Size.Half));
- infoFont.Draw("Keyboard is 'Connected'",
- Rectangle.FromCenter(new Point(0.45f, 0.45f), Size.Half));
- infoFont.Draw("'A' is 'Pressed'",
- Rectangle.FromCenter(new Point(0.45f, 0.5f), Size.Half));
- infoFont.Draw("'Shift' key is 'Pressed'",
- Rectangle.FromCenter(new Point(0.45f, 0.55f), Size.Half));
- infoFont.Draw("Right 'Alt' key is 'Pressed'",
- Rectangle.FromCenter(new Point(0.45f, 0.6f), Size.Half));
-
- // Now we can draw Green points in the same spot of the red points
- // if status is changes to active.
- BaseKeyboard keyboard = Input.Keyboard;
- // Check if Keyboard is Connected
- Circle.DrawFilled(new Point(0.6f, 0.45f), 0.015f,
- (keyboard.IsConnected)
- ? Color.Green
- : Color.Red);
-
- // Is the 'A' key is pressed
- Circle.DrawFilled(new Point(0.6f, 0.5f), 0.015f,
- keyboard.IsPressed(InputButton.A)
- ? Color.Green
- : Color.Red);
-
- // Is the 'Shift' key pressed
- Circle.DrawFilled(new Point(0.6f, 0.55f), 0.015f,
- keyboard.IsPressed(InputButton.Shift)
- ? Color.Green
- : Color.Red);
-
- // Is The right Alt Key Pressed
- Circle.DrawFilled(new Point(0.6f, 0.6f), 0.015f,
- keyboard.IsPressed(InputButton.RightAlt)
- ? Color.Green
- : Color.Red);
- });
- }
- #endregion
-
- #region MouseStatus (Static)
- /// <summary>
- /// Tutorial: Input System Tutorial 2: Check Mouse States
- /// Difficulty: Easy
- /// Description: Check mouse status (i.e. Connected or not, any button
- /// pressed, etc)
- /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial2.png
- /// </summary>
- [Test, Category("Visual")]
- public static void MouseStatus()
- {
- // Flag to check if double click or a mouse move happened.
- bool isDoubleClick = false;
- bool isMoving = false;
- // We need a time stamp to allow viewing the green color when the double
- // click occurs.
- long clickTimeStamp = 0;
- long moveTimeStamp = 0;
- // Start our application here.
- Application.Start(delegate
- {
- // Display messages in the screen
- Font infoFont = Font.Default;
- infoFont.Draw("Green point is 'Active', red is 'Inactive'",
- Rectangle.FromCenter(new Point(0.45f, 0.35f), Size.Half));
- infoFont.Draw("Mouse Connected",
- Rectangle.FromCenter(new Point(0.45f, 0.45f), Size.Half));
- infoFont.Draw("Mouse Click",
- Rectangle.FromCenter(new Point(0.45f, 0.5f), Size.Half));
- infoFont.Draw("Mouse Move",
- Rectangle.FromCenter(new Point(0.45f, 0.55f), Size.Half));
- infoFont.Draw("Mouse DoubleClick",
- Rectangle.FromCenter(new Point(0.45f, 0.6f), Size.Half));
-
- // Now we can draw green points in the same spot of the red points
- // if status is changes to active.
- BaseMouse mouse = Input.Mouse;
-
- // Check if Mouse is Connected
- Circle.DrawFilled(new Point(0.6f, 0.45f), 0.015f,
- (mouse.IsConnected)
- ? Color.Green
- : Color.Red);
-
- // Is the left mouse button pressed?
- if (Input.Mouse.IsPressed(InputButton.MouseLeft))
- {
- Circle.DrawFilled(new Point(0.6f, 0.5f), 0.015f, Color.Green);
- isDoubleClick = false;
- }
- else
- {
- Circle.DrawFilled(new Point(0.6f, 0.5f), 0.015f, Color.Red);
- }
-
- // Mouse is moving?
- if (Input.Mouse.GetState(InputButton.MouseMove) == InputState.Pressed)
- {
- isMoving = true;
- moveTimeStamp = Time.Milliseconds;
- }
-
- Circle.DrawFilled(new Point(0.6f, 0.55f), 0.015f,
- isMoving
- ? Color.Green
- : Color.Red);
-
- // Check if a double click happened.
- if (Input.Gestures.IsDoubleTap)
- {
- isDoubleClick = true;
- clickTimeStamp = Time.Milliseconds;
- }
-
- Circle.DrawFilled(new Point(0.6f, 0.6f), 0.015f,
- isDoubleClick
- ? Color.Green
- : Color.Red);
-
- // We need this to reset the green flag when the double click or a move
- // occurs after few milliseconds.
- if (isDoubleClick &&
- Time.Milliseconds - clickTimeStamp > 300)
- {
- isDoubleClick = false;
- }
-
- if (isMoving &&
- (Time.Milliseconds - moveTimeStamp) > 100)
- {
- isMoving = false;
- }
- });
- }
- #endregion
-
- #region ClickCommand (Static)
- /// <summary>
- /// Tutorial: Input System Tutorial 3: Command Click
- /// Difficulty: Medium
- /// Description: Make an action when a mouse click event is performed.
- /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial3.png
- /// </summary>
- [Test, Category("Visual")]
- public static void ClickCommand()
- {
- // First: Define our Command trigger. The UIClick command will fire if a
- // mouse click or a touch event happened. A click represents a press and
- // release for a mouse or a tap with a touch device.
- Input.Commands[Command.UIClick].Add(delegate
- {
- // Whenever a click happens, change a back ground color by choosing a
- // random color.
- Application.BackgroundColor = Color.Random;
- });
-
- // Start the application
- Application.Start(delegate
- {
- Font.Default.Draw(
- "Click with a mouse, or perform a touch (In case of a touch " +
- "screen) to test the 'Click' command",
- Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
- });
- }
- #endregion
-
- #region DragCommand (Static)
- /// <summary>
- /// Tutorial: Input System Tutorial 4: Command Dragging
- /// Difficulty: Medium
- /// Description: Draw a line by using a mouse drag between two points
- /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial4.png
- /// </summary>
- [Test, Category("Visual")]
- public static void DragCommand()
- {
- // Flags we need for out tutorial.
- bool showDragCircle = false;
- // Start and End points of our drag
- Point startPoint = Point.Zero;
- Point endPoint = Point.Zero;
-
- // Define a command to store our first point of dragging, this point will
- // be the first point of drawing our circle.
- Input.Commands[Command.UIDragBegin].Add(delegate(CommandTrigger input)
- {
- // Define the drag starting point.
- startPoint = input.DragStart;
- showDragCircle = false;
- });
- // As we drag move, we need to keep drawing our circle as a feedback of
- // our 'DragMove' action
- Input.Commands[Command.UIDragMove].Add(delegate(CommandTrigger input)
- {
- // Get the end point where the circle extends and draw it on the screen
- endPoint = input.Position;
- showDragCircle = true;
- });
-
- // Start the application
- Application.Start(delegate
- {
- // Change the background color for a better presentation.
- Application.BackgroundColor = Color.Black;
-
- // Display a message on a screen
- Font.Default.Draw(
- "In this tutorial we will use the 'Drag' command to draw a " +
- "circle as far as we are dragging.",
- Rectangle.FromCenter(new Point(0.5f, 0.15f), Size.Half));
-
- // If we are dragging, draw the circle.
- if (showDragCircle)
- {
- // Get the distance between the two points
- float distance = endPoint.Length - startPoint.Length;
-
- // Draw the circle.
- Circle.DrawFilled(startPoint, distance, Color.White);
- }
- });
- }
- #endregion
-
- #region TriggersIntervals (Static)
- /// <summary>
- /// Tutorial: Input System Tutorial 5: Command Click Intervals
- /// Difficulty: Advanced
- /// Description: Make an action when a mouse click event is performed.
- /// ImagePath: http://DeltaEngine.net/Icons/InputTutorial5.png
- /// </summary>
- [Test, Category("Visual")]
- public static void TriggersIntervals()
- {
- // Define flags for status
- // for testing pressing a key
- bool pressed = false;
- // and for testing a key pressed for a time interval
- bool isPressed = false;
- // Define a command that doesn't exist yet.
- const string CommandName = "IntervalCheckCommand";
-
- // Define the Command triggers
- Input.Commands[CommandName].AddTrigger(new CommandTrigger
- {
- Button = InputButton.Enter,
- State = InputState.Released,
- });
-
- // Add another trigger only if the key you pressed previously has been
- // Pressed again for over 1 second.
- Input.Commands[CommandName].AddTrigger(new CommandTrigger
- {
- Button = InputButton.Enter,
- State = InputState.IsPressed,
- Interval = 1f,
- });
-
- // Define a delegate for the command. A reaction that will happen if
- // the command is triggered.
- Input.Commands[CommandName].Add(delegate(CommandTrigger trigger)
- {
- // Here we define te command which is triggered only when the time
- // interval is 1 second.
- if (trigger.Interval == 1.0f)
- {
- isPressed = true;
- pressed = false;
- }
- else
- {
- pressed = true;
- isPressed = false;
- }
- });
-
- Application.Start(delegate
- {
- Font infoFont = Font.Default;
- infoFont.Draw("Green point is 'Active', red is 'Inactive' ",
- Rectangle.FromCenter(new Point(0.45f, 0.35f), Size.Half));
- infoFont.Draw("Press 'Enter' key to Activate",
- Rectangle.FromCenter(new Point(0.45f, 0.45f), Size.Half));
- infoFont.Draw("Press 'Enter' for 1 second to Activate",
- Rectangle.FromCenter(new Point(0.45f, 0.5f), Size.Half));
-
- Circle.DrawFilled(new Point(0.6f, 0.45f), 0.015f,
- pressed
- ? Color.Green
- : Color.Red);
-
- Circle.DrawFilled(new Point(0.6f, 0.5f), 0.015f,
- isPressed
- ? Color.Green
- : Color.Red);
- });
- }
- #endregion
- }
- }