IMouse.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System.Drawing;
  2. using System.Numerics;
  3. namespace Ryujinx.Input
  4. {
  5. /// <summary>
  6. /// Represent an emulated mouse.
  7. /// </summary>
  8. public interface IMouse : IGamepad
  9. {
  10. private const int SwitchPanelWidth = 1280;
  11. private const int SwitchPanelHeight = 720;
  12. /// <summary>
  13. /// Check if a given button is pressed on the mouse.
  14. /// </summary>
  15. /// <param name="button">The button</param>
  16. /// <returns>True if the given button is pressed on the mouse</returns>
  17. bool IsButtonPressed(MouseButton button);
  18. /// <summary>
  19. /// Get the position of the mouse in the client.
  20. /// </summary>
  21. Vector2 GetPosition();
  22. /// <summary>
  23. /// Get the mouse scroll delta.
  24. /// </summary>
  25. Vector2 GetScroll();
  26. /// <summary>
  27. /// Get the client size.
  28. /// </summary>
  29. Size ClientSize { get; }
  30. /// <summary>
  31. /// Get the button states of the mouse.
  32. /// </summary>
  33. bool[] Buttons { get; }
  34. /// <summary>
  35. /// Get a snaphost of the state of a mouse.
  36. /// </summary>
  37. /// <param name="mouse">The mouse to do a snapshot of</param>
  38. /// <returns>A snaphost of the state of the mouse.</returns>
  39. public static MouseStateSnapshot GetMouseStateSnapshot(IMouse mouse)
  40. {
  41. bool[] buttons = new bool[(int)MouseButton.Count];
  42. mouse.Buttons.CopyTo(buttons, 0);
  43. return new MouseStateSnapshot(buttons, mouse.GetPosition(), mouse.GetScroll());
  44. }
  45. /// <summary>
  46. /// Get the position of a mouse on screen relative to the app's view
  47. /// </summary>
  48. /// <param name="mousePosition">The position of the mouse in the client</param>
  49. /// <param name="clientSize">The size of the client</param>
  50. /// <param name="aspectRatio">The aspect ratio of the view</param>
  51. /// <returns>A snaphost of the state of the mouse.</returns>
  52. public static Vector2 GetScreenPosition(Vector2 mousePosition, Size clientSize, float aspectRatio)
  53. {
  54. float mouseX = mousePosition.X;
  55. float mouseY = mousePosition.Y;
  56. float aspectWidth = SwitchPanelHeight * aspectRatio;
  57. int screenWidth = clientSize.Width;
  58. int screenHeight = clientSize.Height;
  59. if (clientSize.Width > clientSize.Height * aspectWidth / SwitchPanelHeight)
  60. {
  61. screenWidth = (int)(clientSize.Height * aspectWidth) / SwitchPanelHeight;
  62. }
  63. else
  64. {
  65. screenHeight = (clientSize.Width * SwitchPanelHeight) / (int)aspectWidth;
  66. }
  67. int startX = (clientSize.Width - screenWidth) >> 1;
  68. int startY = (clientSize.Height - screenHeight) >> 1;
  69. int endX = startX + screenWidth;
  70. int endY = startY + screenHeight;
  71. if (mouseX >= startX &&
  72. mouseY >= startY &&
  73. mouseX < endX &&
  74. mouseY < endY)
  75. {
  76. int screenMouseX = (int)mouseX - startX;
  77. int screenMouseY = (int)mouseY - startY;
  78. mouseX = (screenMouseX * (int)aspectWidth) / screenWidth;
  79. mouseY = (screenMouseY * SwitchPanelHeight) / screenHeight;
  80. return new Vector2(mouseX, mouseY);
  81. }
  82. return new Vector2();
  83. }
  84. }
  85. }