00001
00002
00003 namespace NewGamePhysics.Utilities
00004 {
00005 using System;
00006 using System.Collections.Generic;
00007 using System.Text;
00008
00009 using Microsoft.DirectX.DirectInput;
00010
00011 using Microsoft.Xna.Framework;
00012 using Microsoft.Xna.Framework.Input;
00013
00014 public class Gamepads
00015 {
00016 private List<DirectXInput> items;
00017
00018 public List<DirectXInput> Items
00019 {
00020 get
00021 {
00022 return items;
00023 }
00024 }
00028 public Gamepads()
00029 {
00030
00031 DeviceList gamepadInstanceList = Manager.GetDevices(DeviceType.Gamepad, EnumDevicesFlags.AttachedOnly);
00032 DeviceList joystickInstanceList = Manager.GetDevices(DeviceType.Joystick, EnumDevicesFlags.AttachedOnly);
00033
00034 items = new List<DirectXInput>(gamepadInstanceList.Count + joystickInstanceList.Count);
00035
00036 foreach (DeviceInstance deviceInstance in gamepadInstanceList)
00037 {
00038 DirectXInput item = new DirectXInput(deviceInstance.InstanceGuid);
00039 items.Add(item);
00040 }
00041
00042 foreach (DeviceInstance deviceInstance in joystickInstanceList)
00043 {
00044 DirectXInput item = new DirectXInput(deviceInstance.InstanceGuid);
00045 items.Add(item);
00046 }
00047 }
00048
00049 }
00050
00051 public class DirectXInput
00052 {
00053 protected Device device;
00054 public Device Device
00055 {
00056 get { return device; }
00057 }
00058
00059 public DirectXInput(Guid gamepadInstanceGuid)
00060 {
00061 device = new Device(gamepadInstanceGuid);
00062 device.SetDataFormat(DeviceDataFormat.Joystick);
00063 device.Acquire();
00064 }
00065
00066 public DirectInputThumbSticks ThumbSticks
00067 {
00068 get { return new DirectInputThumbSticks(device); }
00069 }
00070
00071 public DirectInputDPad DPad
00072 {
00073 get
00074 {
00075 JoystickState t = device.CurrentJoystickState;
00076 return new DirectInputDPad(t.GetPointOfView()[0]);
00077
00078 }
00079 }
00080
00081 public DirectInputButtons Buttons
00082 {
00083 get { return new DirectInputButtons(device); }
00084 }
00085
00086 #region Diagnostics
00087
00088 public string DiagnosticsThumbSticks
00089 {
00090 get
00091 {
00092 return
00093 "X" + Math.Round(ThumbSticks.Left.X, 4) +
00094 " Y" + Math.Round(ThumbSticks.Left.Y, 4) +
00095 " X" + Math.Round(ThumbSticks.Right.X, 4) +
00096 " Y" + Math.Round(ThumbSticks.Right.Y, 4);
00097 }
00098 }
00099
00100 public string DiagnosticsRawGamepadData
00101 {
00102 get
00103 {
00104 return
00105 "X" + Device.CurrentJoystickState.X +
00106 " Y" + Device.CurrentJoystickState.Y +
00107 " Z" + Device.CurrentJoystickState.Z +
00108 " Rx" + Device.CurrentJoystickState.Rx +
00109 " Ry" + Device.CurrentJoystickState.Ry +
00110 " Rz" + Device.CurrentJoystickState.Rz +
00111 " pov[0]" + Device.CurrentJoystickState.GetPointOfView()[0];
00112 }
00113 }
00114
00115 public string DiagnosticsButtons
00116 {
00117 get
00118 {
00119 System.Text.StringBuilder sb = new System.Text.StringBuilder();
00120
00121 int i = 0;
00122 foreach (ButtonState bs in Buttons.List)
00123 {
00124 sb.Append(i);
00125 sb.Append("=");
00126 sb.Append((bs == ButtonState.Pressed ? "1" : "0"));
00127 sb.Append(" ");
00128 i++;
00129 }
00130
00131 return sb.ToString();
00132 }
00133 }
00134
00135 #endregion
00136
00137 }
00138
00147 public struct DirectInputThumbSticks
00148 {
00153 public Vector2 Left;
00154 public Vector2 Right;
00155 public Vector2 Third;
00156
00157
00158 public bool HasLeft;
00159 public bool HasRight;
00160 public bool HasThird;
00161
00162
00163 const float center = 32767.5f;
00164
00165 public DirectInputThumbSticks(Device device)
00166 {
00167 JoystickState t = device.CurrentJoystickState;
00168
00169 HasLeft = false;
00170 Left = Vector2.Zero;
00171 HasRight = false;
00172 Right = Vector2.Zero;
00173 HasThird = false;
00174 Third = Vector2.Zero;
00175
00176 if (device.Caps.NumberAxes > 0)
00177 {
00178 HasLeft = true;
00179 Left = new Vector2((t.X - center) / center, (t.Y - center) / center);
00180
00181 if (device.Caps.NumberAxes > 2)
00182 {
00183 HasRight = true;
00184 Right = new Vector2((t.Rz - center) / center, (t.Z - center) / center);
00185
00186 if (device.Caps.NumberAxes > 4)
00187 {
00188 HasThird = true;
00189 Third = new Vector2((t.Rx - center) / center, (t.Ry - center) / center);
00190 }
00191 }
00192 }
00193 }
00194 }
00195
00196 public struct DirectInputDPad
00197 {
00198 public ButtonState Up;
00199 public ButtonState Right;
00200 public ButtonState Down;
00201 public ButtonState Left;
00202
00203 public DirectInputDPad(int direction)
00204 {
00205 Up = ButtonState.Released;
00206 Right = ButtonState.Released;
00207 Down = ButtonState.Released;
00208 Left = ButtonState.Released;
00209
00210 if (direction == -1)
00211 return;
00212
00213 if (direction > 27000 || direction < 9000)
00214 {
00215 Up = ButtonState.Pressed;
00216 }
00217
00218 if (0 < direction && direction < 18000)
00219 {
00220 Right = ButtonState.Pressed;
00221 }
00222
00223 if (9000 < direction && direction < 27000)
00224 {
00225 Down = ButtonState.Pressed;
00226 }
00227
00228 if (18000 < direction)
00229 {
00230 Left = ButtonState.Pressed;
00231 }
00232 }
00233 }
00234
00245 public struct DirectInputButtons
00246 {
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 public List<ButtonState> List;
00261
00262 public DirectInputButtons(Device device)
00263 {
00264 byte[] buttons = device.CurrentJoystickState.GetButtons();
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 int numButtons = device.Caps.NumberButtons;
00280 List = new List<ButtonState>(numButtons);
00281
00282 for (int i = 0; i < numButtons; i++)
00283 {
00284 List.Add((buttons[i] == 0 ? ButtonState.Released : ButtonState.Pressed));
00285 }
00286 }
00287 }
00288 }