2011-12-14 19:22:17 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using ANX.Framework.NonXNA.InputSystem;
|
2012-08-11 13:06:29 +00:00
|
|
|
using ANX.Framework.NonXNA.Reflection;
|
2012-08-09 09:45:04 +00:00
|
|
|
|
|
|
|
// This file is part of the ANX.Framework created by the
|
|
|
|
// "ANX.Framework developer group" and released under the Ms-PL license.
|
|
|
|
// For details see: http://anxframework.codeplex.com/license
|
2011-12-14 19:22:17 +00:00
|
|
|
|
|
|
|
namespace ANX.Framework.NonXNA
|
|
|
|
{
|
2012-08-09 09:45:04 +00:00
|
|
|
public class InputDeviceFactory
|
|
|
|
{
|
|
|
|
#region Constants
|
|
|
|
internal static readonly Type[] ValidInputDeviceCreators =
|
|
|
|
{
|
|
|
|
typeof(IGamePadCreator),
|
|
|
|
typeof(IKeyboardCreator),
|
|
|
|
typeof(IMouseCreator),
|
2012-08-25 17:27:45 +00:00
|
|
|
typeof(ITouchPanelCreator),
|
2011-12-14 19:22:17 +00:00
|
|
|
#if XNAEXT
|
2012-08-09 09:45:04 +00:00
|
|
|
typeof(IMotionSensingDeviceCreator),
|
2011-12-14 19:22:17 +00:00
|
|
|
#endif
|
2012-08-09 09:45:04 +00:00
|
|
|
};
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Private
|
|
|
|
private static InputDeviceFactory instance;
|
2012-08-25 17:27:45 +00:00
|
|
|
private Dictionary<Type, Dictionary<string, IInputDeviceCreator>> deviceCreators;
|
2012-08-09 09:45:04 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Public
|
|
|
|
public static InputDeviceFactory Instance
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (instance == null)
|
|
|
|
{
|
|
|
|
instance = new InputDeviceFactory();
|
|
|
|
Logger.Info("Created InputDeviceFactory instance");
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IntPtr WindowHandle
|
|
|
|
{
|
2012-08-25 17:27:45 +00:00
|
|
|
get;
|
|
|
|
internal set;
|
2012-08-09 09:45:04 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructor
|
|
|
|
private InputDeviceFactory()
|
|
|
|
{
|
2012-08-22 09:51:35 +00:00
|
|
|
deviceCreators = new Dictionary<Type, Dictionary<string, IInputDeviceCreator>>();
|
2012-08-25 17:27:45 +00:00
|
|
|
foreach (Type creatorType in ValidInputDeviceCreators)
|
|
|
|
deviceCreators.Add(creatorType, new Dictionary<string, IInputDeviceCreator>());
|
2012-08-22 09:51:35 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
#region AddCreator
|
2012-08-25 17:27:45 +00:00
|
|
|
internal void AddCreator(Type deviceType, IInputDeviceCreator creator)
|
2012-08-09 09:45:04 +00:00
|
|
|
{
|
|
|
|
string creatorName = creator.Name.ToLowerInvariant();
|
|
|
|
Type deviceInterface = TypeHelper.GetInterfacesFrom(deviceType)[0];
|
2012-08-25 17:27:45 +00:00
|
|
|
|
2012-08-09 09:45:04 +00:00
|
|
|
if (deviceCreators[deviceInterface].ContainsKey(creatorName))
|
2012-08-22 09:51:35 +00:00
|
|
|
throw new Exception("Duplicate " + deviceType.Name + " found. A " + deviceType.Name +
|
|
|
|
" with the name '" + creator.Name + "' was already registered.");
|
2012-08-09 09:45:04 +00:00
|
|
|
|
|
|
|
deviceCreators[deviceInterface].Add(creatorName, creator);
|
|
|
|
|
2012-08-25 17:27:45 +00:00
|
|
|
Logger.Info("Added InputDeviceCreator '{0}'. Registered creators: {1}.", creatorName,
|
|
|
|
deviceCreators[deviceInterface].Count);
|
2012-08-09 09:45:04 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-08-22 09:51:35 +00:00
|
|
|
#region GetDefaultTouchPanel
|
|
|
|
public ITouchPanel GetDefaultTouchPanel()
|
2012-08-09 09:45:04 +00:00
|
|
|
{
|
2012-08-22 09:51:35 +00:00
|
|
|
ValidateWindowHandle();
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2012-08-25 17:27:45 +00:00
|
|
|
var touchPanel = GetDefaultCreator<ITouchPanelCreator>().CreateDevice();
|
|
|
|
touchPanel.WindowHandle = WindowHandle;
|
2012-08-22 09:51:35 +00:00
|
|
|
return touchPanel;
|
|
|
|
}
|
|
|
|
#endregion
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2012-08-25 17:27:45 +00:00
|
|
|
#region CreateDefaultGamePad
|
|
|
|
public IGamePad CreateDefaultGamePad()
|
2012-08-22 09:51:35 +00:00
|
|
|
{
|
2012-08-25 17:27:45 +00:00
|
|
|
return GetDefaultCreator<IGamePadCreator>().CreateDevice();
|
2012-08-09 09:45:04 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-08-25 17:27:45 +00:00
|
|
|
#region CreateDefaultMouse
|
|
|
|
public IMouse CreateDefaultMouse()
|
2012-08-09 09:45:04 +00:00
|
|
|
{
|
2012-08-22 09:51:35 +00:00
|
|
|
ValidateWindowHandle();
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2012-08-25 17:27:45 +00:00
|
|
|
var mouse = GetDefaultCreator<IMouseCreator>().CreateDevice();
|
|
|
|
mouse.WindowHandle = WindowHandle;
|
2012-08-22 09:51:35 +00:00
|
|
|
return mouse;
|
2012-08-09 09:45:04 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-08-25 17:27:45 +00:00
|
|
|
#region CreateDefaultKeyboard
|
|
|
|
public IKeyboard CreateDefaultKeyboard()
|
2012-08-09 09:45:04 +00:00
|
|
|
{
|
2012-08-22 09:51:35 +00:00
|
|
|
ValidateWindowHandle();
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2012-08-25 17:27:45 +00:00
|
|
|
var keyboard = GetDefaultCreator<IKeyboardCreator>().CreateDevice();
|
|
|
|
keyboard.WindowHandle = WindowHandle;
|
2012-08-22 09:51:35 +00:00
|
|
|
return keyboard;
|
2012-08-09 09:45:04 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
2012-08-25 17:27:45 +00:00
|
|
|
#region CreateDefaultMotionSensingDevice
|
2011-12-14 19:22:17 +00:00
|
|
|
#if XNAEXT
|
2012-08-25 17:27:45 +00:00
|
|
|
public IMotionSensingDevice CreateDefaultMotionSensingDevice()
|
2012-08-09 09:45:04 +00:00
|
|
|
{
|
2012-08-25 17:27:45 +00:00
|
|
|
return GetDefaultCreator<IMotionSensingDeviceCreator>().CreateDevice();
|
2012-08-22 09:51:35 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endregion
|
2012-08-09 09:45:04 +00:00
|
|
|
|
2012-08-22 09:51:35 +00:00
|
|
|
#region GetDefaultCreator
|
2012-08-25 17:27:45 +00:00
|
|
|
private T GetDefaultCreator<T>()
|
2012-08-22 09:51:35 +00:00
|
|
|
{
|
|
|
|
Type creatorType = typeof(T);
|
|
|
|
if (deviceCreators.ContainsKey(creatorType))
|
2012-08-09 09:45:04 +00:00
|
|
|
{
|
2012-08-22 09:51:35 +00:00
|
|
|
var creators = deviceCreators[creatorType];
|
|
|
|
if (creators.Count > 0)
|
|
|
|
return (T)creators.Values.First<IInputDeviceCreator>();
|
2012-08-09 09:45:04 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 09:51:35 +00:00
|
|
|
throw new Exception("Unable to find a default creator for type " + creatorType);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ValidateWindowHandle
|
|
|
|
private void ValidateWindowHandle()
|
|
|
|
{
|
2012-08-25 17:27:45 +00:00
|
|
|
if (WindowHandle == IntPtr.Zero)
|
2012-08-22 09:51:35 +00:00
|
|
|
throw new Exception("Unable to create a mouse instance because the WindowHandle was not set.");
|
2012-08-09 09:45:04 +00:00
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
2011-12-14 19:22:17 +00:00
|
|
|
}
|