InputManager.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // InputManager.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Input;
  14. #endregion
  15. namespace RolePlaying
  16. {
  17. /// <summary>
  18. /// This class handles all keyboard and gamepad actions in the game.
  19. /// </summary>
  20. public static class InputManager
  21. {
  22. #region Action Enumeration
  23. /// <summary>
  24. /// The actions that are possible within the game.
  25. /// </summary>
  26. public enum Action
  27. {
  28. MainMenu,
  29. Ok,
  30. Back,
  31. CharacterManagement,
  32. ExitGame,
  33. TakeView,
  34. DropUnEquip,
  35. MoveCharacterUp,
  36. MoveCharacterDown,
  37. MoveCharacterLeft,
  38. MoveCharacterRight,
  39. CursorUp,
  40. CursorDown,
  41. DecreaseAmount,
  42. IncreaseAmount,
  43. PageLeft,
  44. PageRight,
  45. TargetUp,
  46. TargetDown,
  47. ActiveCharacterLeft,
  48. ActiveCharacterRight,
  49. TotalActionCount,
  50. }
  51. /// <summary>
  52. /// Readable names of each action.
  53. /// </summary>
  54. private static readonly string[] actionNames =
  55. {
  56. "Main Menu",
  57. "Ok",
  58. "Back",
  59. "Character Management",
  60. "Exit Game",
  61. "Take / View",
  62. "Drop / Unequip",
  63. "Move Character - Up",
  64. "Move Character - Down",
  65. "Move Character - Left",
  66. "Move Character - Right",
  67. "Move Cursor - Up",
  68. "Move Cursor - Down",
  69. "Decrease Amount",
  70. "Increase Amount",
  71. "Page Screen Left",
  72. "Page Screen Right",
  73. "Select Target -Up",
  74. "Select Target - Down",
  75. "Select Active Character - Left",
  76. "Select Active Character - Right",
  77. };
  78. /// <summary>
  79. /// Returns the readable name of the given action.
  80. /// </summary>
  81. public static string GetActionName(Action action)
  82. {
  83. int index = (int)action;
  84. if ((index < 0) || (index > actionNames.Length))
  85. {
  86. throw new ArgumentException("action");
  87. }
  88. return actionNames[index];
  89. }
  90. #endregion
  91. #region Support Types
  92. /// <summary>
  93. /// GamePad controls expressed as one type, unified with button semantics.
  94. /// </summary>
  95. public enum GamePadButtons
  96. {
  97. Start,
  98. Back,
  99. A,
  100. B,
  101. X,
  102. Y,
  103. Up,
  104. Down,
  105. Left,
  106. Right,
  107. LeftShoulder,
  108. RightShoulder,
  109. LeftTrigger,
  110. RightTrigger,
  111. }
  112. /// <summary>
  113. /// A combination of gamepad and keyboard keys mapped to a particular action.
  114. /// </summary>
  115. public class ActionMap
  116. {
  117. /// <summary>
  118. /// List of GamePad controls to be mapped to a given action.
  119. /// </summary>
  120. public List<GamePadButtons> gamePadButtons = new List<GamePadButtons>();
  121. /// <summary>
  122. /// List of Keyboard controls to be mapped to a given action.
  123. /// </summary>
  124. public List<Keys> keyboardKeys = new List<Keys>();
  125. }
  126. #endregion
  127. #region Constants
  128. /// <summary>
  129. /// The value of an analog control that reads as a "pressed button".
  130. /// </summary>
  131. const float analogLimit = 0.5f;
  132. #endregion
  133. #region Keyboard Data
  134. /// <summary>
  135. /// The state of the keyboard as of the last update.
  136. /// </summary>
  137. private static KeyboardState currentKeyboardState;
  138. /// <summary>
  139. /// The state of the keyboard as of the last update.
  140. /// </summary>
  141. public static KeyboardState CurrentKeyboardState
  142. {
  143. get { return currentKeyboardState; }
  144. }
  145. /// <summary>
  146. /// The state of the keyboard as of the previous update.
  147. /// </summary>
  148. private static KeyboardState previousKeyboardState;
  149. /// <summary>
  150. /// Check if a key is pressed.
  151. /// </summary>
  152. public static bool IsKeyPressed(Keys key)
  153. {
  154. return currentKeyboardState.IsKeyDown(key);
  155. }
  156. /// <summary>
  157. /// Check if a key was just pressed in the most recent update.
  158. /// </summary>
  159. public static bool IsKeyTriggered(Keys key)
  160. {
  161. return (currentKeyboardState.IsKeyDown(key)) &&
  162. (!previousKeyboardState.IsKeyDown(key));
  163. }
  164. #endregion
  165. #region GamePad Data
  166. /// <summary>
  167. /// The state of the gamepad as of the last update.
  168. /// </summary>
  169. private static GamePadState currentGamePadState;
  170. /// <summary>
  171. /// The state of the gamepad as of the last update.
  172. /// </summary>
  173. public static GamePadState CurrentGamePadState
  174. {
  175. get { return currentGamePadState; }
  176. }
  177. /// <summary>
  178. /// The state of the gamepad as of the previous update.
  179. /// </summary>
  180. private static GamePadState previousGamePadState;
  181. #region GamePadButton Pressed Queries
  182. /// <summary>
  183. /// Check if the gamepad's Start button is pressed.
  184. /// </summary>
  185. public static bool IsGamePadStartPressed()
  186. {
  187. return (currentGamePadState.Buttons.Start == ButtonState.Pressed);
  188. }
  189. /// <summary>
  190. /// Check if the gamepad's Back button is pressed.
  191. /// </summary>
  192. public static bool IsGamePadBackPressed()
  193. {
  194. return (currentGamePadState.Buttons.Back == ButtonState.Pressed);
  195. }
  196. /// <summary>
  197. /// Check if the gamepad's A button is pressed.
  198. /// </summary>
  199. public static bool IsGamePadAPressed()
  200. {
  201. return (currentGamePadState.Buttons.A == ButtonState.Pressed);
  202. }
  203. /// <summary>
  204. /// Check if the gamepad's B button is pressed.
  205. /// </summary>
  206. public static bool IsGamePadBPressed()
  207. {
  208. return (currentGamePadState.Buttons.B == ButtonState.Pressed);
  209. }
  210. /// <summary>
  211. /// Check if the gamepad's X button is pressed.
  212. /// </summary>
  213. public static bool IsGamePadXPressed()
  214. {
  215. return (currentGamePadState.Buttons.X == ButtonState.Pressed);
  216. }
  217. /// <summary>
  218. /// Check if the gamepad's Y button is pressed.
  219. /// </summary>
  220. public static bool IsGamePadYPressed()
  221. {
  222. return (currentGamePadState.Buttons.Y == ButtonState.Pressed);
  223. }
  224. /// <summary>
  225. /// Check if the gamepad's LeftShoulder button is pressed.
  226. /// </summary>
  227. public static bool IsGamePadLeftShoulderPressed()
  228. {
  229. return (currentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed);
  230. }
  231. /// <summary>
  232. /// <summary>
  233. /// Check if the gamepad's RightShoulder button is pressed.
  234. /// </summary>
  235. public static bool IsGamePadRightShoulderPressed()
  236. {
  237. return (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed);
  238. }
  239. /// <summary>
  240. /// Check if Up on the gamepad's directional pad is pressed.
  241. /// </summary>
  242. public static bool IsGamePadDPadUpPressed()
  243. {
  244. return (currentGamePadState.DPad.Up == ButtonState.Pressed);
  245. }
  246. /// <summary>
  247. /// Check if Down on the gamepad's directional pad is pressed.
  248. /// </summary>
  249. public static bool IsGamePadDPadDownPressed()
  250. {
  251. return (currentGamePadState.DPad.Down == ButtonState.Pressed);
  252. }
  253. /// <summary>
  254. /// Check if Left on the gamepad's directional pad is pressed.
  255. /// </summary>
  256. public static bool IsGamePadDPadLeftPressed()
  257. {
  258. return (currentGamePadState.DPad.Left == ButtonState.Pressed);
  259. }
  260. /// <summary>
  261. /// Check if Right on the gamepad's directional pad is pressed.
  262. /// </summary>
  263. public static bool IsGamePadDPadRightPressed()
  264. {
  265. return (currentGamePadState.DPad.Right == ButtonState.Pressed);
  266. }
  267. /// <summary>
  268. /// Check if the gamepad's left trigger is pressed.
  269. /// </summary>
  270. public static bool IsGamePadLeftTriggerPressed()
  271. {
  272. return (currentGamePadState.Triggers.Left > analogLimit);
  273. }
  274. /// <summary>
  275. /// Check if the gamepad's right trigger is pressed.
  276. /// </summary>
  277. public static bool IsGamePadRightTriggerPressed()
  278. {
  279. return (currentGamePadState.Triggers.Right > analogLimit);
  280. }
  281. /// <summary>
  282. /// Check if Up on the gamepad's left analog stick is pressed.
  283. /// </summary>
  284. public static bool IsGamePadLeftStickUpPressed()
  285. {
  286. return (currentGamePadState.ThumbSticks.Left.Y > analogLimit);
  287. }
  288. /// <summary>
  289. /// Check if Down on the gamepad's left analog stick is pressed.
  290. /// </summary>
  291. public static bool IsGamePadLeftStickDownPressed()
  292. {
  293. return (-1f * currentGamePadState.ThumbSticks.Left.Y > analogLimit);
  294. }
  295. /// <summary>
  296. /// Check if Left on the gamepad's left analog stick is pressed.
  297. /// </summary>
  298. public static bool IsGamePadLeftStickLeftPressed()
  299. {
  300. return (-1f * currentGamePadState.ThumbSticks.Left.X > analogLimit);
  301. }
  302. /// <summary>
  303. /// Check if Right on the gamepad's left analog stick is pressed.
  304. /// </summary>
  305. public static bool IsGamePadLeftStickRightPressed()
  306. {
  307. return (currentGamePadState.ThumbSticks.Left.X > analogLimit);
  308. }
  309. /// <summary>
  310. /// Check if the GamePadKey value specified is pressed.
  311. /// </summary>
  312. private static bool IsGamePadButtonPressed(GamePadButtons gamePadKey)
  313. {
  314. switch (gamePadKey)
  315. {
  316. case GamePadButtons.Start:
  317. return IsGamePadStartPressed();
  318. case GamePadButtons.Back:
  319. return IsGamePadBackPressed();
  320. case GamePadButtons.A:
  321. return IsGamePadAPressed();
  322. case GamePadButtons.B:
  323. return IsGamePadBPressed();
  324. case GamePadButtons.X:
  325. return IsGamePadXPressed();
  326. case GamePadButtons.Y:
  327. return IsGamePadYPressed();
  328. case GamePadButtons.LeftShoulder:
  329. return IsGamePadLeftShoulderPressed();
  330. case GamePadButtons.RightShoulder:
  331. return IsGamePadRightShoulderPressed();
  332. case GamePadButtons.LeftTrigger:
  333. return IsGamePadLeftTriggerPressed();
  334. case GamePadButtons.RightTrigger:
  335. return IsGamePadRightTriggerPressed();
  336. case GamePadButtons.Up:
  337. return IsGamePadDPadUpPressed() ||
  338. IsGamePadLeftStickUpPressed();
  339. case GamePadButtons.Down:
  340. return IsGamePadDPadDownPressed() ||
  341. IsGamePadLeftStickDownPressed();
  342. case GamePadButtons.Left:
  343. return IsGamePadDPadLeftPressed() ||
  344. IsGamePadLeftStickLeftPressed();
  345. case GamePadButtons.Right:
  346. return IsGamePadDPadRightPressed() ||
  347. IsGamePadLeftStickRightPressed();
  348. }
  349. return false;
  350. }
  351. #endregion
  352. #region GamePadButton Triggered Queries
  353. /// <summary>
  354. /// Check if the gamepad's Start button was just pressed.
  355. /// </summary>
  356. public static bool IsGamePadStartTriggered()
  357. {
  358. return ((currentGamePadState.Buttons.Start == ButtonState.Pressed) &&
  359. (previousGamePadState.Buttons.Start == ButtonState.Released));
  360. }
  361. /// <summary>
  362. /// Check if the gamepad's Back button was just pressed.
  363. /// </summary>
  364. public static bool IsGamePadBackTriggered()
  365. {
  366. return ((currentGamePadState.Buttons.Back == ButtonState.Pressed) &&
  367. (previousGamePadState.Buttons.Back == ButtonState.Released));
  368. }
  369. /// <summary>
  370. /// Check if the gamepad's A button was just pressed.
  371. /// </summary>
  372. public static bool IsGamePadATriggered()
  373. {
  374. return ((currentGamePadState.Buttons.A == ButtonState.Pressed) &&
  375. (previousGamePadState.Buttons.A == ButtonState.Released));
  376. }
  377. /// <summary>
  378. /// Check if the gamepad's B button was just pressed.
  379. /// </summary>
  380. public static bool IsGamePadBTriggered()
  381. {
  382. return ((currentGamePadState.Buttons.B == ButtonState.Pressed) &&
  383. (previousGamePadState.Buttons.B == ButtonState.Released));
  384. }
  385. /// <summary>
  386. /// Check if the gamepad's X button was just pressed.
  387. /// </summary>
  388. public static bool IsGamePadXTriggered()
  389. {
  390. return ((currentGamePadState.Buttons.X == ButtonState.Pressed) &&
  391. (previousGamePadState.Buttons.X == ButtonState.Released));
  392. }
  393. /// <summary>
  394. /// Check if the gamepad's Y button was just pressed.
  395. /// </summary>
  396. public static bool IsGamePadYTriggered()
  397. {
  398. return ((currentGamePadState.Buttons.Y == ButtonState.Pressed) &&
  399. (previousGamePadState.Buttons.Y == ButtonState.Released));
  400. }
  401. /// <summary>
  402. /// Check if the gamepad's LeftShoulder button was just pressed.
  403. /// </summary>
  404. public static bool IsGamePadLeftShoulderTriggered()
  405. {
  406. return (
  407. (currentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed) &&
  408. (previousGamePadState.Buttons.LeftShoulder == ButtonState.Released));
  409. }
  410. /// <summary>
  411. /// Check if the gamepad's RightShoulder button was just pressed.
  412. /// </summary>
  413. public static bool IsGamePadRightShoulderTriggered()
  414. {
  415. return (
  416. (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed) &&
  417. (previousGamePadState.Buttons.RightShoulder == ButtonState.Released));
  418. }
  419. /// <summary>
  420. /// Check if Up on the gamepad's directional pad was just pressed.
  421. /// </summary>
  422. public static bool IsGamePadDPadUpTriggered()
  423. {
  424. return ((currentGamePadState.DPad.Up == ButtonState.Pressed) &&
  425. (previousGamePadState.DPad.Up == ButtonState.Released));
  426. }
  427. /// <summary>
  428. /// Check if Down on the gamepad's directional pad was just pressed.
  429. /// </summary>
  430. public static bool IsGamePadDPadDownTriggered()
  431. {
  432. return ((currentGamePadState.DPad.Down == ButtonState.Pressed) &&
  433. (previousGamePadState.DPad.Down == ButtonState.Released));
  434. }
  435. /// <summary>
  436. /// Check if Left on the gamepad's directional pad was just pressed.
  437. /// </summary>
  438. public static bool IsGamePadDPadLeftTriggered()
  439. {
  440. return ((currentGamePadState.DPad.Left == ButtonState.Pressed) &&
  441. (previousGamePadState.DPad.Left == ButtonState.Released));
  442. }
  443. /// <summary>
  444. /// Check if Right on the gamepad's directional pad was just pressed.
  445. /// </summary>
  446. public static bool IsGamePadDPadRightTriggered()
  447. {
  448. return ((currentGamePadState.DPad.Right == ButtonState.Pressed) &&
  449. (previousGamePadState.DPad.Right == ButtonState.Released));
  450. }
  451. /// <summary>
  452. /// Check if the gamepad's left trigger was just pressed.
  453. /// </summary>
  454. public static bool IsGamePadLeftTriggerTriggered()
  455. {
  456. return ((currentGamePadState.Triggers.Left > analogLimit) &&
  457. (previousGamePadState.Triggers.Left < analogLimit));
  458. }
  459. /// <summary>
  460. /// Check if the gamepad's right trigger was just pressed.
  461. /// </summary>
  462. public static bool IsGamePadRightTriggerTriggered()
  463. {
  464. return ((currentGamePadState.Triggers.Right > analogLimit) &&
  465. (previousGamePadState.Triggers.Right < analogLimit));
  466. }
  467. /// <summary>
  468. /// Check if Up on the gamepad's left analog stick was just pressed.
  469. /// </summary>
  470. public static bool IsGamePadLeftStickUpTriggered()
  471. {
  472. return ((currentGamePadState.ThumbSticks.Left.Y > analogLimit) &&
  473. (previousGamePadState.ThumbSticks.Left.Y < analogLimit));
  474. }
  475. /// <summary>
  476. /// Check if Down on the gamepad's left analog stick was just pressed.
  477. /// </summary>
  478. public static bool IsGamePadLeftStickDownTriggered()
  479. {
  480. return ((-1f * currentGamePadState.ThumbSticks.Left.Y > analogLimit) &&
  481. (-1f * previousGamePadState.ThumbSticks.Left.Y < analogLimit));
  482. }
  483. /// <summary>
  484. /// Check if Left on the gamepad's left analog stick was just pressed.
  485. /// </summary>
  486. public static bool IsGamePadLeftStickLeftTriggered()
  487. {
  488. return ((-1f * currentGamePadState.ThumbSticks.Left.X > analogLimit) &&
  489. (-1f * previousGamePadState.ThumbSticks.Left.X < analogLimit));
  490. }
  491. /// <summary>
  492. /// Check if Right on the gamepad's left analog stick was just pressed.
  493. /// </summary>
  494. public static bool IsGamePadLeftStickRightTriggered()
  495. {
  496. return ((currentGamePadState.ThumbSticks.Left.X > analogLimit) &&
  497. (previousGamePadState.ThumbSticks.Left.X < analogLimit));
  498. }
  499. /// <summary>
  500. /// Check if the GamePadKey value specified was pressed this frame.
  501. /// </summary>
  502. private static bool IsGamePadButtonTriggered(GamePadButtons gamePadKey)
  503. {
  504. switch (gamePadKey)
  505. {
  506. case GamePadButtons.Start:
  507. return IsGamePadStartTriggered();
  508. case GamePadButtons.Back:
  509. return IsGamePadBackTriggered();
  510. case GamePadButtons.A:
  511. return IsGamePadATriggered();
  512. case GamePadButtons.B:
  513. return IsGamePadBTriggered();
  514. case GamePadButtons.X:
  515. return IsGamePadXTriggered();
  516. case GamePadButtons.Y:
  517. return IsGamePadYTriggered();
  518. case GamePadButtons.LeftShoulder:
  519. return IsGamePadLeftShoulderTriggered();
  520. case GamePadButtons.RightShoulder:
  521. return IsGamePadRightShoulderTriggered();
  522. case GamePadButtons.LeftTrigger:
  523. return IsGamePadLeftTriggerTriggered();
  524. case GamePadButtons.RightTrigger:
  525. return IsGamePadRightTriggerTriggered();
  526. case GamePadButtons.Up:
  527. return IsGamePadDPadUpTriggered() ||
  528. IsGamePadLeftStickUpTriggered();
  529. case GamePadButtons.Down:
  530. return IsGamePadDPadDownTriggered() ||
  531. IsGamePadLeftStickDownTriggered();
  532. case GamePadButtons.Left:
  533. return IsGamePadDPadLeftTriggered() ||
  534. IsGamePadLeftStickLeftTriggered();
  535. case GamePadButtons.Right:
  536. return IsGamePadDPadRightTriggered() ||
  537. IsGamePadLeftStickRightTriggered();
  538. }
  539. return false;
  540. }
  541. #endregion
  542. #endregion
  543. #region Action Mapping
  544. /// <summary>
  545. /// The action mappings for the game.
  546. /// </summary>
  547. private static ActionMap[] actionMaps;
  548. public static ActionMap[] ActionMaps
  549. {
  550. get { return actionMaps; }
  551. }
  552. /// <summary>
  553. /// Reset the action maps to their default values.
  554. /// </summary>
  555. private static void ResetActionMaps()
  556. {
  557. actionMaps = new ActionMap[(int)Action.TotalActionCount];
  558. actionMaps[(int)Action.MainMenu] = new ActionMap();
  559. actionMaps[(int)Action.MainMenu].keyboardKeys.Add(
  560. Keys.Tab);
  561. actionMaps[(int)Action.MainMenu].gamePadButtons.Add(
  562. GamePadButtons.Start);
  563. actionMaps[(int)Action.Ok] = new ActionMap();
  564. actionMaps[(int)Action.Ok].keyboardKeys.Add(
  565. Keys.Enter);
  566. actionMaps[(int)Action.Ok].gamePadButtons.Add(
  567. GamePadButtons.A);
  568. actionMaps[(int)Action.Back] = new ActionMap();
  569. actionMaps[(int)Action.Back].keyboardKeys.Add(
  570. Keys.Escape);
  571. actionMaps[(int)Action.Back].gamePadButtons.Add(
  572. GamePadButtons.B);
  573. actionMaps[(int)Action.CharacterManagement] = new ActionMap();
  574. actionMaps[(int)Action.CharacterManagement].keyboardKeys.Add(
  575. Keys.Space);
  576. actionMaps[(int)Action.CharacterManagement].gamePadButtons.Add(
  577. GamePadButtons.Y);
  578. actionMaps[(int)Action.ExitGame] = new ActionMap();
  579. actionMaps[(int)Action.ExitGame].keyboardKeys.Add(
  580. Keys.Escape);
  581. actionMaps[(int)Action.ExitGame].gamePadButtons.Add(
  582. GamePadButtons.Back);
  583. actionMaps[(int)Action.TakeView] = new ActionMap();
  584. actionMaps[(int)Action.TakeView].keyboardKeys.Add(
  585. Keys.LeftControl);
  586. actionMaps[(int)Action.TakeView].gamePadButtons.Add(
  587. GamePadButtons.Y);
  588. actionMaps[(int)Action.DropUnEquip] = new ActionMap();
  589. actionMaps[(int)Action.DropUnEquip].keyboardKeys.Add(
  590. Keys.D);
  591. actionMaps[(int)Action.DropUnEquip].gamePadButtons.Add(
  592. GamePadButtons.X);
  593. actionMaps[(int)Action.MoveCharacterUp] = new ActionMap();
  594. actionMaps[(int)Action.MoveCharacterUp].keyboardKeys.Add(
  595. Keys.Up);
  596. actionMaps[(int)Action.MoveCharacterUp].gamePadButtons.Add(
  597. GamePadButtons.Up);
  598. actionMaps[(int)Action.MoveCharacterDown] = new ActionMap();
  599. actionMaps[(int)Action.MoveCharacterDown].keyboardKeys.Add(
  600. Keys.Down);
  601. actionMaps[(int)Action.MoveCharacterDown].gamePadButtons.Add(
  602. GamePadButtons.Down);
  603. actionMaps[(int)Action.MoveCharacterLeft] = new ActionMap();
  604. actionMaps[(int)Action.MoveCharacterLeft].keyboardKeys.Add(
  605. Keys.Left);
  606. actionMaps[(int)Action.MoveCharacterLeft].gamePadButtons.Add(
  607. GamePadButtons.Left);
  608. actionMaps[(int)Action.MoveCharacterRight] = new ActionMap();
  609. actionMaps[(int)Action.MoveCharacterRight].keyboardKeys.Add(
  610. Keys.Right);
  611. actionMaps[(int)Action.MoveCharacterRight].gamePadButtons.Add(
  612. GamePadButtons.Right);
  613. actionMaps[(int)Action.CursorUp] = new ActionMap();
  614. actionMaps[(int)Action.CursorUp].keyboardKeys.Add(
  615. Keys.Up);
  616. actionMaps[(int)Action.CursorUp].gamePadButtons.Add(
  617. GamePadButtons.Up);
  618. actionMaps[(int)Action.CursorDown] = new ActionMap();
  619. actionMaps[(int)Action.CursorDown].keyboardKeys.Add(
  620. Keys.Down);
  621. actionMaps[(int)Action.CursorDown].gamePadButtons.Add(
  622. GamePadButtons.Down);
  623. actionMaps[(int)Action.DecreaseAmount] = new ActionMap();
  624. actionMaps[(int)Action.DecreaseAmount].keyboardKeys.Add(
  625. Keys.Left);
  626. actionMaps[(int)Action.DecreaseAmount].gamePadButtons.Add(
  627. GamePadButtons.Left);
  628. actionMaps[(int)Action.IncreaseAmount] = new ActionMap();
  629. actionMaps[(int)Action.IncreaseAmount].keyboardKeys.Add(
  630. Keys.Right);
  631. actionMaps[(int)Action.IncreaseAmount].gamePadButtons.Add(
  632. GamePadButtons.Right);
  633. actionMaps[(int)Action.PageLeft] = new ActionMap();
  634. actionMaps[(int)Action.PageLeft].keyboardKeys.Add(
  635. Keys.LeftShift);
  636. actionMaps[(int)Action.PageLeft].gamePadButtons.Add(
  637. GamePadButtons.LeftTrigger);
  638. actionMaps[(int)Action.PageRight] = new ActionMap();
  639. actionMaps[(int)Action.PageRight].keyboardKeys.Add(
  640. Keys.RightShift);
  641. actionMaps[(int)Action.PageRight].gamePadButtons.Add(
  642. GamePadButtons.RightTrigger);
  643. actionMaps[(int)Action.TargetUp] = new ActionMap();
  644. actionMaps[(int)Action.TargetUp].keyboardKeys.Add(
  645. Keys.Up);
  646. actionMaps[(int)Action.TargetUp].gamePadButtons.Add(
  647. GamePadButtons.Up);
  648. actionMaps[(int)Action.TargetDown] = new ActionMap();
  649. actionMaps[(int)Action.TargetDown].keyboardKeys.Add(
  650. Keys.Down);
  651. actionMaps[(int)Action.TargetDown].gamePadButtons.Add(
  652. GamePadButtons.Down);
  653. actionMaps[(int)Action.ActiveCharacterLeft] = new ActionMap();
  654. actionMaps[(int)Action.ActiveCharacterLeft].keyboardKeys.Add(
  655. Keys.Left);
  656. actionMaps[(int)Action.ActiveCharacterLeft].gamePadButtons.Add(
  657. GamePadButtons.Left);
  658. actionMaps[(int)Action.ActiveCharacterRight] = new ActionMap();
  659. actionMaps[(int)Action.ActiveCharacterRight].keyboardKeys.Add(
  660. Keys.Right);
  661. actionMaps[(int)Action.ActiveCharacterRight].gamePadButtons.Add(
  662. GamePadButtons.Right);
  663. }
  664. /// <summary>
  665. /// Check if an action has been pressed.
  666. /// </summary>
  667. public static bool IsActionPressed(Action action)
  668. {
  669. return IsActionMapPressed(actionMaps[(int)action]);
  670. }
  671. /// <summary>
  672. /// Check if an action was just performed in the most recent update.
  673. /// </summary>
  674. public static bool IsActionTriggered(Action action)
  675. {
  676. return IsActionMapTriggered(actionMaps[(int)action]);
  677. }
  678. /// <summary>
  679. /// Check if an action map has been pressed.
  680. /// </summary>
  681. private static bool IsActionMapPressed(ActionMap actionMap)
  682. {
  683. for (int i = 0; i < actionMap.keyboardKeys.Count; i++)
  684. {
  685. if (IsKeyPressed(actionMap.keyboardKeys[i]))
  686. {
  687. return true;
  688. }
  689. }
  690. if (currentGamePadState.IsConnected)
  691. {
  692. for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
  693. {
  694. if (IsGamePadButtonPressed(actionMap.gamePadButtons[i]))
  695. {
  696. return true;
  697. }
  698. }
  699. }
  700. return false;
  701. }
  702. /// <summary>
  703. /// Check if an action map has been triggered this frame.
  704. /// </summary>
  705. private static bool IsActionMapTriggered(ActionMap actionMap)
  706. {
  707. for (int i = 0; i < actionMap.keyboardKeys.Count; i++)
  708. {
  709. if (IsKeyTriggered(actionMap.keyboardKeys[i]))
  710. {
  711. return true;
  712. }
  713. }
  714. if (currentGamePadState.IsConnected)
  715. {
  716. for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
  717. {
  718. if (IsGamePadButtonTriggered(actionMap.gamePadButtons[i]))
  719. {
  720. return true;
  721. }
  722. }
  723. }
  724. return false;
  725. }
  726. #endregion
  727. #region Initialization
  728. /// <summary>
  729. /// Initializes the default control keys for all actions.
  730. /// </summary>
  731. public static void Initialize()
  732. {
  733. ResetActionMaps();
  734. }
  735. #endregion
  736. #region Updating
  737. /// <summary>
  738. /// Updates the keyboard and gamepad control states.
  739. /// </summary>
  740. public static void Update()
  741. {
  742. // update the keyboard state
  743. previousKeyboardState = currentKeyboardState;
  744. currentKeyboardState = Keyboard.GetState();
  745. // update the gamepad state
  746. previousGamePadState = currentGamePadState;
  747. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  748. }
  749. #endregion
  750. }
  751. }