Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
InputManager.Windows.XInput.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP || SILICONSTUDIO_PLATFORM_WINDOWS_STORE
4 using System;
5 using System.Collections.Generic;
6 
7 using SharpDX.XInput;
8 
9 namespace SiliconStudio.Paradox.Input
10 {
11  public partial class InputManager
12  {
13  /// <summary>
14  /// Internal GamePad factory handling XInput gamepads.
15  /// </summary>
16  private class XInputGamePadFactory : GamePadFactory
17  {
18  private const int XInputGamePadCount = 4;
19 
20  private static readonly Guid[] ControllerGuids = new Guid[XInputGamePadCount];
21 
22  private readonly Controller[] controllers;
23 
24  /// <summary>
25  /// Initializes static members of the <see cref="InputManager" /> class.
26  /// </summary>
27  static XInputGamePadFactory()
28  {
29  // Prebuild fake GUID
30  for (int i = 0; i < XInputGamePadCount; i++)
31  {
32  ControllerGuids[i] = new Guid(i, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
33  }
34  }
35 
36  public XInputGamePadFactory()
37  {
38  controllers = new Controller[XInputGamePadCount];
39  for (int i = 0; i < controllers.Length; i++)
40  {
41  controllers[i] = new Controller((UserIndex)i);
42  }
43  }
44 
45  public override IEnumerable<GamePadKey> GetConnectedPads()
46  {
47  // Check that the XInput.dll is present, if not reset the controllers.
48  try
49  {
50  // ReSharper disable once UnusedVariable
51  var toto = controllers[0].IsConnected;
52  }
53  catch (DllNotFoundException ex)
54  {
55  for (int i = 0; i < XInputGamePadCount; i++)
56  controllers[i] = null;
57 
58  Logger.Warning("XInput dll was not found on the computer. GamePad detection will not fully work for the current game instance. " +
59  "To fix the problem, please install or repair DirectX installation. [Exception details: {0}]", ex.Message);
60 
61  yield break;
62  }
63 
64  // Return only connected controllers
65  foreach (var xinputController in controllers)
66  {
67  if (xinputController.IsConnected)
68  yield return new GamePadKey(ControllerGuids[(int)xinputController.UserIndex], this);
69  }
70  }
71 
72  public override GamePad GetGamePad(Guid guid)
73  {
74  for (int i = 0; i < XInputGamePadCount; i++)
75  {
76  if (controllers[i] != null && ControllerGuids[i] == guid)
77  {
78  return new XInputGamePad(controllers[i], new GamePadKey(guid, this));
79  }
80  }
81  return null;
82  }
83  }
84 
85  /// <summary>
86  /// Internal gamepad handling XInput gamepads.
87  /// </summary>
88  private class XInputGamePad : GamePad
89  {
90 #region Constants and Fields
91 
92  private Controller instance;
93 
94  #endregion
95 
96  public XInputGamePad(Controller instance, GamePadKey key) : base(key)
97  {
98  this.instance = instance;
99  }
100 
101  public override void Dispose()
102  {
103  instance = null;
104  }
105 
106  public override GamePadState GetState()
107  {
108  var gamePadState = new GamePadState();
109 
110  State xinputState;
111  if (instance.GetState(out xinputState))
112  {
113  gamePadState.IsConnected = true;
114  gamePadState.Buttons = (GamePadButton)xinputState.Gamepad.Buttons;
115 
116  gamePadState.LeftTrigger = xinputState.Gamepad.LeftTrigger / 255.0f;
117  gamePadState.RightTrigger = xinputState.Gamepad.RightTrigger / 255.0f;
118 
119  gamePadState.LeftThumb.X = ClampDeadZone(xinputState.Gamepad.LeftThumbX / 32768.0f, Gamepad.LeftThumbDeadZone / 32768.0f);
120  gamePadState.LeftThumb.Y = ClampDeadZone(xinputState.Gamepad.LeftThumbY / 32768.0f, Gamepad.LeftThumbDeadZone / 32768.0f);
121 
122  gamePadState.RightThumb.X = ClampDeadZone(xinputState.Gamepad.RightThumbX / 32768.0f, Gamepad.RightThumbDeadZone / 32768.0f);
123  gamePadState.RightThumb.Y = ClampDeadZone(xinputState.Gamepad.RightThumbY / 32768.0f, Gamepad.RightThumbDeadZone / 32768.0f);
124  }
125 
126  return gamePadState;
127  }
128  }
129  }
130 }
131 
132 #endif
A gamepad virtual button.
GamePadButton
Buttons for gamepad returned by GamePadState.