PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/FITClub.ExternalClasses/Controls/PCGamePad.cs

http://fitclub.codeplex.com
C# | 258 lines | 222 code | 22 blank | 14 comment | 90 complexity | add57c142b29508e75b664458ce3254b MD5 | raw file
  1. #region File Description
  2. // --------------------------------
  3. // <copyright file="PCGamePad.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
  4. // This document is shared by Microsoft Public License (MS-PL)
  5. // For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
  6. // </copyright>
  7. // <author>Vladislav Richter</author>
  8. // <email>richtvl2@fit.cvut.cz</email>
  9. // <date>9. 11. 2011 22:22:37</date>
  10. // <summary>
  11. //
  12. // </summary>
  13. // ------------------------------------------------------------------------------------------------------
  14. #endregion
  15. #if WINDOWS
  16. using System.Collections.Generic;
  17. using Microsoft.Xna.Framework.Input;
  18. using Nuclex.Input;
  19. namespace ExternalClasses.Controls
  20. {
  21. public enum PCGamePadButtons { Up, Down, Left, Right, Attack, Block }
  22. public struct PCGamePadState
  23. {
  24. public bool Up, Down, Left, Right, Attack, Block, Reset;
  25. public bool IsButtonDown(PCGamePadButtons button)
  26. {
  27. switch (button)
  28. {
  29. case PCGamePadButtons.Up: return Up;
  30. case PCGamePadButtons.Down: return Down;
  31. case PCGamePadButtons.Left: return Left;
  32. case PCGamePadButtons.Right: return Right;
  33. case PCGamePadButtons.Attack: return Attack;
  34. case PCGamePadButtons.Block: return Block;
  35. default: return false;
  36. }
  37. }
  38. }
  39. public struct PCGamePadsInfo
  40. {
  41. public ExtendedPlayerIndex index;
  42. public string Name;
  43. }
  44. public class PCGamePad
  45. {
  46. public static InputManager input{set; get;}
  47. public static int GetNumberOfPCGamePadsConnected()
  48. {
  49. int count = 0;
  50. if (input.GetGamePad(ExtendedPlayerIndex.Five).IsAttached) count++;
  51. if (input.GetGamePad(ExtendedPlayerIndex.Six).IsAttached) count++;
  52. if (input.GetGamePad(ExtendedPlayerIndex.Seven).IsAttached) count++;
  53. if (input.GetGamePad(ExtendedPlayerIndex.Eight).IsAttached) count++;
  54. return count;
  55. }
  56. public static List<PCGamePadsInfo> GetPCGamePadsInfos()
  57. {
  58. List<PCGamePadsInfo> Result = new List<PCGamePadsInfo>();
  59. if (input.GetGamePad(ExtendedPlayerIndex.Five).IsAttached)
  60. {
  61. PCGamePadsInfo info = new PCGamePadsInfo();
  62. info.index = ExtendedPlayerIndex.Five;
  63. info.Name = input.GetGamePad(ExtendedPlayerIndex.Five).Name;
  64. Result.Add(info);
  65. }
  66. if (input.GetGamePad(ExtendedPlayerIndex.Six).IsAttached)
  67. {
  68. PCGamePadsInfo info = new PCGamePadsInfo();
  69. info.index = ExtendedPlayerIndex.Six;
  70. info.Name = input.GetGamePad(ExtendedPlayerIndex.Six).Name;
  71. Result.Add(info);
  72. }
  73. if (input.GetGamePad(ExtendedPlayerIndex.Seven).IsAttached)
  74. {
  75. PCGamePadsInfo info = new PCGamePadsInfo();
  76. info.index = ExtendedPlayerIndex.Seven;
  77. info.Name = input.GetGamePad(ExtendedPlayerIndex.Seven).Name;
  78. Result.Add(info);
  79. }
  80. if (input.GetGamePad(ExtendedPlayerIndex.Eight).IsAttached)
  81. {
  82. PCGamePadsInfo info = new PCGamePadsInfo();
  83. info.index = ExtendedPlayerIndex.Eight;
  84. info.Name = input.GetGamePad(ExtendedPlayerIndex.Eight).Name;
  85. Result.Add(info);
  86. }
  87. return Result;
  88. }
  89. public static bool IsAnyButtonPressed(ExtendedPlayerIndex index)
  90. {
  91. if (input.GetGamePad(index).GetExtendedState().GetButton(11) == ButtonState.Pressed) return true;
  92. if (input.GetGamePad(index).GetExtendedState().GetButton(10) == ButtonState.Pressed) return true;
  93. if (input.GetGamePad(index).GetExtendedState().GetButton(9) == ButtonState.Pressed) return true;
  94. if (input.GetGamePad(index).GetExtendedState().GetButton(8) == ButtonState.Pressed) return true;
  95. if (input.GetGamePad(index).GetExtendedState().GetButton(7) == ButtonState.Pressed) return true;
  96. if (input.GetGamePad(index).GetExtendedState().GetButton(6) == ButtonState.Pressed) return true;
  97. if (input.GetGamePad(index).GetExtendedState().GetButton(5) == ButtonState.Pressed) return true;
  98. if (input.GetGamePad(index).GetExtendedState().GetButton(4) == ButtonState.Pressed) return true;
  99. if (input.GetGamePad(index).GetExtendedState().GetButton(3) == ButtonState.Pressed) return true;
  100. if (input.GetGamePad(index).GetExtendedState().GetButton(2) == ButtonState.Pressed) return true;
  101. if (input.GetGamePad(index).GetExtendedState().GetButton(1) == ButtonState.Pressed) return true;
  102. if (input.GetGamePad(index).GetExtendedState().GetButton(0) == ButtonState.Pressed) return true;
  103. return false;
  104. }
  105. public static bool IsAnyButtonAtAnyGamePadPressed()
  106. {
  107. if (IsAnyButtonPressed(ExtendedPlayerIndex.Five)) return true;
  108. if (IsAnyButtonPressed(ExtendedPlayerIndex.Six)) return true;
  109. if (IsAnyButtonPressed(ExtendedPlayerIndex.Seven)) return true;
  110. if (IsAnyButtonPressed(ExtendedPlayerIndex.Eight)) return true;
  111. return false;
  112. }
  113. public static ExtendedPlayerIndex GetNextPCGamepad()
  114. {
  115. if (IsAnyButtonPressed(ExtendedPlayerIndex.Five)) return ExtendedPlayerIndex.Five;
  116. if (IsAnyButtonPressed(ExtendedPlayerIndex.Six)) return ExtendedPlayerIndex.Six;
  117. if (IsAnyButtonPressed(ExtendedPlayerIndex.Seven)) return ExtendedPlayerIndex.Seven;
  118. if (IsAnyButtonPressed(ExtendedPlayerIndex.Eight)) return ExtendedPlayerIndex.Eight;
  119. return ExtendedPlayerIndex.Five;
  120. }
  121. public static bool AnyPCGamePadUpPressed()
  122. {
  123. if (GetState(ExtendedPlayerIndex.Five).Up) return true;
  124. if (GetState(ExtendedPlayerIndex.Six).Up) return true;
  125. if (GetState(ExtendedPlayerIndex.Seven).Up) return true;
  126. if (GetState(ExtendedPlayerIndex.Eight).Up) return true;
  127. return false;
  128. }
  129. public static bool AnyPCGamePadDownPressed()
  130. {
  131. if (GetState(ExtendedPlayerIndex.Five).Down) return true;
  132. if (GetState(ExtendedPlayerIndex.Six).Down) return true;
  133. if (GetState(ExtendedPlayerIndex.Seven).Down) return true;
  134. if (GetState(ExtendedPlayerIndex.Eight).Down) return true;
  135. return false;
  136. }
  137. public static bool AnyPCGamePadLeftPressed()
  138. {
  139. if (GetState(ExtendedPlayerIndex.Five).Left) return true;
  140. if (GetState(ExtendedPlayerIndex.Six).Left) return true;
  141. if (GetState(ExtendedPlayerIndex.Seven).Left) return true;
  142. if (GetState(ExtendedPlayerIndex.Eight).Left) return true;
  143. return false;
  144. }
  145. public static bool AnyPCGamePadRightPressed()
  146. {
  147. if (GetState(ExtendedPlayerIndex.Five).Right) return true;
  148. if (GetState(ExtendedPlayerIndex.Six).Right) return true;
  149. if (GetState(ExtendedPlayerIndex.Seven).Right) return true;
  150. if (GetState(ExtendedPlayerIndex.Eight).Right) return true;
  151. return false;
  152. }
  153. public static bool AnyPCGamePadAttackPressed()
  154. {
  155. if (GetState(ExtendedPlayerIndex.Five).Attack) return true;
  156. if (GetState(ExtendedPlayerIndex.Six).Attack) return true;
  157. if (GetState(ExtendedPlayerIndex.Seven).Attack) return true;
  158. if (GetState(ExtendedPlayerIndex.Eight).Attack) return true;
  159. return false;
  160. }
  161. public static bool AnyPCGamePadBlockPressed()
  162. {
  163. if (GetState(ExtendedPlayerIndex.Five).Block) return true;
  164. if (GetState(ExtendedPlayerIndex.Six).Block) return true;
  165. if (GetState(ExtendedPlayerIndex.Seven).Block) return true;
  166. if (GetState(ExtendedPlayerIndex.Eight).Block) return true;
  167. return false;
  168. }
  169. public static PCGamePadState AnyPCGamePadGetState()
  170. {
  171. PCGamePadState result = new PCGamePadState();
  172. if (AnyPCGamePadUpPressed()) result.Up = true;
  173. if (AnyPCGamePadDownPressed()) result.Down = true;
  174. if (AnyPCGamePadLeftPressed()) result.Left = true;
  175. if (AnyPCGamePadRightPressed()) result.Right = true;
  176. if (AnyPCGamePadAttackPressed()) result.Attack = true;
  177. if (AnyPCGamePadBlockPressed()) result.Block = true;
  178. return result;
  179. }
  180. public static PCGamePadState GetState(ExtendedPlayerIndex Index)
  181. {
  182. PCGamePadState result = new PCGamePadState();
  183. if (input.GetGamePad(Index).GetExtendedState().GetButton(8) == ButtonState.Pressed) result.Reset = true;
  184. if (input.GetGamePad(Index).GetState().Buttons.RightShoulder == ButtonState.Pressed) result.Attack = true;
  185. //if (input.GetGamePad(Index).GetState().Buttons.RightStick == ButtonState.Pressed) result.Attack = true;
  186. if (input.GetGamePad(Index).GetExtendedState().GetButton(2) == ButtonState.Pressed) result.Attack = true;
  187. if (input.GetGamePad(Index).GetState().Buttons.LeftShoulder == ButtonState.Pressed) result.Block = true;
  188. //if (input.GetGamePad(Index).GetState().Buttons.LeftStick == ButtonState.Pressed) result.Block = true;
  189. if (input.GetGamePad(Index).GetExtendedState().GetButton(3) == ButtonState.Pressed) result.Block = true;
  190. int direction = input.GetGamePad(Index).GetExtendedState().Pov1;
  191. switch (direction)
  192. {
  193. case 00:
  194. result.Up = true;
  195. break;
  196. case 4500:
  197. result.Up = true;
  198. result.Right = true;
  199. break;
  200. case 9000:
  201. result.Right = true;
  202. break;
  203. case 13500:
  204. result.Right = true;
  205. result.Down = true;
  206. break;
  207. case 18000:
  208. result.Down = true;
  209. break;
  210. case 22500:
  211. result.Left = true;
  212. result.Down = true;
  213. break;
  214. case 27000:
  215. result.Left = true;
  216. break;
  217. case 31500:
  218. result.Left = true;
  219. result.Up = true;
  220. break;
  221. default:
  222. break;
  223. }
  224. if (input.GetGamePad(Index).GetState().ThumbSticks.Left.X < -0.5f) result.Left = true;
  225. if (input.GetGamePad(Index).GetState().ThumbSticks.Left.X > +0.5f) result.Right = true;
  226. if (input.GetGamePad(Index).GetState().ThumbSticks.Left.Y < -0.5f) result.Down = true;
  227. if (input.GetGamePad(Index).GetState().ThumbSticks.Left.Y > +0.5f) result.Up = true;
  228. if (input.GetGamePad(Index).GetState().ThumbSticks.Right.X < -0.5f) result.Left = true;
  229. if (input.GetGamePad(Index).GetState().ThumbSticks.Right.X > +0.5f) result.Right = true;
  230. if (input.GetGamePad(Index).GetState().ThumbSticks.Right.Y < -0.5f) result.Down = true;
  231. if (input.GetGamePad(Index).GetState().ThumbSticks.Right.Y > +0.5f) result.Up = true;
  232. return result;
  233. }
  234. }
  235. }
  236. #endif