/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
- using System;
- using Delta.Engine;
- using Delta.Utilities;
- using Delta.Utilities.Datatypes;
- using Delta.Rendering.Basics.Fonts;
- using Delta.Rendering.Basics.Drawing;
- using Delta.Utilities.Helpers;
- using NUnit.Framework;
-
- namespace Delta.InputSystem.Tests
- {
- /// <summary>
- /// Command Tests
- /// </summary>
- internal class CommandTests
- {
- #region EnableCommand
- /// <summary>
- /// Enable command
- /// </summary>
- [Test, Category("Visual")]
- public static void EnableCommand()
- {
- const string commandName = "Help";
-
- InputButton pressedKey = InputButton.None;
- int pressedKeyTimeStamp = 0;
-
- // Define now the conditions which will trigger the command
- Input.Commands[commandName].AddTrigger(new CommandTrigger
- {
- Button = InputButton.F1,
- State = InputState.Released
- });
- Input.Commands[commandName].AddTrigger(new CommandTrigger
- {
- Button = InputButton.Tab,
- State = InputState.Released
- });
-
- // Define the new command and the delegate which should be executed if
- // the command gets triggered by the input devices
- // Note: The command will be automatically created by a new name
- Input.Commands[commandName].Add(delegate(CommandTrigger trigger)
- {
- pressedKey = trigger.Button;
- pressedKeyTimeStamp = Time.Seconds;
- });
-
- // Last start the application
- Application.Start(delegate
- {
- // and show which key has triggered the command
- if (pressedKey != InputButton.None)
- {
- Font.Default.Draw(
- commandName + " command triggered by '" + pressedKey + "'",
- ScreenSpace.DrawArea);
- }
- else
- {
- Font.Default.Draw("Press the 'F1' key or the 'Tab' key.",
- ScreenSpace.DrawArea);
- }
-
- // but make a time out for this message of 1 second
- if ((Time.Seconds - pressedKeyTimeStamp) > 1)
- {
- pressedKey = InputButton.None;
- }
- });
- }
- #endregion
-
- #region DisableCommand
- /// <summary>
- /// Disable command
- /// </summary>
- [Test, Category("Visual")]
- public static void DisableCommand()
- {
- // Create the 'Action' and the 'Remove' command and define their triggers
- Input.Commands["Action"].AddTrigger(new CommandTrigger
- {
- Button = InputButton.MouseLeft,
- State = InputState.Released
- });
- Input.Commands["Remove"].AddTrigger(new CommandTrigger
- {
- Button = InputButton.Space,
- State = InputState.Released
- });
-
- // Our fictive owner of the command delegates we will attach by pressing
- // the 'Enter' key and on the application start
- string owner = "DelegateOwner";
- bool isFirstRun = true;
-
- Application.Start(delegate
- {
- if (isFirstRun ||
- Input.Keyboard.EnterReleased)
- {
- Input.Commands["Action"].Add(owner, delegate
- {
- Application.BackgroundColor = Color.Random;
- });
-
- Input.Commands["Remove"].Add(owner, delegate
- {
- Input.Commands.DetachAllDelegates(owner);
- });
-
- isFirstRun = false;
- } // if
-
- Font.Default.Draw(
- "Click the left mouse button to change the background color." +
- Environment.NewLine + "By pressing the 'Space' key the command " +
- "can be disabled and by pressing the 'Enter' it can be enabled " +
- "again.", ScreenSpace.DrawArea);
- });
- }
- #endregion
-
- #region MouseMoveCommand
- /// <summary>
- /// Testing UIPositionChange command, which is triggered for every mouse
- /// move (but also for touch position changes or keyboard or game pad
- /// virtual cursor changes).
- /// </summary>
- [Test, Category("Visual")]
- public static void MouseMoveCommand()
- {
- Input.Commands[Command.UIPositionChange].Add(
- delegate(CommandTrigger input)
- {
- Application.BackgroundColor = Color.Random;
- });
-
- Application.Start(delegate
- {
- Font.Default.Draw("Move mouse to test!", ScreenSpace.DrawArea);
- });
- }
- #endregion
-
- #region MouseWheelCommand
- /// <summary>
- /// Test if the mouse wheel works with the commands.
- /// </summary>
- [Test, Category("Visual")]
- public static void MouseWheelCommand()
- {
- const string MouseWheel = "MouseWheel";
- Input.Commands[MouseWheel].AddTrigger(new CommandTrigger
- {
- Button = InputButton.MouseScrollWheel,
- State = InputState.Released,
- });
- Point position = Point.Half;
- Application.Start(delegate
- {
- Application.BackgroundColor = Color.Black;
- Font.Default.Draw("Move mouse wheel to test!", ScreenSpace.DrawArea);
- // Add the delegate to the mouse wheel move.
- Input.Commands[MouseWheel].Add(delegate
- {
- position.Y = Input.Mouse.TotalScrollWheel / 20;
- // Clamp it to the screen otherwise it will run out of scope.
- position.Y = MathHelper.Clamp(position.Y, 0.35f,0.65f);
- });
- // Draw the cross line
- Line.DrawCross(position, 0.05f, Color.Red);
- });
- }
- #endregion
-
- #region CommandClickTests
- /// <summary>
- /// Command UIClick test
- /// </summary>
- [Test, Category("Visual")]
- public static void CommandClickTest()
- {
- CommandTrigger lastTrigger = null;
- Input.Commands[Command.UIClick].Add(delegate(CommandTrigger trigger)
- {
- Application.BackgroundColor = Color.Random;
- lastTrigger = trigger;
- });
-
- Application.Start(delegate
- {
- Font.Default.Draw(
- "Test UIClick Command (click or touch)!\nLast Trigger: " +
- (lastTrigger == null
- ? "none"
- : lastTrigger.ToString()),
- ScreenSpace.DrawArea);
- });
- }
- #endregion
-
- #region DraggingCommand
- /// <summary>
- /// Dragging command. Note: This only does one drag operation, use the
- /// TouchTests.TouchLineDrawing test for multiple lines and dragging.
- /// </summary>
- [Test, Category("Visual")]
- public static void DraggingCommand()
- {
- // Define our data
- Point startPoint = Point.Zero;
- Point endPoint = Point.Zero;
- // states we use to know what we have to draw in the test
- bool showStartPoint = false;
- bool showDragLine = false;
- bool showEndPoint = false;
-
- // Define now drag commands with the delegates
- Input.Commands[Command.UIDragBegin].Add(delegate(CommandTrigger input)
- {
- startPoint = input.DragStart;
- showStartPoint = true;
- showDragLine = false;
- showEndPoint = false;
- });
- Input.Commands[Command.UIDragMove].Add(delegate(CommandTrigger input)
- {
- endPoint = input.Position;
- showDragLine = true;
- });
- Input.Commands[Command.UIDragEnd].Add(delegate(CommandTrigger input)
- {
- endPoint = input.Position;
- showEndPoint = true;
- });
-
- // and finally start the application
- Application.Start(delegate
- {
- if (showStartPoint)
- {
- Line.DrawCross(startPoint, 0.01f, Color.Yellow);
- } // if
-
- if (showDragLine)
- {
- Line.Draw(startPoint, endPoint, Color.Red);
- } // if
-
- if (showEndPoint)
- {
- Line.DrawCross(endPoint, 0.01f, Color.Green);
- } // if
-
- if (showStartPoint == false &&
- showDragLine == false &&
- showEndPoint == false)
- {
- Font.Default.Draw(
- "Just click and drag the cursor on the screen (using Mouse, " +
- "Touch, GamePad or Keyboard).", ScreenSpace.DrawArea);
- } // if
- });
- }
- #endregion
-
- #region KeyboardCommandTests
- /// <summary>
- /// Keyboard Commands test
- /// </summary>
- [Test, Category("Visual")]
- public static void KeyboardCommandTests()
- {
- bool enterPressed = false;
- bool spacePressed = false;
- Font font = Font.Default;
-
- // Create new TestCommand with 2 triggers and one action.
- Input.Commands["TestCommand"].AddTrigger(new CommandTrigger
- {
- Button = InputButton.Space,
- State = InputState.IsPressed,
- Interval = 1f,
- });
- Input.Commands["TestCommand"].AddTrigger(new CommandTrigger
- {
- Button = InputButton.Enter,
- State = InputState.Pressed,
- });
- Input.Commands["TestCommand"].Add(delegate(CommandTrigger trigger)
- {
- if (trigger.Button == InputButton.Enter)
- {
- enterPressed = true;
- }
- else
- {
- spacePressed = true;
- }
- });
- Application.Start(delegate
- {
- font.Draw(
- "Press Enter to test instant command reaction.",
- Rectangle.FromCenter(new Point(0.5f, 0.45f), Size.Half));
- font.Draw(
- "Keep Space pressed for 1 Second to test delayed command reaction.",
- Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
- font.Draw("Enter",
- Rectangle.FromCenter(new Point(0.4f, 0.55f), Size.Half));
- font.Draw("Space",
- Rectangle.FromCenter(new Point(0.6f, 0.55f), Size.Half));
- Circle.DrawFilled(new Point(0.4f, 0.6f), 0.04f, Color.Red);
- Circle.DrawFilled(new Point(0.6f, 0.6f), 0.04f, Color.Red);
- if (enterPressed)
- {
- Circle.DrawFilled(new Point(0.4f, 0.6f), 0.04f, Color.Green);
- }
- if (spacePressed)
- {
- Circle.DrawFilled(new Point(0.6f, 0.6f), 0.04f, Color.Green);
- }
- });
- }
- #endregion
-
- #region GamePadStateCheck
- /// <summary>
- /// GamePad state check
- /// </summary>
- [Test, Category("Visual")]
- public static void GamePadStateCheck()
- {
- // This test currently only works with the Xna InputModule
- Assert.Equal(Settings.Modules.InputModule, "Xna");
-
- // Command names
- string MoveLeft = "MoveLeft";
- string MoveRight = "MoveRight";
- string MoveUp = "MoveUp";
- string MoveDown = "MoveDown";
- string ButtonA = "ButtonA";
- string ButtonB = "ButtonB";
- string ButtonX = "ButtonX";
- string ButtonY = "ButtonY";
-
- CommandManager commands = Input.Commands;
-
- // GamePad movement directions
- bool left = false;
- #region Move left command
- commands[MoveLeft].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadLeft,
- State = InputState.Pressed,
- });
- commands[MoveLeft].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadLeft,
- State = InputState.Released,
- });
- Input.Commands[MoveLeft].Add(delegate(CommandTrigger trigger)
- {
- left = (trigger.State == InputState.Pressed);
- });
- #endregion
-
- bool right = false;
- #region Move right command
- commands[MoveRight].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadRight,
- State = InputState.Pressed,
- });
- commands[MoveRight].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadRight,
- State = InputState.Released,
- });
- Input.Commands[MoveRight].Add(delegate(CommandTrigger trigger)
- {
- right = (trigger.State == InputState.Pressed);
- });
- #endregion
-
- bool up = false;
- #region Move up command
- commands[MoveUp].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadUp,
- State = InputState.Pressed,
- });
- commands[MoveUp].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadUp,
- State = InputState.Released,
- });
- Input.Commands[MoveUp].Add(delegate(CommandTrigger trigger)
- {
- up = (trigger.State == InputState.Pressed);
- });
- #endregion
-
- bool down = false;
- #region Move down command
- commands[MoveDown].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadDown,
- State = InputState.Pressed,
- });
- commands[MoveDown].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadDown,
- State = InputState.Released,
- });
- Input.Commands[MoveDown].Add(delegate(CommandTrigger trigger)
- {
- down = (trigger.State == InputState.Pressed);
- });
- #endregion
-
- // GamePad buttons
- bool a = false;
- #region Button A command
- commands[ButtonA].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadA,
- State = InputState.Pressed,
- });
- commands[ButtonA].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadA,
- State = InputState.Released,
- });
- commands[ButtonA].Add(delegate(CommandTrigger trigger)
- {
- a = (trigger.State == InputState.Pressed);
- });
- #endregion
-
- bool b = false;
- #region Button B command
- commands[ButtonB].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadB,
- State = InputState.Pressed,
- });
- commands[ButtonB].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadB,
- State = InputState.Released,
- });
- Input.Commands[ButtonB].Add(delegate(CommandTrigger trigger)
- {
- b = (trigger.State == InputState.Pressed);
- });
- #endregion
-
- bool x = false;
- #region Button X command
- commands[ButtonX].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadX,
- State = InputState.Pressed,
- });
- commands[ButtonX].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadX,
- State = InputState.Released,
- });
- Input.Commands[ButtonX].Add(delegate(CommandTrigger trigger)
- {
- x = (trigger.State == InputState.Pressed);
- });
- #endregion
-
- bool y = false;
- #region Button Y command
- commands[ButtonY].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadY,
- State = InputState.Pressed,
- });
- commands[ButtonY].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GamePadY,
- State = InputState.Released,
- });
- Input.Commands[ButtonY].Add(delegate(CommandTrigger trigger)
- {
- y = (trigger.State == InputState.Pressed);
- });
- #endregion
-
- string newLine = Environment.NewLine;
- Font font = Font.Default;
-
- #region CommandDelegates
- commands[Command.CameraMoveDown].Add(delegate
- {
- down = true;
- });
- commands[Command.CameraMoveUp].Add(delegate
- {
- up = true;
- });
- commands[Command.CameraMoveLeft].Add(delegate
- {
- left = true;
- });
- commands[Command.CameraMoveRight].Add(delegate
- {
- right = true;
- });
- #endregion
-
- Application.Start(delegate
- {
- // Digital pad
- font.Draw("Digital Pad",
- Rectangle.FromCenter(new Point(0.2f, 0.4f), Size.Half));
- font.Draw("Up" + newLine + up,
- Rectangle.FromCenter(new Point(0.2f, 0.45f), Size.Half));
- font.Draw("Left" + newLine + left,
- Rectangle.FromCenter(new Point(0.15f, 0.5f), Size.Half));
- font.Draw("Right" + newLine + right,
- Rectangle.FromCenter(new Point(0.25f, 0.5f), Size.Half));
- font.Draw("Down" + newLine + down,
- Rectangle.FromCenter(new Point(0.2f, 0.55f), Size.Half));
-
- // Basic Buttons
- font.Draw("Buttons",
- Rectangle.FromCenter(new Point(0.8f, 0.4f), Size.Half));
- font.Draw("Y" + newLine + y,
- Rectangle.FromCenter(new Point(0.8f, 0.45f), Size.Half));
- font.Draw("X" + newLine + x,
- Rectangle.FromCenter(new Point(0.75f, 0.5f), Size.Half));
- font.Draw("B" + newLine + b,
- Rectangle.FromCenter(new Point(0.85f, 0.5f), Size.Half));
- font.Draw("A" + newLine + a,
- Rectangle.FromCenter(new Point(0.8f, 0.55f), Size.Half));
- });
- }
- #endregion
-
- #region TestUIClickCommand
- /// <summary>
- /// Test UIClick command
- /// </summary>
- [Test, Category("Visual")]
- public static void TestUIClickCommand()
- {
- Input.Commands[Command.UIClick].Add(delegate(CommandTrigger trigger)
- {
- Application.BackgroundColor = Color.Random;
- });
-
- Application.Start(delegate
- {
- Font.Default.Draw("Click, Touch or press Space key",
- ScreenSpace.DrawArea);
- });
- }
- #endregion
-
- #region TestCustomCommandTriggerWithInterval
- /// <summary>
- /// Test Custom Command Trigger with Interval instead of loading from
- /// InputSettings.xml file.
- /// </summary>
- [Test, Category("Visual")]
- public static void TestCustomCommandTriggerWithInterval()
- {
- string CommandName = "TestCommand";
-
- Input.Commands[CommandName].AddTrigger(new CommandTrigger
- {
- Button = InputButton.Space,
- State = InputState.IsPressed,
- StartInterval = 0f,
- Interval = 0.15f,
- });
- Input.Commands[CommandName].Add(delegate(CommandTrigger trigger)
- {
- Application.BackgroundColor = Color.Random;
- });
-
- Application.Start(delegate
- {
- Font.Default.Draw("Hold Space key", ScreenSpace.DrawArea);
- });
- }
- #endregion
-
- #region TestCustomPinchCommand
- /// <summary>
- /// Test Custom Pinch Command Trigger (we don't have pinch commands in
- /// InputSettings.xml yet).
- /// </summary>
- [Test, Category("Visual")]
- public static void TestCustomPinchCommand()
- {
- const string PinchCommandName = "TestCustomPinchCommand";
- Input.Commands[PinchCommandName].AddTrigger(new CommandTrigger
- {
- Button = InputButton.GesturePinch,
- State = InputState.Released,
- });
- Input.Commands[PinchCommandName].Add(delegate(CommandTrigger trigger)
- {
- Application.BackgroundColor = Color.Random;
- });
-
- Application.Start(delegate
- {
- Font.Default.Draw("Pinch to change color", ScreenSpace.DrawArea);
- });
- }
- #endregion
- }
- }