InputManager.cs 29 KB

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