2
0

InputManager.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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 System.Linq;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Input;
  12. using Microsoft.Xna.Framework.Input.Touch;
  13. namespace RolePlaying
  14. {
  15. /// <summary>
  16. /// This class handles all keyboard and gamepad actions in the game.
  17. /// </summary>
  18. public static partial class InputManager
  19. {
  20. /// <summary>
  21. /// Readable names of each action.
  22. /// </summary>
  23. private static readonly string[] actionNames =
  24. {
  25. "Main Menu",
  26. "Ok",
  27. "Back",
  28. "Character Management",
  29. "Exit Game",
  30. "Take / View",
  31. "Drop / Unequip",
  32. "Move Character - Up",
  33. "Move Character - Down",
  34. "Move Character - Left",
  35. "Move Character - Right",
  36. "Move Cursor - Up",
  37. "Move Cursor - Down",
  38. "Decrease Amount",
  39. "Increase Amount",
  40. "Page Screen Left",
  41. "Page Screen Right",
  42. "Select Target -Up",
  43. "Select Target - Down",
  44. "Select Active Character - Left",
  45. "Select Active Character - Right",
  46. };
  47. /// <summary>
  48. /// Returns the readable name of the given action.
  49. /// </summary>
  50. public static string GetActionName(InputAction action)
  51. {
  52. int index = (int)action;
  53. if ((index < 0) || (index > actionNames.Length))
  54. {
  55. throw new ArgumentException("action");
  56. }
  57. return actionNames[index];
  58. }
  59. /// <summary>
  60. /// The value of an analog control that reads as a "pressed button".
  61. /// </summary>
  62. const float analogLimit = 0.5f;
  63. /// <summary>
  64. /// The state of the keyboard as of the last update.
  65. /// </summary>
  66. private static KeyboardState currentKeyboardState;
  67. /// <summary>
  68. /// The state of the keyboard as of the last update.
  69. /// </summary>
  70. public static KeyboardState CurrentKeyboardState
  71. {
  72. get { return currentKeyboardState; }
  73. }
  74. /// <summary>
  75. /// The state of the keyboard as of the previous update.
  76. /// </summary>
  77. private static KeyboardState previousKeyboardState;
  78. /// <summary>
  79. /// Check if a key is pressed.
  80. /// </summary>
  81. public static bool IsKeyPressed(Keys key)
  82. {
  83. return currentKeyboardState.IsKeyDown(key);
  84. }
  85. /// <summary>
  86. /// Check if a key was just pressed in the most recent update.
  87. /// </summary>
  88. public static bool IsKeyTriggered(Keys key)
  89. {
  90. return currentKeyboardState.IsKeyDown(key)
  91. && !previousKeyboardState.IsKeyDown(key);
  92. }
  93. /// <summary>
  94. /// The state of the gamepad as of the last update.
  95. /// </summary>
  96. private static GamePadState currentGamePadState;
  97. /// <summary>
  98. /// The state of the gamepad as of the last update.
  99. /// </summary>
  100. public static GamePadState CurrentGamePadState
  101. {
  102. get { return currentGamePadState; }
  103. }
  104. /// <summary>
  105. /// The state of the gamepad as of the previous update.
  106. /// </summary>
  107. private static GamePadState previousGamePadState;
  108. /// <summary>
  109. /// Check if the gamepad's Start button is pressed.
  110. /// </summary>
  111. public static bool IsGamePadStartPressed()
  112. {
  113. return (currentGamePadState.Buttons.Start == ButtonState.Pressed);
  114. }
  115. /// <summary>
  116. /// Check if the gamepad's Back button is pressed.
  117. /// </summary>
  118. public static bool IsGamePadBackPressed()
  119. {
  120. return (currentGamePadState.Buttons.Back == ButtonState.Pressed);
  121. }
  122. /// <summary>
  123. /// Check if the gamepad's A button is pressed.
  124. /// </summary>
  125. public static bool IsGamePadAPressed()
  126. {
  127. return (currentGamePadState.Buttons.A == ButtonState.Pressed);
  128. }
  129. /// <summary>
  130. /// Check if the gamepad's B button is pressed.
  131. /// </summary>
  132. public static bool IsGamePadBPressed()
  133. {
  134. return (currentGamePadState.Buttons.B == ButtonState.Pressed);
  135. }
  136. /// <summary>
  137. /// Check if the gamepad's X button is pressed.
  138. /// </summary>
  139. public static bool IsGamePadXPressed()
  140. {
  141. return (currentGamePadState.Buttons.X == ButtonState.Pressed);
  142. }
  143. /// <summary>
  144. /// Check if the gamepad's Y button is pressed.
  145. /// </summary>
  146. public static bool IsGamePadYPressed()
  147. {
  148. return (currentGamePadState.Buttons.Y == ButtonState.Pressed);
  149. }
  150. /// <summary>
  151. /// Check if the gamepad's LeftShoulder button is pressed.
  152. /// </summary>
  153. public static bool IsGamePadLeftShoulderPressed()
  154. {
  155. return (currentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed);
  156. }
  157. /// <summary>
  158. /// <summary>
  159. /// Check if the gamepad's RightShoulder button is pressed.
  160. /// </summary>
  161. public static bool IsGamePadRightShoulderPressed()
  162. {
  163. return (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed);
  164. }
  165. /// <summary>
  166. /// Check if Up on the gamepad's directional pad is pressed.
  167. /// </summary>
  168. public static bool IsGamePadDPadUpPressed()
  169. {
  170. return (currentGamePadState.DPad.Up == ButtonState.Pressed);
  171. }
  172. /// <summary>
  173. /// Check if Down on the gamepad's directional pad is pressed.
  174. /// </summary>
  175. public static bool IsGamePadDPadDownPressed()
  176. {
  177. return (currentGamePadState.DPad.Down == ButtonState.Pressed);
  178. }
  179. /// <summary>
  180. /// Check if Left on the gamepad's directional pad is pressed.
  181. /// </summary>
  182. public static bool IsGamePadDPadLeftPressed()
  183. {
  184. return (currentGamePadState.DPad.Left == ButtonState.Pressed);
  185. }
  186. /// <summary>
  187. /// Check if Right on the gamepad's directional pad is pressed.
  188. /// </summary>
  189. public static bool IsGamePadDPadRightPressed()
  190. {
  191. return (currentGamePadState.DPad.Right == ButtonState.Pressed);
  192. }
  193. /// <summary>
  194. /// Check if the gamepad's left trigger is pressed.
  195. /// </summary>
  196. public static bool IsGamePadLeftTriggerPressed()
  197. {
  198. return (currentGamePadState.Triggers.Left > analogLimit);
  199. }
  200. /// <summary>
  201. /// Check if the gamepad's right trigger is pressed.
  202. /// </summary>
  203. public static bool IsGamePadRightTriggerPressed()
  204. {
  205. return (currentGamePadState.Triggers.Right > analogLimit);
  206. }
  207. /// <summary>
  208. /// Check if Up on the gamepad's left analog stick is pressed.
  209. /// </summary>
  210. public static bool IsGamePadLeftStickUpPressed()
  211. {
  212. return (currentGamePadState.ThumbSticks.Left.Y > analogLimit);
  213. }
  214. /// <summary>
  215. /// Check if Down on the gamepad's left analog stick is pressed.
  216. /// </summary>
  217. public static bool IsGamePadLeftStickDownPressed()
  218. {
  219. return (-1f * currentGamePadState.ThumbSticks.Left.Y > analogLimit);
  220. }
  221. /// <summary>
  222. /// Check if Left on the gamepad's left analog stick is pressed.
  223. /// </summary>
  224. public static bool IsGamePadLeftStickLeftPressed()
  225. {
  226. return (-1f * currentGamePadState.ThumbSticks.Left.X > analogLimit);
  227. }
  228. /// <summary>
  229. /// Check if Right on the gamepad's left analog stick is pressed.
  230. /// </summary>
  231. public static bool IsGamePadLeftStickRightPressed()
  232. {
  233. return (currentGamePadState.ThumbSticks.Left.X > analogLimit);
  234. }
  235. /// <summary>
  236. /// Check if the GamePadKey value specified is pressed.
  237. /// </summary>
  238. private static bool IsGamePadButtonPressed(GamePadButtons gamePadKey)
  239. {
  240. switch (gamePadKey)
  241. {
  242. case GamePadButtons.Start:
  243. return IsGamePadStartPressed();
  244. case GamePadButtons.Back:
  245. return IsGamePadBackPressed();
  246. case GamePadButtons.A:
  247. return IsGamePadAPressed();
  248. case GamePadButtons.B:
  249. return IsGamePadBPressed();
  250. case GamePadButtons.X:
  251. return IsGamePadXPressed();
  252. case GamePadButtons.Y:
  253. return IsGamePadYPressed();
  254. case GamePadButtons.LeftShoulder:
  255. return IsGamePadLeftShoulderPressed();
  256. case GamePadButtons.RightShoulder:
  257. return IsGamePadRightShoulderPressed();
  258. case GamePadButtons.LeftTrigger:
  259. return IsGamePadLeftTriggerPressed();
  260. case GamePadButtons.RightTrigger:
  261. return IsGamePadRightTriggerPressed();
  262. case GamePadButtons.Up:
  263. return IsGamePadDPadUpPressed() ||
  264. IsGamePadLeftStickUpPressed();
  265. case GamePadButtons.Down:
  266. return IsGamePadDPadDownPressed() ||
  267. IsGamePadLeftStickDownPressed();
  268. case GamePadButtons.Left:
  269. return IsGamePadDPadLeftPressed() ||
  270. IsGamePadLeftStickLeftPressed();
  271. case GamePadButtons.Right:
  272. return IsGamePadDPadRightPressed() ||
  273. IsGamePadLeftStickRightPressed();
  274. }
  275. return false;
  276. }
  277. /// <summary>
  278. /// Check if the gamepad's Start button was just pressed.
  279. /// </summary>
  280. public static bool IsGamePadStartTriggered()
  281. {
  282. return ((currentGamePadState.Buttons.Start == ButtonState.Pressed) &&
  283. (previousGamePadState.Buttons.Start == ButtonState.Released));
  284. }
  285. /// <summary>
  286. /// Check if the gamepad's Back button was just pressed.
  287. /// </summary>
  288. public static bool IsGamePadBackTriggered()
  289. {
  290. return ((currentGamePadState.Buttons.Back == ButtonState.Pressed) &&
  291. (previousGamePadState.Buttons.Back == ButtonState.Released));
  292. }
  293. /// <summary>
  294. /// Check if the gamepad's A button was just pressed.
  295. /// </summary>
  296. public static bool IsGamePadATriggered()
  297. {
  298. return ((currentGamePadState.Buttons.A == ButtonState.Pressed) &&
  299. (previousGamePadState.Buttons.A == ButtonState.Released));
  300. }
  301. /// <summary>
  302. /// Check if the gamepad's B button was just pressed.
  303. /// </summary>
  304. public static bool IsGamePadBTriggered()
  305. {
  306. return ((currentGamePadState.Buttons.B == ButtonState.Pressed) &&
  307. (previousGamePadState.Buttons.B == ButtonState.Released));
  308. }
  309. /// <summary>
  310. /// Check if the gamepad's X button was just pressed.
  311. /// </summary>
  312. public static bool IsGamePadXTriggered()
  313. {
  314. return ((currentGamePadState.Buttons.X == ButtonState.Pressed) &&
  315. (previousGamePadState.Buttons.X == ButtonState.Released));
  316. }
  317. /// <summary>
  318. /// Check if the gamepad's Y button was just pressed.
  319. /// </summary>
  320. public static bool IsGamePadYTriggered()
  321. {
  322. return ((currentGamePadState.Buttons.Y == ButtonState.Pressed) &&
  323. (previousGamePadState.Buttons.Y == ButtonState.Released));
  324. }
  325. /// <summary>
  326. /// Check if the gamepad's LeftShoulder button was just pressed.
  327. /// </summary>
  328. public static bool IsGamePadLeftShoulderTriggered()
  329. {
  330. return (
  331. (currentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed) &&
  332. (previousGamePadState.Buttons.LeftShoulder == ButtonState.Released));
  333. }
  334. /// <summary>
  335. /// Check if the gamepad's RightShoulder button was just pressed.
  336. /// </summary>
  337. public static bool IsGamePadRightShoulderTriggered()
  338. {
  339. return (
  340. (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed) &&
  341. (previousGamePadState.Buttons.RightShoulder == ButtonState.Released));
  342. }
  343. /// <summary>
  344. /// Check if Up on the gamepad's directional pad was just pressed.
  345. /// </summary>
  346. public static bool IsGamePadDPadUpTriggered()
  347. {
  348. return ((currentGamePadState.DPad.Up == ButtonState.Pressed) &&
  349. (previousGamePadState.DPad.Up == ButtonState.Released));
  350. }
  351. /// <summary>
  352. /// Check if Down on the gamepad's directional pad was just pressed.
  353. /// </summary>
  354. public static bool IsGamePadDPadDownTriggered()
  355. {
  356. return ((currentGamePadState.DPad.Down == ButtonState.Pressed) &&
  357. (previousGamePadState.DPad.Down == ButtonState.Released));
  358. }
  359. /// <summary>
  360. /// Check if Left on the gamepad's directional pad was just pressed.
  361. /// </summary>
  362. public static bool IsGamePadDPadLeftTriggered()
  363. {
  364. return ((currentGamePadState.DPad.Left == ButtonState.Pressed) &&
  365. (previousGamePadState.DPad.Left == ButtonState.Released));
  366. }
  367. /// <summary>
  368. /// Check if Right on the gamepad's directional pad was just pressed.
  369. /// </summary>
  370. public static bool IsGamePadDPadRightTriggered()
  371. {
  372. return ((currentGamePadState.DPad.Right == ButtonState.Pressed) &&
  373. (previousGamePadState.DPad.Right == ButtonState.Released));
  374. }
  375. /// <summary>
  376. /// Check if the gamepad's left trigger was just pressed.
  377. /// </summary>
  378. public static bool IsGamePadLeftTriggerTriggered()
  379. {
  380. return ((currentGamePadState.Triggers.Left > analogLimit) &&
  381. (previousGamePadState.Triggers.Left < analogLimit));
  382. }
  383. /// <summary>
  384. /// Check if the gamepad's right trigger was just pressed.
  385. /// </summary>
  386. public static bool IsGamePadRightTriggerTriggered()
  387. {
  388. return ((currentGamePadState.Triggers.Right > analogLimit) &&
  389. (previousGamePadState.Triggers.Right < analogLimit));
  390. }
  391. /// <summary>
  392. /// Check if Up on the gamepad's left analog stick was just pressed.
  393. /// </summary>
  394. public static bool IsGamePadLeftStickUpTriggered()
  395. {
  396. return ((currentGamePadState.ThumbSticks.Left.Y > analogLimit) &&
  397. (previousGamePadState.ThumbSticks.Left.Y < analogLimit));
  398. }
  399. /// <summary>
  400. /// Check if Down on the gamepad's left analog stick was just pressed.
  401. /// </summary>
  402. public static bool IsGamePadLeftStickDownTriggered()
  403. {
  404. return ((-1f * currentGamePadState.ThumbSticks.Left.Y > analogLimit) &&
  405. (-1f * previousGamePadState.ThumbSticks.Left.Y < analogLimit));
  406. }
  407. /// <summary>
  408. /// Check if Left on the gamepad's left analog stick was just pressed.
  409. /// </summary>
  410. public static bool IsGamePadLeftStickLeftTriggered()
  411. {
  412. return ((-1f * currentGamePadState.ThumbSticks.Left.X > analogLimit) &&
  413. (-1f * previousGamePadState.ThumbSticks.Left.X < analogLimit));
  414. }
  415. /// <summary>
  416. /// Check if Right on the gamepad's left analog stick was just pressed.
  417. /// </summary>
  418. public static bool IsGamePadLeftStickRightTriggered()
  419. {
  420. return ((currentGamePadState.ThumbSticks.Left.X > analogLimit) &&
  421. (previousGamePadState.ThumbSticks.Left.X < analogLimit));
  422. }
  423. /// <summary>
  424. /// Check if the GamePadKey value specified was pressed this frame.
  425. /// </summary>
  426. private static bool IsGamePadButtonTriggered(GamePadButtons gamePadKey)
  427. {
  428. switch (gamePadKey)
  429. {
  430. case GamePadButtons.Start:
  431. return IsGamePadStartTriggered();
  432. case GamePadButtons.Back:
  433. return IsGamePadBackTriggered();
  434. case GamePadButtons.A:
  435. return IsGamePadATriggered();
  436. case GamePadButtons.B:
  437. return IsGamePadBTriggered();
  438. case GamePadButtons.X:
  439. return IsGamePadXTriggered();
  440. case GamePadButtons.Y:
  441. return IsGamePadYTriggered();
  442. case GamePadButtons.LeftShoulder:
  443. return IsGamePadLeftShoulderTriggered();
  444. case GamePadButtons.RightShoulder:
  445. return IsGamePadRightShoulderTriggered();
  446. case GamePadButtons.LeftTrigger:
  447. return IsGamePadLeftTriggerTriggered();
  448. case GamePadButtons.RightTrigger:
  449. return IsGamePadRightTriggerTriggered();
  450. case GamePadButtons.Up:
  451. return IsGamePadDPadUpTriggered() ||
  452. IsGamePadLeftStickUpTriggered();
  453. case GamePadButtons.Down:
  454. return IsGamePadDPadDownTriggered() ||
  455. IsGamePadLeftStickDownTriggered();
  456. case GamePadButtons.Left:
  457. return IsGamePadDPadLeftTriggered() ||
  458. IsGamePadLeftStickLeftTriggered();
  459. case GamePadButtons.Right:
  460. return IsGamePadDPadRightTriggered() ||
  461. IsGamePadLeftStickRightTriggered();
  462. }
  463. return false;
  464. }
  465. /// <summary>
  466. /// The state of the keyboard as of the last update.
  467. /// </summary>
  468. private static MouseState currentMouseState;
  469. /// <summary>
  470. /// The state of the mouse as of the last update.
  471. /// </summary>
  472. public static MouseState CurrentMouseState
  473. {
  474. get { return currentMouseState; }
  475. }
  476. /// <summary>
  477. /// The state of the mouse as of the previous update.
  478. /// </summary>
  479. private static MouseState previousMouseState;
  480. /// <summary>
  481. /// Check if the left mouse button was just clicked.
  482. /// </summary>
  483. public static bool IsMouseLeftButtonClick()
  484. {
  485. return currentMouseState.LeftButton == ButtonState.Pressed &&
  486. previousMouseState.LeftButton == ButtonState.Released;
  487. }
  488. /// <summary>
  489. /// Check if the right mouse button was just clicked.
  490. /// </summary>
  491. public static bool IsMouseRightButtonClick()
  492. {
  493. return currentMouseState.RightButton == ButtonState.Pressed &&
  494. previousMouseState.RightButton == ButtonState.Released;
  495. }
  496. /// <summary>
  497. /// Check if the middle mouse button was just clicked.
  498. /// </summary>
  499. public static bool IsMouseMiddleButtonClick()
  500. {
  501. return currentMouseState.MiddleButton == ButtonState.Pressed &&
  502. previousMouseState.MiddleButton == ButtonState.Released;
  503. }
  504. /// <summary>
  505. /// Check if the mouse wheel was scrolled up.
  506. /// </summary>
  507. public static bool IsMouseWheelScrolledUp()
  508. {
  509. return currentMouseState.ScrollWheelValue > previousMouseState.ScrollWheelValue;
  510. }
  511. /// <summary>
  512. /// Check if the mouse wheel was scrolled down.
  513. /// </summary>
  514. public static bool IsMouseWheelScrolledDown()
  515. {
  516. return currentMouseState.ScrollWheelValue < previousMouseState.ScrollWheelValue;
  517. }
  518. private static int lastClickTime;
  519. private static ButtonState lastClickButtonState;
  520. /// <summary>
  521. /// Check if the left mouse button was double-clicked.
  522. /// </summary>
  523. public static bool IsMouseLeftButtonDoubleClick()
  524. {
  525. int currentTime = Environment.TickCount;
  526. bool isDoubleClick = currentMouseState.LeftButton == ButtonState.Pressed &&
  527. previousMouseState.LeftButton == ButtonState.Released &&
  528. lastClickButtonState == ButtonState.Pressed &&
  529. (currentTime - lastClickTime <= 500); // 500ms threshold
  530. if (currentMouseState.LeftButton == ButtonState.Pressed &&
  531. previousMouseState.LeftButton == ButtonState.Released)
  532. {
  533. lastClickTime = currentTime;
  534. lastClickButtonState = ButtonState.Pressed;
  535. }
  536. else if (currentMouseState.LeftButton == ButtonState.Released)
  537. {
  538. lastClickButtonState = ButtonState.Released;
  539. }
  540. return isDoubleClick;
  541. }
  542. /// <summary>
  543. /// Check if a specific mouse button is pressed.
  544. /// </summary>
  545. public static bool IsMouseButtonPressed(MouseButtons button)
  546. {
  547. switch (button)
  548. {
  549. case MouseButtons.LeftButton:
  550. return IsMouseLeftButtonPressed();
  551. case MouseButtons.RightButton:
  552. return IsMouseRightButtonPressed();
  553. case MouseButtons.MiddleButton:
  554. return IsMouseMiddleButtonPressed();
  555. default:
  556. return false;
  557. }
  558. }
  559. private static bool IsMouseMiddleButtonPressed()
  560. {
  561. return currentMouseState.MiddleButton == ButtonState.Pressed;
  562. }
  563. private static bool IsMouseRightButtonPressed()
  564. {
  565. return currentMouseState.RightButton == ButtonState.Pressed;
  566. }
  567. private static bool IsMouseLeftButtonPressed()
  568. {
  569. return currentMouseState.LeftButton == ButtonState.Pressed;
  570. }
  571. public static void HandleMouseDown(Point playerPosition)
  572. {
  573. var mouseDown = IsMouseButtonPressed(MouseButtons.LeftButton);
  574. var touchDown = IsTouchPressed();
  575. if (mouseDown
  576. || touchDown)
  577. {
  578. // Get the current mouse/touch position
  579. Point mouseDownPosition = mouseDown
  580. ? new Point(currentMouseState.X, currentMouseState.Y)
  581. : currentTouchPanelState.Count > 0 ? new Point((int)currentTouchPanelState[0].Position.X, (int)currentTouchPanelState[0].Position.Y) : Point.Zero;
  582. // Determine which keys to press based on the mouse down position relative to the player position
  583. Keys[] keysToPress = GetKeysForQuadrant(playerPosition, mouseDownPosition);
  584. // Combine existing pressed keys with new keys
  585. var pressedKeys = new HashSet<Keys>(currentKeyboardState.GetPressedKeys());
  586. foreach (Keys key in keysToPress)
  587. {
  588. pressedKeys.Add(key);
  589. }
  590. // Update currentKeyboardState with the combined keys
  591. currentKeyboardState = new KeyboardState(pressedKeys.ToArray());
  592. }
  593. }
  594. /// <summary>
  595. /// The state of the keyboard as of the last update.
  596. /// </summary>
  597. private static TouchCollection currentTouchPanelState;
  598. /// <summary>
  599. /// The state of the mouse as of the last update.
  600. /// </summary>
  601. public static TouchCollection CurrentTouchPanelState
  602. {
  603. get { return currentTouchPanelState; }
  604. }
  605. /// <summary>
  606. /// The state of the mouse as of the previous update.
  607. /// </summary>
  608. private static TouchCollection previousTouchPanelState;
  609. // Update methods to work with TouchCollection
  610. public static bool IsTouchPressed()
  611. {
  612. foreach (var touch in currentTouchPanelState)
  613. {
  614. if (touch.State == TouchLocationState.Pressed)
  615. {
  616. return true;
  617. }
  618. }
  619. return false;
  620. }
  621. public static bool IsTouchReleased()
  622. {
  623. foreach (var touch in currentTouchPanelState)
  624. {
  625. if (touch.State == TouchLocationState.Released)
  626. {
  627. return true;
  628. }
  629. }
  630. return false;
  631. }
  632. public static bool IsTouchMoved()
  633. {
  634. foreach (var touch in currentTouchPanelState)
  635. {
  636. if (touch.State == TouchLocationState.Moved)
  637. {
  638. return true;
  639. }
  640. }
  641. return false;
  642. }
  643. public static bool IsGestureDetected(GestureType gestureType)
  644. {
  645. // Gesture detection is not supported directly by TouchCollection
  646. return false;
  647. }
  648. private static Keys[] GetKeysForQuadrant(Point playerPosition, Point mouseDownPosition)
  649. {
  650. int deltaX = mouseDownPosition.X - playerPosition.X;
  651. int deltaY = mouseDownPosition.Y - playerPosition.Y;
  652. if (deltaY < 0 && Math.Abs(deltaX) <= Math.Abs(deltaY)) // Up
  653. {
  654. return new Keys[] { Keys.Up };
  655. }
  656. else if (deltaY > 0 && Math.Abs(deltaX) <= Math.Abs(deltaY)) // Down
  657. {
  658. return new Keys[] { Keys.Down };
  659. }
  660. else if (deltaX > 0 && Math.Abs(deltaY) <= Math.Abs(deltaX)) // Right
  661. {
  662. return new Keys[] { Keys.Right };
  663. }
  664. else if (deltaX < 0 && Math.Abs(deltaY) <= Math.Abs(deltaX)) // Left
  665. {
  666. return new Keys[] { Keys.Left };
  667. }
  668. else if (deltaX > 0 && deltaY < 0) // Up-Right
  669. {
  670. return new Keys[] { Keys.Up, Keys.Right };
  671. }
  672. else if (deltaX > 0 && deltaY > 0) // Down-Right
  673. {
  674. return new Keys[] { Keys.Down, Keys.Right };
  675. }
  676. else if (deltaX < 0 && deltaY < 0) // Up-Left
  677. {
  678. return new Keys[] { Keys.Up, Keys.Left };
  679. }
  680. else if (deltaX < 0 && deltaY > 0) // Down-Left
  681. {
  682. return new Keys[] { Keys.Down, Keys.Left };
  683. }
  684. return new Keys[] { }; // Default case
  685. }
  686. /// <summary>
  687. /// The action mappings for the game.
  688. /// </summary>
  689. private static ActionMap[] actionMaps;
  690. public static ActionMap[] ActionMaps
  691. {
  692. get { return actionMaps; }
  693. }
  694. /// <summary>
  695. /// Reset the action maps to their default values.
  696. /// </summary>
  697. private static void ResetActionMaps()
  698. {
  699. actionMaps = new ActionMap[(int)InputAction.TotalActionCount];
  700. actionMaps[(int)InputAction.MainMenu] = new ActionMap();
  701. actionMaps[(int)InputAction.MainMenu].keyboardKeys.Add(Keys.Tab);
  702. actionMaps[(int)InputAction.MainMenu].gamePadButtons.Add(GamePadButtons.Start);
  703. actionMaps[(int)InputAction.Ok] = new ActionMap();
  704. actionMaps[(int)InputAction.Ok].keyboardKeys.Add(Keys.Enter);
  705. actionMaps[(int)InputAction.Ok].gamePadButtons.Add(GamePadButtons.A);
  706. actionMaps[(int)InputAction.Back] = new ActionMap();
  707. actionMaps[(int)InputAction.Back].keyboardKeys.Add(Keys.Escape);
  708. actionMaps[(int)InputAction.Back].gamePadButtons.Add(GamePadButtons.B);
  709. actionMaps[(int)InputAction.CharacterManagement] = new ActionMap();
  710. actionMaps[(int)InputAction.CharacterManagement].keyboardKeys.Add(Keys.Space);
  711. actionMaps[(int)InputAction.CharacterManagement].gamePadButtons.Add(GamePadButtons.Y);
  712. actionMaps[(int)InputAction.ExitGame] = new ActionMap();
  713. actionMaps[(int)InputAction.ExitGame].keyboardKeys.Add(Keys.Escape);
  714. actionMaps[(int)InputAction.ExitGame].gamePadButtons.Add(GamePadButtons.Back);
  715. actionMaps[(int)InputAction.TakeView] = new ActionMap();
  716. actionMaps[(int)InputAction.TakeView].keyboardKeys.Add(Keys.LeftControl);
  717. actionMaps[(int)InputAction.TakeView].gamePadButtons.Add(GamePadButtons.Y);
  718. actionMaps[(int)InputAction.DropUnEquip] = new ActionMap();
  719. actionMaps[(int)InputAction.DropUnEquip].keyboardKeys.Add(Keys.D);
  720. actionMaps[(int)InputAction.DropUnEquip].gamePadButtons.Add(GamePadButtons.X);
  721. actionMaps[(int)InputAction.MoveCharacterUp] = new ActionMap();
  722. actionMaps[(int)InputAction.MoveCharacterUp].keyboardKeys.Add(Keys.Up);
  723. actionMaps[(int)InputAction.MoveCharacterUp].gamePadButtons.Add(GamePadButtons.Up);
  724. actionMaps[(int)InputAction.MoveCharacterDown] = new ActionMap();
  725. actionMaps[(int)InputAction.MoveCharacterDown].keyboardKeys.Add(Keys.Down);
  726. actionMaps[(int)InputAction.MoveCharacterDown].gamePadButtons.Add(GamePadButtons.Down);
  727. actionMaps[(int)InputAction.MoveCharacterLeft] = new ActionMap();
  728. actionMaps[(int)InputAction.MoveCharacterLeft].keyboardKeys.Add(Keys.Left);
  729. actionMaps[(int)InputAction.MoveCharacterLeft].gamePadButtons.Add(GamePadButtons.Left);
  730. actionMaps[(int)InputAction.MoveCharacterRight] = new ActionMap();
  731. actionMaps[(int)InputAction.MoveCharacterRight].keyboardKeys.Add(Keys.Right);
  732. actionMaps[(int)InputAction.MoveCharacterRight].gamePadButtons.Add(GamePadButtons.Right);
  733. actionMaps[(int)InputAction.CursorUp] = new ActionMap();
  734. actionMaps[(int)InputAction.CursorUp].keyboardKeys.Add(Keys.Up);
  735. actionMaps[(int)InputAction.CursorUp].gamePadButtons.Add(GamePadButtons.Up);
  736. actionMaps[(int)InputAction.CursorDown] = new ActionMap();
  737. actionMaps[(int)InputAction.CursorDown].keyboardKeys.Add(Keys.Down);
  738. actionMaps[(int)InputAction.CursorDown].gamePadButtons.Add(GamePadButtons.Down);
  739. actionMaps[(int)InputAction.DecreaseAmount] = new ActionMap();
  740. actionMaps[(int)InputAction.DecreaseAmount].keyboardKeys.Add(Keys.Left);
  741. actionMaps[(int)InputAction.DecreaseAmount].gamePadButtons.Add(GamePadButtons.Left);
  742. actionMaps[(int)InputAction.IncreaseAmount] = new ActionMap();
  743. actionMaps[(int)InputAction.IncreaseAmount].keyboardKeys.Add(Keys.Right);
  744. actionMaps[(int)InputAction.IncreaseAmount].gamePadButtons.Add(GamePadButtons.Right);
  745. actionMaps[(int)InputAction.PageLeft] = new ActionMap();
  746. actionMaps[(int)InputAction.PageLeft].keyboardKeys.Add(Keys.LeftShift);
  747. actionMaps[(int)InputAction.PageLeft].gamePadButtons.Add(GamePadButtons.LeftTrigger);
  748. actionMaps[(int)InputAction.PageRight] = new ActionMap();
  749. actionMaps[(int)InputAction.PageRight].keyboardKeys.Add(Keys.RightShift);
  750. actionMaps[(int)InputAction.PageRight].gamePadButtons.Add(GamePadButtons.RightTrigger);
  751. actionMaps[(int)InputAction.TargetUp] = new ActionMap();
  752. actionMaps[(int)InputAction.TargetUp].keyboardKeys.Add(Keys.Up);
  753. actionMaps[(int)InputAction.TargetUp].gamePadButtons.Add(GamePadButtons.Up);
  754. actionMaps[(int)InputAction.TargetDown] = new ActionMap();
  755. actionMaps[(int)InputAction.TargetDown].keyboardKeys.Add(Keys.Down);
  756. actionMaps[(int)InputAction.TargetDown].gamePadButtons.Add(GamePadButtons.Down);
  757. actionMaps[(int)InputAction.ActiveCharacterLeft] = new ActionMap();
  758. actionMaps[(int)InputAction.ActiveCharacterLeft].keyboardKeys.Add(Keys.Left);
  759. actionMaps[(int)InputAction.ActiveCharacterLeft].gamePadButtons.Add(GamePadButtons.Left);
  760. actionMaps[(int)InputAction.ActiveCharacterRight] = new ActionMap();
  761. actionMaps[(int)InputAction.ActiveCharacterRight].keyboardKeys.Add(Keys.Right);
  762. actionMaps[(int)InputAction.ActiveCharacterRight].gamePadButtons.Add(GamePadButtons.Right);
  763. }
  764. /// <summary>
  765. /// Check if an action has been pressed.
  766. /// </summary>
  767. public static bool IsActionPressed(InputAction action)
  768. {
  769. return IsActionMapPressed(actionMaps[(int)action]);
  770. }
  771. /// <summary>
  772. /// Check if an action was just performed in the most recent update.
  773. /// </summary>
  774. public static bool IsActionTriggered(InputAction action)
  775. {
  776. return IsActionMapTriggered(actionMaps[(int)action]);
  777. }
  778. /// <summary>
  779. /// Check if an action map has been pressed.
  780. /// </summary>
  781. private static bool IsActionMapPressed(ActionMap actionMap)
  782. {
  783. for (int i = 0; i < actionMap.keyboardKeys.Count; i++)
  784. {
  785. if (IsKeyPressed(actionMap.keyboardKeys[i]))
  786. {
  787. return true;
  788. }
  789. }
  790. if (currentGamePadState.IsConnected)
  791. {
  792. for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
  793. {
  794. if (IsGamePadButtonPressed(actionMap.gamePadButtons[i]))
  795. {
  796. return true;
  797. }
  798. }
  799. }
  800. return false;
  801. }
  802. /// <summary>
  803. /// Check if an action map has been triggered this frame.
  804. /// </summary>
  805. private static bool IsActionMapTriggered(ActionMap actionMap)
  806. {
  807. for (int i = 0; i < actionMap.keyboardKeys.Count; i++)
  808. {
  809. if (IsKeyTriggered(actionMap.keyboardKeys[i]))
  810. {
  811. return true;
  812. }
  813. }
  814. if (currentGamePadState.IsConnected)
  815. {
  816. for (int i = 0; i < actionMap.gamePadButtons.Count; i++)
  817. {
  818. if (IsGamePadButtonTriggered(actionMap.gamePadButtons[i]))
  819. {
  820. return true;
  821. }
  822. }
  823. }
  824. return false;
  825. }
  826. /// <summary>
  827. /// Initializes the default control keys for all actions.
  828. /// </summary>
  829. public static void Initialize()
  830. {
  831. ResetActionMaps();
  832. }
  833. /// <summary>
  834. /// Updates the keyboard and gamepad control states.
  835. /// </summary>
  836. public static void Update()
  837. {
  838. // update the keyboard state
  839. previousKeyboardState = currentKeyboardState;
  840. currentKeyboardState = Keyboard.GetState();
  841. // update the gamepad state
  842. previousGamePadState = currentGamePadState;
  843. currentGamePadState = GamePad.GetState(PlayerIndex.One);
  844. // update the mouse state
  845. previousMouseState = currentMouseState;
  846. currentMouseState = Mouse.GetState();
  847. // update the touch panel state
  848. previousTouchPanelState = currentTouchPanelState;
  849. currentTouchPanelState = TouchPanel.GetState();
  850. }
  851. }
  852. }