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