MouseExtended.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Input;
  4. namespace MonoGame.Extended.Input;
  5. /// <summary>
  6. /// Represents mouse input.
  7. /// </summary>
  8. /// <remarks>
  9. /// This si an extended version of the default <see cref="Microsoft.Xna.Framework.Input.Mouse"/> class which offers
  10. /// internal tracking of both the previous and current state of mouse input.
  11. /// </remarks>
  12. public static class MouseExtended
  13. {
  14. private static MouseState _currentMouseState;
  15. private static MouseState _previousMouseState;
  16. /// <summary>
  17. /// Gets the state of mouse input.
  18. /// </summary>
  19. /// <returns>
  20. /// A <see cref="MouseStateExtended"/> value that represents the state of mouse input.
  21. /// </returns>
  22. public static MouseStateExtended GetState()
  23. {
  24. return new MouseStateExtended(_currentMouseState, _previousMouseState);
  25. }
  26. /// <summary>
  27. /// Updates the <see cref="MouseExtended"/>.
  28. /// </summary>
  29. /// <remarks>
  30. /// This internally will overwrite the source data for the previous state with the current state, then get the
  31. /// current state from the mouse input. This should only be called once per update cycle. Calling it more than
  32. /// once per update cycle can result in the cached previous state being overwritten with invalid data.
  33. /// </remarks>
  34. public static void Update()
  35. {
  36. _previousMouseState = _currentMouseState;
  37. _currentMouseState = Mouse.GetState();
  38. }
  39. /// <summary>
  40. /// Sets the position of the mouse cursor to the specified coordinates relative to the game window.
  41. /// </summary>
  42. /// <param name="x">The x-coordinate position.</param>
  43. /// <param name="y">The y-coordinate position.</param>
  44. public static void SetPosition(int x, int y) => Mouse.SetPosition(x, y);
  45. /// <summary>
  46. /// Sets the position of the mouse cursor to the specified coordinate relative to the game window.
  47. /// </summary>
  48. /// <param name="point">A <see cref="Point"/> value that represents the x- and y-coordinate positions.</param>
  49. public static void SetPosition(Point point) => Mouse.SetPosition(point.X, point.Y);
  50. #if !FNA
  51. /// <summary>
  52. /// Sets the cursor of the mouse.
  53. /// </summary>
  54. /// <param name="cursor">The cursor to use.</param>
  55. public static void SetCursor(MouseCursor cursor) => Mouse.SetCursor(cursor);
  56. #endif
  57. /// <summary>
  58. /// Gets or Sets the window handle of the mouse.
  59. /// </summary>
  60. public static IntPtr WindowHandle
  61. {
  62. get => Mouse.WindowHandle;
  63. set => Mouse.WindowHandle = value;
  64. }
  65. }