Input.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. //-----------------------------------------------------------------------------
  2. // Input.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Input;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using System.Threading;
  13. using RacingGame.Graphics;
  14. using RacingGame.Helpers;
  15. using RacingGame.Sounds;
  16. namespace RacingGame.GameLogic
  17. {
  18. /// <summary>
  19. /// Input helper class, captures all the mouse, keyboard and XBox 360
  20. /// controller input and provides some nice helper methods and properties.
  21. /// Will also keep track of the last frame states for comparison if
  22. /// a button was just pressed this frame, but not already in the last frame.
  23. /// </summary>
  24. public static class Input
  25. {
  26. #if !XBOX360
  27. /// <summary>
  28. /// Mouse state, set every frame in the Update method.
  29. /// </summary>
  30. private static MouseState mouseState, mouseStateLastFrame;
  31. #endif
  32. /// <summary>
  33. /// Was a mouse detected? Returns true if the user moves the mouse.
  34. /// On the Xbox 360 there will be no mouse movement and theirfore we
  35. /// know that we don't have to display the mouse.
  36. /// </summary>
  37. private static bool mouseDetected = false;
  38. /// <summary>
  39. /// Keyboard state, set every frame in the Update method.
  40. /// Note: KeyboardState is a class and not a struct,
  41. /// we have to initialize it here, else we might run into trouble when
  42. /// accessing any keyboardState data before BaseGame.Update() is called.
  43. /// We can also NOT use the last state because everytime we call
  44. /// Keyboard.GetState() the old state is useless (see XNA help for more
  45. /// information, section Input). We store our own array of keys from
  46. /// the last frame for comparing stuff.
  47. /// </summary>
  48. private static KeyboardState keyboardState =
  49. Microsoft.Xna.Framework.Input.Keyboard.GetState();
  50. /// <summary>
  51. /// Keys pressed last frame, for comparison if a key was just pressed.
  52. /// </summary>
  53. private static List<Keys> keysPressedLastFrame = new List<Keys>();
  54. /// <summary>
  55. /// GamePad state, set every frame in the Update method.
  56. /// </summary>
  57. private static GamePadState gamePadState, gamePadStateLastFrame;
  58. /// <summary>
  59. /// Mouse wheel delta this frame. XNA does report only the total
  60. /// scroll value, but we usually need the current delta!
  61. /// </summary>
  62. /// <returns>0</returns>
  63. private static int mouseWheelDelta = 0;
  64. #if !XBOX360
  65. private static int mouseWheelValue = 0;
  66. #endif
  67. /// <summary>
  68. /// Start dragging pos, will be set when we just pressed the left
  69. /// mouse button. Used for the MouseDraggingAmount property.
  70. /// </summary>
  71. private static Point startDraggingPos;
  72. /// <summary>
  73. /// Was a mouse detected? Returns true if the user moves the mouse.
  74. /// On the Xbox 360 there will be no mouse movement and theirfore we
  75. /// know that we don't have to display the mouse.
  76. /// </summary>
  77. /// <returns>Bool</returns>
  78. public static bool MouseDetected
  79. {
  80. get
  81. {
  82. return mouseDetected;
  83. }
  84. }
  85. /// <summary>
  86. /// Mouse position
  87. /// </summary>
  88. /// <returns>Point</returns>
  89. public static Point MousePos
  90. {
  91. get
  92. {
  93. #if !XBOX360
  94. return new Point(mouseState.X, mouseState.Y);
  95. #else
  96. return Point.Zero;
  97. #endif
  98. }
  99. }
  100. /// <summary>
  101. /// X and y movements of the mouse this frame
  102. /// </summary>
  103. #if !XBOX360
  104. private static float mouseXMovement, mouseYMovement;
  105. private static float lastMouseXMovement, lastMouseYMovement;
  106. #endif
  107. /// <summary>
  108. /// Mouse x movement
  109. /// </summary>
  110. /// <returns>Float</returns>
  111. public static float MouseXMovement
  112. {
  113. get
  114. {
  115. #if !XBOX360
  116. return mouseXMovement;
  117. #else
  118. return 0;
  119. #endif
  120. }
  121. }
  122. /// <summary>
  123. /// Mouse y movement
  124. /// </summary>
  125. /// <returns>Float</returns>
  126. public static float MouseYMovement
  127. {
  128. get
  129. {
  130. #if !XBOX360
  131. return mouseYMovement;
  132. #else
  133. return 0;
  134. #endif
  135. }
  136. }
  137. /// <summary>
  138. /// Mouse has moved in either the X or Y direction
  139. /// </summary>
  140. /// <returns>Boolean</returns>
  141. public static bool HasMouseMoved
  142. {
  143. get
  144. {
  145. #if !XBOX360
  146. //TODO: Introduce a mouse movement threshold constant
  147. if (MouseXMovement > 1 || MouseYMovement > 1)
  148. return true;
  149. #endif
  150. return false;
  151. }
  152. }
  153. /// <summary>
  154. /// Mouse left button pressed
  155. /// </summary>
  156. /// <returns>Bool</returns>
  157. public static bool MouseLeftButtonPressed
  158. {
  159. get
  160. {
  161. #if !XBOX360
  162. return mouseState.LeftButton == ButtonState.Pressed;
  163. #else
  164. return false;
  165. #endif
  166. }
  167. }
  168. /// <summary>
  169. /// Mouse right button pressed
  170. /// </summary>
  171. /// <returns>Bool</returns>
  172. public static bool MouseRightButtonPressed
  173. {
  174. get
  175. {
  176. #if !XBOX360
  177. return mouseState.RightButton == ButtonState.Pressed;
  178. #else
  179. return false;
  180. #endif
  181. }
  182. }
  183. /// <summary>
  184. /// Mouse middle button pressed
  185. /// </summary>
  186. /// <returns>Bool</returns>
  187. public static bool MouseMiddleButtonPressed
  188. {
  189. get
  190. {
  191. #if !XBOX360
  192. return mouseState.MiddleButton == ButtonState.Pressed;
  193. #else
  194. return false;
  195. #endif
  196. }
  197. }
  198. /// <summary>
  199. /// Mouse left button just pressed
  200. /// </summary>
  201. /// <returns>Bool</returns>
  202. public static bool MouseLeftButtonJustPressed
  203. {
  204. get
  205. {
  206. #if !XBOX360
  207. return mouseState.LeftButton == ButtonState.Pressed &&
  208. mouseStateLastFrame.LeftButton == ButtonState.Released;
  209. #else
  210. return false;
  211. #endif
  212. }
  213. }
  214. /// <summary>
  215. /// Mouse right button just pressed
  216. /// </summary>
  217. /// <returns>Bool</returns>
  218. [System.Diagnostics.CodeAnalysis.SuppressMessage(
  219. "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode",
  220. Justification = "Makes this class reuseable.")]
  221. public static bool MouseRightButtonJustPressed
  222. {
  223. get
  224. {
  225. #if !XBOX360
  226. return mouseState.RightButton == ButtonState.Pressed &&
  227. mouseStateLastFrame.RightButton == ButtonState.Released;
  228. #else
  229. return false;
  230. #endif
  231. }
  232. }
  233. /// <summary>
  234. /// Mouse dragging amount
  235. /// </summary>
  236. /// <returns>Point</returns>
  237. [System.Diagnostics.CodeAnalysis.SuppressMessage(
  238. "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode",
  239. Justification = "Makes this class reuseable.")]
  240. public static Point MouseDraggingAmount
  241. {
  242. get
  243. {
  244. return new Point(
  245. startDraggingPos.X - MousePos.X,
  246. startDraggingPos.Y - MousePos.Y);
  247. }
  248. }
  249. /// <summary>
  250. /// Reset mouse dragging amount
  251. /// </summary>
  252. public static void ResetMouseDraggingAmount()
  253. {
  254. startDraggingPos = MousePos;
  255. }
  256. /// <summary>
  257. /// Mouse wheel delta
  258. /// </summary>
  259. /// <returns>Int</returns>
  260. public static int MouseWheelDelta
  261. {
  262. get
  263. {
  264. return mouseWheelDelta;
  265. }
  266. }
  267. /// <summary>
  268. /// Mouse in box
  269. /// </summary>
  270. /// <param name="rect">Rectangle</param>
  271. /// <returns>Bool</returns>
  272. public static bool MouseInBox(Rectangle rect)
  273. {
  274. #if !XBOX360
  275. bool ret = mouseState.X >= rect.X &&
  276. mouseState.Y >= rect.Y &&
  277. mouseState.X < rect.Right &&
  278. mouseState.Y < rect.Bottom;
  279. bool lastRet = mouseStateLastFrame.X >= rect.X &&
  280. mouseStateLastFrame.Y >= rect.Y &&
  281. mouseStateLastFrame.X < rect.Right &&
  282. mouseStateLastFrame.Y < rect.Bottom;
  283. // Highlight happend?
  284. if (ret &&
  285. lastRet == false)
  286. Sound.Play(Sound.Sounds.Highlight);
  287. return ret;
  288. #else
  289. return false;
  290. #endif
  291. }
  292. /// <summary>
  293. /// Mouse in box relative
  294. /// </summary>
  295. /// <param name="rect">Rectangle</param>
  296. /// <returns>Bool</returns>
  297. public static bool MouseInBoxRelative(Rectangle rect)
  298. {
  299. float widthFactor = BaseGame.Width / 1024.0f;
  300. float heightFactor = BaseGame.Height / 640.0f;
  301. return MouseInBox(new Rectangle(
  302. (int)Math.Round(rect.X * widthFactor),
  303. (int)Math.Round(rect.Y * heightFactor),
  304. (int)Math.Round(rect.Right * widthFactor),
  305. (int)Math.Round(rect.Bottom * heightFactor)));
  306. }
  307. /// <summary>
  308. /// Keyboard
  309. /// </summary>
  310. /// <returns>Keyboard state</returns>
  311. public static KeyboardState Keyboard
  312. {
  313. get
  314. {
  315. return keyboardState;
  316. }
  317. }
  318. public static bool IsSpecialKey(Keys key)
  319. {
  320. // All keys except A-Z, 0-9 and `-\[];',./= (and space) are special keys.
  321. // With shift pressed this also results in this keys:
  322. // ~_|{}:"<>? !@#$%^&*().
  323. int keyNum = (int)key;
  324. if ((keyNum >= (int)Keys.A && keyNum <= (int)Keys.Z) ||
  325. (keyNum >= (int)Keys.D0 && keyNum <= (int)Keys.D9) ||
  326. key == Keys.Space || // well, space ^^
  327. key == Keys.OemTilde || // `~
  328. key == Keys.OemMinus || // -_
  329. key == Keys.OemPipe || // \|
  330. key == Keys.OemOpenBrackets || // [{
  331. key == Keys.OemCloseBrackets || // ]}
  332. key == Keys.OemQuotes || // '"
  333. key == Keys.OemQuestion || // /?
  334. key == Keys.OemPlus) // =+
  335. {
  336. return false;
  337. }
  338. // Else is is a special key
  339. return true;
  340. }
  341. /// <summary>
  342. /// Key to char helper conversion method.
  343. /// Note: If the keys are mapped other than on a default QWERTY
  344. /// keyboard, this method will not work properly. Most keyboards
  345. /// will return the same for A-Z and 0-9, but the special keys
  346. /// might be different.
  347. /// </summary>
  348. /// <param name="key">Key</param>
  349. /// <returns>Char</returns>
  350. public static char KeyToChar(Keys key, bool shiftPressed)
  351. {
  352. // If key will not be found, just return space
  353. char ret = ' ';
  354. int keyNum = (int)key;
  355. if (keyNum >= (int)Keys.A && keyNum <= (int)Keys.Z)
  356. {
  357. if (shiftPressed)
  358. ret = key.ToString()[0];
  359. else
  360. ret = key.ToString().ToLower()[0];
  361. }
  362. else if (keyNum >= (int)Keys.D0 && keyNum <= (int)Keys.D9 &&
  363. shiftPressed == false)
  364. {
  365. ret = (char)((int)'0' + (keyNum - Keys.D0));
  366. }
  367. else if (key == Keys.D1 && shiftPressed)
  368. ret = '!';
  369. else if (key == Keys.D2 && shiftPressed)
  370. ret = '@';
  371. else if (key == Keys.D3 && shiftPressed)
  372. ret = '#';
  373. else if (key == Keys.D4 && shiftPressed)
  374. ret = '$';
  375. else if (key == Keys.D5 && shiftPressed)
  376. ret = '%';
  377. else if (key == Keys.D6 && shiftPressed)
  378. ret = '^';
  379. else if (key == Keys.D7 && shiftPressed)
  380. ret = '&';
  381. else if (key == Keys.D8 && shiftPressed)
  382. ret = '*';
  383. else if (key == Keys.D9 && shiftPressed)
  384. ret = '(';
  385. else if (key == Keys.D0 && shiftPressed)
  386. ret = ')';
  387. else if (key == Keys.OemTilde)
  388. ret = shiftPressed ? '~' : '`';
  389. else if (key == Keys.OemMinus)
  390. ret = shiftPressed ? '_' : '-';
  391. else if (key == Keys.OemPipe)
  392. ret = shiftPressed ? '|' : '\\';
  393. else if (key == Keys.OemOpenBrackets)
  394. ret = shiftPressed ? '{' : '[';
  395. else if (key == Keys.OemCloseBrackets)
  396. ret = shiftPressed ? '}' : ']';
  397. else if (key == Keys.OemSemicolon)
  398. ret = shiftPressed ? ':' : ';';
  399. else if (key == Keys.OemQuotes)
  400. ret = shiftPressed ? '"' : '\'';
  401. else if (key == Keys.OemComma)
  402. ret = shiftPressed ? '<' : '.';
  403. else if (key == Keys.OemPeriod)
  404. ret = shiftPressed ? '>' : ',';
  405. else if (key == Keys.OemQuestion)
  406. ret = shiftPressed ? '?' : '/';
  407. else if (key == Keys.OemPlus)
  408. ret = shiftPressed ? '+' : '=';
  409. // Return result
  410. return ret;
  411. }
  412. /// <summary>
  413. /// Handle keyboard input helper method to catch keyboard input
  414. /// for an input text. Only used to enter the player name in the game.
  415. /// </summary>
  416. /// <param name="inputText">Input text</param>
  417. public static void HandleKeyboardInput(ref string inputText)
  418. {
  419. // Is a shift key pressed (we have to check both, left and right)
  420. bool isShiftPressed =
  421. keyboardState.IsKeyDown(Keys.LeftShift) ||
  422. keyboardState.IsKeyDown(Keys.RightShift);
  423. // Go through all pressed keys
  424. foreach (Keys pressedKey in keyboardState.GetPressedKeys())
  425. // Only process if it was not pressed last frame
  426. if (keysPressedLastFrame.Contains(pressedKey) == false)
  427. {
  428. // No special key?
  429. if (IsSpecialKey(pressedKey) == false &&
  430. // Max. allow 32 chars
  431. inputText.Length < 32)
  432. {
  433. // Then add the letter to our inputText.
  434. // Check also the shift state!
  435. inputText += KeyToChar(pressedKey, isShiftPressed);
  436. }
  437. else if (pressedKey == Keys.Back &&
  438. inputText.Length > 0)
  439. {
  440. // Remove 1 character at end
  441. inputText = inputText.Substring(0, inputText.Length - 1);
  442. }
  443. }
  444. }
  445. /// <summary>
  446. /// Keyboard key just pressed
  447. /// </summary>
  448. /// <returns>Bool</returns>
  449. public static bool KeyboardKeyJustPressed(Keys key)
  450. {
  451. return keyboardState.IsKeyDown(key) &&
  452. keysPressedLastFrame.Contains(key) == false;
  453. }
  454. /// <summary>
  455. /// Keyboard space just pressed
  456. /// </summary>
  457. /// <returns>Bool</returns>
  458. public static bool KeyboardSpaceJustPressed
  459. {
  460. get
  461. {
  462. return keyboardState.IsKeyDown(Keys.Space) &&
  463. keysPressedLastFrame.Contains(Keys.Space) == false;
  464. }
  465. }
  466. /// <summary>
  467. /// Keyboard F1 just pressed
  468. /// </summary>
  469. /// <returns>Bool</returns>
  470. public static bool KeyboardF1JustPressed
  471. {
  472. get
  473. {
  474. return keyboardState.IsKeyDown(Keys.F1) &&
  475. keysPressedLastFrame.Contains(Keys.F1) == false;
  476. }
  477. }
  478. /// <summary>
  479. /// Keyboard escape just pressed
  480. /// </summary>
  481. /// <returns>Bool</returns>
  482. public static bool KeyboardEscapeJustPressed
  483. {
  484. get
  485. {
  486. return keyboardState.IsKeyDown(Keys.Escape) &&
  487. keysPressedLastFrame.Contains(Keys.Escape) == false;
  488. }
  489. }
  490. /// <summary>
  491. /// Keyboard left just pressed
  492. /// </summary>
  493. /// <returns>Bool</returns>
  494. public static bool KeyboardLeftJustPressed
  495. {
  496. get
  497. {
  498. return keyboardState.IsKeyDown(Keys.Left) &&
  499. keysPressedLastFrame.Contains(Keys.Left) == false;
  500. }
  501. }
  502. /// <summary>
  503. /// Keyboard right just pressed
  504. /// </summary>
  505. /// <returns>Bool</returns>
  506. public static bool KeyboardRightJustPressed
  507. {
  508. get
  509. {
  510. return keyboardState.IsKeyDown(Keys.Right) &&
  511. keysPressedLastFrame.Contains(Keys.Right) == false;
  512. }
  513. }
  514. /// <summary>
  515. /// Keyboard up just pressed
  516. /// </summary>
  517. /// <returns>Bool</returns>
  518. public static bool KeyboardUpJustPressed
  519. {
  520. get
  521. {
  522. return keyboardState.IsKeyDown(Keys.Up) &&
  523. keysPressedLastFrame.Contains(Keys.Up) == false;
  524. }
  525. }
  526. /// <summary>
  527. /// Keyboard down just pressed
  528. /// </summary>
  529. /// <returns>Bool</returns>
  530. public static bool KeyboardDownJustPressed
  531. {
  532. get
  533. {
  534. return keyboardState.IsKeyDown(Keys.Down) &&
  535. keysPressedLastFrame.Contains(Keys.Down) == false;
  536. }
  537. }
  538. /// <summary>
  539. /// Keyboard left pressed
  540. /// </summary>
  541. /// <returns>Bool</returns>
  542. public static bool KeyboardLeftPressed
  543. {
  544. get
  545. {
  546. return keyboardState.IsKeyDown(Keys.Left);
  547. }
  548. }
  549. /// <summary>
  550. /// Keyboard right pressed
  551. /// </summary>
  552. /// <returns>Bool</returns>
  553. public static bool KeyboardRightPressed
  554. {
  555. get
  556. {
  557. return keyboardState.IsKeyDown(Keys.Right);
  558. }
  559. }
  560. /// <summary>
  561. /// Keyboard up pressed
  562. /// </summary>
  563. /// <returns>Bool</returns>
  564. public static bool KeyboardUpPressed
  565. {
  566. get
  567. {
  568. return keyboardState.IsKeyDown(Keys.Up);
  569. }
  570. }
  571. /// <summary>
  572. /// Keyboard down pressed
  573. /// </summary>
  574. /// <returns>Bool</returns>
  575. public static bool KeyboardDownPressed
  576. {
  577. get
  578. {
  579. return keyboardState.IsKeyDown(Keys.Down);
  580. }
  581. }
  582. /// <summary>
  583. /// Game pad
  584. /// </summary>
  585. /// <returns>Game pad state</returns>
  586. public static GamePadState GamePad
  587. {
  588. get
  589. {
  590. return gamePadState;
  591. }
  592. }
  593. /// <summary>
  594. /// Is game pad connected
  595. /// </summary>
  596. /// <returns>Bool</returns>
  597. public static bool IsGamePadConnected
  598. {
  599. get
  600. {
  601. return gamePadState.IsConnected;
  602. }
  603. }
  604. /// <summary>
  605. /// Game pad start pressed
  606. /// </summary>
  607. /// <returns>Bool</returns>
  608. public static bool GamePadStartPressed
  609. {
  610. get
  611. {
  612. return gamePadState.Buttons.Start == ButtonState.Pressed;
  613. }
  614. }
  615. /// <summary>
  616. /// Game pad a pressed
  617. /// </summary>
  618. /// <returns>Bool</returns>
  619. public static bool GamePadAPressed
  620. {
  621. get
  622. {
  623. return gamePadState.Buttons.A == ButtonState.Pressed;
  624. }
  625. }
  626. /// <summary>
  627. /// Game pad b pressed
  628. /// </summary>
  629. /// <returns>Bool</returns>
  630. public static bool GamePadBPressed
  631. {
  632. get
  633. {
  634. return gamePadState.Buttons.B == ButtonState.Pressed;
  635. }
  636. }
  637. /// <summary>
  638. /// Game pad x pressed
  639. /// </summary>
  640. /// <returns>Bool</returns>
  641. public static bool GamePadXPressed
  642. {
  643. get
  644. {
  645. return gamePadState.Buttons.X == ButtonState.Pressed;
  646. }
  647. }
  648. /// <summary>
  649. /// Game pad y pressed
  650. /// </summary>
  651. /// <returns>Bool</returns>
  652. public static bool GamePadYPressed
  653. {
  654. get
  655. {
  656. return gamePadState.Buttons.Y == ButtonState.Pressed;
  657. }
  658. }
  659. /// <summary>
  660. /// Game pad left pressed
  661. /// </summary>
  662. /// <returns>Bool</returns>
  663. public static bool GamePadLeftPressed
  664. {
  665. get
  666. {
  667. return gamePadState.DPad.Left == ButtonState.Pressed ||
  668. gamePadState.ThumbSticks.Left.X < -0.75f;
  669. }
  670. }
  671. /// <summary>
  672. /// Game pad right pressed
  673. /// </summary>
  674. /// <returns>Bool</returns>
  675. public static bool GamePadRightPressed
  676. {
  677. get
  678. {
  679. return gamePadState.DPad.Right == ButtonState.Pressed ||
  680. gamePadState.ThumbSticks.Left.X > 0.75f;
  681. }
  682. }
  683. /// <summary>
  684. /// Game pad left just pressed
  685. /// </summary>
  686. /// <returns>Bool</returns>
  687. public static bool GamePadLeftJustPressed
  688. {
  689. get
  690. {
  691. return (gamePadState.DPad.Left == ButtonState.Pressed &&
  692. gamePadStateLastFrame.DPad.Left == ButtonState.Released) ||
  693. (gamePadState.ThumbSticks.Left.X < -0.75f &&
  694. gamePadStateLastFrame.ThumbSticks.Left.X > -0.75f);
  695. }
  696. }
  697. /// <summary>
  698. /// Game pad right just pressed
  699. /// </summary>
  700. /// <returns>Bool</returns>
  701. public static bool GamePadRightJustPressed
  702. {
  703. get
  704. {
  705. return (gamePadState.DPad.Right == ButtonState.Pressed &&
  706. gamePadStateLastFrame.DPad.Right == ButtonState.Released) ||
  707. (gamePadState.ThumbSticks.Left.X > 0.75f &&
  708. gamePadStateLastFrame.ThumbSticks.Left.X < 0.75f);
  709. }
  710. }
  711. /// <summary>
  712. /// Game pad up just pressed
  713. /// </summary>
  714. /// <returns>Bool</returns>
  715. public static bool GamePadUpJustPressed
  716. {
  717. get
  718. {
  719. return (gamePadState.DPad.Up == ButtonState.Pressed &&
  720. gamePadStateLastFrame.DPad.Up == ButtonState.Released) ||
  721. (gamePadState.ThumbSticks.Left.Y > 0.75f &&
  722. gamePadStateLastFrame.ThumbSticks.Left.Y < 0.75f);
  723. }
  724. }
  725. /// <summary>
  726. /// Game pad down just pressed
  727. /// </summary>
  728. /// <returns>Bool</returns>
  729. public static bool GamePadDownJustPressed
  730. {
  731. get
  732. {
  733. return (gamePadState.DPad.Down == ButtonState.Pressed &&
  734. gamePadStateLastFrame.DPad.Down == ButtonState.Released) ||
  735. (gamePadState.ThumbSticks.Left.Y < -0.75f &&
  736. gamePadStateLastFrame.ThumbSticks.Left.Y > -0.75f);
  737. }
  738. }
  739. /// <summary>
  740. /// Game pad up pressed
  741. /// </summary>
  742. /// <returns>Bool</returns>
  743. public static bool GamePadUpPressed
  744. {
  745. get
  746. {
  747. return gamePadState.DPad.Up == ButtonState.Pressed ||
  748. gamePadState.ThumbSticks.Left.Y > 0.75f;
  749. }
  750. }
  751. /// <summary>
  752. /// Game pad down pressed
  753. /// </summary>
  754. /// <returns>Bool</returns>
  755. public static bool GamePadDownPressed
  756. {
  757. get
  758. {
  759. return gamePadState.DPad.Down == ButtonState.Pressed ||
  760. gamePadState.ThumbSticks.Left.Y < -0.75f;
  761. }
  762. }
  763. /// <summary>
  764. /// Game pad a just pressed
  765. /// </summary>
  766. /// <returns>Bool</returns>
  767. public static bool GamePadAJustPressed
  768. {
  769. get
  770. {
  771. return gamePadState.Buttons.A == ButtonState.Pressed &&
  772. gamePadStateLastFrame.Buttons.A == ButtonState.Released;
  773. }
  774. }
  775. /// <summary>
  776. /// Game pad b just pressed
  777. /// </summary>
  778. /// <returns>Bool</returns>
  779. public static bool GamePadBJustPressed
  780. {
  781. get
  782. {
  783. return gamePadState.Buttons.B == ButtonState.Pressed &&
  784. gamePadStateLastFrame.Buttons.B == ButtonState.Released;
  785. }
  786. }
  787. /// <summary>
  788. /// Game pad x just pressed
  789. /// </summary>
  790. /// <returns>Bool</returns>
  791. [System.Diagnostics.CodeAnalysis.SuppressMessage(
  792. "Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode",
  793. Justification = "Makes this class reuseable.")]
  794. public static bool GamePadXJustPressed
  795. {
  796. get
  797. {
  798. return gamePadState.Buttons.X == ButtonState.Pressed &&
  799. gamePadStateLastFrame.Buttons.X == ButtonState.Released;
  800. }
  801. }
  802. /// <summary>
  803. /// Game pad y just pressed
  804. /// </summary>
  805. /// <returns>Bool</returns>
  806. public static bool GamePadYJustPressed
  807. {
  808. get
  809. {
  810. return gamePadState.Buttons.Y == ButtonState.Pressed &&
  811. gamePadStateLastFrame.Buttons.Y == ButtonState.Released;
  812. }
  813. }
  814. /// <summary>
  815. /// Game pad back just pressed
  816. /// </summary>
  817. /// <returns>Bool</returns>
  818. public static bool GamePadBackJustPressed
  819. {
  820. get
  821. {
  822. return gamePadState.Buttons.Back == ButtonState.Pressed &&
  823. gamePadStateLastFrame.Buttons.Back == ButtonState.Released;
  824. }
  825. }
  826. /// <summary>
  827. /// Update, called from BaseGame.Update().
  828. /// Will catch all new states for keyboard, mouse and the gamepad.
  829. /// </summary>
  830. internal static void Update()
  831. {
  832. #if XBOX360
  833. // No mouse support on the XBox360 yet :(
  834. mouseDetected = false;
  835. #else
  836. // Handle mouse input variables
  837. mouseStateLastFrame = mouseState;
  838. mouseState = Microsoft.Xna.Framework.Input.Mouse.GetState();
  839. // Update mouseXMovement and mouseYMovement
  840. lastMouseXMovement += mouseState.X - mouseStateLastFrame.X;
  841. lastMouseYMovement += mouseState.Y - mouseStateLastFrame.Y;
  842. mouseXMovement = lastMouseXMovement / 2.0f;
  843. mouseYMovement = lastMouseYMovement / 2.0f;
  844. lastMouseXMovement -= lastMouseXMovement / 2.0f;
  845. lastMouseYMovement -= lastMouseYMovement / 2.0f;
  846. if (MouseLeftButtonPressed == false)
  847. startDraggingPos = MousePos;
  848. mouseWheelDelta = mouseState.ScrollWheelValue - mouseWheelValue;
  849. mouseWheelValue = mouseState.ScrollWheelValue;
  850. // If we are in the game and don't show the mouse cursor anyway,
  851. // reset it to the center to allow moving it around.
  852. if (RacingGameManager.InMenu == false &&
  853. // App must be active
  854. RacingGameManager.IsAppActive)
  855. {
  856. Microsoft.Xna.Framework.Input.Mouse.SetPosition(
  857. BaseGame.Width / 2, BaseGame.Height / 2);
  858. // Also use this for the current mouse pos for next frame,
  859. // else the mouseXMovement is messed up!
  860. mouseState = Microsoft.Xna.Framework.Input.Mouse.GetState();
  861. }
  862. // Check if mouse was moved this frame if it is not detected yet.
  863. // This allows us to ignore the mouse even when it is captured
  864. // on a windows machine if just the gamepad or keyboard is used.
  865. if (mouseDetected == false)// &&
  866. //always returns false: Microsoft.Xna.Framework.Input.Mouse.IsCaptured)
  867. mouseDetected = mouseState.X != mouseStateLastFrame.X ||
  868. mouseState.Y != mouseStateLastFrame.Y ||
  869. mouseState.LeftButton != mouseStateLastFrame.LeftButton;
  870. #endif
  871. // Handle keyboard input
  872. keysPressedLastFrame = new List<Keys>(keyboardState.GetPressedKeys());
  873. keyboardState = Microsoft.Xna.Framework.Input.Keyboard.GetState();
  874. // And finally catch the XBox Controller input (only use 1 player here)
  875. gamePadStateLastFrame = gamePadState;
  876. gamePadState =
  877. Microsoft.Xna.Framework.Input.GamePad.GetState(PlayerIndex.One);
  878. }
  879. }
  880. }