Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
InputManager.Windows.DirectInput.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
4 
5 using System;
6 using System.Collections.Generic;
7 using System.Linq;
8 
10 
11 using SharpDX.DirectInput;
12 
13 namespace SiliconStudio.Paradox.Input
14 {
15  public partial class InputManager
16  {
17  /// <summary>
18  /// Internal GamePad factory handling DirectInput gamepads.
19  /// </summary>
20  private class DirectInputGamePadFactory : GamePadFactory
21  {
22  private readonly DirectInput directInput;
23 
24  public DirectInputGamePadFactory()
25  {
26  directInput = new DirectInput();
27  }
28 
29  public override IEnumerable<GamePadKey> GetConnectedPads()
30  {
31  return directInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AllDevices).Select(deviceInstance => new GamePadKey(deviceInstance.InstanceGuid, this));
32  }
33 
34  public override GamePad GetGamePad(Guid guid)
35  {
36  return new DirectInputGamePad(directInput, new GamePadKey(guid, this));
37  }
38  }
39 
40  /// <summary>
41  /// Internal gamepad handling DirectInput gamepads.
42  /// </summary>
43  private class DirectInputGamePad : GamePad
44  {
45  #region Constants and Fields
46 
47  private readonly GamePadKey key;
48 
49  private readonly DirectInput directInput;
50 
51  private Joystick instance;
52 
53  private JoystickState joystickState;
54 
55  #endregion
56 
57  public DirectInputGamePad(DirectInput directInput, GamePadKey key) : base(key)
58  {
59  this.key = key;
60  this.directInput = directInput;
61  this.instance = new Joystick(directInput, key.Guid);
62  joystickState = new JoystickState();
63  }
64 
65  public override void Dispose()
66  {
67  if (instance != null)
68  {
69  instance.Dispose();
70  instance = null;
71  }
72  }
73 
74  public override GamePadState GetState()
75  {
76  var gamePadState = new GamePadState();
77 
78  // Get the current joystick state
79  try
80  {
81  if (instance == null)
82  {
83  if (directInput.IsDeviceAttached(key.Guid))
84  {
85  instance = new Joystick(directInput, key.Guid);
86  }
87  else
88  {
89  return gamePadState;
90  }
91  }
92 
93  // Acquire the joystick
94  instance.Acquire();
95 
96  // Make sure that the latest state is up to date
97  instance.Poll();
98 
99  // Get the state
100  instance.GetCurrentState(ref joystickState);
101  }
102  catch (SharpDX.SharpDXException)
103  {
104  // If there was an exception, dispose the native instance
105  try
106  {
107  if (instance != null)
108  {
109  instance.Dispose();
110  instance = null;
111  }
112 
113  // Return a GamePadState that specify that it is not connected
114  return gamePadState;
115  }
116  catch (Exception)
117  {
118  }
119  }
120 
121  //Console.WriteLine(joystickState);
122  gamePadState.IsConnected = true;
123 
124  gamePadState.Buttons = GamePadButton.None;
125  if (joystickState.Buttons[0])
126  {
127  gamePadState.Buttons |= GamePadButton.X;
128  }
129  if (joystickState.Buttons[1])
130  {
131  gamePadState.Buttons |= GamePadButton.A;
132  }
133  if (joystickState.Buttons[2])
134  {
135  gamePadState.Buttons |= GamePadButton.B;
136  }
137  if (joystickState.Buttons[3])
138  {
139  gamePadState.Buttons |= GamePadButton.Y;
140  }
141  if (joystickState.Buttons[4])
142  {
143  gamePadState.Buttons |= GamePadButton.LeftShoulder;
144  }
145  if (joystickState.Buttons[5])
146  {
147  gamePadState.Buttons |= GamePadButton.RightShoulder;
148  }
149  if (joystickState.Buttons[6])
150  {
151  gamePadState.LeftTrigger = 1.0f;
152  }
153  if (joystickState.Buttons[7])
154  {
155  gamePadState.RightTrigger = 1.0f;
156  }
157 
158  if (joystickState.Buttons[8])
159  {
160  gamePadState.Buttons |= GamePadButton.Back;
161  }
162  if (joystickState.Buttons[9])
163  {
164  gamePadState.Buttons |= GamePadButton.Start;
165  }
166 
167  if (joystickState.Buttons[10])
168  {
169  gamePadState.Buttons |= GamePadButton.LeftThumb;
170  }
171  if (joystickState.Buttons[11])
172  {
173  gamePadState.Buttons |= GamePadButton.RightThumb;
174  }
175 
176  int dPadRawValue = joystickState.PointOfViewControllers[0];
177  if (dPadRawValue >= 0)
178  {
179  int dPadValue = dPadRawValue / 4500;
180  switch (dPadValue)
181  {
182  case 0:
183  gamePadState.Buttons |= GamePadButton.PadUp;
184  break;
185  case 1:
186  gamePadState.Buttons |= GamePadButton.PadUp;
187  gamePadState.Buttons |= GamePadButton.PadRight;
188  break;
189  case 2:
190  gamePadState.Buttons |= GamePadButton.PadRight;
191  break;
192  case 3:
193  gamePadState.Buttons |= GamePadButton.PadRight;
194  gamePadState.Buttons |= GamePadButton.PadDown;
195  break;
196  case 4:
197  gamePadState.Buttons |= GamePadButton.PadDown;
198  break;
199  case 5:
200  gamePadState.Buttons |= GamePadButton.PadDown;
201  gamePadState.Buttons |= GamePadButton.PadLeft;
202  break;
203  case 6:
204  gamePadState.Buttons |= GamePadButton.PadLeft;
205  break;
206  case 7:
207  gamePadState.Buttons |= GamePadButton.PadLeft;
208  gamePadState.Buttons |= GamePadButton.PadUp;
209  break;
210  }
211  }
212 
213  // Left Thumb
214  gamePadState.LeftThumb = new Vector2(2.0f * (joystickState.X / 65535.0f - 0.5f), -2.0f * (joystickState.Y / 65535.0f - 0.5f));
215  gamePadState.LeftThumb.X = ClampDeadZone(gamePadState.LeftThumb.X, GamePadAxisDeadZone);
216  gamePadState.LeftThumb.Y = ClampDeadZone(gamePadState.LeftThumb.Y, GamePadAxisDeadZone);
217 
218  // Right Thumb
219  gamePadState.RightThumb = new Vector2(2.0f * (joystickState.Z / 65535.0f - 0.5f), -2.0f * (joystickState.RotationZ / 65535.0f - 0.5f));
220  gamePadState.RightThumb.X = ClampDeadZone(gamePadState.RightThumb.X, GamePadAxisDeadZone);
221  gamePadState.RightThumb.Y = ClampDeadZone(gamePadState.RightThumb.Y, GamePadAxisDeadZone);
222 
223  return gamePadState;
224  }
225  }
226  }
227 }
228 #endif
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
A gamepad virtual button.