// IInputManager.cs
// --------------------
// Creating a simple interface such as this allows the manager
// to be used as a service making it much more accessible
// elsewhere in the code.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
namespace InputManagement.Demo
{
interface IInputManager : IGameComponent, IUpdateable, IDisposable
{
/// <summary>
/// Returns the last touch gesture or null if none found
/// </summary>
GestureType? TouchGestureType { get; }
/// <summary>
/// Returns the most recent gesture sample
/// </summary>
GestureSample? TouchGestureSample { get; }
/// <summary>
/// Returns true if the provided gesture was detected
/// </summary>
/// <param name="gesture">The GestureType to check for</param>
/// <returns>True if the provided gesture is detected</returns>
bool TouchGestureDetected(GestureType gesture);
}
}
// InputManager.cs
// ----------------------
// By implementing the IInputManager interface we keep the
// managements functions well defined and can allow future
// classes to take it's place.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;
namespace FreakyMobile.InputManagement
{
class InputManager : GameComponent, IInputManager
{
GestureSample? sample = null;
public GestureSample? TouchGestureSample { get { return this.sample; } }
GestureType? gestureType = null;
public GestureType? TouchGestureType { get { return this.gestureType; } }
private TimeSpan timer = TimeSpan.Zero;
private TimeSpan tick = TimeSpan.FromSeconds(0.5);
public InputManager(Game game)
: base(game)
{
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
sample = null;
gestureType = null;
while (TouchPanel.IsGestureAvailable)
{
sample = TouchPanel.ReadGesture();
gestureType = sample.Value.GestureType;
}
}
public bool TouchGestureDetected(GestureType gesture)
{
if (TouchGestureType == gesture)
return true;
return false;
}
}
}