IMouse.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 client size.
  24. /// </summary>
  25. Size ClientSize { get; }
  26. /// <summary>
  27. /// Get the button states of the mouse.
  28. /// </summary>
  29. bool[] Buttons { get; }
  30. /// <summary>
  31. /// Get a snaphost of the state of a mouse.
  32. /// </summary>
  33. /// <param name="mouse">The mouse to do a snapshot of</param>
  34. /// <returns>A snaphost of the state of the mouse.</returns>
  35. public static MouseStateSnapshot GetMouseStateSnapshot(IMouse mouse)
  36. {
  37. var position = mouse.GetPosition();
  38. bool[] buttons = new bool[(int)MouseButton.Count];
  39. mouse.Buttons.CopyTo(buttons, 0);
  40. return new MouseStateSnapshot(buttons, position);
  41. }
  42. /// <summary>
  43. /// Get the touch position of a mouse position relative to the app's view
  44. /// </summary>
  45. /// <param name="mousePosition">The position of the mouse in the client</param>
  46. /// <param name="clientSize">The size of the client</param>
  47. /// <param name="aspectRatio">The aspect ratio of the view</param>
  48. /// <returns>A snaphost of the state of the mouse.</returns>
  49. public static Vector2 GetTouchPosition(Vector2 mousePosition, Size clientSize, float aspectRatio)
  50. {
  51. float mouseX = mousePosition.X;
  52. float mouseY = mousePosition.Y;
  53. float aspectWidth = SwitchPanelHeight * aspectRatio;
  54. int screenWidth = clientSize.Width;
  55. int screenHeight = clientSize.Height;
  56. if (clientSize.Width > clientSize.Height * aspectWidth / SwitchPanelHeight)
  57. {
  58. screenWidth = (int)(clientSize.Height * aspectWidth) / SwitchPanelHeight;
  59. }
  60. else
  61. {
  62. screenHeight = (clientSize.Width * SwitchPanelHeight) / (int)aspectWidth;
  63. }
  64. int startX = (clientSize.Width - screenWidth) >> 1;
  65. int startY = (clientSize.Height - screenHeight) >> 1;
  66. int endX = startX + screenWidth;
  67. int endY = startY + screenHeight;
  68. if (mouseX >= startX &&
  69. mouseY >= startY &&
  70. mouseX < endX &&
  71. mouseY < endY)
  72. {
  73. int screenMouseX = (int)mouseX - startX;
  74. int screenMouseY = (int)mouseY - startY;
  75. mouseX = (screenMouseX * (int)aspectWidth) / screenWidth;
  76. mouseY = (screenMouseY * SwitchPanelHeight) / screenHeight;
  77. return new Vector2(mouseX, mouseY);
  78. }
  79. return new Vector2();
  80. }
  81. }
  82. }