/InputSystem/Tests/TouchTests.cs
C# | 340 lines | 249 code | 24 blank | 67 comment | 15 complexity | 8255d49a6a017da78105e7d199f5efa3 MD5 | raw file
Possible License(s): Apache-2.0
- using System.Collections.Generic;
- using Delta.Engine;
- using Delta.InputSystem.Devices;
- using Delta.Rendering.Basics.Drawing;
- using Delta.Rendering.Basics.Fonts;
- using Delta.Utilities.Datatypes;
- using Delta.Utilities.Helpers;
- using NUnit.Framework;
-
- namespace Delta.InputSystem.Tests
- {
- /// <summary>
- /// Touch tests
- /// </summary>
- internal class TouchTests
- {
- #region Helpers
- /// <summary>
- /// Touch line helper to draw lines with touches.
- /// </summary>
- private class TouchLine
- {
- #region id (Public)
- /// <summary>
- /// Touch id
- /// </summary>
- public readonly int id;
- #endregion
-
- #region state (Public)
- /// <summary>
- /// Input state to simplify some checking.
- /// </summary>
- public InputState state;
- #endregion
-
- #region start (Public)
- /// <summary>
- /// Start and end point of the line (end point is current position,
- /// start is the drag start position).
- /// </summary>
- public readonly Point start;
- #endregion
-
- #region end (Public)
- /// <summary>
- /// Start and end point of the line (end point is current position,
- /// start is the drag start position).
- /// </summary>
- public Point end;
- #endregion
-
- #region color (Public)
- /// <summary>
- /// Color of this line, randomly set.
- /// </summary>
- public Color color;
- #endregion
-
- #region time (Public)
- /// <summary>
- /// Time, will always be 1 if touch is pressed, will go down to 0
- /// in one second when touch is released (then we remove this line).
- /// </summary>
- public float time;
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create touch line
- /// </summary>
- public TouchLine(TouchData touchData)
- {
- id = touchData.Id;
- start = touchData.Position;
- end = touchData.Position;
- //not colorful enough: color = Color.Random;
- color = Color.SolidColors[
- RandomHelper.RandomInt(Color.SolidColors.Length)];
- time = 1;
- }
- #endregion
- }
- #endregion
-
- #region CheckTouchData (Visual)
- /// <summary>
- /// Check touch states
- /// </summary>
- [Test, Category("Visual")]
- public static void CheckTouchData()
- {
- // Testing is easier with 10 fps
- Settings.Extra.LimitFramerateNumber = 10;
-
- Application.Start(delegate
- {
- Font testFont = Font.Default;
-
- BaseTouch touch = Input.Touch;
- testFont.Draw("Touch IsConnected " + touch.IsConnected,
- Rectangle.FromCenter(new Point(0.5f, 0.5f), Size.Half));
- testFont.Draw("Touch Pressed " + touch.TouchIsPressed,
- Rectangle.FromCenter(new Point(0.5f, 0.55f), Size.Half));
- testFont.Draw("Touch Position " + touch.Position,
- Rectangle.FromCenter(new Point(0.5f, 0.6f), Size.Half));
- testFont.Draw("Active Touches " + touch.ActiveTouches,
- Rectangle.FromCenter(new Point(0.5f, 0.65f), Size.Half));
-
- testFont.Draw("Touches[0]=" + touch.Touches[0].State,
- Rectangle.FromCenter(new Point(0.5f, 0.7f), Size.Half));
- testFont.Draw("Touches[1]=" + touch.Touches[1].State,
- Rectangle.FromCenter(new Point(0.5f, 0.75f), Size.Half));
- });
- }
- #endregion
-
- #region AllTouchInputButtons (Visual)
- /// <summary>
- /// Test all touch input buttons, will display all the input button states
- /// we got for touch input (including normal touches and gestures).
- /// </summary>
- [Test, Category("Visual")]
- public static void AllTouchInputButtons()
- {
- // Testing is easier with 10 fps
- Settings.Extra.LimitFramerateNumber = 10;
- const float lineDistance = 0.03f;
- Application.Start(delegate
- {
- float yPos = 0.3f;
- for (int buttonIndex = (int)InputButton.TouchPress;
- buttonIndex <= (int)InputButton.TouchDeviceShake;
- buttonIndex++)
- {
- Font.Default.Draw(
- (InputButton)buttonIndex + ": " +
- Input.GetState((InputButton)buttonIndex),
- Rectangle.FromCenter(new Point(0.5f, yPos), Size.Half));
- yPos += lineDistance;
- }
-
- // Some additional info
- BaseTouch touch = Input.Touch;
- yPos += lineDistance / 2.0f;
- Font.Default.Draw("Touch Pressed " + touch.TouchIsPressed,
- Rectangle.FromCenter(new Point(0.5f, yPos), Size.Half));
- yPos += lineDistance;
- Font.Default.Draw("Touch Position " + touch.Position,
- Rectangle.FromCenter(new Point(0.5f, yPos), Size.Half));
- yPos += lineDistance;
- Font.Default.Draw("Active Touches " + touch.ActiveTouches,
- Rectangle.FromCenter(new Point(0.5f, yPos), Size.Half));
- });
- }
- #endregion
-
- #region TouchLineDrawing (Visual)
- /// <summary>
- /// Test touch line drawing
- /// </summary>
- [Test, Category("Visual")]
- public static void TouchLineDrawing()
- {
- List<TouchLine> lines = new List<TouchLine>();
- Application.Start(delegate
- {
- // When a touch is pressed, add a new line
- for (int index = 0; index < Input.Touch.ActiveTouches; index++)
- {
- if (Input.Touch.Touches[index].State == InputState.Pressed)
- {
- lines.Add(new TouchLine(Input.Touch.Touches[index]));
- }
- else if (lines.Count > 0)
- {
- // Otherwise update position of end, search for the touchline by id
- for (int i = lines.Count - 1; i >= 0; i--)
- {
- if (lines[i].id == Input.Touch.Touches[index].Id) // &&
- //lines[i].state == InputState.Pressed)
- {
- lines[i].state = Input.Touch.Touches[index].State;
- lines[i].end = Input.Touch.Touches[index].Position;
- lines[i].time = 1;
- break;
- }
- }
- } // else
- } // for
-
- // Draw box for fun
- float pixelSize = ScreenSpace.ToQuadraticSpace(Point.One).X;
- Point oneDown = new Point(0, pixelSize);
- Point oneRight = new Point(pixelSize, 0);
- Rect.DrawOutline(
- ScreenSpace.DrawArea.Shrink(pixelSize), Color.Red);
- // "lines=" + lines.Count);
-
- // Draw all touch lines
- List<TouchLine> remToKillLines = new List<TouchLine>();
- foreach (TouchLine line in lines)
- {
- // Fake bold trick ^^
- Line.Draw(line.start, line.end, line.color);
- Line.Draw(line.start + oneDown,
- line.end + oneDown, line.color);
- Line.Draw(line.start + oneRight,
- line.end + oneRight, line.color);
- // "line=" + line.start + ", " + line.end + ", " + line.color);
-
- // Decrease time of each touch line and kill faded out lines
- if (line.state < InputState.Pressed)
- {
- line.time -= Time.Delta / 4.0f;
- line.color.A = line.time;
- if (line.time <= 0)
- {
- remToKillLines.Add(line);
- }
- } // if
- } // foreach
-
- // Do the delayed killing (we could not modify the lines list above)
- foreach (TouchLine line in remToKillLines)
- {
- lines.Remove(line);
- }
- });
- }
- #endregion
-
- #region MultiTouch (Visual)
- /// <summary>
- /// Testing multi touch
- /// </summary>
- [Test, Category("Visual")]
- public static void MultiTouch()
- {
- // Init two fixed arrays which are corresponding for each touch-start
- // and touch-end
- int maxTouchCount = 10;
- Point[] touchStartPoints = new Point[maxTouchCount];
- Point[] touchEndPoints = new Point[maxTouchCount];
- // and use a (random) color fixed to each touch (id)
- Color[] drawColor = new Color[maxTouchCount];
- for (int i = 0; i < maxTouchCount; i++)
- {
- drawColor[i] = Color.Random;
- }
-
- Application.BackgroundColor = Color.DarkBlue;
- Application.Start(delegate
- {
- Rect.DrawOutline(ScreenSpace.DrawArea, Color.Red);
- int index = 0;
- for (; index < Input.Touch.ActiveTouches; index++)
- {
- TouchData touch = Input.Touch.Touches[index];
- switch (touch.State)
- {
- case InputState.IsPressed:
- touchEndPoints[index] = touch.Position;
- break;
- case InputState.Pressed:
- touchStartPoints[index] = touch.Position;
- touchEndPoints[index] = touch.Position;
- break;
- case InputState.Released:
- touchEndPoints[index] = touch.Position;
- break;
- }
-
- // Draw the graphical line from (remembered) start-point until the
- // current touch-point
- Line.Draw(touchStartPoints[index], touchEndPoints[index],
- drawColor[index]);
-
- // and show the name of the current action at the touch position
- Font.Default.Draw("'" + touch.Id + "' does '" + touch.State + "'",
- Rectangle.FromCenter(touch.Position, Size.Half));
- } // for
-
- for (; index < maxTouchCount; index++)
- {
- touchStartPoints[index] = Point.Zero;
- touchEndPoints[index] = Point.Zero;
- }
- });
- }
- #endregion
-
- #region TouchGestures (Visual)
- /// <summary>
- /// Test all Touch gestures, TouchTap, TouchDoubleTap, TouchHold,
- /// TouchDrag, TouchFlick, and TouchPinchs
- /// </summary>
- [Test, Category("Visual")]
- public static void TouchGestures()
- {
- Font infoFont = Font.Default;
- Application.Start(delegate
- {
- infoFont.Draw(
- "TouchTap " + Input.Touch.GetState(InputButton.GestureTap),
- Rectangle.FromCenter(new Point(0.5f, 0.2f), Size.Half));
- infoFont.Draw(
- "TouchDoubleTap " +
- Input.Touch.GetState(InputButton.GestureDoubleTap),
- Rectangle.FromCenter(new Point(0.5f, 0.24f), Size.Half));
- infoFont.Draw(
- "TouchHold " + Input.Touch.GetState(InputButton.GestureHold),
- Rectangle.FromCenter(new Point(0.5f, 0.28f), Size.Half));
- infoFont.Draw(
- "TouchDrag " + Input.Touch.GetState(InputButton.GestureDrag),
- Rectangle.FromCenter(new Point(0.5f, 0.32f), Size.Half));
- infoFont.Draw(
- "Drag " + Input.Touch.GetMovement(InputButton.GestureDrag),
- Rectangle.FromCenter(new Point(0.5f, 0.36f), Size.Half));
- infoFont.Draw(
- "TouchFlick " + Input.Touch.GetState(InputButton.GestureFlick),
- Rectangle.FromCenter(new Point(0.5f, 0.4f), Size.Half));
- infoFont.Draw(
- "Flick " + Input.Touch.GetMovement(InputButton.GestureFlick),
- Rectangle.FromCenter(new Point(0.5f, 0.44f), Size.Half));
- infoFont.Draw(
- "TouchDoubleDrag " +
- Input.Touch.GetState(InputButton.GestureDualDrag),
- Rectangle.FromCenter(new Point(0.5f, 0.48f), Size.Half));
- infoFont.Draw(
- "DoubleDrag " + Input.Touch.GetMovement(InputButton.GestureDualDrag),
- Rectangle.FromCenter(new Point(0.5f, 0.52f), Size.Half));
- infoFont.Draw(
- "Pinch " + Input.Touch.GetMovement(InputButton.GesturePinch),
- Rectangle.FromCenter(new Point(0.5f, 0.56f), Size.Half));
- });
- }
- #endregion
- }
- }