MouseStateExtended.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Input;
  4. namespace MonoGame.Extended.Input;
  5. /// <summary>
  6. /// Represents the state of mouse input.
  7. /// </summary>
  8. /// <remarks>
  9. /// This is an extended version of the base <see cref="Microsoft.Xna.Framework.Input.MouseState"/> struct
  10. /// that provides utility for checking state differences between the previous and current state.
  11. /// </remarks>
  12. public struct MouseStateExtended
  13. {
  14. private readonly MouseState _currentMouseState;
  15. private readonly MouseState _previousMouseState;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="MouseStateExtended"/> value.
  18. /// </summary>
  19. /// <param name="currentMouseState">The state of mouse input during the current update cycle.</param>
  20. /// <param name="previousMouseState">THe state of mouse input during hte previous update cycle.</param>
  21. public MouseStateExtended(MouseState currentMouseState, MouseState previousMouseState)
  22. {
  23. _currentMouseState = currentMouseState;
  24. _previousMouseState = previousMouseState;
  25. }
  26. /// <summary>
  27. /// Gets the current x-coordinate position of the mouse cursor relative to the game window.
  28. /// </summary>
  29. public int X => _currentMouseState.X;
  30. /// <summary>
  31. /// Gets the current y-coordinate position of the mouse cursor relative to the game window.
  32. /// </summary>
  33. public int Y => _currentMouseState.Y;
  34. /// <summary>
  35. /// Gets the current xy-coordinate position of the mouse cursor relative to the game window.
  36. /// </summary>
  37. public Point Position => new Point(_currentMouseState.X, _currentMouseState.Y);
  38. /// <summary>
  39. /// Gets a value that indicates whether the position of the mouse cursor changes between the previous and current
  40. /// states.
  41. /// </summary>
  42. public bool PositionChanged => _currentMouseState.X != _previousMouseState.X || _currentMouseState.Y != _previousMouseState.Y;
  43. /// <summary>
  44. /// Gets the difference in the x-coordinate position change of the mouse between the previous and current state.
  45. /// </summary>
  46. public int DeltaX => _previousMouseState.X - _currentMouseState.X;
  47. /// <summary>
  48. /// Gets the difference in the y-coordinate position change of the mouse between the previous and current state.
  49. /// </summary>
  50. public int DeltaY => _previousMouseState.Y - _currentMouseState.Y;
  51. /// <summary>
  52. /// Gets the difference in the xy-coordinate position change of the mouse between the previous and curren state.
  53. /// </summary>
  54. public Point DeltaPosition => new Point(DeltaX, DeltaY);
  55. /// <summary>
  56. /// Gets the current value of the mouse scroll wheel.
  57. /// </summary>
  58. public int ScrollWheelValue => _currentMouseState.ScrollWheelValue;
  59. /// <summary>
  60. /// Gets the difference in the mouse scroll wheel value between the previous and current state.
  61. /// </summary>
  62. public int DeltaScrollWheelValue => _previousMouseState.ScrollWheelValue - _currentMouseState.ScrollWheelValue;
  63. /// <summary>
  64. /// Gets the current state of the mouse left button.
  65. /// </summary>
  66. public ButtonState LeftButton => _currentMouseState.LeftButton;
  67. /// <summary>
  68. /// Gets the current state of the mouse middle button.
  69. /// </summary>
  70. public ButtonState MiddleButton => _currentMouseState.MiddleButton;
  71. /// <summary>
  72. /// Gets the current state of the mouse right button.
  73. /// </summary>
  74. public ButtonState RightButton => _currentMouseState.RightButton;
  75. /// <summary>
  76. /// Gets the current state of the first mouse extra button.
  77. /// </summary>
  78. public ButtonState XButton1 => _currentMouseState.XButton1;
  79. /// <summary>
  80. /// Gets the current state of the second mouse extra button.
  81. /// </summary>
  82. public ButtonState XButton2 => _currentMouseState.XButton2;
  83. /// <summary>
  84. /// Returns a value that indicates whether the specified mouse button is down during the current state.
  85. /// </summary>
  86. /// <param name="button">The mouse button to check.</param>
  87. /// <returns>
  88. /// <see langword="true"/> if the mouse button is down during the current state; otherwise, <see langword="false"/>.
  89. /// </returns>
  90. public bool IsButtonDown(MouseButton button)
  91. {
  92. // ReSharper disable once SwitchStatementMissingSomeCases
  93. switch (button)
  94. {
  95. case MouseButton.Left: return IsPressed(m => m.LeftButton);
  96. case MouseButton.Middle: return IsPressed(m => m.MiddleButton);
  97. case MouseButton.Right: return IsPressed(m => m.RightButton);
  98. case MouseButton.XButton1: return IsPressed(m => m.XButton1);
  99. case MouseButton.XButton2: return IsPressed(m => m.XButton2);
  100. }
  101. return false;
  102. }
  103. /// <summary>
  104. /// Returns a value that indicates whether the specified mouse button is up during the current state.
  105. /// </summary>
  106. /// <param name="button">The mouse button to check.</param>
  107. /// <returns>
  108. /// <see langword="true"/> if the mouse button is up during the current state; otherwise, <see langword="false"/>.
  109. /// </returns>
  110. public bool IsButtonUp(MouseButton button)
  111. {
  112. // ReSharper disable once SwitchStatementMissingSomeCases
  113. switch (button)
  114. {
  115. case MouseButton.Left: return IsReleased(m => m.LeftButton);
  116. case MouseButton.Middle: return IsReleased(m => m.MiddleButton);
  117. case MouseButton.Right: return IsReleased(m => m.RightButton);
  118. case MouseButton.XButton1: return IsReleased(m => m.XButton1);
  119. case MouseButton.XButton2: return IsReleased(m => m.XButton2);
  120. }
  121. return false;
  122. }
  123. /// <summary>
  124. /// Returns whether the specified mouse button was up during the previous, but is now down.
  125. /// </summary>
  126. /// <param name="button">The mouse button to check.</param>
  127. /// <returns>
  128. /// <see langword="true"/> if the mouse button was up pressed this state-change; otherwise, <see langword="false"/>.
  129. /// </returns>
  130. public readonly bool WasButtonPressed(MouseButton button) => button switch
  131. {
  132. MouseButton.Left => WasJustPressed(m => m.LeftButton),
  133. MouseButton.Middle => WasJustPressed(m => m.MiddleButton),
  134. MouseButton.Right => WasJustPressed(m => m.RightButton),
  135. MouseButton.XButton1 => WasJustPressed(m => m.XButton1),
  136. MouseButton.XButton2 => WasJustPressed(m => m.XButton2),
  137. _ => false,
  138. };
  139. /// <summary>
  140. /// Returns whether the specified mouse button was down during the previous state, but is now up.
  141. /// </summary>
  142. /// <param name="button">The mouse button to check.</param>
  143. /// <returns>
  144. /// <see langword="true"/> if the mouse button was released this state-change; otherwise, <see langword="false"/>.
  145. /// </returns>
  146. public readonly bool WasButtonReleased(MouseButton button) => button switch
  147. {
  148. MouseButton.Left => WasJustReleased(m => m.LeftButton),
  149. MouseButton.Middle => WasJustReleased(m => m.MiddleButton),
  150. MouseButton.Right => WasJustReleased(m => m.RightButton),
  151. MouseButton.XButton1 => WasJustReleased(m => m.XButton1),
  152. MouseButton.XButton2 => WasJustReleased(m => m.XButton2),
  153. _ => false,
  154. };
  155. private readonly bool IsPressed(Func<MouseState, ButtonState> button)
  156. => button(_currentMouseState) == ButtonState.Pressed;
  157. private readonly bool IsReleased(Func<MouseState, ButtonState> button)
  158. => button(_currentMouseState) == ButtonState.Released;
  159. private readonly bool WasJustPressed(Func<MouseState, ButtonState> button)
  160. => button(_previousMouseState) == ButtonState.Released && button(_currentMouseState) == ButtonState.Pressed;
  161. private readonly bool WasJustReleased(Func<MouseState, ButtonState> button)
  162. => button(_previousMouseState) == ButtonState.Pressed && button(_currentMouseState) == ButtonState.Released;
  163. }